blob: f0f5347a16d8a4094a80b6ac4c11b15230ad4e3d [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());
Eli Friedmana49218e2011-04-09 08:18:08 +0000164 Record.push_back(C.getHasRegParm());
Rafael Espindola425ef722010-03-30 22:15:11 +0000165 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000166 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000167 Record.push_back(C.getCC());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000168}
169
Sebastian Redl3397c552010-08-18 23:56:27 +0000170void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000171 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000172 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000173}
174
Sebastian Redl3397c552010-08-18 23:56:27 +0000175void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000176 VisitFunctionType(T);
177 Record.push_back(T->getNumArgs());
178 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
179 Writer.AddTypeRef(T->getArgType(I), Record);
180 Record.push_back(T->isVariadic());
181 Record.push_back(T->getTypeQuals());
Douglas Gregorc938c162011-01-26 05:01:58 +0000182 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redl60618fa2011-03-12 11:50:43 +0000183 Record.push_back(T->getExceptionSpecType());
184 if (T->getExceptionSpecType() == EST_Dynamic) {
185 Record.push_back(T->getNumExceptions());
186 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
187 Writer.AddTypeRef(T->getExceptionType(I), Record);
188 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
189 Writer.AddStmt(T->getNoexceptExpr());
190 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000191 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000192}
193
Sebastian Redl3397c552010-08-18 23:56:27 +0000194void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000195 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000196 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000197}
John McCalled976492009-12-04 22:46:56 +0000198
Sebastian Redl3397c552010-08-18 23:56:27 +0000199void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000200 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000201 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
202 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000203 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000204}
205
Sebastian Redl3397c552010-08-18 23:56:27 +0000206void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000207 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000208 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000209}
210
Sebastian Redl3397c552010-08-18 23:56:27 +0000211void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000212 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000213 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000214}
215
Sebastian Redl3397c552010-08-18 23:56:27 +0000216void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson395b4752009-06-24 19:06:50 +0000217 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000218 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000219}
220
Richard Smith34b41d92011-02-20 03:19:35 +0000221void ASTTypeWriter::VisitAutoType(const AutoType *T) {
222 Writer.AddTypeRef(T->getDeducedType(), Record);
223 Code = TYPE_AUTO;
224}
225
Sebastian Redl3397c552010-08-18 23:56:27 +0000226void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000227 Record.push_back(T->isDependentType());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000228 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000229 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000230 "Cannot serialize in the middle of a type definition");
231}
232
Sebastian Redl3397c552010-08-18 23:56:27 +0000233void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000234 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000235 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000236}
237
Sebastian Redl3397c552010-08-18 23:56:27 +0000238void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000239 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000240 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000241}
242
John McCall9d156a72011-01-06 01:58:22 +0000243void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
244 Writer.AddTypeRef(T->getModifiedType(), Record);
245 Writer.AddTypeRef(T->getEquivalentType(), Record);
246 Record.push_back(T->getAttrKind());
247 Code = TYPE_ATTRIBUTED;
248}
249
Mike Stump1eb44332009-09-09 15:08:12 +0000250void
Sebastian Redl3397c552010-08-18 23:56:27 +0000251ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000252 const SubstTemplateTypeParmType *T) {
253 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
254 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000255 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000256}
257
258void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000259ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
260 const SubstTemplateTypeParmPackType *T) {
261 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
262 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
263 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
264}
265
266void
Sebastian Redl3397c552010-08-18 23:56:27 +0000267ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000268 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000269 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000270 Writer.AddTemplateName(T->getTemplateName(), Record);
271 Record.push_back(T->getNumArgs());
272 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
273 ArgI != ArgE; ++ArgI)
274 Writer.AddTemplateArgument(*ArgI, Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000275 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
276 : T->getCanonicalTypeInternal(),
277 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000278 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000279}
280
281void
Sebastian Redl3397c552010-08-18 23:56:27 +0000282ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000283 VisitArrayType(T);
284 Writer.AddStmt(T->getSizeExpr());
285 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000286 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000287}
288
289void
Sebastian Redl3397c552010-08-18 23:56:27 +0000290ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000291 const DependentSizedExtVectorType *T) {
292 // FIXME: Serialize this type (C++ only)
293 assert(false && "Cannot serialize dependent sized extended vector types");
294}
295
296void
Sebastian Redl3397c552010-08-18 23:56:27 +0000297ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000298 Record.push_back(T->getDepth());
299 Record.push_back(T->getIndex());
300 Record.push_back(T->isParameterPack());
301 Writer.AddIdentifierRef(T->getName(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000302 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000303}
304
305void
Sebastian Redl3397c552010-08-18 23:56:27 +0000306ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000307 Record.push_back(T->getKeyword());
308 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
309 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000310 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
311 : T->getCanonicalTypeInternal(),
312 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000313 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000314}
315
316void
Sebastian Redl3397c552010-08-18 23:56:27 +0000317ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000318 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000319 Record.push_back(T->getKeyword());
320 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
321 Writer.AddIdentifierRef(T->getIdentifier(), Record);
322 Record.push_back(T->getNumArgs());
323 for (DependentTemplateSpecializationType::iterator
324 I = T->begin(), E = T->end(); I != E; ++I)
325 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000326 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000327}
328
Douglas Gregor7536dd52010-12-20 02:24:11 +0000329void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
330 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregorcded4f62011-01-14 17:04:44 +0000331 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
332 Record.push_back(*NumExpansions + 1);
333 else
334 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000335 Code = TYPE_PACK_EXPANSION;
336}
337
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000338void ASTTypeWriter::VisitParenType(const ParenType *T) {
339 Writer.AddTypeRef(T->getInnerType(), Record);
340 Code = TYPE_PAREN;
341}
342
Sebastian Redl3397c552010-08-18 23:56:27 +0000343void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000344 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000345 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
346 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000347 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000348}
349
Sebastian Redl3397c552010-08-18 23:56:27 +0000350void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCall3cb0ebd2010-03-10 03:28:59 +0000351 Writer.AddDeclRef(T->getDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000352 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000353 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000354}
355
Sebastian Redl3397c552010-08-18 23:56:27 +0000356void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000357 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000358 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000359}
360
Sebastian Redl3397c552010-08-18 23:56:27 +0000361void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000362 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000363 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000364 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000365 E = T->qual_end(); I != E; ++I)
366 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000367 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000368}
369
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000370void
Sebastian Redl3397c552010-08-18 23:56:27 +0000371ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000372 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000373 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000374}
375
John McCalla1ee0c52009-10-16 21:56:05 +0000376namespace {
377
378class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000379 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000380 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000381
382public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000383 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000384 : Writer(Writer), Record(Record) { }
385
John McCall51bd8032009-10-18 01:05:36 +0000386#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000387#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000388 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000389#include "clang/AST/TypeLocNodes.def"
390
John McCall51bd8032009-10-18 01:05:36 +0000391 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
392 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000393};
394
395}
396
John McCall51bd8032009-10-18 01:05:36 +0000397void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
398 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000399}
John McCall51bd8032009-10-18 01:05:36 +0000400void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000401 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
402 if (TL.needsExtraLocalData()) {
403 Record.push_back(TL.getWrittenTypeSpec());
404 Record.push_back(TL.getWrittenSignSpec());
405 Record.push_back(TL.getWrittenWidthSpec());
406 Record.push_back(TL.hasModeAttr());
407 }
John McCalla1ee0c52009-10-16 21:56:05 +0000408}
John McCall51bd8032009-10-18 01:05:36 +0000409void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
410 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000411}
John McCall51bd8032009-10-18 01:05:36 +0000412void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
413 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000414}
John McCall51bd8032009-10-18 01:05:36 +0000415void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
416 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000417}
John McCall51bd8032009-10-18 01:05:36 +0000418void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
419 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000420}
John McCall51bd8032009-10-18 01:05:36 +0000421void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
422 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000423}
John McCall51bd8032009-10-18 01:05:36 +0000424void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
425 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +0000426 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000427}
John McCall51bd8032009-10-18 01:05:36 +0000428void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
429 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
430 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
431 Record.push_back(TL.getSizeExpr() ? 1 : 0);
432 if (TL.getSizeExpr())
433 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000434}
John McCall51bd8032009-10-18 01:05:36 +0000435void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
436 VisitArrayTypeLoc(TL);
437}
438void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
439 VisitArrayTypeLoc(TL);
440}
441void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
442 VisitArrayTypeLoc(TL);
443}
444void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
445 DependentSizedArrayTypeLoc TL) {
446 VisitArrayTypeLoc(TL);
447}
448void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
449 DependentSizedExtVectorTypeLoc TL) {
450 Writer.AddSourceLocation(TL.getNameLoc(), Record);
451}
452void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
453 Writer.AddSourceLocation(TL.getNameLoc(), Record);
454}
455void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
456 Writer.AddSourceLocation(TL.getNameLoc(), Record);
457}
458void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +0000459 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
460 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Douglas Gregordab60ad2010-10-01 18:44:50 +0000461 Record.push_back(TL.getTrailingReturn());
John McCall51bd8032009-10-18 01:05:36 +0000462 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
463 Writer.AddDeclRef(TL.getArg(i), Record);
464}
465void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
466 VisitFunctionTypeLoc(TL);
467}
468void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
469 VisitFunctionTypeLoc(TL);
470}
John McCalled976492009-12-04 22:46:56 +0000471void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
472 Writer.AddSourceLocation(TL.getNameLoc(), Record);
473}
John McCall51bd8032009-10-18 01:05:36 +0000474void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
475 Writer.AddSourceLocation(TL.getNameLoc(), Record);
476}
477void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000478 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
479 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
480 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000481}
482void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000483 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
484 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
485 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
486 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000487}
488void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
489 Writer.AddSourceLocation(TL.getNameLoc(), Record);
490}
Richard Smith34b41d92011-02-20 03:19:35 +0000491void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
492 Writer.AddSourceLocation(TL.getNameLoc(), Record);
493}
John McCall51bd8032009-10-18 01:05:36 +0000494void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
495 Writer.AddSourceLocation(TL.getNameLoc(), Record);
496}
497void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
498 Writer.AddSourceLocation(TL.getNameLoc(), Record);
499}
John McCall9d156a72011-01-06 01:58:22 +0000500void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
501 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
502 if (TL.hasAttrOperand()) {
503 SourceRange range = TL.getAttrOperandParensRange();
504 Writer.AddSourceLocation(range.getBegin(), Record);
505 Writer.AddSourceLocation(range.getEnd(), Record);
506 }
507 if (TL.hasAttrExprOperand()) {
508 Expr *operand = TL.getAttrExprOperand();
509 Record.push_back(operand ? 1 : 0);
510 if (operand) Writer.AddStmt(operand);
511 } else if (TL.hasAttrEnumOperand()) {
512 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
513 }
514}
John McCall51bd8032009-10-18 01:05:36 +0000515void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
516 Writer.AddSourceLocation(TL.getNameLoc(), Record);
517}
John McCall49a832b2009-10-18 09:09:24 +0000518void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
519 SubstTemplateTypeParmTypeLoc TL) {
520 Writer.AddSourceLocation(TL.getNameLoc(), Record);
521}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000522void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
523 SubstTemplateTypeParmPackTypeLoc TL) {
524 Writer.AddSourceLocation(TL.getNameLoc(), Record);
525}
John McCall51bd8032009-10-18 01:05:36 +0000526void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
527 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000528 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
529 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
530 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
531 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000532 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
533 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000534}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000535void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
536 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
537 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
538}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000539void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000540 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor9e876872011-03-01 18:12:44 +0000541 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000542}
John McCall3cb0ebd2010-03-10 03:28:59 +0000543void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
544 Writer.AddSourceLocation(TL.getNameLoc(), Record);
545}
Douglas Gregor4714c122010-03-31 17:34:00 +0000546void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000547 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000548 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000549 Writer.AddSourceLocation(TL.getNameLoc(), Record);
550}
John McCall33500952010-06-11 00:33:02 +0000551void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
552 DependentTemplateSpecializationTypeLoc TL) {
553 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000554 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall33500952010-06-11 00:33:02 +0000555 Writer.AddSourceLocation(TL.getNameLoc(), Record);
556 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
557 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
558 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000559 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
560 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000561}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000562void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
563 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
564}
John McCall51bd8032009-10-18 01:05:36 +0000565void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
566 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000567}
568void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
569 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000570 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
571 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
572 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
573 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000574}
John McCall54e14c42009-10-22 22:37:11 +0000575void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
576 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000577}
John McCalla1ee0c52009-10-16 21:56:05 +0000578
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000579//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000580// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000581//===----------------------------------------------------------------------===//
582
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000583static void EmitBlockID(unsigned ID, const char *Name,
584 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000585 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000586 Record.clear();
587 Record.push_back(ID);
588 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
589
590 // Emit the block name if present.
591 if (Name == 0 || Name[0] == 0) return;
592 Record.clear();
593 while (*Name)
594 Record.push_back(*Name++);
595 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
596}
597
598static void EmitRecordID(unsigned ID, const char *Name,
599 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000600 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000601 Record.clear();
602 Record.push_back(ID);
603 while (*Name)
604 Record.push_back(*Name++);
605 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000606}
607
608static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000609 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000610#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000611 RECORD(STMT_STOP);
612 RECORD(STMT_NULL_PTR);
613 RECORD(STMT_NULL);
614 RECORD(STMT_COMPOUND);
615 RECORD(STMT_CASE);
616 RECORD(STMT_DEFAULT);
617 RECORD(STMT_LABEL);
618 RECORD(STMT_IF);
619 RECORD(STMT_SWITCH);
620 RECORD(STMT_WHILE);
621 RECORD(STMT_DO);
622 RECORD(STMT_FOR);
623 RECORD(STMT_GOTO);
624 RECORD(STMT_INDIRECT_GOTO);
625 RECORD(STMT_CONTINUE);
626 RECORD(STMT_BREAK);
627 RECORD(STMT_RETURN);
628 RECORD(STMT_DECL);
629 RECORD(STMT_ASM);
630 RECORD(EXPR_PREDEFINED);
631 RECORD(EXPR_DECL_REF);
632 RECORD(EXPR_INTEGER_LITERAL);
633 RECORD(EXPR_FLOATING_LITERAL);
634 RECORD(EXPR_IMAGINARY_LITERAL);
635 RECORD(EXPR_STRING_LITERAL);
636 RECORD(EXPR_CHARACTER_LITERAL);
637 RECORD(EXPR_PAREN);
638 RECORD(EXPR_UNARY_OPERATOR);
639 RECORD(EXPR_SIZEOF_ALIGN_OF);
640 RECORD(EXPR_ARRAY_SUBSCRIPT);
641 RECORD(EXPR_CALL);
642 RECORD(EXPR_MEMBER);
643 RECORD(EXPR_BINARY_OPERATOR);
644 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
645 RECORD(EXPR_CONDITIONAL_OPERATOR);
646 RECORD(EXPR_IMPLICIT_CAST);
647 RECORD(EXPR_CSTYLE_CAST);
648 RECORD(EXPR_COMPOUND_LITERAL);
649 RECORD(EXPR_EXT_VECTOR_ELEMENT);
650 RECORD(EXPR_INIT_LIST);
651 RECORD(EXPR_DESIGNATED_INIT);
652 RECORD(EXPR_IMPLICIT_VALUE_INIT);
653 RECORD(EXPR_VA_ARG);
654 RECORD(EXPR_ADDR_LABEL);
655 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000656 RECORD(EXPR_CHOOSE);
657 RECORD(EXPR_GNU_NULL);
658 RECORD(EXPR_SHUFFLE_VECTOR);
659 RECORD(EXPR_BLOCK);
660 RECORD(EXPR_BLOCK_DECL_REF);
661 RECORD(EXPR_OBJC_STRING_LITERAL);
662 RECORD(EXPR_OBJC_ENCODE);
663 RECORD(EXPR_OBJC_SELECTOR_EXPR);
664 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
665 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
666 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
667 RECORD(EXPR_OBJC_KVC_REF_EXPR);
668 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000669 RECORD(STMT_OBJC_FOR_COLLECTION);
670 RECORD(STMT_OBJC_CATCH);
671 RECORD(STMT_OBJC_FINALLY);
672 RECORD(STMT_OBJC_AT_TRY);
673 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
674 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000675 RECORD(EXPR_CXX_OPERATOR_CALL);
676 RECORD(EXPR_CXX_CONSTRUCT);
677 RECORD(EXPR_CXX_STATIC_CAST);
678 RECORD(EXPR_CXX_DYNAMIC_CAST);
679 RECORD(EXPR_CXX_REINTERPRET_CAST);
680 RECORD(EXPR_CXX_CONST_CAST);
681 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
682 RECORD(EXPR_CXX_BOOL_LITERAL);
683 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000684 RECORD(EXPR_CXX_TYPEID_EXPR);
685 RECORD(EXPR_CXX_TYPEID_TYPE);
686 RECORD(EXPR_CXX_UUIDOF_EXPR);
687 RECORD(EXPR_CXX_UUIDOF_TYPE);
688 RECORD(EXPR_CXX_THIS);
689 RECORD(EXPR_CXX_THROW);
690 RECORD(EXPR_CXX_DEFAULT_ARG);
691 RECORD(EXPR_CXX_BIND_TEMPORARY);
692 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
693 RECORD(EXPR_CXX_NEW);
694 RECORD(EXPR_CXX_DELETE);
695 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
696 RECORD(EXPR_EXPR_WITH_CLEANUPS);
697 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
698 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
699 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
700 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
701 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
702 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
703 RECORD(EXPR_CXX_NOEXCEPT);
704 RECORD(EXPR_OPAQUE_VALUE);
705 RECORD(EXPR_BINARY_TYPE_TRAIT);
706 RECORD(EXPR_PACK_EXPANSION);
707 RECORD(EXPR_SIZEOF_PACK);
708 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000709 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000710#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000711}
Mike Stump1eb44332009-09-09 15:08:12 +0000712
Sebastian Redla4232eb2010-08-18 23:56:21 +0000713void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000714 RecordData Record;
715 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000716
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000717#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
718#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000719
Sebastian Redl3397c552010-08-18 23:56:27 +0000720 // AST Top-Level Block.
Sebastian Redlf29f0a22010-08-18 23:57:22 +0000721 BLOCK(AST_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000722 RECORD(ORIGINAL_FILE_NAME);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000723 RECORD(TYPE_OFFSET);
724 RECORD(DECL_OFFSET);
725 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000726 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000727 RECORD(IDENTIFIER_OFFSET);
728 RECORD(IDENTIFIER_TABLE);
729 RECORD(EXTERNAL_DEFINITIONS);
730 RECORD(SPECIAL_TYPES);
731 RECORD(STATISTICS);
732 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000733 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000734 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
735 RECORD(SELECTOR_OFFSETS);
736 RECORD(METHOD_POOL);
737 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000738 RECORD(SOURCE_LOCATION_OFFSETS);
739 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000740 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000741 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000742 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000743 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redla93e3b52010-07-08 22:01:51 +0000744 RECORD(CHAINED_METADATA);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000745 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000746 RECORD(TU_UPDATE_LEXICAL);
747 RECORD(REDECLS_UPDATE_LATEST);
748 RECORD(SEMA_DECL_REFS);
749 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
750 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
751 RECORD(DECL_REPLACEMENTS);
752 RECORD(UPDATE_VISIBLE);
753 RECORD(DECL_UPDATE_OFFSETS);
754 RECORD(DECL_UPDATES);
755 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
756 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000757 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000758 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000759 RECORD(FP_PRAGMA_OPTIONS);
760 RECORD(OPENCL_EXTENSIONS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000761
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000762 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000763 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000764 RECORD(SM_SLOC_FILE_ENTRY);
765 RECORD(SM_SLOC_BUFFER_ENTRY);
766 RECORD(SM_SLOC_BUFFER_BLOB);
767 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
768 RECORD(SM_LINE_TABLE);
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000770 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000771 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000772 RECORD(PP_MACRO_OBJECT_LIKE);
773 RECORD(PP_MACRO_FUNCTION_LIKE);
774 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000775
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000776 // Decls and Types block.
777 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000778 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000779 RECORD(TYPE_COMPLEX);
780 RECORD(TYPE_POINTER);
781 RECORD(TYPE_BLOCK_POINTER);
782 RECORD(TYPE_LVALUE_REFERENCE);
783 RECORD(TYPE_RVALUE_REFERENCE);
784 RECORD(TYPE_MEMBER_POINTER);
785 RECORD(TYPE_CONSTANT_ARRAY);
786 RECORD(TYPE_INCOMPLETE_ARRAY);
787 RECORD(TYPE_VARIABLE_ARRAY);
788 RECORD(TYPE_VECTOR);
789 RECORD(TYPE_EXT_VECTOR);
790 RECORD(TYPE_FUNCTION_PROTO);
791 RECORD(TYPE_FUNCTION_NO_PROTO);
792 RECORD(TYPE_TYPEDEF);
793 RECORD(TYPE_TYPEOF_EXPR);
794 RECORD(TYPE_TYPEOF);
795 RECORD(TYPE_RECORD);
796 RECORD(TYPE_ENUM);
797 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000798 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000799 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000800 RECORD(TYPE_DECLTYPE);
801 RECORD(TYPE_ELABORATED);
802 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
803 RECORD(TYPE_UNRESOLVED_USING);
804 RECORD(TYPE_INJECTED_CLASS_NAME);
805 RECORD(TYPE_OBJC_OBJECT);
806 RECORD(TYPE_TEMPLATE_TYPE_PARM);
807 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
808 RECORD(TYPE_DEPENDENT_NAME);
809 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
810 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
811 RECORD(TYPE_PAREN);
812 RECORD(TYPE_PACK_EXPANSION);
813 RECORD(TYPE_ATTRIBUTED);
814 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000815 RECORD(DECL_TRANSLATION_UNIT);
816 RECORD(DECL_TYPEDEF);
817 RECORD(DECL_ENUM);
818 RECORD(DECL_RECORD);
819 RECORD(DECL_ENUM_CONSTANT);
820 RECORD(DECL_FUNCTION);
821 RECORD(DECL_OBJC_METHOD);
822 RECORD(DECL_OBJC_INTERFACE);
823 RECORD(DECL_OBJC_PROTOCOL);
824 RECORD(DECL_OBJC_IVAR);
825 RECORD(DECL_OBJC_AT_DEFS_FIELD);
826 RECORD(DECL_OBJC_CLASS);
827 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
828 RECORD(DECL_OBJC_CATEGORY);
829 RECORD(DECL_OBJC_CATEGORY_IMPL);
830 RECORD(DECL_OBJC_IMPLEMENTATION);
831 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
832 RECORD(DECL_OBJC_PROPERTY);
833 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000834 RECORD(DECL_FIELD);
835 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000836 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000837 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000838 RECORD(DECL_FILE_SCOPE_ASM);
839 RECORD(DECL_BLOCK);
840 RECORD(DECL_CONTEXT_LEXICAL);
841 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000842 RECORD(DECL_NAMESPACE);
843 RECORD(DECL_NAMESPACE_ALIAS);
844 RECORD(DECL_USING);
845 RECORD(DECL_USING_SHADOW);
846 RECORD(DECL_USING_DIRECTIVE);
847 RECORD(DECL_UNRESOLVED_USING_VALUE);
848 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
849 RECORD(DECL_LINKAGE_SPEC);
850 RECORD(DECL_CXX_RECORD);
851 RECORD(DECL_CXX_METHOD);
852 RECORD(DECL_CXX_CONSTRUCTOR);
853 RECORD(DECL_CXX_DESTRUCTOR);
854 RECORD(DECL_CXX_CONVERSION);
855 RECORD(DECL_ACCESS_SPEC);
856 RECORD(DECL_FRIEND);
857 RECORD(DECL_FRIEND_TEMPLATE);
858 RECORD(DECL_CLASS_TEMPLATE);
859 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
860 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
861 RECORD(DECL_FUNCTION_TEMPLATE);
862 RECORD(DECL_TEMPLATE_TYPE_PARM);
863 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
864 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
865 RECORD(DECL_STATIC_ASSERT);
866 RECORD(DECL_CXX_BASE_SPECIFIERS);
867 RECORD(DECL_INDIRECTFIELD);
868 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
869
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000870 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
871 RECORD(PPD_MACRO_INSTANTIATION);
872 RECORD(PPD_MACRO_DEFINITION);
873 RECORD(PPD_INCLUSION_DIRECTIVE);
874
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000875 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattner0558df22009-04-27 00:49:53 +0000876 AddStmtsExprs(Stream, Record);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000877#undef RECORD
878#undef BLOCK
879 Stream.ExitBlock();
880}
881
Douglas Gregore650c8c2009-07-07 00:12:59 +0000882/// \brief Adjusts the given filename to only write out the portion of the
883/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000884///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000885/// \param Filename the file name to adjust.
886///
887/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
888/// the returned filename will be adjusted by this system root.
889///
890/// \returns either the original filename (if it needs no adjustment) or the
891/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000892static const char *
Douglas Gregore650c8c2009-07-07 00:12:59 +0000893adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
894 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000895
Douglas Gregore650c8c2009-07-07 00:12:59 +0000896 if (!isysroot)
897 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000898
Douglas Gregore650c8c2009-07-07 00:12:59 +0000899 // Verify that the filename and the system root have the same prefix.
900 unsigned Pos = 0;
901 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
902 if (Filename[Pos] != isysroot[Pos])
903 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000904
Douglas Gregore650c8c2009-07-07 00:12:59 +0000905 // We hit the end of the filename before we hit the end of the system root.
906 if (!Filename[Pos])
907 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000908
Douglas Gregore650c8c2009-07-07 00:12:59 +0000909 // If the file name has a '/' at the current position, skip over the '/'.
910 // We distinguish sysroot-based includes from absolute includes by the
911 // absence of '/' at the beginning of sysroot-based includes.
912 if (Filename[Pos] == '/')
913 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000914
Douglas Gregore650c8c2009-07-07 00:12:59 +0000915 return Filename + Pos;
916}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000917
Sebastian Redl3397c552010-08-18 23:56:27 +0000918/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000919void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot,
920 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000921 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000922
Douglas Gregore650c8c2009-07-07 00:12:59 +0000923 // Metadata
924 const TargetInfo &Target = Context.Target;
925 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl77f46032010-07-09 21:00:24 +0000926 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000927 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl3397c552010-08-18 23:56:27 +0000928 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
929 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregore650c8c2009-07-07 00:12:59 +0000930 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
931 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
932 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl77f46032010-07-09 21:00:24 +0000933 // Target triple or chained PCH name
934 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregore650c8c2009-07-07 00:12:59 +0000935 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000936
Douglas Gregore650c8c2009-07-07 00:12:59 +0000937 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000938 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
939 Record.push_back(VERSION_MAJOR);
940 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000941 Record.push_back(CLANG_VERSION_MAJOR);
942 Record.push_back(CLANG_VERSION_MINOR);
943 Record.push_back(isysroot != 0);
Sebastian Redl77f46032010-07-09 21:00:24 +0000944 // FIXME: This writes the absolute path for chained headers.
945 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
946 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000947
Douglas Gregorb64c1932009-05-12 01:31:05 +0000948 // Original file name
949 SourceManager &SM = Context.getSourceManager();
950 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
951 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000952 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregorb64c1932009-05-12 01:31:05 +0000953 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
954 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
955
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000956 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000957
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000958 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000959
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +0000960 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000961 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000962 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000963 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000964 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000965 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000966 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000967
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000968 // Original PCH directory
969 if (!OutputFile.empty() && OutputFile != "-") {
970 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
971 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
972 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
973 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
974
975 llvm::SmallString<128> OutputPath(OutputFile);
976
977 llvm::sys::fs::make_absolute(OutputPath);
978 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
979
980 RecordData Record;
981 Record.push_back(ORIGINAL_PCH_DIR);
982 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
983 }
984
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000985 // Repository branch/version information.
986 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000987 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000988 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
989 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000990 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000991 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000992 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
993 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +0000994}
995
996/// \brief Write the LangOptions structure.
Sebastian Redla4232eb2010-08-18 23:56:21 +0000997void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000998 RecordData Record;
999 Record.push_back(LangOpts.Trigraphs);
1000 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1001 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1002 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1003 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00001004 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001005 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1006 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1007 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1008 Record.push_back(LangOpts.C99); // C99 Support
1009 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencerdae4ac42010-10-21 05:21:48 +00001010 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
1011 // already saved elsewhere.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001012 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1013 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001014 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +00001015
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001016 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1017 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001018 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian412e7982010-02-09 19:31:38 +00001019 // modern abi enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001020 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian412e7982010-02-09 19:31:38 +00001021 // modern abi enabled.
Fariborz Jahanianf84109e2011-01-07 18:59:25 +00001022 Record.push_back(LangOpts.AppleKext); // Apple's kernel extensions ABI
Ted Kremenekc32647d2010-12-23 21:35:43 +00001023 Record.push_back(LangOpts.ObjCDefaultSynthProperties); // Objective-C auto-synthesized
1024 // properties enabled.
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +00001025 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump1eb44332009-09-09 15:08:12 +00001026
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001027 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001028 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1029 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +00001030 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001031 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Anders Carlssonda4b7cf2011-02-19 23:53:54 +00001032 Record.push_back(LangOpts.ObjCExceptions);
Anders Carlsson7da99b02011-02-23 03:04:54 +00001033 Record.push_back(LangOpts.CXXExceptions);
1034 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001035
Douglas Gregor6f755502011-02-01 15:15:22 +00001036 Record.push_back(LangOpts.MSBitfields); // MS-compatible structure layout
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001037 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1038 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1039 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1040
Chris Lattnerea5ce472009-04-27 07:35:58 +00001041 // Whether static initializers are protected by locks.
1042 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00001043 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001044 Record.push_back(LangOpts.Blocks); // block extension to C
1045 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1046 // they are unused.
1047 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1048 // (modulo the platform support).
1049
Chris Lattnera4d71452010-06-26 21:25:03 +00001050 Record.push_back(LangOpts.getSignedOverflowBehavior());
1051 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001052
1053 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +00001054 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001055 // defined.
1056 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1057 // opposed to __DYNAMIC__).
1058 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1059
1060 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1061 // used (instead of C99 semantics).
1062 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +00001063 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
1064 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +00001065 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
1066 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +00001067 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Argyrios Kyrtzidisb1bdced2011-01-15 02:56:16 +00001068 Record.push_back(LangOpts.ShortEnums); // Should the enum type be equivalent
1069 // to the smallest integer type with
1070 // enough room.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001071 Record.push_back(LangOpts.getGCMode());
1072 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +00001073 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001074 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00001075 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne08a53262010-12-01 19:14:57 +00001076 Record.push_back(LangOpts.CUDA);
Mike Stump9c276ae2009-12-12 01:27:46 +00001077 Record.push_back(LangOpts.CatchUndefined);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00001078 Record.push_back(LangOpts.DefaultFPContract);
Anders Carlsson92f58222009-08-22 22:30:33 +00001079 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +00001080 Record.push_back(LangOpts.SpellChecking);
Roman Divackycfe9af22011-03-01 17:40:53 +00001081 Record.push_back(LangOpts.MRTD);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001082 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001083}
1084
Douglas Gregor14f79002009-04-10 03:52:48 +00001085//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001086// stat cache Serialization
1087//===----------------------------------------------------------------------===//
1088
1089namespace {
1090// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +00001091class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001092public:
1093 typedef const char * key_type;
1094 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001095
Chris Lattner74e976b2010-11-23 19:28:12 +00001096 typedef struct stat data_type;
1097 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001098
1099 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001100 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001101 }
Mike Stump1eb44332009-09-09 15:08:12 +00001102
1103 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001104 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1105 data_type_ref Data) {
1106 unsigned StrLen = strlen(path);
1107 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +00001108 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001109 clang::io::Emit8(Out, DataLen);
1110 return std::make_pair(StrLen + 1, DataLen);
1111 }
Mike Stump1eb44332009-09-09 15:08:12 +00001112
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001113 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1114 Out.write(path, KeyLen);
1115 }
Mike Stump1eb44332009-09-09 15:08:12 +00001116
Chris Lattner74e976b2010-11-23 19:28:12 +00001117 void EmitData(llvm::raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001118 data_type_ref Data, unsigned DataLen) {
1119 using namespace clang::io;
1120 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +00001121
Chris Lattner74e976b2010-11-23 19:28:12 +00001122 Emit32(Out, (uint32_t) Data.st_ino);
1123 Emit32(Out, (uint32_t) Data.st_dev);
1124 Emit16(Out, (uint16_t) Data.st_mode);
1125 Emit64(Out, (uint64_t) Data.st_mtime);
1126 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001127
1128 assert(Out.tell() - Start == DataLen && "Wrong data length");
1129 }
1130};
1131} // end anonymous namespace
1132
Sebastian Redl3397c552010-08-18 23:56:27 +00001133/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001134void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001135 // Build the on-disk hash table containing information about every
1136 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +00001137 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001138 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001139 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001140 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001141 Stat != StatEnd; ++Stat, ++NumStatEntries) {
1142 const char *Filename = Stat->first();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001143 Generator.insert(Filename, Stat->second);
1144 }
Mike Stump1eb44332009-09-09 15:08:12 +00001145
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001146 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001147 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001148 uint32_t BucketOffset;
1149 {
1150 llvm::raw_svector_ostream Out(StatCacheData);
1151 // Make sure that no bucket is at offset 0
1152 clang::io::Emit32(Out, 0);
1153 BucketOffset = Generator.Emit(Out);
1154 }
1155
1156 // Create a blob abbreviation
1157 using namespace llvm;
1158 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001159 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001160 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1161 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1162 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1163 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1164
1165 // Write the stat cache
1166 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001167 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001168 Record.push_back(BucketOffset);
1169 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001170 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001171}
1172
1173//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001174// Source Manager Serialization
1175//===----------------------------------------------------------------------===//
1176
1177/// \brief Create an abbreviation for the SLocEntry that refers to a
1178/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001179static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001180 using namespace llvm;
1181 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001182 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001183 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1184 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1185 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1186 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001187 // FileEntry fields.
1188 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1189 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor14f79002009-04-10 03:52:48 +00001190 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001191 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001192}
1193
1194/// \brief Create an abbreviation for the SLocEntry that refers to a
1195/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001196static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001197 using namespace llvm;
1198 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001199 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001200 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1201 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1202 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1203 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1204 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001205 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001206}
1207
1208/// \brief Create an abbreviation for the SLocEntry that refers to a
1209/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001210static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001211 using namespace llvm;
1212 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001213 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001214 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001215 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001216}
1217
1218/// \brief Create an abbreviation for the SLocEntry that refers to an
1219/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001220static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001221 using namespace llvm;
1222 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001223 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001224 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1225 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1226 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1227 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001228 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001229 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001230}
1231
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001232namespace {
1233 // Trait used for the on-disk hash table of header search information.
1234 class HeaderFileInfoTrait {
1235 ASTWriter &Writer;
1236 HeaderSearch &HS;
1237
1238 public:
1239 HeaderFileInfoTrait(ASTWriter &Writer, HeaderSearch &HS)
1240 : Writer(Writer), HS(HS) { }
1241
1242 typedef const char *key_type;
1243 typedef key_type key_type_ref;
1244
1245 typedef HeaderFileInfo data_type;
1246 typedef const data_type &data_type_ref;
1247
1248 static unsigned ComputeHash(const char *path) {
1249 // The hash is based only on the filename portion of the key, so that the
1250 // reader can match based on filenames when symlinking or excess path
1251 // elements ("foo/../", "../") change the form of the name. However,
1252 // complete path is still the key.
1253 return llvm::HashString(llvm::sys::path::filename(path));
1254 }
1255
1256 std::pair<unsigned,unsigned>
1257 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1258 data_type_ref Data) {
1259 unsigned StrLen = strlen(path);
1260 clang::io::Emit16(Out, StrLen);
1261 unsigned DataLen = 1 + 2 + 4;
1262 clang::io::Emit8(Out, DataLen);
1263 return std::make_pair(StrLen + 1, DataLen);
1264 }
1265
1266 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1267 Out.write(path, KeyLen);
1268 }
1269
1270 void EmitData(llvm::raw_ostream &Out, key_type_ref,
1271 data_type_ref Data, unsigned DataLen) {
1272 using namespace clang::io;
1273 uint64_t Start = Out.tell(); (void)Start;
1274
1275 unsigned char Flags = (Data.isImport << 3)
1276 | (Data.DirInfo << 1)
1277 | Data.Resolved;
1278 Emit8(Out, (uint8_t)Flags);
1279 Emit16(Out, (uint16_t) Data.NumIncludes);
1280
1281 if (!Data.ControllingMacro)
1282 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1283 else
1284 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
1285 assert(Out.tell() - Start == DataLen && "Wrong data length");
1286 }
1287 };
1288} // end anonymous namespace
1289
1290/// \brief Write the header search block for the list of files that
1291///
1292/// \param HS The header search structure to save.
1293///
1294/// \param Chain Whether we're creating a chained AST file.
1295void ASTWriter::WriteHeaderSearch(HeaderSearch &HS, const char* isysroot) {
1296 llvm::SmallVector<const FileEntry *, 16> FilesByUID;
1297 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1298
1299 if (FilesByUID.size() > HS.header_file_size())
1300 FilesByUID.resize(HS.header_file_size());
1301
1302 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1303 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
1304 llvm::SmallVector<const char *, 4> SavedStrings;
1305 unsigned NumHeaderSearchEntries = 0;
1306 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1307 const FileEntry *File = FilesByUID[UID];
1308 if (!File)
1309 continue;
1310
1311 const HeaderFileInfo &HFI = HS.header_file_begin()[UID];
1312 if (HFI.External && Chain)
1313 continue;
1314
1315 // Turn the file name into an absolute path, if it isn't already.
1316 const char *Filename = File->getName();
1317 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1318
1319 // If we performed any translation on the file name at all, we need to
1320 // save this string, since the generator will refer to it later.
1321 if (Filename != File->getName()) {
1322 Filename = strdup(Filename);
1323 SavedStrings.push_back(Filename);
1324 }
1325
1326 Generator.insert(Filename, HFI, GeneratorTrait);
1327 ++NumHeaderSearchEntries;
1328 }
1329
1330 // Create the on-disk hash table in a buffer.
1331 llvm::SmallString<4096> TableData;
1332 uint32_t BucketOffset;
1333 {
1334 llvm::raw_svector_ostream Out(TableData);
1335 // Make sure that no bucket is at offset 0
1336 clang::io::Emit32(Out, 0);
1337 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1338 }
1339
1340 // Create a blob abbreviation
1341 using namespace llvm;
1342 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1343 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1344 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1345 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1346 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1347 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1348
1349 // Write the stat cache
1350 RecordData Record;
1351 Record.push_back(HEADER_SEARCH_TABLE);
1352 Record.push_back(BucketOffset);
1353 Record.push_back(NumHeaderSearchEntries);
1354 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1355
1356 // Free all of the strings we had to duplicate.
1357 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1358 free((void*)SavedStrings[I]);
1359}
1360
Douglas Gregor14f79002009-04-10 03:52:48 +00001361/// \brief Writes the block containing the serialized form of the
1362/// source manager.
1363///
1364/// TODO: We should probably use an on-disk hash table (stored in a
1365/// blob), indexed based on the file name, so that we only create
1366/// entries for files that we actually need. In the common case (no
1367/// errors), we probably won't have to create file entries for any of
1368/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001369void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001370 const Preprocessor &PP,
1371 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001372 RecordData Record;
1373
Chris Lattnerf04ad692009-04-10 17:16:57 +00001374 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001375 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001376
1377 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001378 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1379 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1380 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1381 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001382
Douglas Gregorbd945002009-04-13 16:31:14 +00001383 // Write the line table.
1384 if (SourceMgr.hasLineTable()) {
1385 LineTableInfo &LineTable = SourceMgr.getLineTable();
1386
1387 // Emit the file names
1388 Record.push_back(LineTable.getNumFilenames());
1389 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1390 // Emit the file name
1391 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001392 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +00001393 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1394 Record.push_back(FilenameLen);
1395 if (FilenameLen)
1396 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1397 }
Mike Stump1eb44332009-09-09 15:08:12 +00001398
Douglas Gregorbd945002009-04-13 16:31:14 +00001399 // Emit the line entries
1400 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1401 L != LEnd; ++L) {
1402 // Emit the file ID
1403 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +00001404
Douglas Gregorbd945002009-04-13 16:31:14 +00001405 // Emit the line entries
1406 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001407 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +00001408 LEEnd = L->second.end();
1409 LE != LEEnd; ++LE) {
1410 Record.push_back(LE->FileOffset);
1411 Record.push_back(LE->LineNo);
1412 Record.push_back(LE->FilenameID);
1413 Record.push_back((unsigned)LE->FileKind);
1414 Record.push_back(LE->IncludeOffset);
1415 }
Douglas Gregorbd945002009-04-13 16:31:14 +00001416 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001417 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001418 }
1419
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001420 // Write out the source location entry table. We skip the first
1421 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001422 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001423 RecordData PreloadSLocs;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001424 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1425 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1426 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1427 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001428 // Get this source location entry.
1429 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001430
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001431 // Record the offset of this source-location entry.
1432 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1433
1434 // Figure out which record code to use.
1435 unsigned Code;
1436 if (SLoc->isFile()) {
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001437 if (SLoc->getFile().getContentCache()->OrigEntry)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001438 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001439 else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001440 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001441 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001442 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001443 Record.clear();
1444 Record.push_back(Code);
1445
1446 Record.push_back(SLoc->getOffset());
1447 if (SLoc->isFile()) {
1448 const SrcMgr::FileInfo &File = SLoc->getFile();
1449 Record.push_back(File.getIncludeLoc().getRawEncoding());
1450 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1451 Record.push_back(File.hasLineDirectives());
1452
1453 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001454 if (Content->OrigEntry) {
1455 assert(Content->OrigEntry == Content->ContentsEntry &&
1456 "Writing to AST an overriden file is not supported");
1457
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001458 // The source location entry is a file. The blob associated
1459 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001460
Douglas Gregor2d52be52010-03-21 22:49:54 +00001461 // Emit size/modification time for this file.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001462 Record.push_back(Content->OrigEntry->getSize());
1463 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregor2d52be52010-03-21 22:49:54 +00001464
Douglas Gregore650c8c2009-07-07 00:12:59 +00001465 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001466 const char *Filename = Content->OrigEntry->getName();
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001467 llvm::SmallString<128> FilePath(Filename);
Anders Carlsson2c10c802011-03-08 16:04:35 +00001468
1469 // Ask the file manager to fixup the relative path for us. This will
1470 // honor the working directory.
1471 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1472
1473 // FIXME: This call to make_absolute shouldn't be necessary, the
1474 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001475 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001476 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001477
Douglas Gregore650c8c2009-07-07 00:12:59 +00001478 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001479 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001480 } else {
1481 // The source location entry is a buffer. The blob associated
1482 // with this entry contains the contents of the buffer.
1483
1484 // We add one to the size so that we capture the trailing NULL
1485 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1486 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001487 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001488 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001489 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001490 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1491 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001492 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001493 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001494 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001495 llvm::StringRef(Buffer->getBufferStart(),
1496 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001497
1498 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001499 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001500 }
1501 } else {
1502 // The source location entry is an instantiation.
1503 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1504 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1505 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1506 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1507
1508 // Compute the token length for this macro expansion.
1509 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001510 if (I + 1 != N)
1511 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001512 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1513 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1514 }
1515 }
1516
Douglas Gregorc9490c02009-04-16 22:23:12 +00001517 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001518
1519 if (SLocEntryOffsets.empty())
1520 return;
1521
Sebastian Redl3397c552010-08-18 23:56:27 +00001522 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001523 // table is used for lazily loading source-location information.
1524 using namespace llvm;
1525 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001526 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001527 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1528 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1529 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1530 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001531
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001532 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001533 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001534 Record.push_back(SLocEntryOffsets.size());
Sebastian Redl8db9fae2010-09-22 20:19:08 +00001535 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1536 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001537 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001538 (const char *)data(SLocEntryOffsets),
Chris Lattner090d9b52009-04-27 19:01:47 +00001539 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001540
Sebastian Redl3397c552010-08-18 23:56:27 +00001541 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001542 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001543 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001544}
1545
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001546//===----------------------------------------------------------------------===//
1547// Preprocessor Serialization
1548//===----------------------------------------------------------------------===//
1549
Douglas Gregor9c736102011-02-10 18:20:09 +00001550static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1551 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1552 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1553 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1554 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1555 return X.first->getName().compare(Y.first->getName());
1556}
1557
Chris Lattner0b1fb982009-04-10 17:15:23 +00001558/// \brief Writes the block containing the serialized form of the
1559/// preprocessor.
1560///
Sebastian Redla4232eb2010-08-18 23:56:21 +00001561void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001562 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001563
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001564 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1565 if (PP.getCounterValue() != 0) {
1566 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001567 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001568 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001569 }
1570
1571 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001572 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001573
Sebastian Redl3397c552010-08-18 23:56:27 +00001574 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001575 // FIXME: use diagnostics subsystem for localization etc.
1576 if (PP.SawDateOrTime())
1577 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001578
Douglas Gregorecdcb882010-10-20 22:00:55 +00001579
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001580 // Loop over all the macro definitions that are live at the end of the file,
1581 // emitting each to the PP section.
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001582 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001583
Douglas Gregor9c736102011-02-10 18:20:09 +00001584 // Construct the list of macro definitions that need to be serialized.
1585 llvm::SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
1586 MacrosToEmit;
1587 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor040a8042011-02-11 00:26:14 +00001588 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1589 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001590 I != E; ++I) {
Douglas Gregor9c736102011-02-10 18:20:09 +00001591 MacroDefinitionsSeen.insert(I->first);
1592 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1593 }
1594
1595 // Sort the set of macro definitions that need to be serialized by the
1596 // name of the macro, to provide a stable ordering.
1597 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1598 &compareMacroDefinitions);
1599
Douglas Gregor040a8042011-02-11 00:26:14 +00001600 // Resolve any identifiers that defined macros at the time they were
1601 // deserialized, adding them to the list of macros to emit (if appropriate).
1602 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1603 IdentifierInfo *Name
1604 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1605 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1606 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1607 }
1608
Douglas Gregor9c736102011-02-10 18:20:09 +00001609 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1610 const IdentifierInfo *Name = MacrosToEmit[I].first;
1611 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor040a8042011-02-11 00:26:14 +00001612 if (!MI)
1613 continue;
1614
Sebastian Redl3397c552010-08-18 23:56:27 +00001615 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001616 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl3397c552010-08-18 23:56:27 +00001617 // Also skip macros from a AST file if we're chaining.
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001618
1619 // FIXME: There is a (probably minor) optimization we could do here, if
1620 // the macro comes from the original PCH but the identifier comes from a
1621 // chained PCH, by storing the offset into the original PCH rather than
1622 // writing the macro definition a second time.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001623 if (MI->isBuiltinMacro() ||
Douglas Gregor9c736102011-02-10 18:20:09 +00001624 (Chain && Name->isFromAST() && MI->isFromAST()))
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001625 continue;
1626
Douglas Gregor9c736102011-02-10 18:20:09 +00001627 AddIdentifierRef(Name, Record);
1628 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001629 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1630 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001631
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001632 unsigned Code;
1633 if (MI->isObjectLike()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001634 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001635 } else {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001636 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001637
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001638 Record.push_back(MI->isC99Varargs());
1639 Record.push_back(MI->isGNUVarargs());
1640 Record.push_back(MI->getNumArgs());
1641 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1642 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001643 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001644 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001645
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001646 // If we have a detailed preprocessing record, record the macro definition
1647 // ID that corresponds to this macro.
1648 if (PPRec)
1649 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer20249a12010-10-21 03:16:25 +00001650
Douglas Gregorc9490c02009-04-16 22:23:12 +00001651 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001652 Record.clear();
1653
Chris Lattnerdf961c22009-04-10 18:08:30 +00001654 // Emit the tokens array.
1655 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1656 // Note that we know that the preprocessor does not have any annotation
1657 // tokens in it because they are created by the parser, and thus can't be
1658 // in a macro definition.
1659 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001660
Chris Lattnerdf961c22009-04-10 18:08:30 +00001661 Record.push_back(Tok.getLocation().getRawEncoding());
1662 Record.push_back(Tok.getLength());
1663
Chris Lattnerdf961c22009-04-10 18:08:30 +00001664 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1665 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001666 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001667 // FIXME: Should translate token kind to a stable encoding.
1668 Record.push_back(Tok.getKind());
1669 // FIXME: Should translate token flags to a stable encoding.
1670 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001671
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001672 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001673 Record.clear();
1674 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001675 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001676 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001677 Stream.ExitBlock();
1678
1679 if (PPRec)
1680 WritePreprocessorDetail(*PPRec);
1681}
1682
1683void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
1684 if (PPRec.begin(Chain) == PPRec.end(Chain))
1685 return;
1686
1687 // Enter the preprocessor block.
1688 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001689
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001690 // If the preprocessor has a preprocessing record, emit it.
1691 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001692 using namespace llvm;
1693
1694 // Set up the abbreviation for
1695 unsigned InclusionAbbrev = 0;
1696 {
1697 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1698 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
1699 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1700 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1701 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1702 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1703 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1704 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1705 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1706 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1707 }
1708
1709 unsigned IndexBase = Chain ? PPRec.getNumPreallocatedEntities() : 0;
1710 RecordData Record;
1711 for (PreprocessingRecord::iterator E = PPRec.begin(Chain),
1712 EEnd = PPRec.end(Chain);
1713 E != EEnd; ++E) {
1714 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001715
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001716 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1717 // Record this macro definition's location.
1718 MacroID ID = getMacroDefinitionID(MD);
1719
1720 // Don't write the macro definition if it is from another AST file.
1721 if (ID < FirstMacroID)
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001722 continue;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001723
Douglas Gregor89d99802010-11-30 06:16:57 +00001724 // Notify the serialization listener that we're serializing this entity.
1725 if (SerializationListener)
1726 SerializationListener->SerializedPreprocessedEntity(*E,
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001727 Stream.GetCurrentBitNo());
Douglas Gregor89d99802010-11-30 06:16:57 +00001728
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001729 unsigned Position = ID - FirstMacroID;
1730 if (Position != MacroDefinitionOffsets.size()) {
1731 if (Position > MacroDefinitionOffsets.size())
1732 MacroDefinitionOffsets.resize(Position + 1);
1733
1734 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
1735 } else
1736 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor89d99802010-11-30 06:16:57 +00001737
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001738 Record.push_back(IndexBase + NumPreprocessingRecords++);
1739 Record.push_back(ID);
1740 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1741 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1742 AddIdentifierRef(MD->getName(), Record);
1743 AddSourceLocation(MD->getLocation(), Record);
1744 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1745 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001746 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001747
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001748 // Notify the serialization listener that we're serializing this entity.
1749 if (SerializationListener)
1750 SerializationListener->SerializedPreprocessedEntity(*E,
1751 Stream.GetCurrentBitNo());
1752
1753 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1754 Record.push_back(IndexBase + NumPreprocessingRecords++);
1755 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1756 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1757 AddIdentifierRef(MI->getName(), Record);
1758 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1759 Stream.EmitRecord(PPD_MACRO_INSTANTIATION, Record);
1760 continue;
1761 }
1762
1763 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1764 Record.push_back(PPD_INCLUSION_DIRECTIVE);
1765 Record.push_back(IndexBase + NumPreprocessingRecords++);
1766 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1767 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1768 Record.push_back(ID->getFileName().size());
1769 Record.push_back(ID->wasInQuotes());
1770 Record.push_back(static_cast<unsigned>(ID->getKind()));
1771 llvm::SmallString<64> Buffer;
1772 Buffer += ID->getFileName();
1773 Buffer += ID->getFile()->getName();
1774 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1775 continue;
1776 }
1777
1778 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1779 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001780 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001781
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001782 // Write the offsets table for the preprocessing record.
1783 if (NumPreprocessingRecords > 0) {
1784 // Write the offsets table for identifier IDs.
1785 using namespace llvm;
1786 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001787 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001788 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1789 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1790 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1791 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001792
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001793 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001794 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001795 Record.push_back(NumPreprocessingRecords);
1796 Record.push_back(MacroDefinitionOffsets.size());
1797 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001798 (const char *)data(MacroDefinitionOffsets),
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001799 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1800 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001801}
1802
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001803void ASTWriter::WritePragmaDiagnosticMappings(const Diagnostic &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001804 RecordData Record;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001805 for (Diagnostic::DiagStatePointsTy::const_iterator
1806 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1807 I != E; ++I) {
1808 const Diagnostic::DiagStatePoint &point = *I;
1809 if (point.Loc.isInvalid())
1810 continue;
1811
1812 Record.push_back(point.Loc.getRawEncoding());
1813 for (Diagnostic::DiagState::iterator
1814 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
1815 unsigned diag = I->first, map = I->second;
1816 if (map & 0x10) { // mapping from a diagnostic pragma.
1817 Record.push_back(diag);
1818 Record.push_back(map & 0x7);
1819 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001820 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001821 Record.push_back(-1); // mark the end of the diag/map pairs for this
1822 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001823 }
1824
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00001825 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001826 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001827}
1828
Anders Carlssonc8505782011-03-06 18:41:18 +00001829void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
1830 if (CXXBaseSpecifiersOffsets.empty())
1831 return;
1832
1833 RecordData Record;
1834
1835 // Create a blob abbreviation for the C++ base specifiers offsets.
1836 using namespace llvm;
1837
1838 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1839 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
1840 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
1841 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1842 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1843
1844 // Write the selector offsets table.
1845 Record.clear();
1846 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
1847 Record.push_back(CXXBaseSpecifiersOffsets.size());
1848 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
1849 (const char *)CXXBaseSpecifiersOffsets.data(),
1850 CXXBaseSpecifiersOffsets.size() * sizeof(uint32_t));
1851}
1852
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001853//===----------------------------------------------------------------------===//
1854// Type Serialization
1855//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001856
Sebastian Redl3397c552010-08-18 23:56:27 +00001857/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001858void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00001859 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001860 if (Idx.getIndex() == 0) // we haven't seen this type before.
1861 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00001862
Douglas Gregor97475832010-10-05 18:37:06 +00001863 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00001864
Douglas Gregor2cf26342009-04-09 22:27:44 +00001865 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001866 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00001867 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001868 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00001869 else if (TypeOffsets.size() < Index) {
1870 TypeOffsets.resize(Index + 1);
1871 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001872 }
1873
1874 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001875
Douglas Gregor2cf26342009-04-09 22:27:44 +00001876 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00001877 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001878
Douglas Gregora4923eb2009-11-16 21:35:15 +00001879 if (T.hasLocalNonFastQualifiers()) {
1880 Qualifiers Qs = T.getLocalQualifiers();
1881 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001882 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001883 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00001884 } else {
1885 switch (T->getTypeClass()) {
1886 // For all of the concrete, non-dependent types, call the
1887 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001888#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001889 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001890#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001891#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001892 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001893 }
1894
1895 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001896 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001897
1898 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001899 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001900}
1901
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001902//===----------------------------------------------------------------------===//
1903// Declaration Serialization
1904//===----------------------------------------------------------------------===//
1905
Douglas Gregor2cf26342009-04-09 22:27:44 +00001906/// \brief Write the block containing all of the declaration IDs
1907/// lexically declared within the given DeclContext.
1908///
1909/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1910/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001911uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001912 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001913 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001914 return 0;
1915
Douglas Gregorc9490c02009-04-16 22:23:12 +00001916 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001917 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001918 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001919 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001920 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1921 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001922 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001923
Douglas Gregor25123082009-04-22 22:34:57 +00001924 ++NumLexicalDeclContexts;
Sebastian Redl681d7232010-07-27 00:17:23 +00001925 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001926 reinterpret_cast<char*>(Decls.data()),
1927 Decls.size() * sizeof(KindDeclIDPair));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001928 return Offset;
1929}
1930
Sebastian Redla4232eb2010-08-18 23:56:21 +00001931void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00001932 using namespace llvm;
1933 RecordData Record;
1934
1935 // Write the type offsets array
1936 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001937 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001938 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1939 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1940 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1941 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001942 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00001943 Record.push_back(TypeOffsets.size());
1944 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001945 (const char *)data(TypeOffsets),
Sebastian Redl1476ed42010-07-16 16:36:56 +00001946 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1947
1948 // Write the declaration offsets array
1949 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001950 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001951 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1952 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1953 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1954 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001955 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00001956 Record.push_back(DeclOffsets.size());
1957 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001958 (const char *)data(DeclOffsets),
Sebastian Redl1476ed42010-07-16 16:36:56 +00001959 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1960}
1961
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001962//===----------------------------------------------------------------------===//
1963// Global Method Pool and Selector Serialization
1964//===----------------------------------------------------------------------===//
1965
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001966namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001967// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00001968class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00001969 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001970
1971public:
1972 typedef Selector key_type;
1973 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001974
Sebastian Redl5d050072010-08-04 17:20:04 +00001975 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001976 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00001977 ObjCMethodList Instance, Factory;
1978 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001979 typedef const data_type& data_type_ref;
1980
Sebastian Redl3397c552010-08-18 23:56:27 +00001981 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001982
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001983 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00001984 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001985 }
Mike Stump1eb44332009-09-09 15:08:12 +00001986
1987 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001988 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1989 data_type_ref Methods) {
1990 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1991 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00001992 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1993 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001994 Method = Method->Next)
1995 if (Method->Method)
1996 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00001997 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001998 Method = Method->Next)
1999 if (Method->Method)
2000 DataLen += 4;
2001 clang::io::Emit16(Out, DataLen);
2002 return std::make_pair(KeyLen, DataLen);
2003 }
Mike Stump1eb44332009-09-09 15:08:12 +00002004
Douglas Gregor83941df2009-04-25 17:48:32 +00002005 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002006 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002007 assert((Start >> 32) == 0 && "Selector key offset too large");
2008 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002009 unsigned N = Sel.getNumArgs();
2010 clang::io::Emit16(Out, N);
2011 if (N == 0)
2012 N = 1;
2013 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002014 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002015 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2016 }
Mike Stump1eb44332009-09-09 15:08:12 +00002017
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002018 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002019 data_type_ref Methods, unsigned DataLen) {
2020 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002021 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002022 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002023 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002024 Method = Method->Next)
2025 if (Method->Method)
2026 ++NumInstanceMethods;
2027
2028 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002029 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002030 Method = Method->Next)
2031 if (Method->Method)
2032 ++NumFactoryMethods;
2033
2034 clang::io::Emit16(Out, NumInstanceMethods);
2035 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002036 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002037 Method = Method->Next)
2038 if (Method->Method)
2039 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002040 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002041 Method = Method->Next)
2042 if (Method->Method)
2043 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002044
2045 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002046 }
2047};
2048} // end anonymous namespace
2049
Sebastian Redl059612d2010-08-03 21:58:15 +00002050/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002051///
2052/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002053/// in an on-disk hash table indexed by the selector. The hash table also
2054/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002055void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002056 using namespace llvm;
2057
Sebastian Redl059612d2010-08-03 21:58:15 +00002058 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002059 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002060 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002061 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002062 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002063 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002064 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002065 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002066
Sebastian Redl059612d2010-08-03 21:58:15 +00002067 // Create the on-disk hash table representation. We walk through every
2068 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002069 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002070 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002071 I = SelectorIDs.begin(), E = SelectorIDs.end();
2072 I != E; ++I) {
2073 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002074 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002075 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002076 I->second,
2077 ObjCMethodList(),
2078 ObjCMethodList()
2079 };
2080 if (F != SemaRef.MethodPool.end()) {
2081 Data.Instance = F->second.first;
2082 Data.Factory = F->second.second;
2083 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002084 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002085 // changed.
2086 if (Chain && I->second < FirstSelectorID) {
2087 // Selector already exists. Did it change?
2088 bool changed = false;
2089 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2090 M = M->Next) {
2091 if (M->Method->getPCHLevel() == 0)
2092 changed = true;
2093 }
2094 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2095 M = M->Next) {
2096 if (M->Method->getPCHLevel() == 0)
2097 changed = true;
2098 }
2099 if (!changed)
2100 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002101 } else if (Data.Instance.Method || Data.Factory.Method) {
2102 // A new method pool entry.
2103 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002104 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002105 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002106 }
2107
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002108 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002109 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002110 uint32_t BucketOffset;
2111 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002112 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002113 llvm::raw_svector_ostream Out(MethodPool);
2114 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002115 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002116 BucketOffset = Generator.Emit(Out, Trait);
2117 }
2118
2119 // Create a blob abbreviation
2120 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002121 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002122 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002123 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002124 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2125 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2126
Douglas Gregor83941df2009-04-25 17:48:32 +00002127 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002128 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002129 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002130 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002131 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002132 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002133
2134 // Create a blob abbreviation for the selector table offsets.
2135 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002136 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002137 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor83941df2009-04-25 17:48:32 +00002138 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2139 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2140
2141 // Write the selector offsets table.
2142 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002143 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002144 Record.push_back(SelectorOffsets.size());
2145 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00002146 (const char *)data(SelectorOffsets),
Douglas Gregor83941df2009-04-25 17:48:32 +00002147 SelectorOffsets.size() * 4);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002148 }
2149}
2150
Sebastian Redl3397c552010-08-18 23:56:27 +00002151/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002152void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002153 using namespace llvm;
2154 if (SemaRef.ReferencedSelectors.empty())
2155 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002156
Fariborz Jahanian32019832010-07-23 19:11:11 +00002157 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002158
Sebastian Redl3397c552010-08-18 23:56:27 +00002159 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002160 // very tricky to fix, and given that @selector shouldn't really appear in
2161 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002162 for (DenseMap<Selector, SourceLocation>::iterator S =
2163 SemaRef.ReferencedSelectors.begin(),
2164 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2165 Selector Sel = (*S).first;
2166 SourceLocation Loc = (*S).second;
2167 AddSelectorRef(Sel, Record);
2168 AddSourceLocation(Loc, Record);
2169 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002170 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002171}
2172
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002173//===----------------------------------------------------------------------===//
2174// Identifier Table Serialization
2175//===----------------------------------------------------------------------===//
2176
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002177namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002178class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002179 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002180 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002181
Douglas Gregora92193e2009-04-28 21:18:29 +00002182 /// \brief Determines whether this is an "interesting" identifier
2183 /// that needs a full IdentifierInfo structure written into the hash
2184 /// table.
2185 static bool isInterestingIdentifier(const IdentifierInfo *II) {
2186 return II->isPoisoned() ||
2187 II->isExtensionToken() ||
2188 II->hasMacroDefinition() ||
2189 II->getObjCOrBuiltinID() ||
2190 II->getFETokenInfo<void>();
2191 }
2192
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002193public:
2194 typedef const IdentifierInfo* key_type;
2195 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002196
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002197 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002198 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002199
Sebastian Redl3397c552010-08-18 23:56:27 +00002200 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00002201 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002202
2203 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002204 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002205 }
Mike Stump1eb44332009-09-09 15:08:12 +00002206
2207 std::pair<unsigned,unsigned>
2208 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002209 IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002210 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002211 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
2212 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00002213 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00002214 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00002215 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00002216 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00002217 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2218 DEnd = IdentifierResolver::end();
2219 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002220 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002221 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002222 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002223 // We emit the key length after the data length so that every
2224 // string is preceded by a 16-bit length. This matches the PTH
2225 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002226 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002227 return std::make_pair(KeyLen, DataLen);
2228 }
Mike Stump1eb44332009-09-09 15:08:12 +00002229
2230 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002231 unsigned KeyLen) {
2232 // Record the location of the key data. This is used when generating
2233 // the mapping from persistent IDs to strings.
2234 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002235 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002236 }
Mike Stump1eb44332009-09-09 15:08:12 +00002237
2238 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002239 IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002240 if (!isInterestingIdentifier(II)) {
2241 clang::io::Emit32(Out, ID << 1);
2242 return;
2243 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002244
Douglas Gregora92193e2009-04-28 21:18:29 +00002245 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002246 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002247 bool hasMacroDefinition =
2248 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00002249 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00002250 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002251 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
2252 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2253 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002254 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002255 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002256 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002257
Douglas Gregor37e26842009-04-21 23:56:24 +00002258 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00002259 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00002260
Douglas Gregor668c1a42009-04-21 22:25:48 +00002261 // Emit the declaration IDs in reverse order, because the
2262 // IdentifierResolver provides the declarations as they would be
2263 // visible (e.g., the function "stat" would come before the struct
2264 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2265 // adds declarations to the end of the list (so we need to see the
2266 // struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002267 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump1eb44332009-09-09 15:08:12 +00002268 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00002269 IdentifierResolver::end());
2270 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
2271 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002272 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002273 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002274 }
2275};
2276} // end anonymous namespace
2277
Sebastian Redl3397c552010-08-18 23:56:27 +00002278/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002279///
2280/// The identifier table consists of a blob containing string data
2281/// (the actual identifiers themselves) and a separate "offsets" index
2282/// that maps identifier IDs to locations within the blob.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002283void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002284 using namespace llvm;
2285
2286 // Create and write out the blob that contains the identifier
2287 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002288 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002289 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002290 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump1eb44332009-09-09 15:08:12 +00002291
Douglas Gregor92b059e2009-04-28 20:33:11 +00002292 // Look for any identifiers that were named while processing the
2293 // headers, but are otherwise not needed. We add these to the hash
2294 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002295 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002296 // file.
2297 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2298 IDEnd = PP.getIdentifierTable().end();
2299 ID != IDEnd; ++ID)
2300 getIdentifierRef(ID->second);
2301
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002302 // Create the on-disk hash table representation. We only store offsets
2303 // for identifiers that appear here for the first time.
2304 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002305 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002306 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2307 ID != IDEnd; ++ID) {
2308 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002309 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002310 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002311 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002312
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002313 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002314 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002315 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002316 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002317 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002318 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002319 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002320 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002321 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002322 }
2323
2324 // Create a blob abbreviation
2325 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002326 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002327 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002328 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002329 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002330
2331 // Write the identifier table
2332 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002333 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002334 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002335 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002336 }
2337
2338 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002339 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002340 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002341 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2342 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2343 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2344
2345 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002346 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002347 Record.push_back(IdentifierOffsets.size());
2348 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00002349 (const char *)data(IdentifierOffsets),
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002350 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002351}
2352
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002353//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002354// DeclContext's Name Lookup Table Serialization
2355//===----------------------------------------------------------------------===//
2356
2357namespace {
2358// Trait used for the on-disk hash table used in the method pool.
2359class ASTDeclContextNameLookupTrait {
2360 ASTWriter &Writer;
2361
2362public:
2363 typedef DeclarationName key_type;
2364 typedef key_type key_type_ref;
2365
2366 typedef DeclContext::lookup_result data_type;
2367 typedef const data_type& data_type_ref;
2368
2369 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2370
2371 unsigned ComputeHash(DeclarationName Name) {
2372 llvm::FoldingSetNodeID ID;
2373 ID.AddInteger(Name.getNameKind());
2374
2375 switch (Name.getNameKind()) {
2376 case DeclarationName::Identifier:
2377 ID.AddString(Name.getAsIdentifierInfo()->getName());
2378 break;
2379 case DeclarationName::ObjCZeroArgSelector:
2380 case DeclarationName::ObjCOneArgSelector:
2381 case DeclarationName::ObjCMultiArgSelector:
2382 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2383 break;
2384 case DeclarationName::CXXConstructorName:
2385 case DeclarationName::CXXDestructorName:
2386 case DeclarationName::CXXConversionFunctionName:
2387 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
2388 break;
2389 case DeclarationName::CXXOperatorName:
2390 ID.AddInteger(Name.getCXXOverloadedOperator());
2391 break;
2392 case DeclarationName::CXXLiteralOperatorName:
2393 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2394 case DeclarationName::CXXUsingDirective:
2395 break;
2396 }
2397
2398 return ID.ComputeHash();
2399 }
2400
2401 std::pair<unsigned,unsigned>
2402 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2403 data_type_ref Lookup) {
2404 unsigned KeyLen = 1;
2405 switch (Name.getNameKind()) {
2406 case DeclarationName::Identifier:
2407 case DeclarationName::ObjCZeroArgSelector:
2408 case DeclarationName::ObjCOneArgSelector:
2409 case DeclarationName::ObjCMultiArgSelector:
2410 case DeclarationName::CXXConstructorName:
2411 case DeclarationName::CXXDestructorName:
2412 case DeclarationName::CXXConversionFunctionName:
2413 case DeclarationName::CXXLiteralOperatorName:
2414 KeyLen += 4;
2415 break;
2416 case DeclarationName::CXXOperatorName:
2417 KeyLen += 1;
2418 break;
2419 case DeclarationName::CXXUsingDirective:
2420 break;
2421 }
2422 clang::io::Emit16(Out, KeyLen);
2423
2424 // 2 bytes for num of decls and 4 for each DeclID.
2425 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2426 clang::io::Emit16(Out, DataLen);
2427
2428 return std::make_pair(KeyLen, DataLen);
2429 }
2430
2431 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2432 using namespace clang::io;
2433
2434 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2435 Emit8(Out, Name.getNameKind());
2436 switch (Name.getNameKind()) {
2437 case DeclarationName::Identifier:
2438 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2439 break;
2440 case DeclarationName::ObjCZeroArgSelector:
2441 case DeclarationName::ObjCOneArgSelector:
2442 case DeclarationName::ObjCMultiArgSelector:
2443 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2444 break;
2445 case DeclarationName::CXXConstructorName:
2446 case DeclarationName::CXXDestructorName:
2447 case DeclarationName::CXXConversionFunctionName:
2448 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2449 break;
2450 case DeclarationName::CXXOperatorName:
2451 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2452 Emit8(Out, Name.getCXXOverloadedOperator());
2453 break;
2454 case DeclarationName::CXXLiteralOperatorName:
2455 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2456 break;
2457 case DeclarationName::CXXUsingDirective:
2458 break;
2459 }
2460 }
2461
2462 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2463 data_type Lookup, unsigned DataLen) {
2464 uint64_t Start = Out.tell(); (void)Start;
2465 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2466 for (; Lookup.first != Lookup.second; ++Lookup.first)
2467 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2468
2469 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2470 }
2471};
2472} // end anonymous namespace
2473
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002474/// \brief Write the block containing all of the declaration IDs
2475/// visible from the given DeclContext.
2476///
2477/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002478/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002479uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2480 DeclContext *DC) {
2481 if (DC->getPrimaryContext() != DC)
2482 return 0;
2483
2484 // Since there is no name lookup into functions or methods, don't bother to
2485 // build a visible-declarations table for these entities.
2486 if (DC->isFunctionOrMethod())
2487 return 0;
2488
2489 // If not in C++, we perform name lookup for the translation unit via the
2490 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2491 // FIXME: In C++ we need the visible declarations in order to "see" the
2492 // friend declarations, is there a way to do this without writing the table ?
2493 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2494 return 0;
2495
2496 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00002497 if (DC->hasExternalVisibleStorage())
2498 DC->MaterializeVisibleDeclsFromExternalStorage();
2499 else
2500 DC->lookup(DeclarationName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002501
2502 // Serialize the contents of the mapping used for lookup. Note that,
2503 // although we have two very different code paths, the serialized
2504 // representation is the same for both cases: a declaration name,
2505 // followed by a size, followed by references to the visible
2506 // declarations that have that name.
2507 uint64_t Offset = Stream.GetCurrentBitNo();
2508 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2509 if (!Map || Map->empty())
2510 return 0;
2511
2512 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2513 ASTDeclContextNameLookupTrait Trait(*this);
2514
2515 // Create the on-disk hash table representation.
2516 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2517 D != DEnd; ++D) {
2518 DeclarationName Name = D->first;
2519 DeclContext::lookup_result Result = D->second.getLookupResult();
2520 Generator.insert(Name, Result, Trait);
2521 }
2522
2523 // Create the on-disk hash table in a buffer.
2524 llvm::SmallString<4096> LookupTable;
2525 uint32_t BucketOffset;
2526 {
2527 llvm::raw_svector_ostream Out(LookupTable);
2528 // Make sure that no bucket is at offset 0
2529 clang::io::Emit32(Out, 0);
2530 BucketOffset = Generator.Emit(Out, Trait);
2531 }
2532
2533 // Write the lookup table
2534 RecordData Record;
2535 Record.push_back(DECL_CONTEXT_VISIBLE);
2536 Record.push_back(BucketOffset);
2537 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2538 LookupTable.str());
2539
2540 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2541 ++NumVisibleDeclContexts;
2542 return Offset;
2543}
2544
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002545/// \brief Write an UPDATE_VISIBLE block for the given context.
2546///
2547/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2548/// DeclContext in a dependent AST file. As such, they only exist for the TU
2549/// (in C++) and for namespaces.
2550void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002551 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2552 if (!Map || Map->empty())
2553 return;
2554
2555 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2556 ASTDeclContextNameLookupTrait Trait(*this);
2557
2558 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002559 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2560 D != DEnd; ++D) {
2561 DeclarationName Name = D->first;
2562 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002563 // For any name that appears in this table, the results are complete, i.e.
2564 // they overwrite results from previous PCHs. Merging is always a mess.
2565 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002566 }
2567
2568 // Create the on-disk hash table in a buffer.
2569 llvm::SmallString<4096> LookupTable;
2570 uint32_t BucketOffset;
2571 {
2572 llvm::raw_svector_ostream Out(LookupTable);
2573 // Make sure that no bucket is at offset 0
2574 clang::io::Emit32(Out, 0);
2575 BucketOffset = Generator.Emit(Out, Trait);
2576 }
2577
2578 // Write the lookup table
2579 RecordData Record;
2580 Record.push_back(UPDATE_VISIBLE);
2581 Record.push_back(getDeclID(cast<Decl>(DC)));
2582 Record.push_back(BucketOffset);
2583 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2584}
2585
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002586/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2587void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2588 RecordData Record;
2589 Record.push_back(Opts.fp_contract);
2590 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2591}
2592
2593/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2594void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
2595 if (!SemaRef.Context.getLangOptions().OpenCL)
2596 return;
2597
2598 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2599 RecordData Record;
2600#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2601#include "clang/Basic/OpenCLExtensions.def"
2602 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2603}
2604
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002605//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002606// General Serialization Routines
2607//===----------------------------------------------------------------------===//
2608
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002609/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002610void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00002611 Record.push_back(Attrs.size());
Sean Huntcf807c42010-08-18 23:23:40 +00002612 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2613 const Attr * A = *i;
2614 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2615 AddSourceLocation(A->getLocation(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002616
Sean Huntcf807c42010-08-18 23:23:40 +00002617#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00002618
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002619 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002620}
2621
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002622void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002623 Record.push_back(Str.size());
2624 Record.insert(Record.end(), Str.begin(), Str.end());
2625}
2626
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002627void ASTWriter::AddVersionTuple(const VersionTuple &Version,
2628 RecordDataImpl &Record) {
2629 Record.push_back(Version.getMajor());
2630 if (llvm::Optional<unsigned> Minor = Version.getMinor())
2631 Record.push_back(*Minor + 1);
2632 else
2633 Record.push_back(0);
2634 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
2635 Record.push_back(*Subminor + 1);
2636 else
2637 Record.push_back(0);
2638}
2639
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002640/// \brief Note that the identifier II occurs at the given offset
2641/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002642void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002643 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00002644 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002645 // up earlier in the chain and thus don't need an offset.
2646 if (ID >= FirstIdentID)
2647 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002648}
2649
Douglas Gregor83941df2009-04-25 17:48:32 +00002650/// \brief Note that the selector Sel occurs at the given offset
2651/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002652void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00002653 unsigned ID = SelectorIDs[Sel];
2654 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00002655 // Don't record offsets for selectors that are also available in a different
2656 // file.
2657 if (ID < FirstSelectorID)
2658 return;
2659 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00002660}
2661
Sebastian Redla4232eb2010-08-18 23:56:21 +00002662ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregor89d99802010-11-30 06:16:57 +00002663 : Stream(Stream), Chain(0), SerializationListener(0),
2664 FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002665 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redle58aa892010-08-04 18:21:41 +00002666 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor77424bc2010-10-02 19:29:26 +00002667 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2668 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00002669 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor7c789c12010-10-29 22:39:52 +00002670 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
2671 NextCXXBaseSpecifiersID(1)
2672{
Sebastian Redl30c514c2010-07-14 23:45:08 +00002673}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002674
Sebastian Redla4232eb2010-08-18 23:56:21 +00002675void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002676 const std::string &OutputFile,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002677 const char *isysroot) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002678 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002679 Stream.Emit((unsigned)'C', 8);
2680 Stream.Emit((unsigned)'P', 8);
2681 Stream.Emit((unsigned)'C', 8);
2682 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002683
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002684 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002685
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002686 if (Chain)
Sebastian Redla4232eb2010-08-18 23:56:21 +00002687 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002688 else
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002689 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002690}
2691
Sebastian Redla4232eb2010-08-18 23:56:21 +00002692void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002693 const char *isysroot,
2694 const std::string &OutputFile) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002695 using namespace llvm;
2696
2697 ASTContext &Context = SemaRef.Context;
2698 Preprocessor &PP = SemaRef.PP;
2699
Douglas Gregor2cf26342009-04-09 22:27:44 +00002700 // The translation unit is the first declaration we'll emit.
2701 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002702 ++NextDeclID;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002703 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002704
Douglas Gregor2deaea32009-04-22 18:49:13 +00002705 // Make sure that we emit IdentifierInfos (and any attached
2706 // declarations) for builtins.
2707 {
2708 IdentifierTable &Table = PP.getIdentifierTable();
2709 llvm::SmallVector<const char *, 32> BuiltinNames;
2710 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2711 Context.getLangOptions().NoBuiltin);
2712 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2713 getIdentifierRef(&Table.get(BuiltinNames[I]));
2714 }
2715
Chris Lattner63d65f82009-09-08 18:19:27 +00002716 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002717 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002718 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002719 RecordData TentativeDefinitions;
Sebastian Redle9d12b62010-01-31 22:27:38 +00002720 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2721 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner63d65f82009-09-08 18:19:27 +00002722 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002723
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002724 // Build a record containing all of the file scoped decls in this file.
2725 RecordData UnusedFileScopedDecls;
2726 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2727 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00002728
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002729 RecordData WeakUndeclaredIdentifiers;
2730 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2731 WeakUndeclaredIdentifiers.push_back(
2732 SemaRef.WeakUndeclaredIdentifiers.size());
2733 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2734 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2735 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2736 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2737 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2738 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2739 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2740 }
2741 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002742
Douglas Gregor14c22f22009-04-22 22:18:58 +00002743 // Build a record containing all of the locally-scoped external
2744 // declarations in this header file. Generally, this record will be
2745 // empty.
2746 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002747 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00002748 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002749 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002750 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2751 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2752 TD != TDEnd; ++TD)
2753 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2754
Douglas Gregorb81c1702009-04-27 20:06:05 +00002755 // Build a record containing all of the ext_vector declarations.
2756 RecordData ExtVectorDecls;
2757 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2758 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2759
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002760 // Build a record containing all of the VTable uses information.
2761 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00002762 if (!SemaRef.VTableUses.empty()) {
2763 VTableUses.push_back(SemaRef.VTableUses.size());
2764 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2765 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2766 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2767 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2768 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002769 }
2770
2771 // Build a record containing all of dynamic classes declarations.
2772 RecordData DynamicClasses;
2773 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2774 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2775
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002776 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002777 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002778 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00002779 I = SemaRef.PendingInstantiations.begin(),
2780 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2781 AddDeclRef(I->first, PendingInstantiations);
2782 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002783 }
2784 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2785 "There are local ones at end of translation unit!");
2786
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002787 // Build a record containing some declaration references.
2788 RecordData SemaDeclRefs;
2789 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2790 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2791 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2792 }
2793
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002794 RecordData CUDASpecialDeclRefs;
2795 if (Context.getcudaConfigureCallDecl()) {
2796 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
2797 }
2798
Sebastian Redl3397c552010-08-18 23:56:27 +00002799 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002800 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002801 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002802 WriteMetadata(Context, isysroot, OutputFile);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002803 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00002804 if (StatCalls && !isysroot)
Douglas Gregordd41ed52010-07-12 23:48:14 +00002805 WriteStatCache(*StatCalls);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002806 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002807 // Write the record of special types.
2808 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00002809
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002810 AddTypeRef(Context.getBuiltinVaListType(), Record);
2811 AddTypeRef(Context.getObjCIdType(), Record);
2812 AddTypeRef(Context.getObjCSelType(), Record);
2813 AddTypeRef(Context.getObjCProtoType(), Record);
2814 AddTypeRef(Context.getObjCClassType(), Record);
2815 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2816 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2817 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002818 AddTypeRef(Context.getjmp_bufType(), Record);
2819 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002820 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2821 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpadaaad32009-10-20 02:12:22 +00002822 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002823 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002824 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2825 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002826 Record.push_back(Context.isInt128Installed());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002827 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002828
Douglas Gregor366809a2009-04-26 03:49:13 +00002829 // Keep writing types and declarations until all types and
2830 // declarations have been written.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002831 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002832 WriteDeclsBlockAbbrevs();
2833 while (!DeclTypesToEmit.empty()) {
2834 DeclOrType DOT = DeclTypesToEmit.front();
2835 DeclTypesToEmit.pop();
2836 if (DOT.isType())
2837 WriteType(DOT.getType());
2838 else
2839 WriteDecl(Context, DOT.getDecl());
2840 }
2841 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002842
Douglas Gregor813a97b2009-10-17 17:25:45 +00002843 WritePreprocessor(PP);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002844 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00002845 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002846 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002847 WriteIdentifierTable(PP);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002848 WriteFPPragmaOptions(SemaRef.getFPOptions());
2849 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002850
Sebastian Redl1476ed42010-07-16 16:36:56 +00002851 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002852 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00002853
Anders Carlssonc8505782011-03-06 18:41:18 +00002854 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00002855
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002856 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002857 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002858 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002859
2860 // Write the record containing tentative definitions.
2861 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002862 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002863
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002864 // Write the record containing unused file scoped decls.
2865 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002866 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002867
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002868 // Write the record containing weak undeclared identifiers.
2869 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002870 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002871 WeakUndeclaredIdentifiers);
2872
Douglas Gregor14c22f22009-04-22 22:18:58 +00002873 // Write the record containing locally-scoped external definitions.
2874 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002875 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002876 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002877
2878 // Write the record containing ext_vector type names.
2879 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002880 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002881
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002882 // Write the record containing VTable uses information.
2883 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002884 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002885
2886 // Write the record containing dynamic classes declarations.
2887 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002888 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002889
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002890 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002891 if (!PendingInstantiations.empty())
2892 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002893
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002894 // Write the record containing declaration references of Sema.
2895 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002896 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002897
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002898 // Write the record containing CUDA-specific declaration references.
2899 if (!CUDASpecialDeclRefs.empty())
2900 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
2901
Douglas Gregor3e1af842009-04-17 22:13:46 +00002902 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002903 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002904 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002905 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002906 Record.push_back(NumLexicalDeclContexts);
2907 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002908 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002909 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002910}
2911
Sebastian Redla4232eb2010-08-18 23:56:21 +00002912void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002913 const char *isysroot) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002914 using namespace llvm;
2915
2916 ASTContext &Context = SemaRef.Context;
2917 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1476ed42010-07-16 16:36:56 +00002918
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002919 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002920 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002921 WriteMetadata(Context, isysroot, "");
Sebastian Redl1476ed42010-07-16 16:36:56 +00002922 if (StatCalls && !isysroot)
2923 WriteStatCache(*StatCalls);
2924 // FIXME: Source manager block should only write new stuff, which could be
2925 // done by tracking the largest ID in the chain
2926 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002927
2928 // The special types are in the chained PCH.
2929
2930 // We don't start with the translation unit, but with its decls that
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002931 // don't come from the chained PCH.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002932 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002933 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl681d7232010-07-27 00:17:23 +00002934 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2935 E = TU->noload_decls_end();
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002936 I != E; ++I) {
Sebastian Redld692af72010-07-27 18:24:41 +00002937 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002938 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redl0b17c612010-08-13 00:28:03 +00002939 else if ((*I)->isChangedSinceDeserialization())
2940 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002941 }
Sebastian Redl681d7232010-07-27 00:17:23 +00002942 // We also need to write a lexical updates block for the TU.
Sebastian Redld692af72010-07-27 18:24:41 +00002943 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002944 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redld692af72010-07-27 18:24:41 +00002945 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2946 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2947 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002948 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redld692af72010-07-27 18:24:41 +00002949 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2950 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002951 NewGlobalDecls.size() * sizeof(KindDeclIDPair));
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00002952 // And a visible updates block for the DeclContexts.
2953 Abv = new llvm::BitCodeAbbrev();
2954 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2955 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2956 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2957 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2958 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2959 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002960
Sebastian Redl083abdf2010-07-27 23:01:28 +00002961 // Build a record containing all of the new tentative definitions in this
2962 // file, in TentativeDefinitions order.
2963 RecordData TentativeDefinitions;
2964 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2965 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2966 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2967 }
2968
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002969 // Build a record containing all of the file scoped decls in this file.
2970 RecordData UnusedFileScopedDecls;
2971 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2972 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2973 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002974 }
2975
Sebastian Redl40566802010-08-05 18:21:25 +00002976 // We write the entire table, overwriting the tables from the chain.
2977 RecordData WeakUndeclaredIdentifiers;
2978 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2979 WeakUndeclaredIdentifiers.push_back(
2980 SemaRef.WeakUndeclaredIdentifiers.size());
2981 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2982 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2983 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2984 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2985 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2986 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2987 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2988 }
2989 }
2990
Sebastian Redl083abdf2010-07-27 23:01:28 +00002991 // Build a record containing all of the locally-scoped external
2992 // declarations in this header file. Generally, this record will be
2993 // empty.
2994 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002995 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl083abdf2010-07-27 23:01:28 +00002996 // nondeterminstic!
2997 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2998 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2999 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
3000 TD != TDEnd; ++TD) {
3001 if (TD->second->getPCHLevel() == 0)
3002 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3003 }
3004
3005 // Build a record containing all of the ext_vector declarations.
3006 RecordData ExtVectorDecls;
3007 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
3008 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
3009 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
3010 }
3011
Sebastian Redl40566802010-08-05 18:21:25 +00003012 // Build a record containing all of the VTable uses information.
3013 // We write everything here, because it's too hard to determine whether
3014 // a use is new to this part.
3015 RecordData VTableUses;
3016 if (!SemaRef.VTableUses.empty()) {
3017 VTableUses.push_back(SemaRef.VTableUses.size());
3018 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3019 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3020 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3021 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3022 }
3023 }
3024
3025 // Build a record containing all of dynamic classes declarations.
3026 RecordData DynamicClasses;
3027 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
3028 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
3029 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
3030
3031 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003032 RecordData PendingInstantiations;
Sebastian Redl40566802010-08-05 18:21:25 +00003033 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003034 I = SemaRef.PendingInstantiations.begin(),
3035 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl40566802010-08-05 18:21:25 +00003036 if (I->first->getPCHLevel() == 0) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00003037 AddDeclRef(I->first, PendingInstantiations);
3038 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl40566802010-08-05 18:21:25 +00003039 }
3040 }
3041 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3042 "There are local ones at end of translation unit!");
3043
3044 // Build a record containing some declaration references.
3045 // It's not worth the effort to avoid duplication here.
3046 RecordData SemaDeclRefs;
3047 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3048 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3049 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3050 }
3051
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003052 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003053 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00003054 for (DeclsToRewriteTy::iterator
3055 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
3056 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003057 while (!DeclTypesToEmit.empty()) {
3058 DeclOrType DOT = DeclTypesToEmit.front();
3059 DeclTypesToEmit.pop();
3060 if (DOT.isType())
3061 WriteType(DOT.getType());
3062 else
3063 WriteDecl(Context, DOT.getDecl());
3064 }
3065 Stream.ExitBlock();
3066
Sebastian Redl083abdf2010-07-27 23:01:28 +00003067 WritePreprocessor(PP);
Sebastian Redla68340f2010-08-04 22:21:29 +00003068 WriteSelectors(SemaRef);
3069 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003070 WriteIdentifierTable(PP);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003071 WriteFPPragmaOptions(SemaRef.getFPOptions());
3072 WriteOpenCLExtensions(SemaRef);
3073
Sebastian Redl1476ed42010-07-16 16:36:56 +00003074 WriteTypeDeclOffsets();
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00003075 // FIXME: For chained PCH only write the new mappings (we currently
3076 // write all of them again).
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003077 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl083abdf2010-07-27 23:01:28 +00003078
Anders Carlssonc8505782011-03-06 18:41:18 +00003079 WriteCXXBaseSpecifiersOffsets();
3080
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003081 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl3397c552010-08-18 23:56:27 +00003082 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003083 RecordData FirstLatestDeclIDs;
3084 for (FirstLatestDeclMap::iterator
3085 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
3086 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
3087 "Expected first & second to be in different PCHs");
3088 AddDeclRef(I->first, FirstLatestDeclIDs);
3089 AddDeclRef(I->second, FirstLatestDeclIDs);
3090 }
3091 if (!FirstLatestDeclIDs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003092 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003093
Sebastian Redl083abdf2010-07-27 23:01:28 +00003094 // Write the record containing external, unnamed definitions.
3095 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003096 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003097
3098 // Write the record containing tentative definitions.
3099 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003100 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003101
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003102 // Write the record containing unused file scoped decls.
3103 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003104 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003105
Sebastian Redl40566802010-08-05 18:21:25 +00003106 // Write the record containing weak undeclared identifiers.
3107 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003108 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl40566802010-08-05 18:21:25 +00003109 WeakUndeclaredIdentifiers);
3110
Sebastian Redl083abdf2010-07-27 23:01:28 +00003111 // Write the record containing locally-scoped external definitions.
3112 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003113 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl083abdf2010-07-27 23:01:28 +00003114 LocallyScopedExternalDecls);
3115
3116 // Write the record containing ext_vector type names.
3117 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003118 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003119
Sebastian Redl40566802010-08-05 18:21:25 +00003120 // Write the record containing VTable uses information.
3121 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003122 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl40566802010-08-05 18:21:25 +00003123
3124 // Write the record containing dynamic classes declarations.
3125 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003126 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl40566802010-08-05 18:21:25 +00003127
3128 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003129 if (!PendingInstantiations.empty())
3130 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl40566802010-08-05 18:21:25 +00003131
3132 // Write the record containing declaration references of Sema.
3133 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003134 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003135
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00003136 // Write the updates to DeclContexts.
3137 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3138 I = UpdatedDeclContexts.begin(),
3139 E = UpdatedDeclContexts.end();
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003140 I != E; ++I)
3141 WriteDeclContextVisibleUpdate(*I);
3142
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003143 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003144
Sebastian Redl083abdf2010-07-27 23:01:28 +00003145 Record.clear();
3146 Record.push_back(NumStatements);
3147 Record.push_back(NumMacros);
3148 Record.push_back(NumLexicalDeclContexts);
3149 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003150 WriteDeclReplacementsBlock();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003151 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003152 Stream.ExitBlock();
3153}
3154
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003155void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003156 if (DeclUpdates.empty())
3157 return;
3158
3159 RecordData OffsetsRecord;
3160 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
3161 for (DeclUpdateMap::iterator
3162 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3163 const Decl *D = I->first;
3164 UpdateRecord &URec = I->second;
3165
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003166 if (DeclsToRewrite.count(D))
3167 continue; // The decl will be written completely,no need to store updates.
3168
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003169 uint64_t Offset = Stream.GetCurrentBitNo();
3170 Stream.EmitRecord(DECL_UPDATES, URec);
3171
3172 OffsetsRecord.push_back(GetDeclRef(D));
3173 OffsetsRecord.push_back(Offset);
3174 }
3175 Stream.ExitBlock();
3176 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3177}
3178
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003179void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003180 if (ReplacedDecls.empty())
3181 return;
3182
3183 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003184 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003185 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
3186 Record.push_back(I->first);
3187 Record.push_back(I->second);
3188 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003189 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003190}
3191
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003192void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003193 Record.push_back(Loc.getRawEncoding());
3194}
3195
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003196void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003197 AddSourceLocation(Range.getBegin(), Record);
3198 AddSourceLocation(Range.getEnd(), Record);
3199}
3200
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003201void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003202 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003203 const uint64_t *Words = Value.getRawData();
3204 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003205}
3206
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003207void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003208 Record.push_back(Value.isUnsigned());
3209 AddAPInt(Value, Record);
3210}
3211
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003212void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003213 AddAPInt(Value.bitcastToAPInt(), Record);
3214}
3215
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003216void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003217 Record.push_back(getIdentifierRef(II));
3218}
3219
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003220IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003221 if (II == 0)
3222 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003223
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003224 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003225 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003226 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003227 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003228}
3229
Sebastian Redlf73c93f2010-09-15 19:54:06 +00003230MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003231 if (MD == 0)
3232 return 0;
Sebastian Redlf73c93f2010-09-15 19:54:06 +00003233
3234 MacroID &ID = MacroDefinitions[MD];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003235 if (ID == 0)
Douglas Gregor77424bc2010-10-02 19:29:26 +00003236 ID = NextMacroID++;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003237 return ID;
3238}
3239
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003240void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003241 Record.push_back(getSelectorRef(SelRef));
3242}
3243
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003244SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003245 if (Sel.getAsOpaquePtr() == 0) {
3246 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003247 }
3248
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003249 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003250 if (SID == 0 && Chain) {
3251 // This might trigger a ReadSelector callback, which will set the ID for
3252 // this selector.
3253 Chain->LoadSelector(Sel);
3254 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003255 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003256 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003257 }
Sebastian Redl5d050072010-08-04 17:20:04 +00003258 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003259}
3260
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003261void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00003262 AddDeclRef(Temp->getDestructor(), Record);
3263}
3264
Douglas Gregor7c789c12010-10-29 22:39:52 +00003265void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3266 CXXBaseSpecifier const *BasesEnd,
3267 RecordDataImpl &Record) {
3268 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3269 CXXBaseSpecifiersToWrite.push_back(
3270 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3271 Bases, BasesEnd));
3272 Record.push_back(NextCXXBaseSpecifiersID++);
3273}
3274
Sebastian Redla4232eb2010-08-18 23:56:21 +00003275void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003276 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003277 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003278 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00003279 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003280 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00003281 break;
3282 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003283 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00003284 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00003285 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003286 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003287 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003288 break;
3289 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003290 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003291 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003292 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00003293 break;
John McCall833ca992009-10-29 08:12:44 +00003294 case TemplateArgument::Null:
3295 case TemplateArgument::Integral:
3296 case TemplateArgument::Declaration:
3297 case TemplateArgument::Pack:
3298 break;
3299 }
3300}
3301
Sebastian Redla4232eb2010-08-18 23:56:21 +00003302void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003303 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003304 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003305
3306 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3307 bool InfoHasSameExpr
3308 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3309 Record.push_back(InfoHasSameExpr);
3310 if (InfoHasSameExpr)
3311 return; // Avoid storing the same expr twice.
3312 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003313 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3314 Record);
3315}
3316
Douglas Gregordc355712011-02-25 00:36:19 +00003317void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3318 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00003319 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00003320 AddTypeRef(QualType(), Record);
3321 return;
3322 }
3323
Douglas Gregordc355712011-02-25 00:36:19 +00003324 AddTypeLoc(TInfo->getTypeLoc(), Record);
3325}
3326
3327void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3328 AddTypeRef(TL.getType(), Record);
3329
John McCalla1ee0c52009-10-16 21:56:05 +00003330 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00003331 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003332 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00003333}
3334
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003335void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00003336 Record.push_back(GetOrCreateTypeID(T));
3337}
3338
3339TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003340 return MakeTypeID(T,
3341 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3342}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003343
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003344TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003345 return MakeTypeID(T,
3346 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003347}
3348
3349TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3350 if (T.isNull())
3351 return TypeIdx();
3352 assert(!T.getLocalFastQualifiers());
3353
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00003354 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003355 if (Idx.getIndex() == 0) {
Douglas Gregor366809a2009-04-26 03:49:13 +00003356 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00003357 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003358 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003359 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00003360 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003361 return Idx;
3362}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003363
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003364TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003365 if (T.isNull())
3366 return TypeIdx();
3367 assert(!T.getLocalFastQualifiers());
3368
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003369 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3370 assert(I != TypeIdxs.end() && "Type not emitted!");
3371 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003372}
3373
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003374void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003375 Record.push_back(GetDeclRef(D));
3376}
3377
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003378DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003379 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003380 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003381 }
Douglas Gregor97475832010-10-05 18:37:06 +00003382 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003383 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00003384 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003385 // We haven't seen this declaration before. Give it a new ID and
3386 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003387 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003388 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redl0b17c612010-08-13 00:28:03 +00003389 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3390 // We don't add it to the replacement collection here, because we don't
3391 // have the offset yet.
3392 DeclTypesToEmit.push(const_cast<Decl *>(D));
3393 // Reset the flag, so that we don't add this decl multiple times.
3394 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003395 }
3396
Sebastian Redl681d7232010-07-27 00:17:23 +00003397 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003398}
3399
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003400DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003401 if (D == 0)
3402 return 0;
3403
3404 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3405 return DeclIDs[D];
3406}
3407
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003408void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00003409 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00003410 Record.push_back(Name.getNameKind());
3411 switch (Name.getNameKind()) {
3412 case DeclarationName::Identifier:
3413 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3414 break;
3415
3416 case DeclarationName::ObjCZeroArgSelector:
3417 case DeclarationName::ObjCOneArgSelector:
3418 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003419 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003420 break;
3421
3422 case DeclarationName::CXXConstructorName:
3423 case DeclarationName::CXXDestructorName:
3424 case DeclarationName::CXXConversionFunctionName:
3425 AddTypeRef(Name.getCXXNameType(), Record);
3426 break;
3427
3428 case DeclarationName::CXXOperatorName:
3429 Record.push_back(Name.getCXXOverloadedOperator());
3430 break;
3431
Sean Hunt3e518bd2009-11-29 07:34:05 +00003432 case DeclarationName::CXXLiteralOperatorName:
3433 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3434 break;
3435
Douglas Gregor2cf26342009-04-09 22:27:44 +00003436 case DeclarationName::CXXUsingDirective:
3437 // No extra data to emit
3438 break;
3439 }
3440}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003441
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003442void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003443 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003444 switch (Name.getNameKind()) {
3445 case DeclarationName::CXXConstructorName:
3446 case DeclarationName::CXXDestructorName:
3447 case DeclarationName::CXXConversionFunctionName:
3448 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3449 break;
3450
3451 case DeclarationName::CXXOperatorName:
3452 AddSourceLocation(
3453 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3454 Record);
3455 AddSourceLocation(
3456 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3457 Record);
3458 break;
3459
3460 case DeclarationName::CXXLiteralOperatorName:
3461 AddSourceLocation(
3462 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3463 Record);
3464 break;
3465
3466 case DeclarationName::Identifier:
3467 case DeclarationName::ObjCZeroArgSelector:
3468 case DeclarationName::ObjCOneArgSelector:
3469 case DeclarationName::ObjCMultiArgSelector:
3470 case DeclarationName::CXXUsingDirective:
3471 break;
3472 }
3473}
3474
3475void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003476 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003477 AddDeclarationName(NameInfo.getName(), Record);
3478 AddSourceLocation(NameInfo.getLoc(), Record);
3479 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3480}
3481
3482void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003483 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003484 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003485 Record.push_back(Info.NumTemplParamLists);
3486 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3487 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3488}
3489
Sebastian Redla4232eb2010-08-18 23:56:21 +00003490void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003491 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003492 // Nested name specifiers usually aren't too long. I think that 8 would
3493 // typically accomodate the vast majority.
3494 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3495
3496 // Push each of the NNS's onto a stack for serialization in reverse order.
3497 while (NNS) {
3498 NestedNames.push_back(NNS);
3499 NNS = NNS->getPrefix();
3500 }
3501
3502 Record.push_back(NestedNames.size());
3503 while(!NestedNames.empty()) {
3504 NNS = NestedNames.pop_back_val();
3505 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3506 Record.push_back(Kind);
3507 switch (Kind) {
3508 case NestedNameSpecifier::Identifier:
3509 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3510 break;
3511
3512 case NestedNameSpecifier::Namespace:
3513 AddDeclRef(NNS->getAsNamespace(), Record);
3514 break;
3515
Douglas Gregor14aba762011-02-24 02:36:08 +00003516 case NestedNameSpecifier::NamespaceAlias:
3517 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
3518 break;
3519
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003520 case NestedNameSpecifier::TypeSpec:
3521 case NestedNameSpecifier::TypeSpecWithTemplate:
3522 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3523 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3524 break;
3525
3526 case NestedNameSpecifier::Global:
3527 // Don't need to write an associated value.
3528 break;
3529 }
3530 }
3531}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003532
Douglas Gregordc355712011-02-25 00:36:19 +00003533void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
3534 RecordDataImpl &Record) {
3535 // Nested name specifiers usually aren't too long. I think that 8 would
3536 // typically accomodate the vast majority.
3537 llvm::SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
3538
3539 // Push each of the nested-name-specifiers's onto a stack for
3540 // serialization in reverse order.
3541 while (NNS) {
3542 NestedNames.push_back(NNS);
3543 NNS = NNS.getPrefix();
3544 }
3545
3546 Record.push_back(NestedNames.size());
3547 while(!NestedNames.empty()) {
3548 NNS = NestedNames.pop_back_val();
3549 NestedNameSpecifier::SpecifierKind Kind
3550 = NNS.getNestedNameSpecifier()->getKind();
3551 Record.push_back(Kind);
3552 switch (Kind) {
3553 case NestedNameSpecifier::Identifier:
3554 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
3555 AddSourceRange(NNS.getLocalSourceRange(), Record);
3556 break;
3557
3558 case NestedNameSpecifier::Namespace:
3559 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
3560 AddSourceRange(NNS.getLocalSourceRange(), Record);
3561 break;
3562
3563 case NestedNameSpecifier::NamespaceAlias:
3564 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
3565 AddSourceRange(NNS.getLocalSourceRange(), Record);
3566 break;
3567
3568 case NestedNameSpecifier::TypeSpec:
3569 case NestedNameSpecifier::TypeSpecWithTemplate:
3570 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3571 AddTypeLoc(NNS.getTypeLoc(), Record);
3572 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3573 break;
3574
3575 case NestedNameSpecifier::Global:
3576 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3577 break;
3578 }
3579 }
3580}
3581
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003582void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00003583 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003584 Record.push_back(Kind);
3585 switch (Kind) {
3586 case TemplateName::Template:
3587 AddDeclRef(Name.getAsTemplateDecl(), Record);
3588 break;
3589
3590 case TemplateName::OverloadedTemplate: {
3591 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3592 Record.push_back(OvT->size());
3593 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3594 I != E; ++I)
3595 AddDeclRef(*I, Record);
3596 break;
3597 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003598
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003599 case TemplateName::QualifiedTemplate: {
3600 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3601 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3602 Record.push_back(QualT->hasTemplateKeyword());
3603 AddDeclRef(QualT->getTemplateDecl(), Record);
3604 break;
3605 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003606
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003607 case TemplateName::DependentTemplate: {
3608 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3609 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3610 Record.push_back(DepT->isIdentifier());
3611 if (DepT->isIdentifier())
3612 AddIdentifierRef(DepT->getIdentifier(), Record);
3613 else
3614 Record.push_back(DepT->getOperator());
3615 break;
3616 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00003617
3618 case TemplateName::SubstTemplateTemplateParmPack: {
3619 SubstTemplateTemplateParmPackStorage *SubstPack
3620 = Name.getAsSubstTemplateTemplateParmPack();
3621 AddDeclRef(SubstPack->getParameterPack(), Record);
3622 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
3623 break;
3624 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003625 }
3626}
3627
Michael J. Spencer20249a12010-10-21 03:16:25 +00003628void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003629 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003630 Record.push_back(Arg.getKind());
3631 switch (Arg.getKind()) {
3632 case TemplateArgument::Null:
3633 break;
3634 case TemplateArgument::Type:
3635 AddTypeRef(Arg.getAsType(), Record);
3636 break;
3637 case TemplateArgument::Declaration:
3638 AddDeclRef(Arg.getAsDecl(), Record);
3639 break;
3640 case TemplateArgument::Integral:
3641 AddAPSInt(*Arg.getAsIntegral(), Record);
3642 AddTypeRef(Arg.getIntegralType(), Record);
3643 break;
3644 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00003645 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3646 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00003647 case TemplateArgument::TemplateExpansion:
3648 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00003649 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3650 Record.push_back(*NumExpansions + 1);
3651 else
3652 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003653 break;
3654 case TemplateArgument::Expression:
3655 AddStmt(Arg.getAsExpr());
3656 break;
3657 case TemplateArgument::Pack:
3658 Record.push_back(Arg.pack_size());
3659 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3660 I != E; ++I)
3661 AddTemplateArgument(*I, Record);
3662 break;
3663 }
3664}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003665
3666void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003667ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003668 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003669 assert(TemplateParams && "No TemplateParams!");
3670 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3671 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3672 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3673 Record.push_back(TemplateParams->size());
3674 for (TemplateParameterList::const_iterator
3675 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3676 P != PEnd; ++P)
3677 AddDeclRef(*P, Record);
3678}
3679
3680/// \brief Emit a template argument list.
3681void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003682ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003683 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003684 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00003685 Record.push_back(TemplateArgs->size());
3686 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003687 AddTemplateArgument(TemplateArgs->get(i), Record);
3688}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003689
3690
3691void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003692ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003693 Record.push_back(Set.size());
3694 for (UnresolvedSetImpl::const_iterator
3695 I = Set.begin(), E = Set.end(); I != E; ++I) {
3696 AddDeclRef(I.getDecl(), Record);
3697 Record.push_back(I.getAccess());
3698 }
3699}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003700
Sebastian Redla4232eb2010-08-18 23:56:21 +00003701void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003702 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003703 Record.push_back(Base.isVirtual());
3704 Record.push_back(Base.isBaseOfClass());
3705 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00003706 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00003707 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003708 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00003709 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3710 : SourceLocation(),
3711 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003712}
Sebastian Redl30c514c2010-07-14 23:45:08 +00003713
Douglas Gregor7c789c12010-10-29 22:39:52 +00003714void ASTWriter::FlushCXXBaseSpecifiers() {
3715 RecordData Record;
3716 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3717 Record.clear();
3718
3719 // Record the offset of this base-specifier set.
3720 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3721 if (Index == CXXBaseSpecifiersOffsets.size())
3722 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3723 else {
3724 if (Index > CXXBaseSpecifiersOffsets.size())
3725 CXXBaseSpecifiersOffsets.resize(Index + 1);
3726 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3727 }
3728
3729 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3730 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3731 Record.push_back(BEnd - B);
3732 for (; B != BEnd; ++B)
3733 AddCXXBaseSpecifier(*B, Record);
3734 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00003735
3736 // Flush any expressions that were written as part of the base specifiers.
3737 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003738 }
3739
3740 CXXBaseSpecifiersToWrite.clear();
3741}
3742
Sean Huntcbb67482011-01-08 20:30:50 +00003743void ASTWriter::AddCXXCtorInitializers(
3744 const CXXCtorInitializer * const *CtorInitializers,
3745 unsigned NumCtorInitializers,
3746 RecordDataImpl &Record) {
3747 Record.push_back(NumCtorInitializers);
3748 for (unsigned i=0; i != NumCtorInitializers; ++i) {
3749 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003750
3751 Record.push_back(Init->isBaseInitializer());
3752 if (Init->isBaseInitializer()) {
3753 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3754 Record.push_back(Init->isBaseVirtual());
3755 } else {
Francois Pichet00eb3f92010-12-04 09:14:42 +00003756 Record.push_back(Init->isIndirectMemberInitializer());
3757 if (Init->isIndirectMemberInitializer())
3758 AddDeclRef(Init->getIndirectMember(), Record);
3759 else
3760 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003761 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00003762
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003763 AddSourceLocation(Init->getMemberLocation(), Record);
3764 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003765 AddSourceLocation(Init->getLParenLoc(), Record);
3766 AddSourceLocation(Init->getRParenLoc(), Record);
3767 Record.push_back(Init->isWritten());
3768 if (Init->isWritten()) {
3769 Record.push_back(Init->getSourceOrder());
3770 } else {
3771 Record.push_back(Init->getNumArrayIndices());
3772 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3773 AddDeclRef(Init->getArrayIndex(i), Record);
3774 }
3775 }
3776}
3777
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003778void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3779 assert(D->DefinitionData);
3780 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3781 Record.push_back(Data.UserDeclaredConstructor);
3782 Record.push_back(Data.UserDeclaredCopyConstructor);
3783 Record.push_back(Data.UserDeclaredCopyAssignment);
3784 Record.push_back(Data.UserDeclaredDestructor);
3785 Record.push_back(Data.Aggregate);
3786 Record.push_back(Data.PlainOldData);
3787 Record.push_back(Data.Empty);
3788 Record.push_back(Data.Polymorphic);
3789 Record.push_back(Data.Abstract);
3790 Record.push_back(Data.HasTrivialConstructor);
3791 Record.push_back(Data.HasTrivialCopyConstructor);
3792 Record.push_back(Data.HasTrivialCopyAssignment);
3793 Record.push_back(Data.HasTrivialDestructor);
3794 Record.push_back(Data.ComputedVisibleConversions);
3795 Record.push_back(Data.DeclaredDefaultConstructor);
3796 Record.push_back(Data.DeclaredCopyConstructor);
3797 Record.push_back(Data.DeclaredCopyAssignment);
3798 Record.push_back(Data.DeclaredDestructor);
3799
3800 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003801 if (Data.NumBases > 0)
3802 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3803 Record);
3804
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003805 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3806 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003807 if (Data.NumVBases > 0)
3808 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3809 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003810
3811 AddUnresolvedSet(Data.Conversions, Record);
3812 AddUnresolvedSet(Data.VisibleConversions, Record);
3813 // Data.Definition is the owning decl, no need to write it.
3814 AddDeclRef(Data.FirstFriend, Record);
3815}
3816
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003817void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003818 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003819 assert(!Chain && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003820 assert(FirstDeclID == NextDeclID &&
3821 FirstTypeID == NextTypeID &&
3822 FirstIdentID == NextIdentID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00003823 FirstSelectorID == NextSelectorID &&
Douglas Gregor77424bc2010-10-02 19:29:26 +00003824 FirstMacroID == NextMacroID &&
Douglas Gregor7c789c12010-10-29 22:39:52 +00003825 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003826 "Setting chain after writing has started.");
3827 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003828
3829 FirstDeclID += Chain->getTotalNumDecls();
3830 FirstTypeID += Chain->getTotalNumTypes();
3831 FirstIdentID += Chain->getTotalNumIdentifiers();
3832 FirstSelectorID += Chain->getTotalNumSelectors();
3833 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003834 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003835 NextDeclID = FirstDeclID;
3836 NextTypeID = FirstTypeID;
3837 NextIdentID = FirstIdentID;
3838 NextSelectorID = FirstSelectorID;
3839 NextMacroID = FirstMacroID;
Douglas Gregor7c789c12010-10-29 22:39:52 +00003840 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003841}
3842
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003843void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003844 IdentifierIDs[II] = ID;
Douglas Gregor040a8042011-02-11 00:26:14 +00003845 if (II->hasMacroDefinition())
3846 DeserializedMacroNames.push_back(II);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003847}
3848
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003849void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00003850 // Always take the highest-numbered type index. This copes with an interesting
3851 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00003852 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00003853 // keep the higher-numbered entry so that we can properly write it out to
3854 // the AST file.
3855 TypeIdx &StoredIdx = TypeIdxs[T];
3856 if (Idx.getIndex() >= StoredIdx.getIndex())
3857 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003858}
3859
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003860void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1476ed42010-07-16 16:36:56 +00003861 DeclIDs[D] = ID;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003862}
Sebastian Redl5d050072010-08-04 17:20:04 +00003863
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003864void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003865 SelectorIDs[S] = ID;
3866}
Douglas Gregor77424bc2010-10-02 19:29:26 +00003867
Michael J. Spencer20249a12010-10-21 03:16:25 +00003868void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00003869 MacroDefinition *MD) {
3870 MacroDefinitions[MD] = ID;
3871}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003872
3873void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3874 assert(D->isDefinition());
3875 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3876 // We are interested when a PCH decl is modified.
3877 if (RD->getPCHLevel() > 0) {
3878 // A forward reference was mutated into a definition. Rewrite it.
3879 // FIXME: This happens during template instantiation, should we
3880 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00003881 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003882 }
3883
3884 for (CXXRecordDecl::redecl_iterator
3885 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3886 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3887 if (Redecl == RD)
3888 continue;
3889
3890 // We are interested when a PCH decl is modified.
3891 if (Redecl->getPCHLevel() > 0) {
3892 UpdateRecord &Record = DeclUpdates[Redecl];
3893 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3894 assert(Redecl->DefinitionData);
3895 assert(Redecl->DefinitionData->Definition == D);
3896 AddDeclRef(D, Record); // the DefinitionDecl
3897 }
3898 }
3899 }
3900}
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00003901void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3902 // TU and namespaces are handled elsewhere.
3903 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3904 return;
3905
3906 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3907 return; // Not a source decl added to a DeclContext from PCH.
3908
3909 AddUpdatedDeclContext(DC);
3910}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00003911
3912void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3913 assert(D->isImplicit());
3914 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3915 return; // Not a source member added to a class from PCH.
3916 if (!isa<CXXMethodDecl>(D))
3917 return; // We are interested in lazily declared implicit methods.
3918
3919 // A decl coming from PCH was modified.
3920 assert(RD->isDefinition());
3921 UpdateRecord &Record = DeclUpdates[RD];
3922 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3923 AddDeclRef(D, Record);
3924}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00003925
3926void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3927 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00003928 // The specializations set is kept in the canonical template.
3929 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00003930 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3931 return; // Not a source specialization added to a template from PCH.
3932
3933 UpdateRecord &Record = DeclUpdates[TD];
3934 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3935 AddDeclRef(D, Record);
3936}
Douglas Gregor89d99802010-11-30 06:16:57 +00003937
3938ASTSerializationListener::~ASTSerializationListener() { }