blob: d46ddf8f2f8f580c92a514fcb013b7e6a2dc4b7d [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 Gregor17fc2232009-04-14 21:55:33 +000040#include "llvm/ADT/APFloat.h"
41#include "llvm/ADT/APInt.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000042#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000043#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000044#include "llvm/Support/FileSystem.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000045#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000046#include "llvm/Support/Path.h"
Chris Lattner3c304bd2009-04-11 18:40:46 +000047#include <cstdio>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000048#include <string.h>
Douglas Gregor2cf26342009-04-09 22:27:44 +000049using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000050using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000051
Sebastian Redlade50002010-07-30 17:03:48 +000052template <typename T, typename Allocator>
53T *data(std::vector<T, Allocator> &v) {
54 return v.empty() ? 0 : &v.front();
55}
56template <typename T, typename Allocator>
57const T *data(const std::vector<T, Allocator> &v) {
58 return v.empty() ? 0 : &v.front();
59}
60
Douglas Gregor2cf26342009-04-09 22:27:44 +000061//===----------------------------------------------------------------------===//
62// Type serialization
63//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000064
Douglas Gregor2cf26342009-04-09 22:27:44 +000065namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000066 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000067 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000068 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000069
70 public:
71 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000072 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000073
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000074 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000075 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000076
77 void VisitArrayType(const ArrayType *T);
78 void VisitFunctionType(const FunctionType *T);
79 void VisitTagType(const TagType *T);
80
81#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
82#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000083#include "clang/AST/TypeNodes.def"
84 };
85}
86
Sebastian Redl3397c552010-08-18 23:56:27 +000087void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000088 assert(false && "Built-in types are never serialized");
89}
90
Sebastian Redl3397c552010-08-18 23:56:27 +000091void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000092 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +000093 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +000094}
95
Sebastian Redl3397c552010-08-18 23:56:27 +000096void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000097 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +000098 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +000099}
100
Sebastian Redl3397c552010-08-18 23:56:27 +0000101void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000102 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000103 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000104}
105
Sebastian Redl3397c552010-08-18 23:56:27 +0000106void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000107 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000108 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000109}
110
Sebastian Redl3397c552010-08-18 23:56:27 +0000111void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000112 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000113 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000114}
115
Sebastian Redl3397c552010-08-18 23:56:27 +0000116void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000117 Writer.AddTypeRef(T->getPointeeType(), Record);
118 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000119 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000120}
121
Sebastian Redl3397c552010-08-18 23:56:27 +0000122void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000123 Writer.AddTypeRef(T->getElementType(), Record);
124 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000125 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000126}
127
Sebastian Redl3397c552010-08-18 23:56:27 +0000128void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000129 VisitArrayType(T);
130 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000131 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000132}
133
Sebastian Redl3397c552010-08-18 23:56:27 +0000134void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000135 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000136 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000137}
138
Sebastian Redl3397c552010-08-18 23:56:27 +0000139void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000140 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000141 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
142 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000143 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000144 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000145}
146
Sebastian Redl3397c552010-08-18 23:56:27 +0000147void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000148 Writer.AddTypeRef(T->getElementType(), Record);
149 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000150 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000151 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000152}
153
Sebastian Redl3397c552010-08-18 23:56:27 +0000154void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000155 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000156 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000157}
158
Sebastian Redl3397c552010-08-18 23:56:27 +0000159void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000160 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000161 FunctionType::ExtInfo C = T->getExtInfo();
162 Record.push_back(C.getNoReturn());
Rafael Espindola425ef722010-03-30 22:15:11 +0000163 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000164 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000165 Record.push_back(C.getCC());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000166}
167
Sebastian Redl3397c552010-08-18 23:56:27 +0000168void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000169 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000170 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000171}
172
Sebastian Redl3397c552010-08-18 23:56:27 +0000173void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000174 VisitFunctionType(T);
175 Record.push_back(T->getNumArgs());
176 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
177 Writer.AddTypeRef(T->getArgType(I), Record);
178 Record.push_back(T->isVariadic());
179 Record.push_back(T->getTypeQuals());
Douglas Gregorc938c162011-01-26 05:01:58 +0000180 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redl465226e2009-05-27 22:11:52 +0000181 Record.push_back(T->hasExceptionSpec());
182 Record.push_back(T->hasAnyExceptionSpec());
183 Record.push_back(T->getNumExceptions());
184 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
185 Writer.AddTypeRef(T->getExceptionType(I), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000186 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000187}
188
Sebastian Redl3397c552010-08-18 23:56:27 +0000189void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000190 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000191 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000192}
John McCalled976492009-12-04 22:46:56 +0000193
Sebastian Redl3397c552010-08-18 23:56:27 +0000194void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000195 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000196 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
197 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000198 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000199}
200
Sebastian Redl3397c552010-08-18 23:56:27 +0000201void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000202 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000203 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000204}
205
Sebastian Redl3397c552010-08-18 23:56:27 +0000206void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000207 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000208 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000209}
210
Sebastian Redl3397c552010-08-18 23:56:27 +0000211void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson395b4752009-06-24 19:06:50 +0000212 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000213 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000214}
215
Sebastian Redl3397c552010-08-18 23:56:27 +0000216void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000217 Record.push_back(T->isDependentType());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000218 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000219 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000220 "Cannot serialize in the middle of a type definition");
221}
222
Sebastian Redl3397c552010-08-18 23:56:27 +0000223void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000224 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000225 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000226}
227
Sebastian Redl3397c552010-08-18 23:56:27 +0000228void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000229 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000230 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000231}
232
John McCall9d156a72011-01-06 01:58:22 +0000233void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
234 Writer.AddTypeRef(T->getModifiedType(), Record);
235 Writer.AddTypeRef(T->getEquivalentType(), Record);
236 Record.push_back(T->getAttrKind());
237 Code = TYPE_ATTRIBUTED;
238}
239
Mike Stump1eb44332009-09-09 15:08:12 +0000240void
Sebastian Redl3397c552010-08-18 23:56:27 +0000241ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000242 const SubstTemplateTypeParmType *T) {
243 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
244 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000245 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000246}
247
248void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000249ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
250 const SubstTemplateTypeParmPackType *T) {
251 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
252 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
253 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
254}
255
256void
Sebastian Redl3397c552010-08-18 23:56:27 +0000257ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000258 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000259 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000260 Writer.AddTemplateName(T->getTemplateName(), Record);
261 Record.push_back(T->getNumArgs());
262 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
263 ArgI != ArgE; ++ArgI)
264 Writer.AddTemplateArgument(*ArgI, Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000265 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
266 : T->getCanonicalTypeInternal(),
267 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000268 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000269}
270
271void
Sebastian Redl3397c552010-08-18 23:56:27 +0000272ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000273 VisitArrayType(T);
274 Writer.AddStmt(T->getSizeExpr());
275 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000276 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000277}
278
279void
Sebastian Redl3397c552010-08-18 23:56:27 +0000280ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000281 const DependentSizedExtVectorType *T) {
282 // FIXME: Serialize this type (C++ only)
283 assert(false && "Cannot serialize dependent sized extended vector types");
284}
285
286void
Sebastian Redl3397c552010-08-18 23:56:27 +0000287ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000288 Record.push_back(T->getDepth());
289 Record.push_back(T->getIndex());
290 Record.push_back(T->isParameterPack());
291 Writer.AddIdentifierRef(T->getName(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000292 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000293}
294
295void
Sebastian Redl3397c552010-08-18 23:56:27 +0000296ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000297 Record.push_back(T->getKeyword());
298 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
299 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000300 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
301 : T->getCanonicalTypeInternal(),
302 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000303 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000304}
305
306void
Sebastian Redl3397c552010-08-18 23:56:27 +0000307ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000308 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000309 Record.push_back(T->getKeyword());
310 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
311 Writer.AddIdentifierRef(T->getIdentifier(), Record);
312 Record.push_back(T->getNumArgs());
313 for (DependentTemplateSpecializationType::iterator
314 I = T->begin(), E = T->end(); I != E; ++I)
315 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000316 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000317}
318
Douglas Gregor7536dd52010-12-20 02:24:11 +0000319void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
320 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregorcded4f62011-01-14 17:04:44 +0000321 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
322 Record.push_back(*NumExpansions + 1);
323 else
324 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000325 Code = TYPE_PACK_EXPANSION;
326}
327
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000328void ASTTypeWriter::VisitParenType(const ParenType *T) {
329 Writer.AddTypeRef(T->getInnerType(), Record);
330 Code = TYPE_PAREN;
331}
332
Sebastian Redl3397c552010-08-18 23:56:27 +0000333void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000334 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000335 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
336 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000337 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000338}
339
Sebastian Redl3397c552010-08-18 23:56:27 +0000340void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCall3cb0ebd2010-03-10 03:28:59 +0000341 Writer.AddDeclRef(T->getDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000342 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000343 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000344}
345
Sebastian Redl3397c552010-08-18 23:56:27 +0000346void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000347 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000348 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000349}
350
Sebastian Redl3397c552010-08-18 23:56:27 +0000351void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000352 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000353 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000354 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000355 E = T->qual_end(); I != E; ++I)
356 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000357 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000358}
359
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000360void
Sebastian Redl3397c552010-08-18 23:56:27 +0000361ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000362 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000363 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000364}
365
John McCalla1ee0c52009-10-16 21:56:05 +0000366namespace {
367
368class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000369 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000370 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000371
372public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000373 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000374 : Writer(Writer), Record(Record) { }
375
John McCall51bd8032009-10-18 01:05:36 +0000376#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000377#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000378 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000379#include "clang/AST/TypeLocNodes.def"
380
John McCall51bd8032009-10-18 01:05:36 +0000381 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
382 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000383};
384
385}
386
John McCall51bd8032009-10-18 01:05:36 +0000387void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
388 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000389}
John McCall51bd8032009-10-18 01:05:36 +0000390void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000391 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
392 if (TL.needsExtraLocalData()) {
393 Record.push_back(TL.getWrittenTypeSpec());
394 Record.push_back(TL.getWrittenSignSpec());
395 Record.push_back(TL.getWrittenWidthSpec());
396 Record.push_back(TL.hasModeAttr());
397 }
John McCalla1ee0c52009-10-16 21:56:05 +0000398}
John McCall51bd8032009-10-18 01:05:36 +0000399void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
400 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000401}
John McCall51bd8032009-10-18 01:05:36 +0000402void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
403 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000404}
John McCall51bd8032009-10-18 01:05:36 +0000405void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
406 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000407}
John McCall51bd8032009-10-18 01:05:36 +0000408void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
409 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000410}
John McCall51bd8032009-10-18 01:05:36 +0000411void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
412 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000413}
John McCall51bd8032009-10-18 01:05:36 +0000414void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
415 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000416}
John McCall51bd8032009-10-18 01:05:36 +0000417void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
418 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
419 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
420 Record.push_back(TL.getSizeExpr() ? 1 : 0);
421 if (TL.getSizeExpr())
422 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000423}
John McCall51bd8032009-10-18 01:05:36 +0000424void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
425 VisitArrayTypeLoc(TL);
426}
427void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
428 VisitArrayTypeLoc(TL);
429}
430void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
431 VisitArrayTypeLoc(TL);
432}
433void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
434 DependentSizedArrayTypeLoc TL) {
435 VisitArrayTypeLoc(TL);
436}
437void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
438 DependentSizedExtVectorTypeLoc TL) {
439 Writer.AddSourceLocation(TL.getNameLoc(), Record);
440}
441void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
442 Writer.AddSourceLocation(TL.getNameLoc(), Record);
443}
444void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
445 Writer.AddSourceLocation(TL.getNameLoc(), Record);
446}
447void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
448 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
449 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Douglas Gregordab60ad2010-10-01 18:44:50 +0000450 Record.push_back(TL.getTrailingReturn());
John McCall51bd8032009-10-18 01:05:36 +0000451 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
452 Writer.AddDeclRef(TL.getArg(i), Record);
453}
454void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
455 VisitFunctionTypeLoc(TL);
456}
457void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
458 VisitFunctionTypeLoc(TL);
459}
John McCalled976492009-12-04 22:46:56 +0000460void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
461 Writer.AddSourceLocation(TL.getNameLoc(), Record);
462}
John McCall51bd8032009-10-18 01:05:36 +0000463void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
464 Writer.AddSourceLocation(TL.getNameLoc(), Record);
465}
466void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000467 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
468 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
469 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000470}
471void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000472 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
473 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
474 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
475 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000476}
477void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
478 Writer.AddSourceLocation(TL.getNameLoc(), Record);
479}
480void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
481 Writer.AddSourceLocation(TL.getNameLoc(), Record);
482}
483void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
484 Writer.AddSourceLocation(TL.getNameLoc(), Record);
485}
John McCall9d156a72011-01-06 01:58:22 +0000486void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
487 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
488 if (TL.hasAttrOperand()) {
489 SourceRange range = TL.getAttrOperandParensRange();
490 Writer.AddSourceLocation(range.getBegin(), Record);
491 Writer.AddSourceLocation(range.getEnd(), Record);
492 }
493 if (TL.hasAttrExprOperand()) {
494 Expr *operand = TL.getAttrExprOperand();
495 Record.push_back(operand ? 1 : 0);
496 if (operand) Writer.AddStmt(operand);
497 } else if (TL.hasAttrEnumOperand()) {
498 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
499 }
500}
John McCall51bd8032009-10-18 01:05:36 +0000501void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
502 Writer.AddSourceLocation(TL.getNameLoc(), Record);
503}
John McCall49a832b2009-10-18 09:09:24 +0000504void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
505 SubstTemplateTypeParmTypeLoc TL) {
506 Writer.AddSourceLocation(TL.getNameLoc(), Record);
507}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000508void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
509 SubstTemplateTypeParmPackTypeLoc TL) {
510 Writer.AddSourceLocation(TL.getNameLoc(), Record);
511}
John McCall51bd8032009-10-18 01:05:36 +0000512void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
513 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000514 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
515 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
516 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
517 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000518 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
519 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000520}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000521void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
522 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
523 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
524}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000525void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000526 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
527 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000528}
John McCall3cb0ebd2010-03-10 03:28:59 +0000529void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
530 Writer.AddSourceLocation(TL.getNameLoc(), Record);
531}
Douglas Gregor4714c122010-03-31 17:34:00 +0000532void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000533 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
534 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000535 Writer.AddSourceLocation(TL.getNameLoc(), Record);
536}
John McCall33500952010-06-11 00:33:02 +0000537void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
538 DependentTemplateSpecializationTypeLoc TL) {
539 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
540 Writer.AddSourceRange(TL.getQualifierRange(), Record);
541 Writer.AddSourceLocation(TL.getNameLoc(), Record);
542 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
543 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
544 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000545 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
546 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000547}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000548void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
549 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
550}
John McCall51bd8032009-10-18 01:05:36 +0000551void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
552 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000553}
554void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
555 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000556 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
557 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
558 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
559 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000560}
John McCall54e14c42009-10-22 22:37:11 +0000561void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
562 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000563}
John McCalla1ee0c52009-10-16 21:56:05 +0000564
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000565//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000566// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000567//===----------------------------------------------------------------------===//
568
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000569static void EmitBlockID(unsigned ID, const char *Name,
570 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000571 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000572 Record.clear();
573 Record.push_back(ID);
574 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
575
576 // Emit the block name if present.
577 if (Name == 0 || Name[0] == 0) return;
578 Record.clear();
579 while (*Name)
580 Record.push_back(*Name++);
581 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
582}
583
584static void EmitRecordID(unsigned ID, const char *Name,
585 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000586 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000587 Record.clear();
588 Record.push_back(ID);
589 while (*Name)
590 Record.push_back(*Name++);
591 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000592}
593
594static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000595 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000596#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000597 RECORD(STMT_STOP);
598 RECORD(STMT_NULL_PTR);
599 RECORD(STMT_NULL);
600 RECORD(STMT_COMPOUND);
601 RECORD(STMT_CASE);
602 RECORD(STMT_DEFAULT);
603 RECORD(STMT_LABEL);
604 RECORD(STMT_IF);
605 RECORD(STMT_SWITCH);
606 RECORD(STMT_WHILE);
607 RECORD(STMT_DO);
608 RECORD(STMT_FOR);
609 RECORD(STMT_GOTO);
610 RECORD(STMT_INDIRECT_GOTO);
611 RECORD(STMT_CONTINUE);
612 RECORD(STMT_BREAK);
613 RECORD(STMT_RETURN);
614 RECORD(STMT_DECL);
615 RECORD(STMT_ASM);
616 RECORD(EXPR_PREDEFINED);
617 RECORD(EXPR_DECL_REF);
618 RECORD(EXPR_INTEGER_LITERAL);
619 RECORD(EXPR_FLOATING_LITERAL);
620 RECORD(EXPR_IMAGINARY_LITERAL);
621 RECORD(EXPR_STRING_LITERAL);
622 RECORD(EXPR_CHARACTER_LITERAL);
623 RECORD(EXPR_PAREN);
624 RECORD(EXPR_UNARY_OPERATOR);
625 RECORD(EXPR_SIZEOF_ALIGN_OF);
626 RECORD(EXPR_ARRAY_SUBSCRIPT);
627 RECORD(EXPR_CALL);
628 RECORD(EXPR_MEMBER);
629 RECORD(EXPR_BINARY_OPERATOR);
630 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
631 RECORD(EXPR_CONDITIONAL_OPERATOR);
632 RECORD(EXPR_IMPLICIT_CAST);
633 RECORD(EXPR_CSTYLE_CAST);
634 RECORD(EXPR_COMPOUND_LITERAL);
635 RECORD(EXPR_EXT_VECTOR_ELEMENT);
636 RECORD(EXPR_INIT_LIST);
637 RECORD(EXPR_DESIGNATED_INIT);
638 RECORD(EXPR_IMPLICIT_VALUE_INIT);
639 RECORD(EXPR_VA_ARG);
640 RECORD(EXPR_ADDR_LABEL);
641 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000642 RECORD(EXPR_CHOOSE);
643 RECORD(EXPR_GNU_NULL);
644 RECORD(EXPR_SHUFFLE_VECTOR);
645 RECORD(EXPR_BLOCK);
646 RECORD(EXPR_BLOCK_DECL_REF);
647 RECORD(EXPR_OBJC_STRING_LITERAL);
648 RECORD(EXPR_OBJC_ENCODE);
649 RECORD(EXPR_OBJC_SELECTOR_EXPR);
650 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
651 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
652 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
653 RECORD(EXPR_OBJC_KVC_REF_EXPR);
654 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000655 RECORD(STMT_OBJC_FOR_COLLECTION);
656 RECORD(STMT_OBJC_CATCH);
657 RECORD(STMT_OBJC_FINALLY);
658 RECORD(STMT_OBJC_AT_TRY);
659 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
660 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000661 RECORD(EXPR_CXX_OPERATOR_CALL);
662 RECORD(EXPR_CXX_CONSTRUCT);
663 RECORD(EXPR_CXX_STATIC_CAST);
664 RECORD(EXPR_CXX_DYNAMIC_CAST);
665 RECORD(EXPR_CXX_REINTERPRET_CAST);
666 RECORD(EXPR_CXX_CONST_CAST);
667 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
668 RECORD(EXPR_CXX_BOOL_LITERAL);
669 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000670 RECORD(EXPR_CXX_TYPEID_EXPR);
671 RECORD(EXPR_CXX_TYPEID_TYPE);
672 RECORD(EXPR_CXX_UUIDOF_EXPR);
673 RECORD(EXPR_CXX_UUIDOF_TYPE);
674 RECORD(EXPR_CXX_THIS);
675 RECORD(EXPR_CXX_THROW);
676 RECORD(EXPR_CXX_DEFAULT_ARG);
677 RECORD(EXPR_CXX_BIND_TEMPORARY);
678 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
679 RECORD(EXPR_CXX_NEW);
680 RECORD(EXPR_CXX_DELETE);
681 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
682 RECORD(EXPR_EXPR_WITH_CLEANUPS);
683 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
684 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
685 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
686 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
687 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
688 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
689 RECORD(EXPR_CXX_NOEXCEPT);
690 RECORD(EXPR_OPAQUE_VALUE);
691 RECORD(EXPR_BINARY_TYPE_TRAIT);
692 RECORD(EXPR_PACK_EXPANSION);
693 RECORD(EXPR_SIZEOF_PACK);
694 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000695 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000696#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000697}
Mike Stump1eb44332009-09-09 15:08:12 +0000698
Sebastian Redla4232eb2010-08-18 23:56:21 +0000699void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000700 RecordData Record;
701 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000702
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000703#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
704#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000705
Sebastian Redl3397c552010-08-18 23:56:27 +0000706 // AST Top-Level Block.
Sebastian Redlf29f0a22010-08-18 23:57:22 +0000707 BLOCK(AST_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000708 RECORD(ORIGINAL_FILE_NAME);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000709 RECORD(TYPE_OFFSET);
710 RECORD(DECL_OFFSET);
711 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000712 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000713 RECORD(IDENTIFIER_OFFSET);
714 RECORD(IDENTIFIER_TABLE);
715 RECORD(EXTERNAL_DEFINITIONS);
716 RECORD(SPECIAL_TYPES);
717 RECORD(STATISTICS);
718 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000719 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000720 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
721 RECORD(SELECTOR_OFFSETS);
722 RECORD(METHOD_POOL);
723 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000724 RECORD(SOURCE_LOCATION_OFFSETS);
725 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000726 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000727 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000728 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000729 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redla93e3b52010-07-08 22:01:51 +0000730 RECORD(CHAINED_METADATA);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000731 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000732 RECORD(TU_UPDATE_LEXICAL);
733 RECORD(REDECLS_UPDATE_LATEST);
734 RECORD(SEMA_DECL_REFS);
735 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
736 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
737 RECORD(DECL_REPLACEMENTS);
738 RECORD(UPDATE_VISIBLE);
739 RECORD(DECL_UPDATE_OFFSETS);
740 RECORD(DECL_UPDATES);
741 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
742 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000743 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000744 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000745 RECORD(FP_PRAGMA_OPTIONS);
746 RECORD(OPENCL_EXTENSIONS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000747
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000748 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000749 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000750 RECORD(SM_SLOC_FILE_ENTRY);
751 RECORD(SM_SLOC_BUFFER_ENTRY);
752 RECORD(SM_SLOC_BUFFER_BLOB);
753 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
754 RECORD(SM_LINE_TABLE);
Mike Stump1eb44332009-09-09 15:08:12 +0000755
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000756 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000757 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000758 RECORD(PP_MACRO_OBJECT_LIKE);
759 RECORD(PP_MACRO_FUNCTION_LIKE);
760 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000761
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000762 // Decls and Types block.
763 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000764 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000765 RECORD(TYPE_COMPLEX);
766 RECORD(TYPE_POINTER);
767 RECORD(TYPE_BLOCK_POINTER);
768 RECORD(TYPE_LVALUE_REFERENCE);
769 RECORD(TYPE_RVALUE_REFERENCE);
770 RECORD(TYPE_MEMBER_POINTER);
771 RECORD(TYPE_CONSTANT_ARRAY);
772 RECORD(TYPE_INCOMPLETE_ARRAY);
773 RECORD(TYPE_VARIABLE_ARRAY);
774 RECORD(TYPE_VECTOR);
775 RECORD(TYPE_EXT_VECTOR);
776 RECORD(TYPE_FUNCTION_PROTO);
777 RECORD(TYPE_FUNCTION_NO_PROTO);
778 RECORD(TYPE_TYPEDEF);
779 RECORD(TYPE_TYPEOF_EXPR);
780 RECORD(TYPE_TYPEOF);
781 RECORD(TYPE_RECORD);
782 RECORD(TYPE_ENUM);
783 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000784 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000785 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000786 RECORD(TYPE_DECLTYPE);
787 RECORD(TYPE_ELABORATED);
788 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
789 RECORD(TYPE_UNRESOLVED_USING);
790 RECORD(TYPE_INJECTED_CLASS_NAME);
791 RECORD(TYPE_OBJC_OBJECT);
792 RECORD(TYPE_TEMPLATE_TYPE_PARM);
793 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
794 RECORD(TYPE_DEPENDENT_NAME);
795 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
796 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
797 RECORD(TYPE_PAREN);
798 RECORD(TYPE_PACK_EXPANSION);
799 RECORD(TYPE_ATTRIBUTED);
800 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000801 RECORD(DECL_TRANSLATION_UNIT);
802 RECORD(DECL_TYPEDEF);
803 RECORD(DECL_ENUM);
804 RECORD(DECL_RECORD);
805 RECORD(DECL_ENUM_CONSTANT);
806 RECORD(DECL_FUNCTION);
807 RECORD(DECL_OBJC_METHOD);
808 RECORD(DECL_OBJC_INTERFACE);
809 RECORD(DECL_OBJC_PROTOCOL);
810 RECORD(DECL_OBJC_IVAR);
811 RECORD(DECL_OBJC_AT_DEFS_FIELD);
812 RECORD(DECL_OBJC_CLASS);
813 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
814 RECORD(DECL_OBJC_CATEGORY);
815 RECORD(DECL_OBJC_CATEGORY_IMPL);
816 RECORD(DECL_OBJC_IMPLEMENTATION);
817 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
818 RECORD(DECL_OBJC_PROPERTY);
819 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000820 RECORD(DECL_FIELD);
821 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000822 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000823 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000824 RECORD(DECL_FILE_SCOPE_ASM);
825 RECORD(DECL_BLOCK);
826 RECORD(DECL_CONTEXT_LEXICAL);
827 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000828 RECORD(DECL_NAMESPACE);
829 RECORD(DECL_NAMESPACE_ALIAS);
830 RECORD(DECL_USING);
831 RECORD(DECL_USING_SHADOW);
832 RECORD(DECL_USING_DIRECTIVE);
833 RECORD(DECL_UNRESOLVED_USING_VALUE);
834 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
835 RECORD(DECL_LINKAGE_SPEC);
836 RECORD(DECL_CXX_RECORD);
837 RECORD(DECL_CXX_METHOD);
838 RECORD(DECL_CXX_CONSTRUCTOR);
839 RECORD(DECL_CXX_DESTRUCTOR);
840 RECORD(DECL_CXX_CONVERSION);
841 RECORD(DECL_ACCESS_SPEC);
842 RECORD(DECL_FRIEND);
843 RECORD(DECL_FRIEND_TEMPLATE);
844 RECORD(DECL_CLASS_TEMPLATE);
845 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
846 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
847 RECORD(DECL_FUNCTION_TEMPLATE);
848 RECORD(DECL_TEMPLATE_TYPE_PARM);
849 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
850 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
851 RECORD(DECL_STATIC_ASSERT);
852 RECORD(DECL_CXX_BASE_SPECIFIERS);
853 RECORD(DECL_INDIRECTFIELD);
854 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
855
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000856 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
857 RECORD(PPD_MACRO_INSTANTIATION);
858 RECORD(PPD_MACRO_DEFINITION);
859 RECORD(PPD_INCLUSION_DIRECTIVE);
860
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000861 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattner0558df22009-04-27 00:49:53 +0000862 AddStmtsExprs(Stream, Record);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000863#undef RECORD
864#undef BLOCK
865 Stream.ExitBlock();
866}
867
Douglas Gregore650c8c2009-07-07 00:12:59 +0000868/// \brief Adjusts the given filename to only write out the portion of the
869/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000870///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000871/// \param Filename the file name to adjust.
872///
873/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
874/// the returned filename will be adjusted by this system root.
875///
876/// \returns either the original filename (if it needs no adjustment) or the
877/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000878static const char *
Douglas Gregore650c8c2009-07-07 00:12:59 +0000879adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
880 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Douglas Gregore650c8c2009-07-07 00:12:59 +0000882 if (!isysroot)
883 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000884
Douglas Gregore650c8c2009-07-07 00:12:59 +0000885 // Verify that the filename and the system root have the same prefix.
886 unsigned Pos = 0;
887 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
888 if (Filename[Pos] != isysroot[Pos])
889 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000890
Douglas Gregore650c8c2009-07-07 00:12:59 +0000891 // We hit the end of the filename before we hit the end of the system root.
892 if (!Filename[Pos])
893 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000894
Douglas Gregore650c8c2009-07-07 00:12:59 +0000895 // If the file name has a '/' at the current position, skip over the '/'.
896 // We distinguish sysroot-based includes from absolute includes by the
897 // absence of '/' at the beginning of sysroot-based includes.
898 if (Filename[Pos] == '/')
899 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000900
Douglas Gregore650c8c2009-07-07 00:12:59 +0000901 return Filename + Pos;
902}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000903
Sebastian Redl3397c552010-08-18 23:56:27 +0000904/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000905void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot,
906 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000907 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000908
Douglas Gregore650c8c2009-07-07 00:12:59 +0000909 // Metadata
910 const TargetInfo &Target = Context.Target;
911 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl77f46032010-07-09 21:00:24 +0000912 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000913 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl3397c552010-08-18 23:56:27 +0000914 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
915 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregore650c8c2009-07-07 00:12:59 +0000916 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
917 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
918 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl77f46032010-07-09 21:00:24 +0000919 // Target triple or chained PCH name
920 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregore650c8c2009-07-07 00:12:59 +0000921 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000922
Douglas Gregore650c8c2009-07-07 00:12:59 +0000923 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000924 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
925 Record.push_back(VERSION_MAJOR);
926 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000927 Record.push_back(CLANG_VERSION_MAJOR);
928 Record.push_back(CLANG_VERSION_MINOR);
929 Record.push_back(isysroot != 0);
Sebastian Redl77f46032010-07-09 21:00:24 +0000930 // FIXME: This writes the absolute path for chained headers.
931 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
932 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Douglas Gregorb64c1932009-05-12 01:31:05 +0000934 // Original file name
935 SourceManager &SM = Context.getSourceManager();
936 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
937 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000938 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregorb64c1932009-05-12 01:31:05 +0000939 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
940 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
941
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000942 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000944 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000945
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +0000946 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000947 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000948 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000949 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000950 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000951 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000952 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000953
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000954 // Original PCH directory
955 if (!OutputFile.empty() && OutputFile != "-") {
956 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
957 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
958 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
959 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
960
961 llvm::SmallString<128> OutputPath(OutputFile);
962
963 llvm::sys::fs::make_absolute(OutputPath);
964 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
965
966 RecordData Record;
967 Record.push_back(ORIGINAL_PCH_DIR);
968 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
969 }
970
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000971 // Repository branch/version information.
972 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000973 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000974 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
975 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000976 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000977 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000978 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
979 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +0000980}
981
982/// \brief Write the LangOptions structure.
Sebastian Redla4232eb2010-08-18 23:56:21 +0000983void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000984 RecordData Record;
985 Record.push_back(LangOpts.Trigraphs);
986 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
987 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
988 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
989 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000990 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000991 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
992 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
993 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
994 Record.push_back(LangOpts.C99); // C99 Support
995 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencerdae4ac42010-10-21 05:21:48 +0000996 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
997 // already saved elsewhere.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000998 Record.push_back(LangOpts.CPlusPlus); // C++ Support
999 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001000 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001002 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1003 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001004 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian412e7982010-02-09 19:31:38 +00001005 // modern abi enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001006 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian412e7982010-02-09 19:31:38 +00001007 // modern abi enabled.
Fariborz Jahanianf84109e2011-01-07 18:59:25 +00001008 Record.push_back(LangOpts.AppleKext); // Apple's kernel extensions ABI
Ted Kremenekc32647d2010-12-23 21:35:43 +00001009 Record.push_back(LangOpts.ObjCDefaultSynthProperties); // Objective-C auto-synthesized
1010 // properties enabled.
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +00001011 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump1eb44332009-09-09 15:08:12 +00001012
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001013 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001014 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1015 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +00001016 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001017 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar73482882010-02-10 18:48:44 +00001018 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001019
Douglas Gregor6f755502011-02-01 15:15:22 +00001020 Record.push_back(LangOpts.MSBitfields); // MS-compatible structure layout
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001021 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1022 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1023 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1024
Chris Lattnerea5ce472009-04-27 07:35:58 +00001025 // Whether static initializers are protected by locks.
1026 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00001027 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001028 Record.push_back(LangOpts.Blocks); // block extension to C
1029 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1030 // they are unused.
1031 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1032 // (modulo the platform support).
1033
Chris Lattnera4d71452010-06-26 21:25:03 +00001034 Record.push_back(LangOpts.getSignedOverflowBehavior());
1035 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001036
1037 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +00001038 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001039 // defined.
1040 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1041 // opposed to __DYNAMIC__).
1042 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1043
1044 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1045 // used (instead of C99 semantics).
1046 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +00001047 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
1048 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +00001049 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
1050 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +00001051 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Argyrios Kyrtzidisb1bdced2011-01-15 02:56:16 +00001052 Record.push_back(LangOpts.ShortEnums); // Should the enum type be equivalent
1053 // to the smallest integer type with
1054 // enough room.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001055 Record.push_back(LangOpts.getGCMode());
1056 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +00001057 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001058 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00001059 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne08a53262010-12-01 19:14:57 +00001060 Record.push_back(LangOpts.CUDA);
Mike Stump9c276ae2009-12-12 01:27:46 +00001061 Record.push_back(LangOpts.CatchUndefined);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00001062 Record.push_back(LangOpts.DefaultFPContract);
Anders Carlsson92f58222009-08-22 22:30:33 +00001063 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +00001064 Record.push_back(LangOpts.SpellChecking);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001065 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001066}
1067
Douglas Gregor14f79002009-04-10 03:52:48 +00001068//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001069// stat cache Serialization
1070//===----------------------------------------------------------------------===//
1071
1072namespace {
1073// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +00001074class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001075public:
1076 typedef const char * key_type;
1077 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001078
Chris Lattner74e976b2010-11-23 19:28:12 +00001079 typedef struct stat data_type;
1080 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001081
1082 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001083 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001084 }
Mike Stump1eb44332009-09-09 15:08:12 +00001085
1086 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001087 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1088 data_type_ref Data) {
1089 unsigned StrLen = strlen(path);
1090 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +00001091 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001092 clang::io::Emit8(Out, DataLen);
1093 return std::make_pair(StrLen + 1, DataLen);
1094 }
Mike Stump1eb44332009-09-09 15:08:12 +00001095
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001096 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1097 Out.write(path, KeyLen);
1098 }
Mike Stump1eb44332009-09-09 15:08:12 +00001099
Chris Lattner74e976b2010-11-23 19:28:12 +00001100 void EmitData(llvm::raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001101 data_type_ref Data, unsigned DataLen) {
1102 using namespace clang::io;
1103 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +00001104
Chris Lattner74e976b2010-11-23 19:28:12 +00001105 Emit32(Out, (uint32_t) Data.st_ino);
1106 Emit32(Out, (uint32_t) Data.st_dev);
1107 Emit16(Out, (uint16_t) Data.st_mode);
1108 Emit64(Out, (uint64_t) Data.st_mtime);
1109 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001110
1111 assert(Out.tell() - Start == DataLen && "Wrong data length");
1112 }
1113};
1114} // end anonymous namespace
1115
Sebastian Redl3397c552010-08-18 23:56:27 +00001116/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001117void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001118 // Build the on-disk hash table containing information about every
1119 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +00001120 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001121 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001122 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001123 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001124 Stat != StatEnd; ++Stat, ++NumStatEntries) {
1125 const char *Filename = Stat->first();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001126 Generator.insert(Filename, Stat->second);
1127 }
Mike Stump1eb44332009-09-09 15:08:12 +00001128
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001129 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001130 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001131 uint32_t BucketOffset;
1132 {
1133 llvm::raw_svector_ostream Out(StatCacheData);
1134 // Make sure that no bucket is at offset 0
1135 clang::io::Emit32(Out, 0);
1136 BucketOffset = Generator.Emit(Out);
1137 }
1138
1139 // Create a blob abbreviation
1140 using namespace llvm;
1141 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001142 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001143 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1144 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1145 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1146 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1147
1148 // Write the stat cache
1149 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001150 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001151 Record.push_back(BucketOffset);
1152 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001153 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001154}
1155
1156//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001157// Source Manager Serialization
1158//===----------------------------------------------------------------------===//
1159
1160/// \brief Create an abbreviation for the SLocEntry that refers to a
1161/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001162static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001163 using namespace llvm;
1164 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001165 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001166 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1167 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1168 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1169 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001170 // FileEntry fields.
1171 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1172 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor14f79002009-04-10 03:52:48 +00001173 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001174 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001175}
1176
1177/// \brief Create an abbreviation for the SLocEntry that refers to a
1178/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001179static unsigned CreateSLocBufferAbbrev(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_BUFFER_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
1187 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001188 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001189}
1190
1191/// \brief Create an abbreviation for the SLocEntry that refers to a
1192/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001193static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001194 using namespace llvm;
1195 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001196 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001197 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001198 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001199}
1200
1201/// \brief Create an abbreviation for the SLocEntry that refers to an
1202/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001203static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001204 using namespace llvm;
1205 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001206 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001207 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1208 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1209 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1210 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001211 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001212 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001213}
1214
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001215namespace {
1216 // Trait used for the on-disk hash table of header search information.
1217 class HeaderFileInfoTrait {
1218 ASTWriter &Writer;
1219 HeaderSearch &HS;
1220
1221 public:
1222 HeaderFileInfoTrait(ASTWriter &Writer, HeaderSearch &HS)
1223 : Writer(Writer), HS(HS) { }
1224
1225 typedef const char *key_type;
1226 typedef key_type key_type_ref;
1227
1228 typedef HeaderFileInfo data_type;
1229 typedef const data_type &data_type_ref;
1230
1231 static unsigned ComputeHash(const char *path) {
1232 // The hash is based only on the filename portion of the key, so that the
1233 // reader can match based on filenames when symlinking or excess path
1234 // elements ("foo/../", "../") change the form of the name. However,
1235 // complete path is still the key.
1236 return llvm::HashString(llvm::sys::path::filename(path));
1237 }
1238
1239 std::pair<unsigned,unsigned>
1240 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1241 data_type_ref Data) {
1242 unsigned StrLen = strlen(path);
1243 clang::io::Emit16(Out, StrLen);
1244 unsigned DataLen = 1 + 2 + 4;
1245 clang::io::Emit8(Out, DataLen);
1246 return std::make_pair(StrLen + 1, DataLen);
1247 }
1248
1249 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1250 Out.write(path, KeyLen);
1251 }
1252
1253 void EmitData(llvm::raw_ostream &Out, key_type_ref,
1254 data_type_ref Data, unsigned DataLen) {
1255 using namespace clang::io;
1256 uint64_t Start = Out.tell(); (void)Start;
1257
1258 unsigned char Flags = (Data.isImport << 3)
1259 | (Data.DirInfo << 1)
1260 | Data.Resolved;
1261 Emit8(Out, (uint8_t)Flags);
1262 Emit16(Out, (uint16_t) Data.NumIncludes);
1263
1264 if (!Data.ControllingMacro)
1265 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1266 else
1267 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
1268 assert(Out.tell() - Start == DataLen && "Wrong data length");
1269 }
1270 };
1271} // end anonymous namespace
1272
1273/// \brief Write the header search block for the list of files that
1274///
1275/// \param HS The header search structure to save.
1276///
1277/// \param Chain Whether we're creating a chained AST file.
1278void ASTWriter::WriteHeaderSearch(HeaderSearch &HS, const char* isysroot) {
1279 llvm::SmallVector<const FileEntry *, 16> FilesByUID;
1280 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1281
1282 if (FilesByUID.size() > HS.header_file_size())
1283 FilesByUID.resize(HS.header_file_size());
1284
1285 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1286 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
1287 llvm::SmallVector<const char *, 4> SavedStrings;
1288 unsigned NumHeaderSearchEntries = 0;
1289 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1290 const FileEntry *File = FilesByUID[UID];
1291 if (!File)
1292 continue;
1293
1294 const HeaderFileInfo &HFI = HS.header_file_begin()[UID];
1295 if (HFI.External && Chain)
1296 continue;
1297
1298 // Turn the file name into an absolute path, if it isn't already.
1299 const char *Filename = File->getName();
1300 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1301
1302 // If we performed any translation on the file name at all, we need to
1303 // save this string, since the generator will refer to it later.
1304 if (Filename != File->getName()) {
1305 Filename = strdup(Filename);
1306 SavedStrings.push_back(Filename);
1307 }
1308
1309 Generator.insert(Filename, HFI, GeneratorTrait);
1310 ++NumHeaderSearchEntries;
1311 }
1312
1313 // Create the on-disk hash table in a buffer.
1314 llvm::SmallString<4096> TableData;
1315 uint32_t BucketOffset;
1316 {
1317 llvm::raw_svector_ostream Out(TableData);
1318 // Make sure that no bucket is at offset 0
1319 clang::io::Emit32(Out, 0);
1320 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1321 }
1322
1323 // Create a blob abbreviation
1324 using namespace llvm;
1325 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1326 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1327 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1328 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1329 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1330 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1331
1332 // Write the stat cache
1333 RecordData Record;
1334 Record.push_back(HEADER_SEARCH_TABLE);
1335 Record.push_back(BucketOffset);
1336 Record.push_back(NumHeaderSearchEntries);
1337 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1338
1339 // Free all of the strings we had to duplicate.
1340 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1341 free((void*)SavedStrings[I]);
1342}
1343
Douglas Gregor14f79002009-04-10 03:52:48 +00001344/// \brief Writes the block containing the serialized form of the
1345/// source manager.
1346///
1347/// TODO: We should probably use an on-disk hash table (stored in a
1348/// blob), indexed based on the file name, so that we only create
1349/// entries for files that we actually need. In the common case (no
1350/// errors), we probably won't have to create file entries for any of
1351/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001352void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001353 const Preprocessor &PP,
1354 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001355 RecordData Record;
1356
Chris Lattnerf04ad692009-04-10 17:16:57 +00001357 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001358 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001359
1360 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001361 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1362 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1363 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1364 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001365
Douglas Gregorbd945002009-04-13 16:31:14 +00001366 // Write the line table.
1367 if (SourceMgr.hasLineTable()) {
1368 LineTableInfo &LineTable = SourceMgr.getLineTable();
1369
1370 // Emit the file names
1371 Record.push_back(LineTable.getNumFilenames());
1372 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1373 // Emit the file name
1374 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001375 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +00001376 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1377 Record.push_back(FilenameLen);
1378 if (FilenameLen)
1379 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1380 }
Mike Stump1eb44332009-09-09 15:08:12 +00001381
Douglas Gregorbd945002009-04-13 16:31:14 +00001382 // Emit the line entries
1383 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1384 L != LEnd; ++L) {
1385 // Emit the file ID
1386 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +00001387
Douglas Gregorbd945002009-04-13 16:31:14 +00001388 // Emit the line entries
1389 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001390 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +00001391 LEEnd = L->second.end();
1392 LE != LEEnd; ++LE) {
1393 Record.push_back(LE->FileOffset);
1394 Record.push_back(LE->LineNo);
1395 Record.push_back(LE->FilenameID);
1396 Record.push_back((unsigned)LE->FileKind);
1397 Record.push_back(LE->IncludeOffset);
1398 }
Douglas Gregorbd945002009-04-13 16:31:14 +00001399 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001400 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001401 }
1402
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001403 // Write out the source location entry table. We skip the first
1404 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001405 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001406 RecordData PreloadSLocs;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001407 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1408 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1409 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1410 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001411 // Get this source location entry.
1412 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001413
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001414 // Record the offset of this source-location entry.
1415 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1416
1417 // Figure out which record code to use.
1418 unsigned Code;
1419 if (SLoc->isFile()) {
1420 if (SLoc->getFile().getContentCache()->Entry)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001421 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001422 else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001423 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001424 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001425 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001426 Record.clear();
1427 Record.push_back(Code);
1428
1429 Record.push_back(SLoc->getOffset());
1430 if (SLoc->isFile()) {
1431 const SrcMgr::FileInfo &File = SLoc->getFile();
1432 Record.push_back(File.getIncludeLoc().getRawEncoding());
1433 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1434 Record.push_back(File.hasLineDirectives());
1435
1436 const SrcMgr::ContentCache *Content = File.getContentCache();
1437 if (Content->Entry) {
1438 // The source location entry is a file. The blob associated
1439 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001440
Douglas Gregor2d52be52010-03-21 22:49:54 +00001441 // Emit size/modification time for this file.
1442 Record.push_back(Content->Entry->getSize());
1443 Record.push_back(Content->Entry->getModificationTime());
1444
Douglas Gregore650c8c2009-07-07 00:12:59 +00001445 // Turn the file name into an absolute path, if it isn't already.
1446 const char *Filename = Content->Entry->getName();
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001447 llvm::SmallString<128> FilePath(Filename);
1448 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001449 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001450
Douglas Gregore650c8c2009-07-07 00:12:59 +00001451 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001452 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001453 } else {
1454 // The source location entry is a buffer. The blob associated
1455 // with this entry contains the contents of the buffer.
1456
1457 // We add one to the size so that we capture the trailing NULL
1458 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1459 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001460 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001461 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001462 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001463 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1464 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001465 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001466 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001467 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001468 llvm::StringRef(Buffer->getBufferStart(),
1469 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001470
1471 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001472 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001473 }
1474 } else {
1475 // The source location entry is an instantiation.
1476 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1477 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1478 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1479 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1480
1481 // Compute the token length for this macro expansion.
1482 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001483 if (I + 1 != N)
1484 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001485 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1486 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1487 }
1488 }
1489
Douglas Gregorc9490c02009-04-16 22:23:12 +00001490 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001491
1492 if (SLocEntryOffsets.empty())
1493 return;
1494
Sebastian Redl3397c552010-08-18 23:56:27 +00001495 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001496 // table is used for lazily loading source-location information.
1497 using namespace llvm;
1498 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001499 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001500 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1501 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1502 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1503 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001504
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001505 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001506 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001507 Record.push_back(SLocEntryOffsets.size());
Sebastian Redl8db9fae2010-09-22 20:19:08 +00001508 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1509 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001510 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001511 (const char *)data(SLocEntryOffsets),
Chris Lattner090d9b52009-04-27 19:01:47 +00001512 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001513
Sebastian Redl3397c552010-08-18 23:56:27 +00001514 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001515 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001516 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001517}
1518
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001519//===----------------------------------------------------------------------===//
1520// Preprocessor Serialization
1521//===----------------------------------------------------------------------===//
1522
Douglas Gregor9c736102011-02-10 18:20:09 +00001523static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1524 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1525 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1526 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1527 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1528 return X.first->getName().compare(Y.first->getName());
1529}
1530
Chris Lattner0b1fb982009-04-10 17:15:23 +00001531/// \brief Writes the block containing the serialized form of the
1532/// preprocessor.
1533///
Sebastian Redla4232eb2010-08-18 23:56:21 +00001534void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001535 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001536
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001537 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1538 if (PP.getCounterValue() != 0) {
1539 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001540 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001541 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001542 }
1543
1544 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001545 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001546
Sebastian Redl3397c552010-08-18 23:56:27 +00001547 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001548 // FIXME: use diagnostics subsystem for localization etc.
1549 if (PP.SawDateOrTime())
1550 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001551
Douglas Gregorecdcb882010-10-20 22:00:55 +00001552
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001553 // Loop over all the macro definitions that are live at the end of the file,
1554 // emitting each to the PP section.
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001555 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001556
Douglas Gregor9c736102011-02-10 18:20:09 +00001557 // Construct the list of macro definitions that need to be serialized.
1558 llvm::SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
1559 MacrosToEmit;
1560 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor040a8042011-02-11 00:26:14 +00001561 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1562 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001563 I != E; ++I) {
Douglas Gregor9c736102011-02-10 18:20:09 +00001564 MacroDefinitionsSeen.insert(I->first);
1565 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1566 }
1567
1568 // Sort the set of macro definitions that need to be serialized by the
1569 // name of the macro, to provide a stable ordering.
1570 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1571 &compareMacroDefinitions);
1572
Douglas Gregor040a8042011-02-11 00:26:14 +00001573 // Resolve any identifiers that defined macros at the time they were
1574 // deserialized, adding them to the list of macros to emit (if appropriate).
1575 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1576 IdentifierInfo *Name
1577 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1578 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1579 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1580 }
1581
Douglas Gregor9c736102011-02-10 18:20:09 +00001582 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1583 const IdentifierInfo *Name = MacrosToEmit[I].first;
1584 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor040a8042011-02-11 00:26:14 +00001585 if (!MI)
1586 continue;
1587
Sebastian Redl3397c552010-08-18 23:56:27 +00001588 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001589 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl3397c552010-08-18 23:56:27 +00001590 // Also skip macros from a AST file if we're chaining.
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001591
1592 // FIXME: There is a (probably minor) optimization we could do here, if
1593 // the macro comes from the original PCH but the identifier comes from a
1594 // chained PCH, by storing the offset into the original PCH rather than
1595 // writing the macro definition a second time.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001596 if (MI->isBuiltinMacro() ||
Douglas Gregor9c736102011-02-10 18:20:09 +00001597 (Chain && Name->isFromAST() && MI->isFromAST()))
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001598 continue;
1599
Douglas Gregor9c736102011-02-10 18:20:09 +00001600 AddIdentifierRef(Name, Record);
1601 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001602 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1603 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001604
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001605 unsigned Code;
1606 if (MI->isObjectLike()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001607 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001608 } else {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001609 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001610
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001611 Record.push_back(MI->isC99Varargs());
1612 Record.push_back(MI->isGNUVarargs());
1613 Record.push_back(MI->getNumArgs());
1614 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1615 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001616 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001617 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001618
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001619 // If we have a detailed preprocessing record, record the macro definition
1620 // ID that corresponds to this macro.
1621 if (PPRec)
1622 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer20249a12010-10-21 03:16:25 +00001623
Douglas Gregorc9490c02009-04-16 22:23:12 +00001624 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001625 Record.clear();
1626
Chris Lattnerdf961c22009-04-10 18:08:30 +00001627 // Emit the tokens array.
1628 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1629 // Note that we know that the preprocessor does not have any annotation
1630 // tokens in it because they are created by the parser, and thus can't be
1631 // in a macro definition.
1632 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001633
Chris Lattnerdf961c22009-04-10 18:08:30 +00001634 Record.push_back(Tok.getLocation().getRawEncoding());
1635 Record.push_back(Tok.getLength());
1636
Chris Lattnerdf961c22009-04-10 18:08:30 +00001637 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1638 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001639 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001640 // FIXME: Should translate token kind to a stable encoding.
1641 Record.push_back(Tok.getKind());
1642 // FIXME: Should translate token flags to a stable encoding.
1643 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001644
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001645 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001646 Record.clear();
1647 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001648 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001649 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001650 Stream.ExitBlock();
1651
1652 if (PPRec)
1653 WritePreprocessorDetail(*PPRec);
1654}
1655
1656void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
1657 if (PPRec.begin(Chain) == PPRec.end(Chain))
1658 return;
1659
1660 // Enter the preprocessor block.
1661 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001662
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001663 // If the preprocessor has a preprocessing record, emit it.
1664 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001665 using namespace llvm;
1666
1667 // Set up the abbreviation for
1668 unsigned InclusionAbbrev = 0;
1669 {
1670 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1671 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
1672 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1673 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1674 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1675 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1676 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1677 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1678 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1679 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1680 }
1681
1682 unsigned IndexBase = Chain ? PPRec.getNumPreallocatedEntities() : 0;
1683 RecordData Record;
1684 for (PreprocessingRecord::iterator E = PPRec.begin(Chain),
1685 EEnd = PPRec.end(Chain);
1686 E != EEnd; ++E) {
1687 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001688
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001689 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1690 // Record this macro definition's location.
1691 MacroID ID = getMacroDefinitionID(MD);
1692
1693 // Don't write the macro definition if it is from another AST file.
1694 if (ID < FirstMacroID)
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001695 continue;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001696
Douglas Gregor89d99802010-11-30 06:16:57 +00001697 // Notify the serialization listener that we're serializing this entity.
1698 if (SerializationListener)
1699 SerializationListener->SerializedPreprocessedEntity(*E,
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001700 Stream.GetCurrentBitNo());
Douglas Gregor89d99802010-11-30 06:16:57 +00001701
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001702 unsigned Position = ID - FirstMacroID;
1703 if (Position != MacroDefinitionOffsets.size()) {
1704 if (Position > MacroDefinitionOffsets.size())
1705 MacroDefinitionOffsets.resize(Position + 1);
1706
1707 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
1708 } else
1709 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor89d99802010-11-30 06:16:57 +00001710
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001711 Record.push_back(IndexBase + NumPreprocessingRecords++);
1712 Record.push_back(ID);
1713 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1714 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1715 AddIdentifierRef(MD->getName(), Record);
1716 AddSourceLocation(MD->getLocation(), Record);
1717 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1718 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001719 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001720
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001721 // Notify the serialization listener that we're serializing this entity.
1722 if (SerializationListener)
1723 SerializationListener->SerializedPreprocessedEntity(*E,
1724 Stream.GetCurrentBitNo());
1725
1726 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1727 Record.push_back(IndexBase + NumPreprocessingRecords++);
1728 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1729 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1730 AddIdentifierRef(MI->getName(), Record);
1731 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1732 Stream.EmitRecord(PPD_MACRO_INSTANTIATION, Record);
1733 continue;
1734 }
1735
1736 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1737 Record.push_back(PPD_INCLUSION_DIRECTIVE);
1738 Record.push_back(IndexBase + NumPreprocessingRecords++);
1739 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1740 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1741 Record.push_back(ID->getFileName().size());
1742 Record.push_back(ID->wasInQuotes());
1743 Record.push_back(static_cast<unsigned>(ID->getKind()));
1744 llvm::SmallString<64> Buffer;
1745 Buffer += ID->getFileName();
1746 Buffer += ID->getFile()->getName();
1747 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1748 continue;
1749 }
1750
1751 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1752 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001753 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001754
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001755 // Write the offsets table for the preprocessing record.
1756 if (NumPreprocessingRecords > 0) {
1757 // Write the offsets table for identifier IDs.
1758 using namespace llvm;
1759 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001760 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001761 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1762 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1763 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1764 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001765
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001766 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001767 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001768 Record.push_back(NumPreprocessingRecords);
1769 Record.push_back(MacroDefinitionOffsets.size());
1770 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001771 (const char *)data(MacroDefinitionOffsets),
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001772 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1773 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001774}
1775
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001776void ASTWriter::WritePragmaDiagnosticMappings(const Diagnostic &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001777 RecordData Record;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001778 for (Diagnostic::DiagStatePointsTy::const_iterator
1779 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1780 I != E; ++I) {
1781 const Diagnostic::DiagStatePoint &point = *I;
1782 if (point.Loc.isInvalid())
1783 continue;
1784
1785 Record.push_back(point.Loc.getRawEncoding());
1786 for (Diagnostic::DiagState::iterator
1787 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
1788 unsigned diag = I->first, map = I->second;
1789 if (map & 0x10) { // mapping from a diagnostic pragma.
1790 Record.push_back(diag);
1791 Record.push_back(map & 0x7);
1792 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001793 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001794 Record.push_back(-1); // mark the end of the diag/map pairs for this
1795 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001796 }
1797
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00001798 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001799 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001800}
1801
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001802//===----------------------------------------------------------------------===//
1803// Type Serialization
1804//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001805
Sebastian Redl3397c552010-08-18 23:56:27 +00001806/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001807void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00001808 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001809 if (Idx.getIndex() == 0) // we haven't seen this type before.
1810 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00001811
Douglas Gregor97475832010-10-05 18:37:06 +00001812 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00001813
Douglas Gregor2cf26342009-04-09 22:27:44 +00001814 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001815 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00001816 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001817 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00001818 else if (TypeOffsets.size() < Index) {
1819 TypeOffsets.resize(Index + 1);
1820 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001821 }
1822
1823 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001824
Douglas Gregor2cf26342009-04-09 22:27:44 +00001825 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00001826 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001827
Douglas Gregora4923eb2009-11-16 21:35:15 +00001828 if (T.hasLocalNonFastQualifiers()) {
1829 Qualifiers Qs = T.getLocalQualifiers();
1830 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001831 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001832 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00001833 } else {
1834 switch (T->getTypeClass()) {
1835 // For all of the concrete, non-dependent types, call the
1836 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001837#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001838 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001839#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001840#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001841 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001842 }
1843
1844 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001845 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001846
1847 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001848 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001849}
1850
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001851//===----------------------------------------------------------------------===//
1852// Declaration Serialization
1853//===----------------------------------------------------------------------===//
1854
Douglas Gregor2cf26342009-04-09 22:27:44 +00001855/// \brief Write the block containing all of the declaration IDs
1856/// lexically declared within the given DeclContext.
1857///
1858/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1859/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001860uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001861 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001862 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001863 return 0;
1864
Douglas Gregorc9490c02009-04-16 22:23:12 +00001865 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001866 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001867 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001868 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001869 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1870 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001871 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001872
Douglas Gregor25123082009-04-22 22:34:57 +00001873 ++NumLexicalDeclContexts;
Sebastian Redl681d7232010-07-27 00:17:23 +00001874 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001875 reinterpret_cast<char*>(Decls.data()),
1876 Decls.size() * sizeof(KindDeclIDPair));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001877 return Offset;
1878}
1879
Sebastian Redla4232eb2010-08-18 23:56:21 +00001880void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00001881 using namespace llvm;
1882 RecordData Record;
1883
1884 // Write the type offsets array
1885 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001886 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001887 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1888 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1889 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1890 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001891 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00001892 Record.push_back(TypeOffsets.size());
1893 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001894 (const char *)data(TypeOffsets),
Sebastian Redl1476ed42010-07-16 16:36:56 +00001895 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1896
1897 // Write the declaration offsets array
1898 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001899 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001900 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1901 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1902 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1903 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001904 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00001905 Record.push_back(DeclOffsets.size());
1906 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001907 (const char *)data(DeclOffsets),
Sebastian Redl1476ed42010-07-16 16:36:56 +00001908 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1909}
1910
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001911//===----------------------------------------------------------------------===//
1912// Global Method Pool and Selector Serialization
1913//===----------------------------------------------------------------------===//
1914
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001915namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001916// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00001917class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00001918 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001919
1920public:
1921 typedef Selector key_type;
1922 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001923
Sebastian Redl5d050072010-08-04 17:20:04 +00001924 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001925 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00001926 ObjCMethodList Instance, Factory;
1927 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001928 typedef const data_type& data_type_ref;
1929
Sebastian Redl3397c552010-08-18 23:56:27 +00001930 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001931
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001932 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00001933 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001934 }
Mike Stump1eb44332009-09-09 15:08:12 +00001935
1936 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001937 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1938 data_type_ref Methods) {
1939 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1940 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00001941 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1942 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001943 Method = Method->Next)
1944 if (Method->Method)
1945 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00001946 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001947 Method = Method->Next)
1948 if (Method->Method)
1949 DataLen += 4;
1950 clang::io::Emit16(Out, DataLen);
1951 return std::make_pair(KeyLen, DataLen);
1952 }
Mike Stump1eb44332009-09-09 15:08:12 +00001953
Douglas Gregor83941df2009-04-25 17:48:32 +00001954 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00001955 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00001956 assert((Start >> 32) == 0 && "Selector key offset too large");
1957 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001958 unsigned N = Sel.getNumArgs();
1959 clang::io::Emit16(Out, N);
1960 if (N == 0)
1961 N = 1;
1962 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001963 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001964 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1965 }
Mike Stump1eb44332009-09-09 15:08:12 +00001966
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001967 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00001968 data_type_ref Methods, unsigned DataLen) {
1969 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00001970 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001971 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00001972 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001973 Method = Method->Next)
1974 if (Method->Method)
1975 ++NumInstanceMethods;
1976
1977 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00001978 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001979 Method = Method->Next)
1980 if (Method->Method)
1981 ++NumFactoryMethods;
1982
1983 clang::io::Emit16(Out, NumInstanceMethods);
1984 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00001985 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001986 Method = Method->Next)
1987 if (Method->Method)
1988 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00001989 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001990 Method = Method->Next)
1991 if (Method->Method)
1992 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00001993
1994 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001995 }
1996};
1997} // end anonymous namespace
1998
Sebastian Redl059612d2010-08-03 21:58:15 +00001999/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002000///
2001/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002002/// in an on-disk hash table indexed by the selector. The hash table also
2003/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002004void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002005 using namespace llvm;
2006
Sebastian Redl059612d2010-08-03 21:58:15 +00002007 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002008 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002009 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002010 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002011 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002012 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002013 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002014 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002015
Sebastian Redl059612d2010-08-03 21:58:15 +00002016 // Create the on-disk hash table representation. We walk through every
2017 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002018 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002019 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002020 I = SelectorIDs.begin(), E = SelectorIDs.end();
2021 I != E; ++I) {
2022 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002023 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002024 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002025 I->second,
2026 ObjCMethodList(),
2027 ObjCMethodList()
2028 };
2029 if (F != SemaRef.MethodPool.end()) {
2030 Data.Instance = F->second.first;
2031 Data.Factory = F->second.second;
2032 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002033 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002034 // changed.
2035 if (Chain && I->second < FirstSelectorID) {
2036 // Selector already exists. Did it change?
2037 bool changed = false;
2038 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2039 M = M->Next) {
2040 if (M->Method->getPCHLevel() == 0)
2041 changed = true;
2042 }
2043 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2044 M = M->Next) {
2045 if (M->Method->getPCHLevel() == 0)
2046 changed = true;
2047 }
2048 if (!changed)
2049 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002050 } else if (Data.Instance.Method || Data.Factory.Method) {
2051 // A new method pool entry.
2052 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002053 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002054 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002055 }
2056
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002057 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002058 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002059 uint32_t BucketOffset;
2060 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002061 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002062 llvm::raw_svector_ostream Out(MethodPool);
2063 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002064 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002065 BucketOffset = Generator.Emit(Out, Trait);
2066 }
2067
2068 // Create a blob abbreviation
2069 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002070 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002071 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002072 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002073 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2074 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2075
Douglas Gregor83941df2009-04-25 17:48:32 +00002076 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002077 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002078 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002079 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002080 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002081 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002082
2083 // Create a blob abbreviation for the selector table offsets.
2084 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002085 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002086 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor83941df2009-04-25 17:48:32 +00002087 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2088 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2089
2090 // Write the selector offsets table.
2091 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002092 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002093 Record.push_back(SelectorOffsets.size());
2094 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00002095 (const char *)data(SelectorOffsets),
Douglas Gregor83941df2009-04-25 17:48:32 +00002096 SelectorOffsets.size() * 4);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002097 }
2098}
2099
Sebastian Redl3397c552010-08-18 23:56:27 +00002100/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002101void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002102 using namespace llvm;
2103 if (SemaRef.ReferencedSelectors.empty())
2104 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002105
Fariborz Jahanian32019832010-07-23 19:11:11 +00002106 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002107
Sebastian Redl3397c552010-08-18 23:56:27 +00002108 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002109 // very tricky to fix, and given that @selector shouldn't really appear in
2110 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002111 for (DenseMap<Selector, SourceLocation>::iterator S =
2112 SemaRef.ReferencedSelectors.begin(),
2113 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2114 Selector Sel = (*S).first;
2115 SourceLocation Loc = (*S).second;
2116 AddSelectorRef(Sel, Record);
2117 AddSourceLocation(Loc, Record);
2118 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002119 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002120}
2121
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002122//===----------------------------------------------------------------------===//
2123// Identifier Table Serialization
2124//===----------------------------------------------------------------------===//
2125
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002126namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002127class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002128 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002129 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002130
Douglas Gregora92193e2009-04-28 21:18:29 +00002131 /// \brief Determines whether this is an "interesting" identifier
2132 /// that needs a full IdentifierInfo structure written into the hash
2133 /// table.
2134 static bool isInterestingIdentifier(const IdentifierInfo *II) {
2135 return II->isPoisoned() ||
2136 II->isExtensionToken() ||
2137 II->hasMacroDefinition() ||
2138 II->getObjCOrBuiltinID() ||
2139 II->getFETokenInfo<void>();
2140 }
2141
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002142public:
2143 typedef const IdentifierInfo* key_type;
2144 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002145
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002146 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002147 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002148
Sebastian Redl3397c552010-08-18 23:56:27 +00002149 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00002150 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002151
2152 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002153 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002154 }
Mike Stump1eb44332009-09-09 15:08:12 +00002155
2156 std::pair<unsigned,unsigned>
2157 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002158 IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002159 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002160 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
2161 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00002162 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00002163 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00002164 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00002165 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00002166 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2167 DEnd = IdentifierResolver::end();
2168 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002169 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002170 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002171 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002172 // We emit the key length after the data length so that every
2173 // string is preceded by a 16-bit length. This matches the PTH
2174 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002175 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002176 return std::make_pair(KeyLen, DataLen);
2177 }
Mike Stump1eb44332009-09-09 15:08:12 +00002178
2179 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002180 unsigned KeyLen) {
2181 // Record the location of the key data. This is used when generating
2182 // the mapping from persistent IDs to strings.
2183 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002184 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002185 }
Mike Stump1eb44332009-09-09 15:08:12 +00002186
2187 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002188 IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002189 if (!isInterestingIdentifier(II)) {
2190 clang::io::Emit32(Out, ID << 1);
2191 return;
2192 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002193
Douglas Gregora92193e2009-04-28 21:18:29 +00002194 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002195 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002196 bool hasMacroDefinition =
2197 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00002198 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00002199 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002200 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
2201 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2202 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002203 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002204 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002205 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002206
Douglas Gregor37e26842009-04-21 23:56:24 +00002207 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00002208 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00002209
Douglas Gregor668c1a42009-04-21 22:25:48 +00002210 // Emit the declaration IDs in reverse order, because the
2211 // IdentifierResolver provides the declarations as they would be
2212 // visible (e.g., the function "stat" would come before the struct
2213 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2214 // adds declarations to the end of the list (so we need to see the
2215 // struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002216 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump1eb44332009-09-09 15:08:12 +00002217 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00002218 IdentifierResolver::end());
2219 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
2220 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002221 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002222 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002223 }
2224};
2225} // end anonymous namespace
2226
Sebastian Redl3397c552010-08-18 23:56:27 +00002227/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002228///
2229/// The identifier table consists of a blob containing string data
2230/// (the actual identifiers themselves) and a separate "offsets" index
2231/// that maps identifier IDs to locations within the blob.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002232void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002233 using namespace llvm;
2234
2235 // Create and write out the blob that contains the identifier
2236 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002237 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002238 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002239 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump1eb44332009-09-09 15:08:12 +00002240
Douglas Gregor92b059e2009-04-28 20:33:11 +00002241 // Look for any identifiers that were named while processing the
2242 // headers, but are otherwise not needed. We add these to the hash
2243 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002244 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002245 // file.
2246 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2247 IDEnd = PP.getIdentifierTable().end();
2248 ID != IDEnd; ++ID)
2249 getIdentifierRef(ID->second);
2250
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002251 // Create the on-disk hash table representation. We only store offsets
2252 // for identifiers that appear here for the first time.
2253 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002254 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002255 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2256 ID != IDEnd; ++ID) {
2257 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002258 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002259 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002260 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002261
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002262 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002263 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002264 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002265 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002266 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002267 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002268 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002269 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002270 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002271 }
2272
2273 // Create a blob abbreviation
2274 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002275 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002276 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002277 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002278 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002279
2280 // Write the identifier table
2281 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002282 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002283 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002284 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002285 }
2286
2287 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002288 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002289 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002290 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2291 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2292 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2293
2294 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002295 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002296 Record.push_back(IdentifierOffsets.size());
2297 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00002298 (const char *)data(IdentifierOffsets),
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002299 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002300}
2301
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002302//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002303// DeclContext's Name Lookup Table Serialization
2304//===----------------------------------------------------------------------===//
2305
2306namespace {
2307// Trait used for the on-disk hash table used in the method pool.
2308class ASTDeclContextNameLookupTrait {
2309 ASTWriter &Writer;
2310
2311public:
2312 typedef DeclarationName key_type;
2313 typedef key_type key_type_ref;
2314
2315 typedef DeclContext::lookup_result data_type;
2316 typedef const data_type& data_type_ref;
2317
2318 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2319
2320 unsigned ComputeHash(DeclarationName Name) {
2321 llvm::FoldingSetNodeID ID;
2322 ID.AddInteger(Name.getNameKind());
2323
2324 switch (Name.getNameKind()) {
2325 case DeclarationName::Identifier:
2326 ID.AddString(Name.getAsIdentifierInfo()->getName());
2327 break;
2328 case DeclarationName::ObjCZeroArgSelector:
2329 case DeclarationName::ObjCOneArgSelector:
2330 case DeclarationName::ObjCMultiArgSelector:
2331 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2332 break;
2333 case DeclarationName::CXXConstructorName:
2334 case DeclarationName::CXXDestructorName:
2335 case DeclarationName::CXXConversionFunctionName:
2336 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
2337 break;
2338 case DeclarationName::CXXOperatorName:
2339 ID.AddInteger(Name.getCXXOverloadedOperator());
2340 break;
2341 case DeclarationName::CXXLiteralOperatorName:
2342 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2343 case DeclarationName::CXXUsingDirective:
2344 break;
2345 }
2346
2347 return ID.ComputeHash();
2348 }
2349
2350 std::pair<unsigned,unsigned>
2351 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2352 data_type_ref Lookup) {
2353 unsigned KeyLen = 1;
2354 switch (Name.getNameKind()) {
2355 case DeclarationName::Identifier:
2356 case DeclarationName::ObjCZeroArgSelector:
2357 case DeclarationName::ObjCOneArgSelector:
2358 case DeclarationName::ObjCMultiArgSelector:
2359 case DeclarationName::CXXConstructorName:
2360 case DeclarationName::CXXDestructorName:
2361 case DeclarationName::CXXConversionFunctionName:
2362 case DeclarationName::CXXLiteralOperatorName:
2363 KeyLen += 4;
2364 break;
2365 case DeclarationName::CXXOperatorName:
2366 KeyLen += 1;
2367 break;
2368 case DeclarationName::CXXUsingDirective:
2369 break;
2370 }
2371 clang::io::Emit16(Out, KeyLen);
2372
2373 // 2 bytes for num of decls and 4 for each DeclID.
2374 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2375 clang::io::Emit16(Out, DataLen);
2376
2377 return std::make_pair(KeyLen, DataLen);
2378 }
2379
2380 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2381 using namespace clang::io;
2382
2383 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2384 Emit8(Out, Name.getNameKind());
2385 switch (Name.getNameKind()) {
2386 case DeclarationName::Identifier:
2387 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2388 break;
2389 case DeclarationName::ObjCZeroArgSelector:
2390 case DeclarationName::ObjCOneArgSelector:
2391 case DeclarationName::ObjCMultiArgSelector:
2392 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2393 break;
2394 case DeclarationName::CXXConstructorName:
2395 case DeclarationName::CXXDestructorName:
2396 case DeclarationName::CXXConversionFunctionName:
2397 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2398 break;
2399 case DeclarationName::CXXOperatorName:
2400 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2401 Emit8(Out, Name.getCXXOverloadedOperator());
2402 break;
2403 case DeclarationName::CXXLiteralOperatorName:
2404 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2405 break;
2406 case DeclarationName::CXXUsingDirective:
2407 break;
2408 }
2409 }
2410
2411 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2412 data_type Lookup, unsigned DataLen) {
2413 uint64_t Start = Out.tell(); (void)Start;
2414 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2415 for (; Lookup.first != Lookup.second; ++Lookup.first)
2416 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2417
2418 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2419 }
2420};
2421} // end anonymous namespace
2422
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002423/// \brief Write the block containing all of the declaration IDs
2424/// visible from the given DeclContext.
2425///
2426/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002427/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002428uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2429 DeclContext *DC) {
2430 if (DC->getPrimaryContext() != DC)
2431 return 0;
2432
2433 // Since there is no name lookup into functions or methods, don't bother to
2434 // build a visible-declarations table for these entities.
2435 if (DC->isFunctionOrMethod())
2436 return 0;
2437
2438 // If not in C++, we perform name lookup for the translation unit via the
2439 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2440 // FIXME: In C++ we need the visible declarations in order to "see" the
2441 // friend declarations, is there a way to do this without writing the table ?
2442 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2443 return 0;
2444
2445 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00002446 if (DC->hasExternalVisibleStorage())
2447 DC->MaterializeVisibleDeclsFromExternalStorage();
2448 else
2449 DC->lookup(DeclarationName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002450
2451 // Serialize the contents of the mapping used for lookup. Note that,
2452 // although we have two very different code paths, the serialized
2453 // representation is the same for both cases: a declaration name,
2454 // followed by a size, followed by references to the visible
2455 // declarations that have that name.
2456 uint64_t Offset = Stream.GetCurrentBitNo();
2457 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2458 if (!Map || Map->empty())
2459 return 0;
2460
2461 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2462 ASTDeclContextNameLookupTrait Trait(*this);
2463
2464 // Create the on-disk hash table representation.
2465 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2466 D != DEnd; ++D) {
2467 DeclarationName Name = D->first;
2468 DeclContext::lookup_result Result = D->second.getLookupResult();
2469 Generator.insert(Name, Result, Trait);
2470 }
2471
2472 // Create the on-disk hash table in a buffer.
2473 llvm::SmallString<4096> LookupTable;
2474 uint32_t BucketOffset;
2475 {
2476 llvm::raw_svector_ostream Out(LookupTable);
2477 // Make sure that no bucket is at offset 0
2478 clang::io::Emit32(Out, 0);
2479 BucketOffset = Generator.Emit(Out, Trait);
2480 }
2481
2482 // Write the lookup table
2483 RecordData Record;
2484 Record.push_back(DECL_CONTEXT_VISIBLE);
2485 Record.push_back(BucketOffset);
2486 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2487 LookupTable.str());
2488
2489 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2490 ++NumVisibleDeclContexts;
2491 return Offset;
2492}
2493
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002494/// \brief Write an UPDATE_VISIBLE block for the given context.
2495///
2496/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2497/// DeclContext in a dependent AST file. As such, they only exist for the TU
2498/// (in C++) and for namespaces.
2499void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002500 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2501 if (!Map || Map->empty())
2502 return;
2503
2504 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2505 ASTDeclContextNameLookupTrait Trait(*this);
2506
2507 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002508 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2509 D != DEnd; ++D) {
2510 DeclarationName Name = D->first;
2511 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002512 // For any name that appears in this table, the results are complete, i.e.
2513 // they overwrite results from previous PCHs. Merging is always a mess.
2514 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002515 }
2516
2517 // Create the on-disk hash table in a buffer.
2518 llvm::SmallString<4096> LookupTable;
2519 uint32_t BucketOffset;
2520 {
2521 llvm::raw_svector_ostream Out(LookupTable);
2522 // Make sure that no bucket is at offset 0
2523 clang::io::Emit32(Out, 0);
2524 BucketOffset = Generator.Emit(Out, Trait);
2525 }
2526
2527 // Write the lookup table
2528 RecordData Record;
2529 Record.push_back(UPDATE_VISIBLE);
2530 Record.push_back(getDeclID(cast<Decl>(DC)));
2531 Record.push_back(BucketOffset);
2532 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2533}
2534
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002535/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2536void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2537 RecordData Record;
2538 Record.push_back(Opts.fp_contract);
2539 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2540}
2541
2542/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2543void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
2544 if (!SemaRef.Context.getLangOptions().OpenCL)
2545 return;
2546
2547 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2548 RecordData Record;
2549#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2550#include "clang/Basic/OpenCLExtensions.def"
2551 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2552}
2553
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002554//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002555// General Serialization Routines
2556//===----------------------------------------------------------------------===//
2557
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002558/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002559void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00002560 Record.push_back(Attrs.size());
Sean Huntcf807c42010-08-18 23:23:40 +00002561 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2562 const Attr * A = *i;
2563 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2564 AddSourceLocation(A->getLocation(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002565
Sean Huntcf807c42010-08-18 23:23:40 +00002566#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00002567
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002568 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002569}
2570
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002571void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002572 Record.push_back(Str.size());
2573 Record.insert(Record.end(), Str.begin(), Str.end());
2574}
2575
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002576/// \brief Note that the identifier II occurs at the given offset
2577/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002578void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002579 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00002580 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002581 // up earlier in the chain and thus don't need an offset.
2582 if (ID >= FirstIdentID)
2583 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002584}
2585
Douglas Gregor83941df2009-04-25 17:48:32 +00002586/// \brief Note that the selector Sel occurs at the given offset
2587/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002588void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00002589 unsigned ID = SelectorIDs[Sel];
2590 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00002591 // Don't record offsets for selectors that are also available in a different
2592 // file.
2593 if (ID < FirstSelectorID)
2594 return;
2595 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00002596}
2597
Sebastian Redla4232eb2010-08-18 23:56:21 +00002598ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregor89d99802010-11-30 06:16:57 +00002599 : Stream(Stream), Chain(0), SerializationListener(0),
2600 FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002601 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redle58aa892010-08-04 18:21:41 +00002602 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor77424bc2010-10-02 19:29:26 +00002603 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2604 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00002605 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor7c789c12010-10-29 22:39:52 +00002606 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
2607 NextCXXBaseSpecifiersID(1)
2608{
Sebastian Redl30c514c2010-07-14 23:45:08 +00002609}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002610
Sebastian Redla4232eb2010-08-18 23:56:21 +00002611void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002612 const std::string &OutputFile,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002613 const char *isysroot) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002614 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002615 Stream.Emit((unsigned)'C', 8);
2616 Stream.Emit((unsigned)'P', 8);
2617 Stream.Emit((unsigned)'C', 8);
2618 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002619
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002620 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002621
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002622 if (Chain)
Sebastian Redla4232eb2010-08-18 23:56:21 +00002623 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002624 else
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002625 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002626}
2627
Sebastian Redla4232eb2010-08-18 23:56:21 +00002628void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002629 const char *isysroot,
2630 const std::string &OutputFile) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002631 using namespace llvm;
2632
2633 ASTContext &Context = SemaRef.Context;
2634 Preprocessor &PP = SemaRef.PP;
2635
Douglas Gregor2cf26342009-04-09 22:27:44 +00002636 // The translation unit is the first declaration we'll emit.
2637 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002638 ++NextDeclID;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002639 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002640
Douglas Gregor2deaea32009-04-22 18:49:13 +00002641 // Make sure that we emit IdentifierInfos (and any attached
2642 // declarations) for builtins.
2643 {
2644 IdentifierTable &Table = PP.getIdentifierTable();
2645 llvm::SmallVector<const char *, 32> BuiltinNames;
2646 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2647 Context.getLangOptions().NoBuiltin);
2648 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2649 getIdentifierRef(&Table.get(BuiltinNames[I]));
2650 }
2651
Chris Lattner63d65f82009-09-08 18:19:27 +00002652 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002653 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002654 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002655 RecordData TentativeDefinitions;
Sebastian Redle9d12b62010-01-31 22:27:38 +00002656 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2657 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner63d65f82009-09-08 18:19:27 +00002658 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002659
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002660 // Build a record containing all of the file scoped decls in this file.
2661 RecordData UnusedFileScopedDecls;
2662 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2663 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00002664
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002665 RecordData WeakUndeclaredIdentifiers;
2666 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2667 WeakUndeclaredIdentifiers.push_back(
2668 SemaRef.WeakUndeclaredIdentifiers.size());
2669 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2670 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2671 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2672 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2673 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2674 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2675 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2676 }
2677 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002678
Douglas Gregor14c22f22009-04-22 22:18:58 +00002679 // Build a record containing all of the locally-scoped external
2680 // declarations in this header file. Generally, this record will be
2681 // empty.
2682 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002683 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00002684 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002685 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002686 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2687 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2688 TD != TDEnd; ++TD)
2689 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2690
Douglas Gregorb81c1702009-04-27 20:06:05 +00002691 // Build a record containing all of the ext_vector declarations.
2692 RecordData ExtVectorDecls;
2693 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2694 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2695
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002696 // Build a record containing all of the VTable uses information.
2697 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00002698 if (!SemaRef.VTableUses.empty()) {
2699 VTableUses.push_back(SemaRef.VTableUses.size());
2700 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2701 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2702 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2703 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2704 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002705 }
2706
2707 // Build a record containing all of dynamic classes declarations.
2708 RecordData DynamicClasses;
2709 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2710 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2711
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002712 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002713 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002714 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00002715 I = SemaRef.PendingInstantiations.begin(),
2716 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2717 AddDeclRef(I->first, PendingInstantiations);
2718 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002719 }
2720 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2721 "There are local ones at end of translation unit!");
2722
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002723 // Build a record containing some declaration references.
2724 RecordData SemaDeclRefs;
2725 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2726 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2727 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2728 }
2729
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002730 RecordData CUDASpecialDeclRefs;
2731 if (Context.getcudaConfigureCallDecl()) {
2732 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
2733 }
2734
Sebastian Redl3397c552010-08-18 23:56:27 +00002735 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002736 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002737 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002738 WriteMetadata(Context, isysroot, OutputFile);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002739 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00002740 if (StatCalls && !isysroot)
Douglas Gregordd41ed52010-07-12 23:48:14 +00002741 WriteStatCache(*StatCalls);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002742 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002743 // Write the record of special types.
2744 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00002745
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002746 AddTypeRef(Context.getBuiltinVaListType(), Record);
2747 AddTypeRef(Context.getObjCIdType(), Record);
2748 AddTypeRef(Context.getObjCSelType(), Record);
2749 AddTypeRef(Context.getObjCProtoType(), Record);
2750 AddTypeRef(Context.getObjCClassType(), Record);
2751 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2752 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2753 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002754 AddTypeRef(Context.getjmp_bufType(), Record);
2755 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002756 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2757 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpadaaad32009-10-20 02:12:22 +00002758 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002759 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002760 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2761 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002762 Record.push_back(Context.isInt128Installed());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002763 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002764
Douglas Gregor366809a2009-04-26 03:49:13 +00002765 // Keep writing types and declarations until all types and
2766 // declarations have been written.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002767 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002768 WriteDeclsBlockAbbrevs();
2769 while (!DeclTypesToEmit.empty()) {
2770 DeclOrType DOT = DeclTypesToEmit.front();
2771 DeclTypesToEmit.pop();
2772 if (DOT.isType())
2773 WriteType(DOT.getType());
2774 else
2775 WriteDecl(Context, DOT.getDecl());
2776 }
2777 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002778
Douglas Gregor813a97b2009-10-17 17:25:45 +00002779 WritePreprocessor(PP);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002780 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00002781 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002782 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002783 WriteIdentifierTable(PP);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002784 WriteFPPragmaOptions(SemaRef.getFPOptions());
2785 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002786
Sebastian Redl1476ed42010-07-16 16:36:56 +00002787 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002788 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00002789
Douglas Gregor7c789c12010-10-29 22:39:52 +00002790 // Write the C++ base-specifier set offsets.
2791 if (!CXXBaseSpecifiersOffsets.empty()) {
2792 // Create a blob abbreviation for the C++ base specifiers offsets.
2793 using namespace llvm;
2794
2795 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2796 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2797 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2798 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2799 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2800
2801 // Write the selector offsets table.
2802 Record.clear();
2803 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2804 Record.push_back(CXXBaseSpecifiersOffsets.size());
2805 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
2806 (const char *)CXXBaseSpecifiersOffsets.data(),
2807 CXXBaseSpecifiersOffsets.size() * sizeof(uint32_t));
2808 }
2809
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002810 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002811 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002812 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002813
2814 // Write the record containing tentative definitions.
2815 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002816 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002817
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002818 // Write the record containing unused file scoped decls.
2819 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002820 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002821
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002822 // Write the record containing weak undeclared identifiers.
2823 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002824 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002825 WeakUndeclaredIdentifiers);
2826
Douglas Gregor14c22f22009-04-22 22:18:58 +00002827 // Write the record containing locally-scoped external definitions.
2828 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002829 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002830 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002831
2832 // Write the record containing ext_vector type names.
2833 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002834 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002835
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002836 // Write the record containing VTable uses information.
2837 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002838 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002839
2840 // Write the record containing dynamic classes declarations.
2841 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002842 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002843
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002844 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002845 if (!PendingInstantiations.empty())
2846 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002847
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002848 // Write the record containing declaration references of Sema.
2849 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002850 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002851
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002852 // Write the record containing CUDA-specific declaration references.
2853 if (!CUDASpecialDeclRefs.empty())
2854 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
2855
Douglas Gregor3e1af842009-04-17 22:13:46 +00002856 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002857 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002858 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002859 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002860 Record.push_back(NumLexicalDeclContexts);
2861 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002862 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002863 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002864}
2865
Sebastian Redla4232eb2010-08-18 23:56:21 +00002866void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002867 const char *isysroot) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002868 using namespace llvm;
2869
2870 ASTContext &Context = SemaRef.Context;
2871 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1476ed42010-07-16 16:36:56 +00002872
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002873 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002874 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002875 WriteMetadata(Context, isysroot, "");
Sebastian Redl1476ed42010-07-16 16:36:56 +00002876 if (StatCalls && !isysroot)
2877 WriteStatCache(*StatCalls);
2878 // FIXME: Source manager block should only write new stuff, which could be
2879 // done by tracking the largest ID in the chain
2880 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002881
2882 // The special types are in the chained PCH.
2883
2884 // We don't start with the translation unit, but with its decls that
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002885 // don't come from the chained PCH.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002886 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002887 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl681d7232010-07-27 00:17:23 +00002888 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2889 E = TU->noload_decls_end();
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002890 I != E; ++I) {
Sebastian Redld692af72010-07-27 18:24:41 +00002891 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002892 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redl0b17c612010-08-13 00:28:03 +00002893 else if ((*I)->isChangedSinceDeserialization())
2894 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002895 }
Sebastian Redl681d7232010-07-27 00:17:23 +00002896 // We also need to write a lexical updates block for the TU.
Sebastian Redld692af72010-07-27 18:24:41 +00002897 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002898 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redld692af72010-07-27 18:24:41 +00002899 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2900 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2901 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002902 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redld692af72010-07-27 18:24:41 +00002903 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2904 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002905 NewGlobalDecls.size() * sizeof(KindDeclIDPair));
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00002906 // And a visible updates block for the DeclContexts.
2907 Abv = new llvm::BitCodeAbbrev();
2908 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2909 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2910 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2911 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2912 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2913 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002914
Sebastian Redl083abdf2010-07-27 23:01:28 +00002915 // Build a record containing all of the new tentative definitions in this
2916 // file, in TentativeDefinitions order.
2917 RecordData TentativeDefinitions;
2918 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2919 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2920 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2921 }
2922
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002923 // Build a record containing all of the file scoped decls in this file.
2924 RecordData UnusedFileScopedDecls;
2925 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2926 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2927 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002928 }
2929
Sebastian Redl40566802010-08-05 18:21:25 +00002930 // We write the entire table, overwriting the tables from the chain.
2931 RecordData WeakUndeclaredIdentifiers;
2932 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2933 WeakUndeclaredIdentifiers.push_back(
2934 SemaRef.WeakUndeclaredIdentifiers.size());
2935 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2936 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2937 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2938 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2939 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2940 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2941 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2942 }
2943 }
2944
Sebastian Redl083abdf2010-07-27 23:01:28 +00002945 // Build a record containing all of the locally-scoped external
2946 // declarations in this header file. Generally, this record will be
2947 // empty.
2948 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002949 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl083abdf2010-07-27 23:01:28 +00002950 // nondeterminstic!
2951 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2952 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2953 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2954 TD != TDEnd; ++TD) {
2955 if (TD->second->getPCHLevel() == 0)
2956 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2957 }
2958
2959 // Build a record containing all of the ext_vector declarations.
2960 RecordData ExtVectorDecls;
2961 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2962 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2963 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2964 }
2965
Sebastian Redl40566802010-08-05 18:21:25 +00002966 // Build a record containing all of the VTable uses information.
2967 // We write everything here, because it's too hard to determine whether
2968 // a use is new to this part.
2969 RecordData VTableUses;
2970 if (!SemaRef.VTableUses.empty()) {
2971 VTableUses.push_back(SemaRef.VTableUses.size());
2972 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2973 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2974 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2975 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2976 }
2977 }
2978
2979 // Build a record containing all of dynamic classes declarations.
2980 RecordData DynamicClasses;
2981 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2982 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
2983 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2984
2985 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002986 RecordData PendingInstantiations;
Sebastian Redl40566802010-08-05 18:21:25 +00002987 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00002988 I = SemaRef.PendingInstantiations.begin(),
2989 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl40566802010-08-05 18:21:25 +00002990 if (I->first->getPCHLevel() == 0) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002991 AddDeclRef(I->first, PendingInstantiations);
2992 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl40566802010-08-05 18:21:25 +00002993 }
2994 }
2995 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2996 "There are local ones at end of translation unit!");
2997
2998 // Build a record containing some declaration references.
2999 // It's not worth the effort to avoid duplication here.
3000 RecordData SemaDeclRefs;
3001 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3002 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3003 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3004 }
3005
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003006 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003007 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00003008 for (DeclsToRewriteTy::iterator
3009 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
3010 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003011 while (!DeclTypesToEmit.empty()) {
3012 DeclOrType DOT = DeclTypesToEmit.front();
3013 DeclTypesToEmit.pop();
3014 if (DOT.isType())
3015 WriteType(DOT.getType());
3016 else
3017 WriteDecl(Context, DOT.getDecl());
3018 }
3019 Stream.ExitBlock();
3020
Sebastian Redl083abdf2010-07-27 23:01:28 +00003021 WritePreprocessor(PP);
Sebastian Redla68340f2010-08-04 22:21:29 +00003022 WriteSelectors(SemaRef);
3023 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003024 WriteIdentifierTable(PP);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003025 WriteFPPragmaOptions(SemaRef.getFPOptions());
3026 WriteOpenCLExtensions(SemaRef);
3027
Sebastian Redl1476ed42010-07-16 16:36:56 +00003028 WriteTypeDeclOffsets();
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00003029 // FIXME: For chained PCH only write the new mappings (we currently
3030 // write all of them again).
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003031 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl083abdf2010-07-27 23:01:28 +00003032
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003033 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl3397c552010-08-18 23:56:27 +00003034 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003035 RecordData FirstLatestDeclIDs;
3036 for (FirstLatestDeclMap::iterator
3037 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
3038 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
3039 "Expected first & second to be in different PCHs");
3040 AddDeclRef(I->first, FirstLatestDeclIDs);
3041 AddDeclRef(I->second, FirstLatestDeclIDs);
3042 }
3043 if (!FirstLatestDeclIDs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003044 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003045
Sebastian Redl083abdf2010-07-27 23:01:28 +00003046 // Write the record containing external, unnamed definitions.
3047 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003048 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003049
3050 // Write the record containing tentative definitions.
3051 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003052 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003053
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003054 // Write the record containing unused file scoped decls.
3055 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003056 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003057
Sebastian Redl40566802010-08-05 18:21:25 +00003058 // Write the record containing weak undeclared identifiers.
3059 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003060 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl40566802010-08-05 18:21:25 +00003061 WeakUndeclaredIdentifiers);
3062
Sebastian Redl083abdf2010-07-27 23:01:28 +00003063 // Write the record containing locally-scoped external definitions.
3064 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003065 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl083abdf2010-07-27 23:01:28 +00003066 LocallyScopedExternalDecls);
3067
3068 // Write the record containing ext_vector type names.
3069 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003070 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003071
Sebastian Redl40566802010-08-05 18:21:25 +00003072 // Write the record containing VTable uses information.
3073 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003074 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl40566802010-08-05 18:21:25 +00003075
3076 // Write the record containing dynamic classes declarations.
3077 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003078 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl40566802010-08-05 18:21:25 +00003079
3080 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003081 if (!PendingInstantiations.empty())
3082 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl40566802010-08-05 18:21:25 +00003083
3084 // Write the record containing declaration references of Sema.
3085 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003086 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003087
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00003088 // Write the updates to DeclContexts.
3089 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3090 I = UpdatedDeclContexts.begin(),
3091 E = UpdatedDeclContexts.end();
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003092 I != E; ++I)
3093 WriteDeclContextVisibleUpdate(*I);
3094
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003095 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003096
Sebastian Redl083abdf2010-07-27 23:01:28 +00003097 Record.clear();
3098 Record.push_back(NumStatements);
3099 Record.push_back(NumMacros);
3100 Record.push_back(NumLexicalDeclContexts);
3101 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003102 WriteDeclReplacementsBlock();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003103 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003104 Stream.ExitBlock();
3105}
3106
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003107void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003108 if (DeclUpdates.empty())
3109 return;
3110
3111 RecordData OffsetsRecord;
3112 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
3113 for (DeclUpdateMap::iterator
3114 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3115 const Decl *D = I->first;
3116 UpdateRecord &URec = I->second;
3117
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003118 if (DeclsToRewrite.count(D))
3119 continue; // The decl will be written completely,no need to store updates.
3120
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003121 uint64_t Offset = Stream.GetCurrentBitNo();
3122 Stream.EmitRecord(DECL_UPDATES, URec);
3123
3124 OffsetsRecord.push_back(GetDeclRef(D));
3125 OffsetsRecord.push_back(Offset);
3126 }
3127 Stream.ExitBlock();
3128 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3129}
3130
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003131void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003132 if (ReplacedDecls.empty())
3133 return;
3134
3135 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003136 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003137 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
3138 Record.push_back(I->first);
3139 Record.push_back(I->second);
3140 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003141 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003142}
3143
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003144void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003145 Record.push_back(Loc.getRawEncoding());
3146}
3147
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003148void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003149 AddSourceLocation(Range.getBegin(), Record);
3150 AddSourceLocation(Range.getEnd(), Record);
3151}
3152
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003153void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003154 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003155 const uint64_t *Words = Value.getRawData();
3156 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003157}
3158
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003159void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003160 Record.push_back(Value.isUnsigned());
3161 AddAPInt(Value, Record);
3162}
3163
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003164void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003165 AddAPInt(Value.bitcastToAPInt(), Record);
3166}
3167
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003168void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003169 Record.push_back(getIdentifierRef(II));
3170}
3171
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003172IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003173 if (II == 0)
3174 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003175
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003176 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003177 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003178 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003179 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003180}
3181
Sebastian Redlf73c93f2010-09-15 19:54:06 +00003182MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003183 if (MD == 0)
3184 return 0;
Sebastian Redlf73c93f2010-09-15 19:54:06 +00003185
3186 MacroID &ID = MacroDefinitions[MD];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003187 if (ID == 0)
Douglas Gregor77424bc2010-10-02 19:29:26 +00003188 ID = NextMacroID++;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003189 return ID;
3190}
3191
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003192void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003193 Record.push_back(getSelectorRef(SelRef));
3194}
3195
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003196SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003197 if (Sel.getAsOpaquePtr() == 0) {
3198 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003199 }
3200
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003201 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003202 if (SID == 0 && Chain) {
3203 // This might trigger a ReadSelector callback, which will set the ID for
3204 // this selector.
3205 Chain->LoadSelector(Sel);
3206 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003207 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003208 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003209 }
Sebastian Redl5d050072010-08-04 17:20:04 +00003210 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003211}
3212
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003213void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00003214 AddDeclRef(Temp->getDestructor(), Record);
3215}
3216
Douglas Gregor7c789c12010-10-29 22:39:52 +00003217void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3218 CXXBaseSpecifier const *BasesEnd,
3219 RecordDataImpl &Record) {
3220 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3221 CXXBaseSpecifiersToWrite.push_back(
3222 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3223 Bases, BasesEnd));
3224 Record.push_back(NextCXXBaseSpecifiersID++);
3225}
3226
Sebastian Redla4232eb2010-08-18 23:56:21 +00003227void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003228 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003229 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003230 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00003231 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003232 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00003233 break;
3234 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003235 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00003236 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00003237 case TemplateArgument::Template:
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003238 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
3239 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003240 break;
3241 case TemplateArgument::TemplateExpansion:
3242 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
3243 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003244 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00003245 break;
John McCall833ca992009-10-29 08:12:44 +00003246 case TemplateArgument::Null:
3247 case TemplateArgument::Integral:
3248 case TemplateArgument::Declaration:
3249 case TemplateArgument::Pack:
3250 break;
3251 }
3252}
3253
Sebastian Redla4232eb2010-08-18 23:56:21 +00003254void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003255 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003256 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003257
3258 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3259 bool InfoHasSameExpr
3260 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3261 Record.push_back(InfoHasSameExpr);
3262 if (InfoHasSameExpr)
3263 return; // Avoid storing the same expr twice.
3264 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003265 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3266 Record);
3267}
3268
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003269void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00003270 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00003271 AddTypeRef(QualType(), Record);
3272 return;
3273 }
3274
John McCalla93c9342009-12-07 02:54:59 +00003275 AddTypeRef(TInfo->getType(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +00003276 TypeLocWriter TLW(*this, Record);
John McCalla93c9342009-12-07 02:54:59 +00003277 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003278 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00003279}
3280
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003281void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00003282 Record.push_back(GetOrCreateTypeID(T));
3283}
3284
3285TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003286 return MakeTypeID(T,
3287 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3288}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003289
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003290TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003291 return MakeTypeID(T,
3292 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003293}
3294
3295TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3296 if (T.isNull())
3297 return TypeIdx();
3298 assert(!T.getLocalFastQualifiers());
3299
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00003300 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003301 if (Idx.getIndex() == 0) {
Douglas Gregor366809a2009-04-26 03:49:13 +00003302 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00003303 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003304 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003305 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00003306 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003307 return Idx;
3308}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003309
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003310TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003311 if (T.isNull())
3312 return TypeIdx();
3313 assert(!T.getLocalFastQualifiers());
3314
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003315 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3316 assert(I != TypeIdxs.end() && "Type not emitted!");
3317 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003318}
3319
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003320void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003321 Record.push_back(GetDeclRef(D));
3322}
3323
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003324DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003325 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003326 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003327 }
Douglas Gregor97475832010-10-05 18:37:06 +00003328 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003329 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00003330 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003331 // We haven't seen this declaration before. Give it a new ID and
3332 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003333 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003334 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redl0b17c612010-08-13 00:28:03 +00003335 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3336 // We don't add it to the replacement collection here, because we don't
3337 // have the offset yet.
3338 DeclTypesToEmit.push(const_cast<Decl *>(D));
3339 // Reset the flag, so that we don't add this decl multiple times.
3340 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003341 }
3342
Sebastian Redl681d7232010-07-27 00:17:23 +00003343 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003344}
3345
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003346DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003347 if (D == 0)
3348 return 0;
3349
3350 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3351 return DeclIDs[D];
3352}
3353
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003354void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00003355 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00003356 Record.push_back(Name.getNameKind());
3357 switch (Name.getNameKind()) {
3358 case DeclarationName::Identifier:
3359 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3360 break;
3361
3362 case DeclarationName::ObjCZeroArgSelector:
3363 case DeclarationName::ObjCOneArgSelector:
3364 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003365 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003366 break;
3367
3368 case DeclarationName::CXXConstructorName:
3369 case DeclarationName::CXXDestructorName:
3370 case DeclarationName::CXXConversionFunctionName:
3371 AddTypeRef(Name.getCXXNameType(), Record);
3372 break;
3373
3374 case DeclarationName::CXXOperatorName:
3375 Record.push_back(Name.getCXXOverloadedOperator());
3376 break;
3377
Sean Hunt3e518bd2009-11-29 07:34:05 +00003378 case DeclarationName::CXXLiteralOperatorName:
3379 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3380 break;
3381
Douglas Gregor2cf26342009-04-09 22:27:44 +00003382 case DeclarationName::CXXUsingDirective:
3383 // No extra data to emit
3384 break;
3385 }
3386}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003387
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003388void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003389 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003390 switch (Name.getNameKind()) {
3391 case DeclarationName::CXXConstructorName:
3392 case DeclarationName::CXXDestructorName:
3393 case DeclarationName::CXXConversionFunctionName:
3394 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3395 break;
3396
3397 case DeclarationName::CXXOperatorName:
3398 AddSourceLocation(
3399 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3400 Record);
3401 AddSourceLocation(
3402 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3403 Record);
3404 break;
3405
3406 case DeclarationName::CXXLiteralOperatorName:
3407 AddSourceLocation(
3408 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3409 Record);
3410 break;
3411
3412 case DeclarationName::Identifier:
3413 case DeclarationName::ObjCZeroArgSelector:
3414 case DeclarationName::ObjCOneArgSelector:
3415 case DeclarationName::ObjCMultiArgSelector:
3416 case DeclarationName::CXXUsingDirective:
3417 break;
3418 }
3419}
3420
3421void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003422 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003423 AddDeclarationName(NameInfo.getName(), Record);
3424 AddSourceLocation(NameInfo.getLoc(), Record);
3425 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3426}
3427
3428void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003429 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003430 AddNestedNameSpecifier(Info.NNS, Record);
3431 AddSourceRange(Info.NNSRange, Record);
3432 Record.push_back(Info.NumTemplParamLists);
3433 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3434 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3435}
3436
Sebastian Redla4232eb2010-08-18 23:56:21 +00003437void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003438 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003439 // Nested name specifiers usually aren't too long. I think that 8 would
3440 // typically accomodate the vast majority.
3441 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3442
3443 // Push each of the NNS's onto a stack for serialization in reverse order.
3444 while (NNS) {
3445 NestedNames.push_back(NNS);
3446 NNS = NNS->getPrefix();
3447 }
3448
3449 Record.push_back(NestedNames.size());
3450 while(!NestedNames.empty()) {
3451 NNS = NestedNames.pop_back_val();
3452 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3453 Record.push_back(Kind);
3454 switch (Kind) {
3455 case NestedNameSpecifier::Identifier:
3456 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3457 break;
3458
3459 case NestedNameSpecifier::Namespace:
3460 AddDeclRef(NNS->getAsNamespace(), Record);
3461 break;
3462
3463 case NestedNameSpecifier::TypeSpec:
3464 case NestedNameSpecifier::TypeSpecWithTemplate:
3465 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3466 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3467 break;
3468
3469 case NestedNameSpecifier::Global:
3470 // Don't need to write an associated value.
3471 break;
3472 }
3473 }
3474}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003475
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003476void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00003477 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003478 Record.push_back(Kind);
3479 switch (Kind) {
3480 case TemplateName::Template:
3481 AddDeclRef(Name.getAsTemplateDecl(), Record);
3482 break;
3483
3484 case TemplateName::OverloadedTemplate: {
3485 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3486 Record.push_back(OvT->size());
3487 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3488 I != E; ++I)
3489 AddDeclRef(*I, Record);
3490 break;
3491 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003492
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003493 case TemplateName::QualifiedTemplate: {
3494 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3495 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3496 Record.push_back(QualT->hasTemplateKeyword());
3497 AddDeclRef(QualT->getTemplateDecl(), Record);
3498 break;
3499 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003500
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003501 case TemplateName::DependentTemplate: {
3502 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3503 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3504 Record.push_back(DepT->isIdentifier());
3505 if (DepT->isIdentifier())
3506 AddIdentifierRef(DepT->getIdentifier(), Record);
3507 else
3508 Record.push_back(DepT->getOperator());
3509 break;
3510 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00003511
3512 case TemplateName::SubstTemplateTemplateParmPack: {
3513 SubstTemplateTemplateParmPackStorage *SubstPack
3514 = Name.getAsSubstTemplateTemplateParmPack();
3515 AddDeclRef(SubstPack->getParameterPack(), Record);
3516 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
3517 break;
3518 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003519 }
3520}
3521
Michael J. Spencer20249a12010-10-21 03:16:25 +00003522void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003523 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003524 Record.push_back(Arg.getKind());
3525 switch (Arg.getKind()) {
3526 case TemplateArgument::Null:
3527 break;
3528 case TemplateArgument::Type:
3529 AddTypeRef(Arg.getAsType(), Record);
3530 break;
3531 case TemplateArgument::Declaration:
3532 AddDeclRef(Arg.getAsDecl(), Record);
3533 break;
3534 case TemplateArgument::Integral:
3535 AddAPSInt(*Arg.getAsIntegral(), Record);
3536 AddTypeRef(Arg.getIntegralType(), Record);
3537 break;
3538 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00003539 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3540 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00003541 case TemplateArgument::TemplateExpansion:
3542 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00003543 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3544 Record.push_back(*NumExpansions + 1);
3545 else
3546 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003547 break;
3548 case TemplateArgument::Expression:
3549 AddStmt(Arg.getAsExpr());
3550 break;
3551 case TemplateArgument::Pack:
3552 Record.push_back(Arg.pack_size());
3553 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3554 I != E; ++I)
3555 AddTemplateArgument(*I, Record);
3556 break;
3557 }
3558}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003559
3560void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003561ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003562 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003563 assert(TemplateParams && "No TemplateParams!");
3564 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3565 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3566 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3567 Record.push_back(TemplateParams->size());
3568 for (TemplateParameterList::const_iterator
3569 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3570 P != PEnd; ++P)
3571 AddDeclRef(*P, Record);
3572}
3573
3574/// \brief Emit a template argument list.
3575void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003576ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003577 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003578 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00003579 Record.push_back(TemplateArgs->size());
3580 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003581 AddTemplateArgument(TemplateArgs->get(i), Record);
3582}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003583
3584
3585void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003586ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003587 Record.push_back(Set.size());
3588 for (UnresolvedSetImpl::const_iterator
3589 I = Set.begin(), E = Set.end(); I != E; ++I) {
3590 AddDeclRef(I.getDecl(), Record);
3591 Record.push_back(I.getAccess());
3592 }
3593}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003594
Sebastian Redla4232eb2010-08-18 23:56:21 +00003595void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003596 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003597 Record.push_back(Base.isVirtual());
3598 Record.push_back(Base.isBaseOfClass());
3599 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00003600 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00003601 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003602 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00003603 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3604 : SourceLocation(),
3605 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003606}
Sebastian Redl30c514c2010-07-14 23:45:08 +00003607
Douglas Gregor7c789c12010-10-29 22:39:52 +00003608void ASTWriter::FlushCXXBaseSpecifiers() {
3609 RecordData Record;
3610 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3611 Record.clear();
3612
3613 // Record the offset of this base-specifier set.
3614 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3615 if (Index == CXXBaseSpecifiersOffsets.size())
3616 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3617 else {
3618 if (Index > CXXBaseSpecifiersOffsets.size())
3619 CXXBaseSpecifiersOffsets.resize(Index + 1);
3620 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3621 }
3622
3623 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3624 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3625 Record.push_back(BEnd - B);
3626 for (; B != BEnd; ++B)
3627 AddCXXBaseSpecifier(*B, Record);
3628 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00003629
3630 // Flush any expressions that were written as part of the base specifiers.
3631 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003632 }
3633
3634 CXXBaseSpecifiersToWrite.clear();
3635}
3636
Sean Huntcbb67482011-01-08 20:30:50 +00003637void ASTWriter::AddCXXCtorInitializers(
3638 const CXXCtorInitializer * const *CtorInitializers,
3639 unsigned NumCtorInitializers,
3640 RecordDataImpl &Record) {
3641 Record.push_back(NumCtorInitializers);
3642 for (unsigned i=0; i != NumCtorInitializers; ++i) {
3643 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003644
3645 Record.push_back(Init->isBaseInitializer());
3646 if (Init->isBaseInitializer()) {
3647 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3648 Record.push_back(Init->isBaseVirtual());
3649 } else {
Francois Pichet00eb3f92010-12-04 09:14:42 +00003650 Record.push_back(Init->isIndirectMemberInitializer());
3651 if (Init->isIndirectMemberInitializer())
3652 AddDeclRef(Init->getIndirectMember(), Record);
3653 else
3654 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003655 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00003656
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003657 AddSourceLocation(Init->getMemberLocation(), Record);
3658 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003659 AddSourceLocation(Init->getLParenLoc(), Record);
3660 AddSourceLocation(Init->getRParenLoc(), Record);
3661 Record.push_back(Init->isWritten());
3662 if (Init->isWritten()) {
3663 Record.push_back(Init->getSourceOrder());
3664 } else {
3665 Record.push_back(Init->getNumArrayIndices());
3666 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3667 AddDeclRef(Init->getArrayIndex(i), Record);
3668 }
3669 }
3670}
3671
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003672void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3673 assert(D->DefinitionData);
3674 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3675 Record.push_back(Data.UserDeclaredConstructor);
3676 Record.push_back(Data.UserDeclaredCopyConstructor);
3677 Record.push_back(Data.UserDeclaredCopyAssignment);
3678 Record.push_back(Data.UserDeclaredDestructor);
3679 Record.push_back(Data.Aggregate);
3680 Record.push_back(Data.PlainOldData);
3681 Record.push_back(Data.Empty);
3682 Record.push_back(Data.Polymorphic);
3683 Record.push_back(Data.Abstract);
3684 Record.push_back(Data.HasTrivialConstructor);
3685 Record.push_back(Data.HasTrivialCopyConstructor);
3686 Record.push_back(Data.HasTrivialCopyAssignment);
3687 Record.push_back(Data.HasTrivialDestructor);
3688 Record.push_back(Data.ComputedVisibleConversions);
3689 Record.push_back(Data.DeclaredDefaultConstructor);
3690 Record.push_back(Data.DeclaredCopyConstructor);
3691 Record.push_back(Data.DeclaredCopyAssignment);
3692 Record.push_back(Data.DeclaredDestructor);
3693
3694 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003695 if (Data.NumBases > 0)
3696 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3697 Record);
3698
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003699 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3700 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003701 if (Data.NumVBases > 0)
3702 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3703 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003704
3705 AddUnresolvedSet(Data.Conversions, Record);
3706 AddUnresolvedSet(Data.VisibleConversions, Record);
3707 // Data.Definition is the owning decl, no need to write it.
3708 AddDeclRef(Data.FirstFriend, Record);
3709}
3710
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003711void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003712 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003713 assert(!Chain && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003714 assert(FirstDeclID == NextDeclID &&
3715 FirstTypeID == NextTypeID &&
3716 FirstIdentID == NextIdentID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00003717 FirstSelectorID == NextSelectorID &&
Douglas Gregor77424bc2010-10-02 19:29:26 +00003718 FirstMacroID == NextMacroID &&
Douglas Gregor7c789c12010-10-29 22:39:52 +00003719 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003720 "Setting chain after writing has started.");
3721 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003722
3723 FirstDeclID += Chain->getTotalNumDecls();
3724 FirstTypeID += Chain->getTotalNumTypes();
3725 FirstIdentID += Chain->getTotalNumIdentifiers();
3726 FirstSelectorID += Chain->getTotalNumSelectors();
3727 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003728 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003729 NextDeclID = FirstDeclID;
3730 NextTypeID = FirstTypeID;
3731 NextIdentID = FirstIdentID;
3732 NextSelectorID = FirstSelectorID;
3733 NextMacroID = FirstMacroID;
Douglas Gregor7c789c12010-10-29 22:39:52 +00003734 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003735}
3736
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003737void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003738 IdentifierIDs[II] = ID;
Douglas Gregor040a8042011-02-11 00:26:14 +00003739 if (II->hasMacroDefinition())
3740 DeserializedMacroNames.push_back(II);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003741}
3742
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003743void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00003744 // Always take the highest-numbered type index. This copes with an interesting
3745 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00003746 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00003747 // keep the higher-numbered entry so that we can properly write it out to
3748 // the AST file.
3749 TypeIdx &StoredIdx = TypeIdxs[T];
3750 if (Idx.getIndex() >= StoredIdx.getIndex())
3751 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003752}
3753
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003754void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1476ed42010-07-16 16:36:56 +00003755 DeclIDs[D] = ID;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003756}
Sebastian Redl5d050072010-08-04 17:20:04 +00003757
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003758void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003759 SelectorIDs[S] = ID;
3760}
Douglas Gregor77424bc2010-10-02 19:29:26 +00003761
Michael J. Spencer20249a12010-10-21 03:16:25 +00003762void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00003763 MacroDefinition *MD) {
3764 MacroDefinitions[MD] = ID;
3765}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003766
3767void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3768 assert(D->isDefinition());
3769 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3770 // We are interested when a PCH decl is modified.
3771 if (RD->getPCHLevel() > 0) {
3772 // A forward reference was mutated into a definition. Rewrite it.
3773 // FIXME: This happens during template instantiation, should we
3774 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00003775 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003776 }
3777
3778 for (CXXRecordDecl::redecl_iterator
3779 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3780 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3781 if (Redecl == RD)
3782 continue;
3783
3784 // We are interested when a PCH decl is modified.
3785 if (Redecl->getPCHLevel() > 0) {
3786 UpdateRecord &Record = DeclUpdates[Redecl];
3787 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3788 assert(Redecl->DefinitionData);
3789 assert(Redecl->DefinitionData->Definition == D);
3790 AddDeclRef(D, Record); // the DefinitionDecl
3791 }
3792 }
3793 }
3794}
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00003795void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3796 // TU and namespaces are handled elsewhere.
3797 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3798 return;
3799
3800 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3801 return; // Not a source decl added to a DeclContext from PCH.
3802
3803 AddUpdatedDeclContext(DC);
3804}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00003805
3806void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3807 assert(D->isImplicit());
3808 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3809 return; // Not a source member added to a class from PCH.
3810 if (!isa<CXXMethodDecl>(D))
3811 return; // We are interested in lazily declared implicit methods.
3812
3813 // A decl coming from PCH was modified.
3814 assert(RD->isDefinition());
3815 UpdateRecord &Record = DeclUpdates[RD];
3816 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3817 AddDeclRef(D, Record);
3818}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00003819
3820void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3821 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00003822 // The specializations set is kept in the canonical template.
3823 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00003824 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3825 return; // Not a source specialization added to a template from PCH.
3826
3827 UpdateRecord &Record = DeclUpdates[TD];
3828 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3829 AddDeclRef(D, Record);
3830}
Douglas Gregor89d99802010-11-30 06:16:57 +00003831
3832ASTSerializationListener::~ASTSerializationListener() { }