blob: ba2494b5c51bd1d5a88d3bc0a6ed96f688f38021 [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
Richard Smith34b41d92011-02-20 03:19:35 +0000216void ASTTypeWriter::VisitAutoType(const AutoType *T) {
217 Writer.AddTypeRef(T->getDeducedType(), Record);
218 Code = TYPE_AUTO;
219}
220
Sebastian Redl3397c552010-08-18 23:56:27 +0000221void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000222 Record.push_back(T->isDependentType());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000223 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000224 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000225 "Cannot serialize in the middle of a type definition");
226}
227
Sebastian Redl3397c552010-08-18 23:56:27 +0000228void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000229 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000230 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000231}
232
Sebastian Redl3397c552010-08-18 23:56:27 +0000233void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000234 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000235 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000236}
237
John McCall9d156a72011-01-06 01:58:22 +0000238void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
239 Writer.AddTypeRef(T->getModifiedType(), Record);
240 Writer.AddTypeRef(T->getEquivalentType(), Record);
241 Record.push_back(T->getAttrKind());
242 Code = TYPE_ATTRIBUTED;
243}
244
Mike Stump1eb44332009-09-09 15:08:12 +0000245void
Sebastian Redl3397c552010-08-18 23:56:27 +0000246ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000247 const SubstTemplateTypeParmType *T) {
248 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
249 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000250 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000251}
252
253void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000254ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
255 const SubstTemplateTypeParmPackType *T) {
256 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
257 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
258 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
259}
260
261void
Sebastian Redl3397c552010-08-18 23:56:27 +0000262ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000263 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000264 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000265 Writer.AddTemplateName(T->getTemplateName(), Record);
266 Record.push_back(T->getNumArgs());
267 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
268 ArgI != ArgE; ++ArgI)
269 Writer.AddTemplateArgument(*ArgI, Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000270 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
271 : T->getCanonicalTypeInternal(),
272 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000273 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000274}
275
276void
Sebastian Redl3397c552010-08-18 23:56:27 +0000277ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000278 VisitArrayType(T);
279 Writer.AddStmt(T->getSizeExpr());
280 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000281 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000282}
283
284void
Sebastian Redl3397c552010-08-18 23:56:27 +0000285ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000286 const DependentSizedExtVectorType *T) {
287 // FIXME: Serialize this type (C++ only)
288 assert(false && "Cannot serialize dependent sized extended vector types");
289}
290
291void
Sebastian Redl3397c552010-08-18 23:56:27 +0000292ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000293 Record.push_back(T->getDepth());
294 Record.push_back(T->getIndex());
295 Record.push_back(T->isParameterPack());
296 Writer.AddIdentifierRef(T->getName(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000297 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000298}
299
300void
Sebastian Redl3397c552010-08-18 23:56:27 +0000301ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000302 Record.push_back(T->getKeyword());
303 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
304 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000305 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
306 : T->getCanonicalTypeInternal(),
307 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000308 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000309}
310
311void
Sebastian Redl3397c552010-08-18 23:56:27 +0000312ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000313 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000314 Record.push_back(T->getKeyword());
315 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
316 Writer.AddIdentifierRef(T->getIdentifier(), Record);
317 Record.push_back(T->getNumArgs());
318 for (DependentTemplateSpecializationType::iterator
319 I = T->begin(), E = T->end(); I != E; ++I)
320 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000321 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000322}
323
Douglas Gregor7536dd52010-12-20 02:24:11 +0000324void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
325 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregorcded4f62011-01-14 17:04:44 +0000326 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
327 Record.push_back(*NumExpansions + 1);
328 else
329 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000330 Code = TYPE_PACK_EXPANSION;
331}
332
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000333void ASTTypeWriter::VisitParenType(const ParenType *T) {
334 Writer.AddTypeRef(T->getInnerType(), Record);
335 Code = TYPE_PAREN;
336}
337
Sebastian Redl3397c552010-08-18 23:56:27 +0000338void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000339 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000340 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
341 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000342 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000343}
344
Sebastian Redl3397c552010-08-18 23:56:27 +0000345void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCall3cb0ebd2010-03-10 03:28:59 +0000346 Writer.AddDeclRef(T->getDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000347 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000348 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000349}
350
Sebastian Redl3397c552010-08-18 23:56:27 +0000351void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000352 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000353 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000354}
355
Sebastian Redl3397c552010-08-18 23:56:27 +0000356void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000357 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000358 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000359 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000360 E = T->qual_end(); I != E; ++I)
361 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000362 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000363}
364
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000365void
Sebastian Redl3397c552010-08-18 23:56:27 +0000366ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000367 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000368 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000369}
370
John McCalla1ee0c52009-10-16 21:56:05 +0000371namespace {
372
373class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000374 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000375 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000376
377public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000378 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000379 : Writer(Writer), Record(Record) { }
380
John McCall51bd8032009-10-18 01:05:36 +0000381#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000382#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000383 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000384#include "clang/AST/TypeLocNodes.def"
385
John McCall51bd8032009-10-18 01:05:36 +0000386 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
387 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000388};
389
390}
391
John McCall51bd8032009-10-18 01:05:36 +0000392void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
393 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000394}
John McCall51bd8032009-10-18 01:05:36 +0000395void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000396 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
397 if (TL.needsExtraLocalData()) {
398 Record.push_back(TL.getWrittenTypeSpec());
399 Record.push_back(TL.getWrittenSignSpec());
400 Record.push_back(TL.getWrittenWidthSpec());
401 Record.push_back(TL.hasModeAttr());
402 }
John McCalla1ee0c52009-10-16 21:56:05 +0000403}
John McCall51bd8032009-10-18 01:05:36 +0000404void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
405 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000406}
John McCall51bd8032009-10-18 01:05:36 +0000407void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
408 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000409}
John McCall51bd8032009-10-18 01:05:36 +0000410void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
411 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000412}
John McCall51bd8032009-10-18 01:05:36 +0000413void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
414 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000415}
John McCall51bd8032009-10-18 01:05:36 +0000416void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
417 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000418}
John McCall51bd8032009-10-18 01:05:36 +0000419void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
420 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000421}
John McCall51bd8032009-10-18 01:05:36 +0000422void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
423 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
424 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
425 Record.push_back(TL.getSizeExpr() ? 1 : 0);
426 if (TL.getSizeExpr())
427 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000428}
John McCall51bd8032009-10-18 01:05:36 +0000429void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
430 VisitArrayTypeLoc(TL);
431}
432void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
433 VisitArrayTypeLoc(TL);
434}
435void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
436 VisitArrayTypeLoc(TL);
437}
438void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
439 DependentSizedArrayTypeLoc TL) {
440 VisitArrayTypeLoc(TL);
441}
442void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
443 DependentSizedExtVectorTypeLoc TL) {
444 Writer.AddSourceLocation(TL.getNameLoc(), Record);
445}
446void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
447 Writer.AddSourceLocation(TL.getNameLoc(), Record);
448}
449void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
450 Writer.AddSourceLocation(TL.getNameLoc(), Record);
451}
452void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
453 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
454 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Douglas Gregordab60ad2010-10-01 18:44:50 +0000455 Record.push_back(TL.getTrailingReturn());
John McCall51bd8032009-10-18 01:05:36 +0000456 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
457 Writer.AddDeclRef(TL.getArg(i), Record);
458}
459void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
460 VisitFunctionTypeLoc(TL);
461}
462void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
463 VisitFunctionTypeLoc(TL);
464}
John McCalled976492009-12-04 22:46:56 +0000465void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
466 Writer.AddSourceLocation(TL.getNameLoc(), Record);
467}
John McCall51bd8032009-10-18 01:05:36 +0000468void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
469 Writer.AddSourceLocation(TL.getNameLoc(), Record);
470}
471void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc 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);
John McCall51bd8032009-10-18 01:05:36 +0000475}
476void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000477 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
478 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
479 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
480 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000481}
482void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
483 Writer.AddSourceLocation(TL.getNameLoc(), Record);
484}
Richard Smith34b41d92011-02-20 03:19:35 +0000485void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
486 Writer.AddSourceLocation(TL.getNameLoc(), Record);
487}
John McCall51bd8032009-10-18 01:05:36 +0000488void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
489 Writer.AddSourceLocation(TL.getNameLoc(), Record);
490}
491void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
492 Writer.AddSourceLocation(TL.getNameLoc(), Record);
493}
John McCall9d156a72011-01-06 01:58:22 +0000494void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
495 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
496 if (TL.hasAttrOperand()) {
497 SourceRange range = TL.getAttrOperandParensRange();
498 Writer.AddSourceLocation(range.getBegin(), Record);
499 Writer.AddSourceLocation(range.getEnd(), Record);
500 }
501 if (TL.hasAttrExprOperand()) {
502 Expr *operand = TL.getAttrExprOperand();
503 Record.push_back(operand ? 1 : 0);
504 if (operand) Writer.AddStmt(operand);
505 } else if (TL.hasAttrEnumOperand()) {
506 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
507 }
508}
John McCall51bd8032009-10-18 01:05:36 +0000509void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
510 Writer.AddSourceLocation(TL.getNameLoc(), Record);
511}
John McCall49a832b2009-10-18 09:09:24 +0000512void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
513 SubstTemplateTypeParmTypeLoc TL) {
514 Writer.AddSourceLocation(TL.getNameLoc(), Record);
515}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000516void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
517 SubstTemplateTypeParmPackTypeLoc TL) {
518 Writer.AddSourceLocation(TL.getNameLoc(), Record);
519}
John McCall51bd8032009-10-18 01:05:36 +0000520void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
521 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000522 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
523 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
524 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
525 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000526 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
527 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000528}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000529void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
530 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
531 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
532}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000533void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000534 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
535 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000536}
John McCall3cb0ebd2010-03-10 03:28:59 +0000537void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
538 Writer.AddSourceLocation(TL.getNameLoc(), Record);
539}
Douglas Gregor4714c122010-03-31 17:34:00 +0000540void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000541 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
542 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000543 Writer.AddSourceLocation(TL.getNameLoc(), Record);
544}
John McCall33500952010-06-11 00:33:02 +0000545void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
546 DependentTemplateSpecializationTypeLoc TL) {
547 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
548 Writer.AddSourceRange(TL.getQualifierRange(), Record);
549 Writer.AddSourceLocation(TL.getNameLoc(), Record);
550 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
551 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
552 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000553 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
554 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000555}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000556void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
557 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
558}
John McCall51bd8032009-10-18 01:05:36 +0000559void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
560 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000561}
562void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
563 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000564 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
565 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
566 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
567 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000568}
John McCall54e14c42009-10-22 22:37:11 +0000569void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
570 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000571}
John McCalla1ee0c52009-10-16 21:56:05 +0000572
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000573//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000574// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000575//===----------------------------------------------------------------------===//
576
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000577static void EmitBlockID(unsigned ID, const char *Name,
578 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000579 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000580 Record.clear();
581 Record.push_back(ID);
582 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
583
584 // Emit the block name if present.
585 if (Name == 0 || Name[0] == 0) return;
586 Record.clear();
587 while (*Name)
588 Record.push_back(*Name++);
589 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
590}
591
592static void EmitRecordID(unsigned ID, const char *Name,
593 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000594 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000595 Record.clear();
596 Record.push_back(ID);
597 while (*Name)
598 Record.push_back(*Name++);
599 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000600}
601
602static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000603 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000604#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000605 RECORD(STMT_STOP);
606 RECORD(STMT_NULL_PTR);
607 RECORD(STMT_NULL);
608 RECORD(STMT_COMPOUND);
609 RECORD(STMT_CASE);
610 RECORD(STMT_DEFAULT);
611 RECORD(STMT_LABEL);
612 RECORD(STMT_IF);
613 RECORD(STMT_SWITCH);
614 RECORD(STMT_WHILE);
615 RECORD(STMT_DO);
616 RECORD(STMT_FOR);
617 RECORD(STMT_GOTO);
618 RECORD(STMT_INDIRECT_GOTO);
619 RECORD(STMT_CONTINUE);
620 RECORD(STMT_BREAK);
621 RECORD(STMT_RETURN);
622 RECORD(STMT_DECL);
623 RECORD(STMT_ASM);
624 RECORD(EXPR_PREDEFINED);
625 RECORD(EXPR_DECL_REF);
626 RECORD(EXPR_INTEGER_LITERAL);
627 RECORD(EXPR_FLOATING_LITERAL);
628 RECORD(EXPR_IMAGINARY_LITERAL);
629 RECORD(EXPR_STRING_LITERAL);
630 RECORD(EXPR_CHARACTER_LITERAL);
631 RECORD(EXPR_PAREN);
632 RECORD(EXPR_UNARY_OPERATOR);
633 RECORD(EXPR_SIZEOF_ALIGN_OF);
634 RECORD(EXPR_ARRAY_SUBSCRIPT);
635 RECORD(EXPR_CALL);
636 RECORD(EXPR_MEMBER);
637 RECORD(EXPR_BINARY_OPERATOR);
638 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
639 RECORD(EXPR_CONDITIONAL_OPERATOR);
640 RECORD(EXPR_IMPLICIT_CAST);
641 RECORD(EXPR_CSTYLE_CAST);
642 RECORD(EXPR_COMPOUND_LITERAL);
643 RECORD(EXPR_EXT_VECTOR_ELEMENT);
644 RECORD(EXPR_INIT_LIST);
645 RECORD(EXPR_DESIGNATED_INIT);
646 RECORD(EXPR_IMPLICIT_VALUE_INIT);
647 RECORD(EXPR_VA_ARG);
648 RECORD(EXPR_ADDR_LABEL);
649 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000650 RECORD(EXPR_CHOOSE);
651 RECORD(EXPR_GNU_NULL);
652 RECORD(EXPR_SHUFFLE_VECTOR);
653 RECORD(EXPR_BLOCK);
654 RECORD(EXPR_BLOCK_DECL_REF);
655 RECORD(EXPR_OBJC_STRING_LITERAL);
656 RECORD(EXPR_OBJC_ENCODE);
657 RECORD(EXPR_OBJC_SELECTOR_EXPR);
658 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
659 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
660 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
661 RECORD(EXPR_OBJC_KVC_REF_EXPR);
662 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000663 RECORD(STMT_OBJC_FOR_COLLECTION);
664 RECORD(STMT_OBJC_CATCH);
665 RECORD(STMT_OBJC_FINALLY);
666 RECORD(STMT_OBJC_AT_TRY);
667 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
668 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000669 RECORD(EXPR_CXX_OPERATOR_CALL);
670 RECORD(EXPR_CXX_CONSTRUCT);
671 RECORD(EXPR_CXX_STATIC_CAST);
672 RECORD(EXPR_CXX_DYNAMIC_CAST);
673 RECORD(EXPR_CXX_REINTERPRET_CAST);
674 RECORD(EXPR_CXX_CONST_CAST);
675 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
676 RECORD(EXPR_CXX_BOOL_LITERAL);
677 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000678 RECORD(EXPR_CXX_TYPEID_EXPR);
679 RECORD(EXPR_CXX_TYPEID_TYPE);
680 RECORD(EXPR_CXX_UUIDOF_EXPR);
681 RECORD(EXPR_CXX_UUIDOF_TYPE);
682 RECORD(EXPR_CXX_THIS);
683 RECORD(EXPR_CXX_THROW);
684 RECORD(EXPR_CXX_DEFAULT_ARG);
685 RECORD(EXPR_CXX_BIND_TEMPORARY);
686 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
687 RECORD(EXPR_CXX_NEW);
688 RECORD(EXPR_CXX_DELETE);
689 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
690 RECORD(EXPR_EXPR_WITH_CLEANUPS);
691 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
692 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
693 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
694 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
695 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
696 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
697 RECORD(EXPR_CXX_NOEXCEPT);
698 RECORD(EXPR_OPAQUE_VALUE);
699 RECORD(EXPR_BINARY_TYPE_TRAIT);
700 RECORD(EXPR_PACK_EXPANSION);
701 RECORD(EXPR_SIZEOF_PACK);
702 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000703 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000704#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000705}
Mike Stump1eb44332009-09-09 15:08:12 +0000706
Sebastian Redla4232eb2010-08-18 23:56:21 +0000707void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000708 RecordData Record;
709 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000710
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000711#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
712#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Sebastian Redl3397c552010-08-18 23:56:27 +0000714 // AST Top-Level Block.
Sebastian Redlf29f0a22010-08-18 23:57:22 +0000715 BLOCK(AST_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000716 RECORD(ORIGINAL_FILE_NAME);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000717 RECORD(TYPE_OFFSET);
718 RECORD(DECL_OFFSET);
719 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000720 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000721 RECORD(IDENTIFIER_OFFSET);
722 RECORD(IDENTIFIER_TABLE);
723 RECORD(EXTERNAL_DEFINITIONS);
724 RECORD(SPECIAL_TYPES);
725 RECORD(STATISTICS);
726 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000727 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000728 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
729 RECORD(SELECTOR_OFFSETS);
730 RECORD(METHOD_POOL);
731 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000732 RECORD(SOURCE_LOCATION_OFFSETS);
733 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000734 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000735 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000736 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000737 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redla93e3b52010-07-08 22:01:51 +0000738 RECORD(CHAINED_METADATA);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000739 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000740 RECORD(TU_UPDATE_LEXICAL);
741 RECORD(REDECLS_UPDATE_LATEST);
742 RECORD(SEMA_DECL_REFS);
743 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
744 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
745 RECORD(DECL_REPLACEMENTS);
746 RECORD(UPDATE_VISIBLE);
747 RECORD(DECL_UPDATE_OFFSETS);
748 RECORD(DECL_UPDATES);
749 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
750 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000751 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000752 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000753 RECORD(FP_PRAGMA_OPTIONS);
754 RECORD(OPENCL_EXTENSIONS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000755
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000756 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000757 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000758 RECORD(SM_SLOC_FILE_ENTRY);
759 RECORD(SM_SLOC_BUFFER_ENTRY);
760 RECORD(SM_SLOC_BUFFER_BLOB);
761 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
762 RECORD(SM_LINE_TABLE);
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000764 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000765 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000766 RECORD(PP_MACRO_OBJECT_LIKE);
767 RECORD(PP_MACRO_FUNCTION_LIKE);
768 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000769
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000770 // Decls and Types block.
771 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000772 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000773 RECORD(TYPE_COMPLEX);
774 RECORD(TYPE_POINTER);
775 RECORD(TYPE_BLOCK_POINTER);
776 RECORD(TYPE_LVALUE_REFERENCE);
777 RECORD(TYPE_RVALUE_REFERENCE);
778 RECORD(TYPE_MEMBER_POINTER);
779 RECORD(TYPE_CONSTANT_ARRAY);
780 RECORD(TYPE_INCOMPLETE_ARRAY);
781 RECORD(TYPE_VARIABLE_ARRAY);
782 RECORD(TYPE_VECTOR);
783 RECORD(TYPE_EXT_VECTOR);
784 RECORD(TYPE_FUNCTION_PROTO);
785 RECORD(TYPE_FUNCTION_NO_PROTO);
786 RECORD(TYPE_TYPEDEF);
787 RECORD(TYPE_TYPEOF_EXPR);
788 RECORD(TYPE_TYPEOF);
789 RECORD(TYPE_RECORD);
790 RECORD(TYPE_ENUM);
791 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000792 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000793 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000794 RECORD(TYPE_DECLTYPE);
795 RECORD(TYPE_ELABORATED);
796 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
797 RECORD(TYPE_UNRESOLVED_USING);
798 RECORD(TYPE_INJECTED_CLASS_NAME);
799 RECORD(TYPE_OBJC_OBJECT);
800 RECORD(TYPE_TEMPLATE_TYPE_PARM);
801 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
802 RECORD(TYPE_DEPENDENT_NAME);
803 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
804 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
805 RECORD(TYPE_PAREN);
806 RECORD(TYPE_PACK_EXPANSION);
807 RECORD(TYPE_ATTRIBUTED);
808 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000809 RECORD(DECL_TRANSLATION_UNIT);
810 RECORD(DECL_TYPEDEF);
811 RECORD(DECL_ENUM);
812 RECORD(DECL_RECORD);
813 RECORD(DECL_ENUM_CONSTANT);
814 RECORD(DECL_FUNCTION);
815 RECORD(DECL_OBJC_METHOD);
816 RECORD(DECL_OBJC_INTERFACE);
817 RECORD(DECL_OBJC_PROTOCOL);
818 RECORD(DECL_OBJC_IVAR);
819 RECORD(DECL_OBJC_AT_DEFS_FIELD);
820 RECORD(DECL_OBJC_CLASS);
821 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
822 RECORD(DECL_OBJC_CATEGORY);
823 RECORD(DECL_OBJC_CATEGORY_IMPL);
824 RECORD(DECL_OBJC_IMPLEMENTATION);
825 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
826 RECORD(DECL_OBJC_PROPERTY);
827 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000828 RECORD(DECL_FIELD);
829 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000830 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000831 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000832 RECORD(DECL_FILE_SCOPE_ASM);
833 RECORD(DECL_BLOCK);
834 RECORD(DECL_CONTEXT_LEXICAL);
835 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000836 RECORD(DECL_NAMESPACE);
837 RECORD(DECL_NAMESPACE_ALIAS);
838 RECORD(DECL_USING);
839 RECORD(DECL_USING_SHADOW);
840 RECORD(DECL_USING_DIRECTIVE);
841 RECORD(DECL_UNRESOLVED_USING_VALUE);
842 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
843 RECORD(DECL_LINKAGE_SPEC);
844 RECORD(DECL_CXX_RECORD);
845 RECORD(DECL_CXX_METHOD);
846 RECORD(DECL_CXX_CONSTRUCTOR);
847 RECORD(DECL_CXX_DESTRUCTOR);
848 RECORD(DECL_CXX_CONVERSION);
849 RECORD(DECL_ACCESS_SPEC);
850 RECORD(DECL_FRIEND);
851 RECORD(DECL_FRIEND_TEMPLATE);
852 RECORD(DECL_CLASS_TEMPLATE);
853 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
854 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
855 RECORD(DECL_FUNCTION_TEMPLATE);
856 RECORD(DECL_TEMPLATE_TYPE_PARM);
857 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
858 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
859 RECORD(DECL_STATIC_ASSERT);
860 RECORD(DECL_CXX_BASE_SPECIFIERS);
861 RECORD(DECL_INDIRECTFIELD);
862 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
863
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000864 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
865 RECORD(PPD_MACRO_INSTANTIATION);
866 RECORD(PPD_MACRO_DEFINITION);
867 RECORD(PPD_INCLUSION_DIRECTIVE);
868
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000869 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattner0558df22009-04-27 00:49:53 +0000870 AddStmtsExprs(Stream, Record);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000871#undef RECORD
872#undef BLOCK
873 Stream.ExitBlock();
874}
875
Douglas Gregore650c8c2009-07-07 00:12:59 +0000876/// \brief Adjusts the given filename to only write out the portion of the
877/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000878///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000879/// \param Filename the file name to adjust.
880///
881/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
882/// the returned filename will be adjusted by this system root.
883///
884/// \returns either the original filename (if it needs no adjustment) or the
885/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000886static const char *
Douglas Gregore650c8c2009-07-07 00:12:59 +0000887adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
888 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000889
Douglas Gregore650c8c2009-07-07 00:12:59 +0000890 if (!isysroot)
891 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000892
Douglas Gregore650c8c2009-07-07 00:12:59 +0000893 // Verify that the filename and the system root have the same prefix.
894 unsigned Pos = 0;
895 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
896 if (Filename[Pos] != isysroot[Pos])
897 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000898
Douglas Gregore650c8c2009-07-07 00:12:59 +0000899 // We hit the end of the filename before we hit the end of the system root.
900 if (!Filename[Pos])
901 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000902
Douglas Gregore650c8c2009-07-07 00:12:59 +0000903 // If the file name has a '/' at the current position, skip over the '/'.
904 // We distinguish sysroot-based includes from absolute includes by the
905 // absence of '/' at the beginning of sysroot-based includes.
906 if (Filename[Pos] == '/')
907 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000908
Douglas Gregore650c8c2009-07-07 00:12:59 +0000909 return Filename + Pos;
910}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000911
Sebastian Redl3397c552010-08-18 23:56:27 +0000912/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000913void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot,
914 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000915 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000916
Douglas Gregore650c8c2009-07-07 00:12:59 +0000917 // Metadata
918 const TargetInfo &Target = Context.Target;
919 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl77f46032010-07-09 21:00:24 +0000920 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000921 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl3397c552010-08-18 23:56:27 +0000922 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
923 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregore650c8c2009-07-07 00:12:59 +0000924 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
925 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
926 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl77f46032010-07-09 21:00:24 +0000927 // Target triple or chained PCH name
928 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregore650c8c2009-07-07 00:12:59 +0000929 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000930
Douglas Gregore650c8c2009-07-07 00:12:59 +0000931 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000932 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
933 Record.push_back(VERSION_MAJOR);
934 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000935 Record.push_back(CLANG_VERSION_MAJOR);
936 Record.push_back(CLANG_VERSION_MINOR);
937 Record.push_back(isysroot != 0);
Sebastian Redl77f46032010-07-09 21:00:24 +0000938 // FIXME: This writes the absolute path for chained headers.
939 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
940 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Douglas Gregorb64c1932009-05-12 01:31:05 +0000942 // Original file name
943 SourceManager &SM = Context.getSourceManager();
944 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
945 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000946 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregorb64c1932009-05-12 01:31:05 +0000947 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
948 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
949
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000950 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000951
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000952 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000953
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +0000954 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000955 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000956 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000957 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000958 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000959 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000960 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000961
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000962 // Original PCH directory
963 if (!OutputFile.empty() && OutputFile != "-") {
964 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
965 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
966 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
967 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
968
969 llvm::SmallString<128> OutputPath(OutputFile);
970
971 llvm::sys::fs::make_absolute(OutputPath);
972 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
973
974 RecordData Record;
975 Record.push_back(ORIGINAL_PCH_DIR);
976 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
977 }
978
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000979 // Repository branch/version information.
980 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000981 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000982 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
983 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000984 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000985 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000986 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
987 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +0000988}
989
990/// \brief Write the LangOptions structure.
Sebastian Redla4232eb2010-08-18 23:56:21 +0000991void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000992 RecordData Record;
993 Record.push_back(LangOpts.Trigraphs);
994 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
995 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
996 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
997 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000998 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000999 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1000 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1001 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1002 Record.push_back(LangOpts.C99); // C99 Support
1003 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencerdae4ac42010-10-21 05:21:48 +00001004 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
1005 // already saved elsewhere.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001006 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1007 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001008 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001010 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1011 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001012 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian412e7982010-02-09 19:31:38 +00001013 // modern abi enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001014 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian412e7982010-02-09 19:31:38 +00001015 // modern abi enabled.
Fariborz Jahanianf84109e2011-01-07 18:59:25 +00001016 Record.push_back(LangOpts.AppleKext); // Apple's kernel extensions ABI
Ted Kremenekc32647d2010-12-23 21:35:43 +00001017 Record.push_back(LangOpts.ObjCDefaultSynthProperties); // Objective-C auto-synthesized
1018 // properties enabled.
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +00001019 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump1eb44332009-09-09 15:08:12 +00001020
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001021 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001022 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1023 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +00001024 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001025 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Anders Carlssonda4b7cf2011-02-19 23:53:54 +00001026 Record.push_back(LangOpts.ObjCExceptions);
Anders Carlsson7da99b02011-02-23 03:04:54 +00001027 Record.push_back(LangOpts.CXXExceptions);
1028 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001029
Douglas Gregor6f755502011-02-01 15:15:22 +00001030 Record.push_back(LangOpts.MSBitfields); // MS-compatible structure layout
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001031 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1032 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1033 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1034
Chris Lattnerea5ce472009-04-27 07:35:58 +00001035 // Whether static initializers are protected by locks.
1036 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00001037 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001038 Record.push_back(LangOpts.Blocks); // block extension to C
1039 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1040 // they are unused.
1041 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1042 // (modulo the platform support).
1043
Chris Lattnera4d71452010-06-26 21:25:03 +00001044 Record.push_back(LangOpts.getSignedOverflowBehavior());
1045 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001046
1047 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +00001048 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001049 // defined.
1050 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1051 // opposed to __DYNAMIC__).
1052 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1053
1054 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1055 // used (instead of C99 semantics).
1056 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +00001057 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
1058 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +00001059 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
1060 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +00001061 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Argyrios Kyrtzidisb1bdced2011-01-15 02:56:16 +00001062 Record.push_back(LangOpts.ShortEnums); // Should the enum type be equivalent
1063 // to the smallest integer type with
1064 // enough room.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001065 Record.push_back(LangOpts.getGCMode());
1066 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +00001067 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001068 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00001069 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne08a53262010-12-01 19:14:57 +00001070 Record.push_back(LangOpts.CUDA);
Mike Stump9c276ae2009-12-12 01:27:46 +00001071 Record.push_back(LangOpts.CatchUndefined);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00001072 Record.push_back(LangOpts.DefaultFPContract);
Anders Carlsson92f58222009-08-22 22:30:33 +00001073 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +00001074 Record.push_back(LangOpts.SpellChecking);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001075 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001076}
1077
Douglas Gregor14f79002009-04-10 03:52:48 +00001078//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001079// stat cache Serialization
1080//===----------------------------------------------------------------------===//
1081
1082namespace {
1083// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +00001084class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001085public:
1086 typedef const char * key_type;
1087 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001088
Chris Lattner74e976b2010-11-23 19:28:12 +00001089 typedef struct stat data_type;
1090 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001091
1092 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001093 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001094 }
Mike Stump1eb44332009-09-09 15:08:12 +00001095
1096 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001097 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1098 data_type_ref Data) {
1099 unsigned StrLen = strlen(path);
1100 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +00001101 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001102 clang::io::Emit8(Out, DataLen);
1103 return std::make_pair(StrLen + 1, DataLen);
1104 }
Mike Stump1eb44332009-09-09 15:08:12 +00001105
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001106 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1107 Out.write(path, KeyLen);
1108 }
Mike Stump1eb44332009-09-09 15:08:12 +00001109
Chris Lattner74e976b2010-11-23 19:28:12 +00001110 void EmitData(llvm::raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001111 data_type_ref Data, unsigned DataLen) {
1112 using namespace clang::io;
1113 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +00001114
Chris Lattner74e976b2010-11-23 19:28:12 +00001115 Emit32(Out, (uint32_t) Data.st_ino);
1116 Emit32(Out, (uint32_t) Data.st_dev);
1117 Emit16(Out, (uint16_t) Data.st_mode);
1118 Emit64(Out, (uint64_t) Data.st_mtime);
1119 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001120
1121 assert(Out.tell() - Start == DataLen && "Wrong data length");
1122 }
1123};
1124} // end anonymous namespace
1125
Sebastian Redl3397c552010-08-18 23:56:27 +00001126/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001127void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001128 // Build the on-disk hash table containing information about every
1129 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +00001130 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001131 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001132 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001133 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001134 Stat != StatEnd; ++Stat, ++NumStatEntries) {
1135 const char *Filename = Stat->first();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001136 Generator.insert(Filename, Stat->second);
1137 }
Mike Stump1eb44332009-09-09 15:08:12 +00001138
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001139 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001140 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001141 uint32_t BucketOffset;
1142 {
1143 llvm::raw_svector_ostream Out(StatCacheData);
1144 // Make sure that no bucket is at offset 0
1145 clang::io::Emit32(Out, 0);
1146 BucketOffset = Generator.Emit(Out);
1147 }
1148
1149 // Create a blob abbreviation
1150 using namespace llvm;
1151 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001152 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001153 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1154 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1155 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1156 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1157
1158 // Write the stat cache
1159 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001160 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001161 Record.push_back(BucketOffset);
1162 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001163 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001164}
1165
1166//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001167// Source Manager Serialization
1168//===----------------------------------------------------------------------===//
1169
1170/// \brief Create an abbreviation for the SLocEntry that refers to a
1171/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001172static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001173 using namespace llvm;
1174 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001175 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001176 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1177 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1178 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1179 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001180 // FileEntry fields.
1181 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1182 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor14f79002009-04-10 03:52:48 +00001183 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001184 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001185}
1186
1187/// \brief Create an abbreviation for the SLocEntry that refers to a
1188/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001189static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001190 using namespace llvm;
1191 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001192 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001193 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1194 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1195 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1196 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1197 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name 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 a
1202/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001203static unsigned CreateSLocBufferBlobAbbrev(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_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001207 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001208 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001209}
1210
1211/// \brief Create an abbreviation for the SLocEntry that refers to an
1212/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001213static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001214 using namespace llvm;
1215 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001216 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001217 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1218 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1219 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1220 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001221 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001222 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001223}
1224
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001225namespace {
1226 // Trait used for the on-disk hash table of header search information.
1227 class HeaderFileInfoTrait {
1228 ASTWriter &Writer;
1229 HeaderSearch &HS;
1230
1231 public:
1232 HeaderFileInfoTrait(ASTWriter &Writer, HeaderSearch &HS)
1233 : Writer(Writer), HS(HS) { }
1234
1235 typedef const char *key_type;
1236 typedef key_type key_type_ref;
1237
1238 typedef HeaderFileInfo data_type;
1239 typedef const data_type &data_type_ref;
1240
1241 static unsigned ComputeHash(const char *path) {
1242 // The hash is based only on the filename portion of the key, so that the
1243 // reader can match based on filenames when symlinking or excess path
1244 // elements ("foo/../", "../") change the form of the name. However,
1245 // complete path is still the key.
1246 return llvm::HashString(llvm::sys::path::filename(path));
1247 }
1248
1249 std::pair<unsigned,unsigned>
1250 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1251 data_type_ref Data) {
1252 unsigned StrLen = strlen(path);
1253 clang::io::Emit16(Out, StrLen);
1254 unsigned DataLen = 1 + 2 + 4;
1255 clang::io::Emit8(Out, DataLen);
1256 return std::make_pair(StrLen + 1, DataLen);
1257 }
1258
1259 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1260 Out.write(path, KeyLen);
1261 }
1262
1263 void EmitData(llvm::raw_ostream &Out, key_type_ref,
1264 data_type_ref Data, unsigned DataLen) {
1265 using namespace clang::io;
1266 uint64_t Start = Out.tell(); (void)Start;
1267
1268 unsigned char Flags = (Data.isImport << 3)
1269 | (Data.DirInfo << 1)
1270 | Data.Resolved;
1271 Emit8(Out, (uint8_t)Flags);
1272 Emit16(Out, (uint16_t) Data.NumIncludes);
1273
1274 if (!Data.ControllingMacro)
1275 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1276 else
1277 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
1278 assert(Out.tell() - Start == DataLen && "Wrong data length");
1279 }
1280 };
1281} // end anonymous namespace
1282
1283/// \brief Write the header search block for the list of files that
1284///
1285/// \param HS The header search structure to save.
1286///
1287/// \param Chain Whether we're creating a chained AST file.
1288void ASTWriter::WriteHeaderSearch(HeaderSearch &HS, const char* isysroot) {
1289 llvm::SmallVector<const FileEntry *, 16> FilesByUID;
1290 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1291
1292 if (FilesByUID.size() > HS.header_file_size())
1293 FilesByUID.resize(HS.header_file_size());
1294
1295 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1296 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
1297 llvm::SmallVector<const char *, 4> SavedStrings;
1298 unsigned NumHeaderSearchEntries = 0;
1299 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1300 const FileEntry *File = FilesByUID[UID];
1301 if (!File)
1302 continue;
1303
1304 const HeaderFileInfo &HFI = HS.header_file_begin()[UID];
1305 if (HFI.External && Chain)
1306 continue;
1307
1308 // Turn the file name into an absolute path, if it isn't already.
1309 const char *Filename = File->getName();
1310 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1311
1312 // If we performed any translation on the file name at all, we need to
1313 // save this string, since the generator will refer to it later.
1314 if (Filename != File->getName()) {
1315 Filename = strdup(Filename);
1316 SavedStrings.push_back(Filename);
1317 }
1318
1319 Generator.insert(Filename, HFI, GeneratorTrait);
1320 ++NumHeaderSearchEntries;
1321 }
1322
1323 // Create the on-disk hash table in a buffer.
1324 llvm::SmallString<4096> TableData;
1325 uint32_t BucketOffset;
1326 {
1327 llvm::raw_svector_ostream Out(TableData);
1328 // Make sure that no bucket is at offset 0
1329 clang::io::Emit32(Out, 0);
1330 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1331 }
1332
1333 // Create a blob abbreviation
1334 using namespace llvm;
1335 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1336 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1337 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1338 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1339 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1340 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1341
1342 // Write the stat cache
1343 RecordData Record;
1344 Record.push_back(HEADER_SEARCH_TABLE);
1345 Record.push_back(BucketOffset);
1346 Record.push_back(NumHeaderSearchEntries);
1347 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1348
1349 // Free all of the strings we had to duplicate.
1350 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1351 free((void*)SavedStrings[I]);
1352}
1353
Douglas Gregor14f79002009-04-10 03:52:48 +00001354/// \brief Writes the block containing the serialized form of the
1355/// source manager.
1356///
1357/// TODO: We should probably use an on-disk hash table (stored in a
1358/// blob), indexed based on the file name, so that we only create
1359/// entries for files that we actually need. In the common case (no
1360/// errors), we probably won't have to create file entries for any of
1361/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001362void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001363 const Preprocessor &PP,
1364 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001365 RecordData Record;
1366
Chris Lattnerf04ad692009-04-10 17:16:57 +00001367 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001368 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001369
1370 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001371 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1372 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1373 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1374 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001375
Douglas Gregorbd945002009-04-13 16:31:14 +00001376 // Write the line table.
1377 if (SourceMgr.hasLineTable()) {
1378 LineTableInfo &LineTable = SourceMgr.getLineTable();
1379
1380 // Emit the file names
1381 Record.push_back(LineTable.getNumFilenames());
1382 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1383 // Emit the file name
1384 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001385 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +00001386 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1387 Record.push_back(FilenameLen);
1388 if (FilenameLen)
1389 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1390 }
Mike Stump1eb44332009-09-09 15:08:12 +00001391
Douglas Gregorbd945002009-04-13 16:31:14 +00001392 // Emit the line entries
1393 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1394 L != LEnd; ++L) {
1395 // Emit the file ID
1396 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +00001397
Douglas Gregorbd945002009-04-13 16:31:14 +00001398 // Emit the line entries
1399 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001400 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +00001401 LEEnd = L->second.end();
1402 LE != LEEnd; ++LE) {
1403 Record.push_back(LE->FileOffset);
1404 Record.push_back(LE->LineNo);
1405 Record.push_back(LE->FilenameID);
1406 Record.push_back((unsigned)LE->FileKind);
1407 Record.push_back(LE->IncludeOffset);
1408 }
Douglas Gregorbd945002009-04-13 16:31:14 +00001409 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001410 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001411 }
1412
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001413 // Write out the source location entry table. We skip the first
1414 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001415 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001416 RecordData PreloadSLocs;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001417 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1418 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1419 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1420 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001421 // Get this source location entry.
1422 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001423
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001424 // Record the offset of this source-location entry.
1425 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1426
1427 // Figure out which record code to use.
1428 unsigned Code;
1429 if (SLoc->isFile()) {
1430 if (SLoc->getFile().getContentCache()->Entry)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001431 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001432 else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001433 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001434 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001435 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001436 Record.clear();
1437 Record.push_back(Code);
1438
1439 Record.push_back(SLoc->getOffset());
1440 if (SLoc->isFile()) {
1441 const SrcMgr::FileInfo &File = SLoc->getFile();
1442 Record.push_back(File.getIncludeLoc().getRawEncoding());
1443 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1444 Record.push_back(File.hasLineDirectives());
1445
1446 const SrcMgr::ContentCache *Content = File.getContentCache();
1447 if (Content->Entry) {
1448 // The source location entry is a file. The blob associated
1449 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001450
Douglas Gregor2d52be52010-03-21 22:49:54 +00001451 // Emit size/modification time for this file.
1452 Record.push_back(Content->Entry->getSize());
1453 Record.push_back(Content->Entry->getModificationTime());
1454
Douglas Gregore650c8c2009-07-07 00:12:59 +00001455 // Turn the file name into an absolute path, if it isn't already.
1456 const char *Filename = Content->Entry->getName();
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001457 llvm::SmallString<128> FilePath(Filename);
1458 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001459 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001460
Douglas Gregore650c8c2009-07-07 00:12:59 +00001461 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001462 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001463 } else {
1464 // The source location entry is a buffer. The blob associated
1465 // with this entry contains the contents of the buffer.
1466
1467 // We add one to the size so that we capture the trailing NULL
1468 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1469 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001470 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001471 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001472 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001473 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1474 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001475 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001476 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001477 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001478 llvm::StringRef(Buffer->getBufferStart(),
1479 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001480
1481 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001482 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001483 }
1484 } else {
1485 // The source location entry is an instantiation.
1486 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1487 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1488 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1489 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1490
1491 // Compute the token length for this macro expansion.
1492 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001493 if (I + 1 != N)
1494 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001495 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1496 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1497 }
1498 }
1499
Douglas Gregorc9490c02009-04-16 22:23:12 +00001500 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001501
1502 if (SLocEntryOffsets.empty())
1503 return;
1504
Sebastian Redl3397c552010-08-18 23:56:27 +00001505 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001506 // table is used for lazily loading source-location information.
1507 using namespace llvm;
1508 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001509 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001510 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1511 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1512 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1513 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001515 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001516 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001517 Record.push_back(SLocEntryOffsets.size());
Sebastian Redl8db9fae2010-09-22 20:19:08 +00001518 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1519 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001520 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001521 (const char *)data(SLocEntryOffsets),
Chris Lattner090d9b52009-04-27 19:01:47 +00001522 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001523
Sebastian Redl3397c552010-08-18 23:56:27 +00001524 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001525 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001526 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001527}
1528
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001529//===----------------------------------------------------------------------===//
1530// Preprocessor Serialization
1531//===----------------------------------------------------------------------===//
1532
Douglas Gregor9c736102011-02-10 18:20:09 +00001533static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1534 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1535 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1536 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1537 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1538 return X.first->getName().compare(Y.first->getName());
1539}
1540
Chris Lattner0b1fb982009-04-10 17:15:23 +00001541/// \brief Writes the block containing the serialized form of the
1542/// preprocessor.
1543///
Sebastian Redla4232eb2010-08-18 23:56:21 +00001544void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001545 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001546
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001547 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1548 if (PP.getCounterValue() != 0) {
1549 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001550 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001551 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001552 }
1553
1554 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001555 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001556
Sebastian Redl3397c552010-08-18 23:56:27 +00001557 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001558 // FIXME: use diagnostics subsystem for localization etc.
1559 if (PP.SawDateOrTime())
1560 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001561
Douglas Gregorecdcb882010-10-20 22:00:55 +00001562
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001563 // Loop over all the macro definitions that are live at the end of the file,
1564 // emitting each to the PP section.
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001565 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001566
Douglas Gregor9c736102011-02-10 18:20:09 +00001567 // Construct the list of macro definitions that need to be serialized.
1568 llvm::SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
1569 MacrosToEmit;
1570 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor040a8042011-02-11 00:26:14 +00001571 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1572 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001573 I != E; ++I) {
Douglas Gregor9c736102011-02-10 18:20:09 +00001574 MacroDefinitionsSeen.insert(I->first);
1575 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1576 }
1577
1578 // Sort the set of macro definitions that need to be serialized by the
1579 // name of the macro, to provide a stable ordering.
1580 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1581 &compareMacroDefinitions);
1582
Douglas Gregor040a8042011-02-11 00:26:14 +00001583 // Resolve any identifiers that defined macros at the time they were
1584 // deserialized, adding them to the list of macros to emit (if appropriate).
1585 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1586 IdentifierInfo *Name
1587 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1588 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1589 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1590 }
1591
Douglas Gregor9c736102011-02-10 18:20:09 +00001592 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1593 const IdentifierInfo *Name = MacrosToEmit[I].first;
1594 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor040a8042011-02-11 00:26:14 +00001595 if (!MI)
1596 continue;
1597
Sebastian Redl3397c552010-08-18 23:56:27 +00001598 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001599 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl3397c552010-08-18 23:56:27 +00001600 // Also skip macros from a AST file if we're chaining.
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001601
1602 // FIXME: There is a (probably minor) optimization we could do here, if
1603 // the macro comes from the original PCH but the identifier comes from a
1604 // chained PCH, by storing the offset into the original PCH rather than
1605 // writing the macro definition a second time.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001606 if (MI->isBuiltinMacro() ||
Douglas Gregor9c736102011-02-10 18:20:09 +00001607 (Chain && Name->isFromAST() && MI->isFromAST()))
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001608 continue;
1609
Douglas Gregor9c736102011-02-10 18:20:09 +00001610 AddIdentifierRef(Name, Record);
1611 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001612 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1613 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001614
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001615 unsigned Code;
1616 if (MI->isObjectLike()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001617 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001618 } else {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001619 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001620
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001621 Record.push_back(MI->isC99Varargs());
1622 Record.push_back(MI->isGNUVarargs());
1623 Record.push_back(MI->getNumArgs());
1624 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1625 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001626 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001627 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001628
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001629 // If we have a detailed preprocessing record, record the macro definition
1630 // ID that corresponds to this macro.
1631 if (PPRec)
1632 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer20249a12010-10-21 03:16:25 +00001633
Douglas Gregorc9490c02009-04-16 22:23:12 +00001634 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001635 Record.clear();
1636
Chris Lattnerdf961c22009-04-10 18:08:30 +00001637 // Emit the tokens array.
1638 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1639 // Note that we know that the preprocessor does not have any annotation
1640 // tokens in it because they are created by the parser, and thus can't be
1641 // in a macro definition.
1642 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001643
Chris Lattnerdf961c22009-04-10 18:08:30 +00001644 Record.push_back(Tok.getLocation().getRawEncoding());
1645 Record.push_back(Tok.getLength());
1646
Chris Lattnerdf961c22009-04-10 18:08:30 +00001647 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1648 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001649 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001650 // FIXME: Should translate token kind to a stable encoding.
1651 Record.push_back(Tok.getKind());
1652 // FIXME: Should translate token flags to a stable encoding.
1653 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001654
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001655 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001656 Record.clear();
1657 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001658 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001659 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001660 Stream.ExitBlock();
1661
1662 if (PPRec)
1663 WritePreprocessorDetail(*PPRec);
1664}
1665
1666void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
1667 if (PPRec.begin(Chain) == PPRec.end(Chain))
1668 return;
1669
1670 // Enter the preprocessor block.
1671 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001672
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001673 // If the preprocessor has a preprocessing record, emit it.
1674 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001675 using namespace llvm;
1676
1677 // Set up the abbreviation for
1678 unsigned InclusionAbbrev = 0;
1679 {
1680 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1681 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
1682 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1683 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1684 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1685 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1686 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1687 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1688 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1689 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1690 }
1691
1692 unsigned IndexBase = Chain ? PPRec.getNumPreallocatedEntities() : 0;
1693 RecordData Record;
1694 for (PreprocessingRecord::iterator E = PPRec.begin(Chain),
1695 EEnd = PPRec.end(Chain);
1696 E != EEnd; ++E) {
1697 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001698
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001699 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1700 // Record this macro definition's location.
1701 MacroID ID = getMacroDefinitionID(MD);
1702
1703 // Don't write the macro definition if it is from another AST file.
1704 if (ID < FirstMacroID)
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001705 continue;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001706
Douglas Gregor89d99802010-11-30 06:16:57 +00001707 // Notify the serialization listener that we're serializing this entity.
1708 if (SerializationListener)
1709 SerializationListener->SerializedPreprocessedEntity(*E,
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001710 Stream.GetCurrentBitNo());
Douglas Gregor89d99802010-11-30 06:16:57 +00001711
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001712 unsigned Position = ID - FirstMacroID;
1713 if (Position != MacroDefinitionOffsets.size()) {
1714 if (Position > MacroDefinitionOffsets.size())
1715 MacroDefinitionOffsets.resize(Position + 1);
1716
1717 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
1718 } else
1719 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor89d99802010-11-30 06:16:57 +00001720
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001721 Record.push_back(IndexBase + NumPreprocessingRecords++);
1722 Record.push_back(ID);
1723 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1724 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1725 AddIdentifierRef(MD->getName(), Record);
1726 AddSourceLocation(MD->getLocation(), Record);
1727 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1728 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001729 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001730
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001731 // Notify the serialization listener that we're serializing this entity.
1732 if (SerializationListener)
1733 SerializationListener->SerializedPreprocessedEntity(*E,
1734 Stream.GetCurrentBitNo());
1735
1736 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1737 Record.push_back(IndexBase + NumPreprocessingRecords++);
1738 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1739 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1740 AddIdentifierRef(MI->getName(), Record);
1741 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1742 Stream.EmitRecord(PPD_MACRO_INSTANTIATION, Record);
1743 continue;
1744 }
1745
1746 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1747 Record.push_back(PPD_INCLUSION_DIRECTIVE);
1748 Record.push_back(IndexBase + NumPreprocessingRecords++);
1749 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1750 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1751 Record.push_back(ID->getFileName().size());
1752 Record.push_back(ID->wasInQuotes());
1753 Record.push_back(static_cast<unsigned>(ID->getKind()));
1754 llvm::SmallString<64> Buffer;
1755 Buffer += ID->getFileName();
1756 Buffer += ID->getFile()->getName();
1757 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1758 continue;
1759 }
1760
1761 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1762 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001763 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001764
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001765 // Write the offsets table for the preprocessing record.
1766 if (NumPreprocessingRecords > 0) {
1767 // Write the offsets table for identifier IDs.
1768 using namespace llvm;
1769 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001770 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001771 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1772 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1773 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1774 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001775
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001776 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001777 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001778 Record.push_back(NumPreprocessingRecords);
1779 Record.push_back(MacroDefinitionOffsets.size());
1780 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001781 (const char *)data(MacroDefinitionOffsets),
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001782 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1783 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001784}
1785
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001786void ASTWriter::WritePragmaDiagnosticMappings(const Diagnostic &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001787 RecordData Record;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001788 for (Diagnostic::DiagStatePointsTy::const_iterator
1789 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1790 I != E; ++I) {
1791 const Diagnostic::DiagStatePoint &point = *I;
1792 if (point.Loc.isInvalid())
1793 continue;
1794
1795 Record.push_back(point.Loc.getRawEncoding());
1796 for (Diagnostic::DiagState::iterator
1797 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
1798 unsigned diag = I->first, map = I->second;
1799 if (map & 0x10) { // mapping from a diagnostic pragma.
1800 Record.push_back(diag);
1801 Record.push_back(map & 0x7);
1802 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001803 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001804 Record.push_back(-1); // mark the end of the diag/map pairs for this
1805 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001806 }
1807
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00001808 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001809 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001810}
1811
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001812//===----------------------------------------------------------------------===//
1813// Type Serialization
1814//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001815
Sebastian Redl3397c552010-08-18 23:56:27 +00001816/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001817void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00001818 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001819 if (Idx.getIndex() == 0) // we haven't seen this type before.
1820 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00001821
Douglas Gregor97475832010-10-05 18:37:06 +00001822 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00001823
Douglas Gregor2cf26342009-04-09 22:27:44 +00001824 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001825 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00001826 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001827 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00001828 else if (TypeOffsets.size() < Index) {
1829 TypeOffsets.resize(Index + 1);
1830 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001831 }
1832
1833 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001834
Douglas Gregor2cf26342009-04-09 22:27:44 +00001835 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00001836 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001837
Douglas Gregora4923eb2009-11-16 21:35:15 +00001838 if (T.hasLocalNonFastQualifiers()) {
1839 Qualifiers Qs = T.getLocalQualifiers();
1840 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001841 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001842 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00001843 } else {
1844 switch (T->getTypeClass()) {
1845 // For all of the concrete, non-dependent types, call the
1846 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001847#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001848 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001849#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001850#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001851 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001852 }
1853
1854 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001855 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001856
1857 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001858 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001859}
1860
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001861//===----------------------------------------------------------------------===//
1862// Declaration Serialization
1863//===----------------------------------------------------------------------===//
1864
Douglas Gregor2cf26342009-04-09 22:27:44 +00001865/// \brief Write the block containing all of the declaration IDs
1866/// lexically declared within the given DeclContext.
1867///
1868/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1869/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001870uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001871 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001872 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001873 return 0;
1874
Douglas Gregorc9490c02009-04-16 22:23:12 +00001875 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001876 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001877 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001878 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001879 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1880 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001881 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001882
Douglas Gregor25123082009-04-22 22:34:57 +00001883 ++NumLexicalDeclContexts;
Sebastian Redl681d7232010-07-27 00:17:23 +00001884 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001885 reinterpret_cast<char*>(Decls.data()),
1886 Decls.size() * sizeof(KindDeclIDPair));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001887 return Offset;
1888}
1889
Sebastian Redla4232eb2010-08-18 23:56:21 +00001890void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00001891 using namespace llvm;
1892 RecordData Record;
1893
1894 // Write the type offsets array
1895 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001896 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001897 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1898 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1899 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1900 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001901 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00001902 Record.push_back(TypeOffsets.size());
1903 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001904 (const char *)data(TypeOffsets),
Sebastian Redl1476ed42010-07-16 16:36:56 +00001905 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1906
1907 // Write the declaration offsets array
1908 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001909 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001910 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1911 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1912 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1913 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001914 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00001915 Record.push_back(DeclOffsets.size());
1916 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001917 (const char *)data(DeclOffsets),
Sebastian Redl1476ed42010-07-16 16:36:56 +00001918 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1919}
1920
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001921//===----------------------------------------------------------------------===//
1922// Global Method Pool and Selector Serialization
1923//===----------------------------------------------------------------------===//
1924
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001925namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001926// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00001927class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00001928 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001929
1930public:
1931 typedef Selector key_type;
1932 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001933
Sebastian Redl5d050072010-08-04 17:20:04 +00001934 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001935 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00001936 ObjCMethodList Instance, Factory;
1937 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001938 typedef const data_type& data_type_ref;
1939
Sebastian Redl3397c552010-08-18 23:56:27 +00001940 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001941
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001942 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00001943 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001944 }
Mike Stump1eb44332009-09-09 15:08:12 +00001945
1946 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001947 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1948 data_type_ref Methods) {
1949 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1950 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00001951 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1952 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001953 Method = Method->Next)
1954 if (Method->Method)
1955 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00001956 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001957 Method = Method->Next)
1958 if (Method->Method)
1959 DataLen += 4;
1960 clang::io::Emit16(Out, DataLen);
1961 return std::make_pair(KeyLen, DataLen);
1962 }
Mike Stump1eb44332009-09-09 15:08:12 +00001963
Douglas Gregor83941df2009-04-25 17:48:32 +00001964 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00001965 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00001966 assert((Start >> 32) == 0 && "Selector key offset too large");
1967 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001968 unsigned N = Sel.getNumArgs();
1969 clang::io::Emit16(Out, N);
1970 if (N == 0)
1971 N = 1;
1972 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001973 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001974 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1975 }
Mike Stump1eb44332009-09-09 15:08:12 +00001976
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001977 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00001978 data_type_ref Methods, unsigned DataLen) {
1979 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00001980 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001981 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00001982 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001983 Method = Method->Next)
1984 if (Method->Method)
1985 ++NumInstanceMethods;
1986
1987 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00001988 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001989 Method = Method->Next)
1990 if (Method->Method)
1991 ++NumFactoryMethods;
1992
1993 clang::io::Emit16(Out, NumInstanceMethods);
1994 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00001995 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001996 Method = Method->Next)
1997 if (Method->Method)
1998 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00001999 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002000 Method = Method->Next)
2001 if (Method->Method)
2002 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002003
2004 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002005 }
2006};
2007} // end anonymous namespace
2008
Sebastian Redl059612d2010-08-03 21:58:15 +00002009/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002010///
2011/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002012/// in an on-disk hash table indexed by the selector. The hash table also
2013/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002014void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002015 using namespace llvm;
2016
Sebastian Redl059612d2010-08-03 21:58:15 +00002017 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002018 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002019 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002020 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002021 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002022 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002023 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002024 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002025
Sebastian Redl059612d2010-08-03 21:58:15 +00002026 // Create the on-disk hash table representation. We walk through every
2027 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002028 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002029 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002030 I = SelectorIDs.begin(), E = SelectorIDs.end();
2031 I != E; ++I) {
2032 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002033 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002034 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002035 I->second,
2036 ObjCMethodList(),
2037 ObjCMethodList()
2038 };
2039 if (F != SemaRef.MethodPool.end()) {
2040 Data.Instance = F->second.first;
2041 Data.Factory = F->second.second;
2042 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002043 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002044 // changed.
2045 if (Chain && I->second < FirstSelectorID) {
2046 // Selector already exists. Did it change?
2047 bool changed = false;
2048 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2049 M = M->Next) {
2050 if (M->Method->getPCHLevel() == 0)
2051 changed = true;
2052 }
2053 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2054 M = M->Next) {
2055 if (M->Method->getPCHLevel() == 0)
2056 changed = true;
2057 }
2058 if (!changed)
2059 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002060 } else if (Data.Instance.Method || Data.Factory.Method) {
2061 // A new method pool entry.
2062 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002063 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002064 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002065 }
2066
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002067 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002068 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002069 uint32_t BucketOffset;
2070 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002071 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002072 llvm::raw_svector_ostream Out(MethodPool);
2073 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002074 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002075 BucketOffset = Generator.Emit(Out, Trait);
2076 }
2077
2078 // Create a blob abbreviation
2079 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002080 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002081 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002082 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002083 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2084 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2085
Douglas Gregor83941df2009-04-25 17:48:32 +00002086 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002087 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002088 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002089 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002090 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002091 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002092
2093 // Create a blob abbreviation for the selector table offsets.
2094 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002095 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002096 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor83941df2009-04-25 17:48:32 +00002097 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2098 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2099
2100 // Write the selector offsets table.
2101 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002102 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002103 Record.push_back(SelectorOffsets.size());
2104 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00002105 (const char *)data(SelectorOffsets),
Douglas Gregor83941df2009-04-25 17:48:32 +00002106 SelectorOffsets.size() * 4);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002107 }
2108}
2109
Sebastian Redl3397c552010-08-18 23:56:27 +00002110/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002111void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002112 using namespace llvm;
2113 if (SemaRef.ReferencedSelectors.empty())
2114 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002115
Fariborz Jahanian32019832010-07-23 19:11:11 +00002116 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002117
Sebastian Redl3397c552010-08-18 23:56:27 +00002118 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002119 // very tricky to fix, and given that @selector shouldn't really appear in
2120 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002121 for (DenseMap<Selector, SourceLocation>::iterator S =
2122 SemaRef.ReferencedSelectors.begin(),
2123 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2124 Selector Sel = (*S).first;
2125 SourceLocation Loc = (*S).second;
2126 AddSelectorRef(Sel, Record);
2127 AddSourceLocation(Loc, Record);
2128 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002129 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002130}
2131
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002132//===----------------------------------------------------------------------===//
2133// Identifier Table Serialization
2134//===----------------------------------------------------------------------===//
2135
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002136namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002137class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002138 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002139 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002140
Douglas Gregora92193e2009-04-28 21:18:29 +00002141 /// \brief Determines whether this is an "interesting" identifier
2142 /// that needs a full IdentifierInfo structure written into the hash
2143 /// table.
2144 static bool isInterestingIdentifier(const IdentifierInfo *II) {
2145 return II->isPoisoned() ||
2146 II->isExtensionToken() ||
2147 II->hasMacroDefinition() ||
2148 II->getObjCOrBuiltinID() ||
2149 II->getFETokenInfo<void>();
2150 }
2151
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002152public:
2153 typedef const IdentifierInfo* key_type;
2154 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002155
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002156 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002157 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002158
Sebastian Redl3397c552010-08-18 23:56:27 +00002159 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00002160 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002161
2162 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002163 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002164 }
Mike Stump1eb44332009-09-09 15:08:12 +00002165
2166 std::pair<unsigned,unsigned>
2167 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002168 IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002169 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002170 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
2171 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00002172 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00002173 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00002174 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00002175 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00002176 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2177 DEnd = IdentifierResolver::end();
2178 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002179 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002180 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002181 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002182 // We emit the key length after the data length so that every
2183 // string is preceded by a 16-bit length. This matches the PTH
2184 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002185 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002186 return std::make_pair(KeyLen, DataLen);
2187 }
Mike Stump1eb44332009-09-09 15:08:12 +00002188
2189 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002190 unsigned KeyLen) {
2191 // Record the location of the key data. This is used when generating
2192 // the mapping from persistent IDs to strings.
2193 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002194 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002195 }
Mike Stump1eb44332009-09-09 15:08:12 +00002196
2197 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002198 IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002199 if (!isInterestingIdentifier(II)) {
2200 clang::io::Emit32(Out, ID << 1);
2201 return;
2202 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002203
Douglas Gregora92193e2009-04-28 21:18:29 +00002204 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002205 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002206 bool hasMacroDefinition =
2207 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00002208 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00002209 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002210 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
2211 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2212 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002213 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002214 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002215 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002216
Douglas Gregor37e26842009-04-21 23:56:24 +00002217 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00002218 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00002219
Douglas Gregor668c1a42009-04-21 22:25:48 +00002220 // Emit the declaration IDs in reverse order, because the
2221 // IdentifierResolver provides the declarations as they would be
2222 // visible (e.g., the function "stat" would come before the struct
2223 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2224 // adds declarations to the end of the list (so we need to see the
2225 // struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002226 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump1eb44332009-09-09 15:08:12 +00002227 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00002228 IdentifierResolver::end());
2229 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
2230 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002231 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002232 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002233 }
2234};
2235} // end anonymous namespace
2236
Sebastian Redl3397c552010-08-18 23:56:27 +00002237/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002238///
2239/// The identifier table consists of a blob containing string data
2240/// (the actual identifiers themselves) and a separate "offsets" index
2241/// that maps identifier IDs to locations within the blob.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002242void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002243 using namespace llvm;
2244
2245 // Create and write out the blob that contains the identifier
2246 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002247 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002248 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002249 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump1eb44332009-09-09 15:08:12 +00002250
Douglas Gregor92b059e2009-04-28 20:33:11 +00002251 // Look for any identifiers that were named while processing the
2252 // headers, but are otherwise not needed. We add these to the hash
2253 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002254 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002255 // file.
2256 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2257 IDEnd = PP.getIdentifierTable().end();
2258 ID != IDEnd; ++ID)
2259 getIdentifierRef(ID->second);
2260
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002261 // Create the on-disk hash table representation. We only store offsets
2262 // for identifiers that appear here for the first time.
2263 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002264 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002265 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2266 ID != IDEnd; ++ID) {
2267 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002268 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002269 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002270 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002271
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002272 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002273 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002274 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002275 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002276 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002277 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002278 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002279 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002280 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002281 }
2282
2283 // Create a blob abbreviation
2284 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002285 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002286 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002287 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002288 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002289
2290 // Write the identifier table
2291 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002292 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002293 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002294 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002295 }
2296
2297 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002298 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002299 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002300 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2301 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2302 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2303
2304 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002305 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002306 Record.push_back(IdentifierOffsets.size());
2307 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00002308 (const char *)data(IdentifierOffsets),
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002309 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002310}
2311
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002312//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002313// DeclContext's Name Lookup Table Serialization
2314//===----------------------------------------------------------------------===//
2315
2316namespace {
2317// Trait used for the on-disk hash table used in the method pool.
2318class ASTDeclContextNameLookupTrait {
2319 ASTWriter &Writer;
2320
2321public:
2322 typedef DeclarationName key_type;
2323 typedef key_type key_type_ref;
2324
2325 typedef DeclContext::lookup_result data_type;
2326 typedef const data_type& data_type_ref;
2327
2328 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2329
2330 unsigned ComputeHash(DeclarationName Name) {
2331 llvm::FoldingSetNodeID ID;
2332 ID.AddInteger(Name.getNameKind());
2333
2334 switch (Name.getNameKind()) {
2335 case DeclarationName::Identifier:
2336 ID.AddString(Name.getAsIdentifierInfo()->getName());
2337 break;
2338 case DeclarationName::ObjCZeroArgSelector:
2339 case DeclarationName::ObjCOneArgSelector:
2340 case DeclarationName::ObjCMultiArgSelector:
2341 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2342 break;
2343 case DeclarationName::CXXConstructorName:
2344 case DeclarationName::CXXDestructorName:
2345 case DeclarationName::CXXConversionFunctionName:
2346 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
2347 break;
2348 case DeclarationName::CXXOperatorName:
2349 ID.AddInteger(Name.getCXXOverloadedOperator());
2350 break;
2351 case DeclarationName::CXXLiteralOperatorName:
2352 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2353 case DeclarationName::CXXUsingDirective:
2354 break;
2355 }
2356
2357 return ID.ComputeHash();
2358 }
2359
2360 std::pair<unsigned,unsigned>
2361 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2362 data_type_ref Lookup) {
2363 unsigned KeyLen = 1;
2364 switch (Name.getNameKind()) {
2365 case DeclarationName::Identifier:
2366 case DeclarationName::ObjCZeroArgSelector:
2367 case DeclarationName::ObjCOneArgSelector:
2368 case DeclarationName::ObjCMultiArgSelector:
2369 case DeclarationName::CXXConstructorName:
2370 case DeclarationName::CXXDestructorName:
2371 case DeclarationName::CXXConversionFunctionName:
2372 case DeclarationName::CXXLiteralOperatorName:
2373 KeyLen += 4;
2374 break;
2375 case DeclarationName::CXXOperatorName:
2376 KeyLen += 1;
2377 break;
2378 case DeclarationName::CXXUsingDirective:
2379 break;
2380 }
2381 clang::io::Emit16(Out, KeyLen);
2382
2383 // 2 bytes for num of decls and 4 for each DeclID.
2384 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2385 clang::io::Emit16(Out, DataLen);
2386
2387 return std::make_pair(KeyLen, DataLen);
2388 }
2389
2390 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2391 using namespace clang::io;
2392
2393 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2394 Emit8(Out, Name.getNameKind());
2395 switch (Name.getNameKind()) {
2396 case DeclarationName::Identifier:
2397 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2398 break;
2399 case DeclarationName::ObjCZeroArgSelector:
2400 case DeclarationName::ObjCOneArgSelector:
2401 case DeclarationName::ObjCMultiArgSelector:
2402 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2403 break;
2404 case DeclarationName::CXXConstructorName:
2405 case DeclarationName::CXXDestructorName:
2406 case DeclarationName::CXXConversionFunctionName:
2407 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2408 break;
2409 case DeclarationName::CXXOperatorName:
2410 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2411 Emit8(Out, Name.getCXXOverloadedOperator());
2412 break;
2413 case DeclarationName::CXXLiteralOperatorName:
2414 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2415 break;
2416 case DeclarationName::CXXUsingDirective:
2417 break;
2418 }
2419 }
2420
2421 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2422 data_type Lookup, unsigned DataLen) {
2423 uint64_t Start = Out.tell(); (void)Start;
2424 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2425 for (; Lookup.first != Lookup.second; ++Lookup.first)
2426 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2427
2428 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2429 }
2430};
2431} // end anonymous namespace
2432
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002433/// \brief Write the block containing all of the declaration IDs
2434/// visible from the given DeclContext.
2435///
2436/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002437/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002438uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2439 DeclContext *DC) {
2440 if (DC->getPrimaryContext() != DC)
2441 return 0;
2442
2443 // Since there is no name lookup into functions or methods, don't bother to
2444 // build a visible-declarations table for these entities.
2445 if (DC->isFunctionOrMethod())
2446 return 0;
2447
2448 // If not in C++, we perform name lookup for the translation unit via the
2449 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2450 // FIXME: In C++ we need the visible declarations in order to "see" the
2451 // friend declarations, is there a way to do this without writing the table ?
2452 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2453 return 0;
2454
2455 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00002456 if (DC->hasExternalVisibleStorage())
2457 DC->MaterializeVisibleDeclsFromExternalStorage();
2458 else
2459 DC->lookup(DeclarationName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002460
2461 // Serialize the contents of the mapping used for lookup. Note that,
2462 // although we have two very different code paths, the serialized
2463 // representation is the same for both cases: a declaration name,
2464 // followed by a size, followed by references to the visible
2465 // declarations that have that name.
2466 uint64_t Offset = Stream.GetCurrentBitNo();
2467 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2468 if (!Map || Map->empty())
2469 return 0;
2470
2471 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2472 ASTDeclContextNameLookupTrait Trait(*this);
2473
2474 // Create the on-disk hash table representation.
2475 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2476 D != DEnd; ++D) {
2477 DeclarationName Name = D->first;
2478 DeclContext::lookup_result Result = D->second.getLookupResult();
2479 Generator.insert(Name, Result, Trait);
2480 }
2481
2482 // Create the on-disk hash table in a buffer.
2483 llvm::SmallString<4096> LookupTable;
2484 uint32_t BucketOffset;
2485 {
2486 llvm::raw_svector_ostream Out(LookupTable);
2487 // Make sure that no bucket is at offset 0
2488 clang::io::Emit32(Out, 0);
2489 BucketOffset = Generator.Emit(Out, Trait);
2490 }
2491
2492 // Write the lookup table
2493 RecordData Record;
2494 Record.push_back(DECL_CONTEXT_VISIBLE);
2495 Record.push_back(BucketOffset);
2496 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2497 LookupTable.str());
2498
2499 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2500 ++NumVisibleDeclContexts;
2501 return Offset;
2502}
2503
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002504/// \brief Write an UPDATE_VISIBLE block for the given context.
2505///
2506/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2507/// DeclContext in a dependent AST file. As such, they only exist for the TU
2508/// (in C++) and for namespaces.
2509void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002510 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2511 if (!Map || Map->empty())
2512 return;
2513
2514 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2515 ASTDeclContextNameLookupTrait Trait(*this);
2516
2517 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002518 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2519 D != DEnd; ++D) {
2520 DeclarationName Name = D->first;
2521 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002522 // For any name that appears in this table, the results are complete, i.e.
2523 // they overwrite results from previous PCHs. Merging is always a mess.
2524 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002525 }
2526
2527 // Create the on-disk hash table in a buffer.
2528 llvm::SmallString<4096> LookupTable;
2529 uint32_t BucketOffset;
2530 {
2531 llvm::raw_svector_ostream Out(LookupTable);
2532 // Make sure that no bucket is at offset 0
2533 clang::io::Emit32(Out, 0);
2534 BucketOffset = Generator.Emit(Out, Trait);
2535 }
2536
2537 // Write the lookup table
2538 RecordData Record;
2539 Record.push_back(UPDATE_VISIBLE);
2540 Record.push_back(getDeclID(cast<Decl>(DC)));
2541 Record.push_back(BucketOffset);
2542 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2543}
2544
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002545/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2546void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2547 RecordData Record;
2548 Record.push_back(Opts.fp_contract);
2549 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2550}
2551
2552/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2553void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
2554 if (!SemaRef.Context.getLangOptions().OpenCL)
2555 return;
2556
2557 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2558 RecordData Record;
2559#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2560#include "clang/Basic/OpenCLExtensions.def"
2561 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2562}
2563
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002564//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002565// General Serialization Routines
2566//===----------------------------------------------------------------------===//
2567
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002568/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002569void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00002570 Record.push_back(Attrs.size());
Sean Huntcf807c42010-08-18 23:23:40 +00002571 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2572 const Attr * A = *i;
2573 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2574 AddSourceLocation(A->getLocation(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002575
Sean Huntcf807c42010-08-18 23:23:40 +00002576#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00002577
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002578 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002579}
2580
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002581void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002582 Record.push_back(Str.size());
2583 Record.insert(Record.end(), Str.begin(), Str.end());
2584}
2585
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002586/// \brief Note that the identifier II occurs at the given offset
2587/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002588void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002589 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00002590 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002591 // up earlier in the chain and thus don't need an offset.
2592 if (ID >= FirstIdentID)
2593 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002594}
2595
Douglas Gregor83941df2009-04-25 17:48:32 +00002596/// \brief Note that the selector Sel occurs at the given offset
2597/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002598void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00002599 unsigned ID = SelectorIDs[Sel];
2600 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00002601 // Don't record offsets for selectors that are also available in a different
2602 // file.
2603 if (ID < FirstSelectorID)
2604 return;
2605 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00002606}
2607
Sebastian Redla4232eb2010-08-18 23:56:21 +00002608ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregor89d99802010-11-30 06:16:57 +00002609 : Stream(Stream), Chain(0), SerializationListener(0),
2610 FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002611 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redle58aa892010-08-04 18:21:41 +00002612 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor77424bc2010-10-02 19:29:26 +00002613 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2614 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00002615 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor7c789c12010-10-29 22:39:52 +00002616 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
2617 NextCXXBaseSpecifiersID(1)
2618{
Sebastian Redl30c514c2010-07-14 23:45:08 +00002619}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002620
Sebastian Redla4232eb2010-08-18 23:56:21 +00002621void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002622 const std::string &OutputFile,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002623 const char *isysroot) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002624 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002625 Stream.Emit((unsigned)'C', 8);
2626 Stream.Emit((unsigned)'P', 8);
2627 Stream.Emit((unsigned)'C', 8);
2628 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002629
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002630 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002631
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002632 if (Chain)
Sebastian Redla4232eb2010-08-18 23:56:21 +00002633 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002634 else
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002635 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002636}
2637
Sebastian Redla4232eb2010-08-18 23:56:21 +00002638void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002639 const char *isysroot,
2640 const std::string &OutputFile) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002641 using namespace llvm;
2642
2643 ASTContext &Context = SemaRef.Context;
2644 Preprocessor &PP = SemaRef.PP;
2645
Douglas Gregor2cf26342009-04-09 22:27:44 +00002646 // The translation unit is the first declaration we'll emit.
2647 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002648 ++NextDeclID;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002649 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002650
Douglas Gregor2deaea32009-04-22 18:49:13 +00002651 // Make sure that we emit IdentifierInfos (and any attached
2652 // declarations) for builtins.
2653 {
2654 IdentifierTable &Table = PP.getIdentifierTable();
2655 llvm::SmallVector<const char *, 32> BuiltinNames;
2656 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2657 Context.getLangOptions().NoBuiltin);
2658 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2659 getIdentifierRef(&Table.get(BuiltinNames[I]));
2660 }
2661
Chris Lattner63d65f82009-09-08 18:19:27 +00002662 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002663 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002664 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002665 RecordData TentativeDefinitions;
Sebastian Redle9d12b62010-01-31 22:27:38 +00002666 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2667 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner63d65f82009-09-08 18:19:27 +00002668 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002669
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002670 // Build a record containing all of the file scoped decls in this file.
2671 RecordData UnusedFileScopedDecls;
2672 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2673 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00002674
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002675 RecordData WeakUndeclaredIdentifiers;
2676 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2677 WeakUndeclaredIdentifiers.push_back(
2678 SemaRef.WeakUndeclaredIdentifiers.size());
2679 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2680 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2681 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2682 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2683 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2684 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2685 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2686 }
2687 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002688
Douglas Gregor14c22f22009-04-22 22:18:58 +00002689 // Build a record containing all of the locally-scoped external
2690 // declarations in this header file. Generally, this record will be
2691 // empty.
2692 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002693 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00002694 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002695 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002696 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2697 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2698 TD != TDEnd; ++TD)
2699 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2700
Douglas Gregorb81c1702009-04-27 20:06:05 +00002701 // Build a record containing all of the ext_vector declarations.
2702 RecordData ExtVectorDecls;
2703 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2704 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2705
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002706 // Build a record containing all of the VTable uses information.
2707 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00002708 if (!SemaRef.VTableUses.empty()) {
2709 VTableUses.push_back(SemaRef.VTableUses.size());
2710 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2711 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2712 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2713 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2714 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002715 }
2716
2717 // Build a record containing all of dynamic classes declarations.
2718 RecordData DynamicClasses;
2719 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2720 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2721
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002722 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002723 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002724 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00002725 I = SemaRef.PendingInstantiations.begin(),
2726 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2727 AddDeclRef(I->first, PendingInstantiations);
2728 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002729 }
2730 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2731 "There are local ones at end of translation unit!");
2732
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002733 // Build a record containing some declaration references.
2734 RecordData SemaDeclRefs;
2735 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2736 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2737 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2738 }
2739
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002740 RecordData CUDASpecialDeclRefs;
2741 if (Context.getcudaConfigureCallDecl()) {
2742 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
2743 }
2744
Sebastian Redl3397c552010-08-18 23:56:27 +00002745 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002746 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002747 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002748 WriteMetadata(Context, isysroot, OutputFile);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002749 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00002750 if (StatCalls && !isysroot)
Douglas Gregordd41ed52010-07-12 23:48:14 +00002751 WriteStatCache(*StatCalls);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002752 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002753 // Write the record of special types.
2754 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00002755
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002756 AddTypeRef(Context.getBuiltinVaListType(), Record);
2757 AddTypeRef(Context.getObjCIdType(), Record);
2758 AddTypeRef(Context.getObjCSelType(), Record);
2759 AddTypeRef(Context.getObjCProtoType(), Record);
2760 AddTypeRef(Context.getObjCClassType(), Record);
2761 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2762 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2763 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002764 AddTypeRef(Context.getjmp_bufType(), Record);
2765 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002766 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2767 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpadaaad32009-10-20 02:12:22 +00002768 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002769 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002770 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2771 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002772 Record.push_back(Context.isInt128Installed());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002773 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002774
Douglas Gregor366809a2009-04-26 03:49:13 +00002775 // Keep writing types and declarations until all types and
2776 // declarations have been written.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002777 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002778 WriteDeclsBlockAbbrevs();
2779 while (!DeclTypesToEmit.empty()) {
2780 DeclOrType DOT = DeclTypesToEmit.front();
2781 DeclTypesToEmit.pop();
2782 if (DOT.isType())
2783 WriteType(DOT.getType());
2784 else
2785 WriteDecl(Context, DOT.getDecl());
2786 }
2787 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002788
Douglas Gregor813a97b2009-10-17 17:25:45 +00002789 WritePreprocessor(PP);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002790 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00002791 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002792 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002793 WriteIdentifierTable(PP);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002794 WriteFPPragmaOptions(SemaRef.getFPOptions());
2795 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002796
Sebastian Redl1476ed42010-07-16 16:36:56 +00002797 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002798 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00002799
Douglas Gregor7c789c12010-10-29 22:39:52 +00002800 // Write the C++ base-specifier set offsets.
2801 if (!CXXBaseSpecifiersOffsets.empty()) {
2802 // Create a blob abbreviation for the C++ base specifiers offsets.
2803 using namespace llvm;
2804
2805 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2806 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2807 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2808 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2809 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2810
2811 // Write the selector offsets table.
2812 Record.clear();
2813 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2814 Record.push_back(CXXBaseSpecifiersOffsets.size());
2815 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
2816 (const char *)CXXBaseSpecifiersOffsets.data(),
2817 CXXBaseSpecifiersOffsets.size() * sizeof(uint32_t));
2818 }
2819
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002820 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002821 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002822 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002823
2824 // Write the record containing tentative definitions.
2825 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002826 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002827
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002828 // Write the record containing unused file scoped decls.
2829 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002830 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002831
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002832 // Write the record containing weak undeclared identifiers.
2833 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002834 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002835 WeakUndeclaredIdentifiers);
2836
Douglas Gregor14c22f22009-04-22 22:18:58 +00002837 // Write the record containing locally-scoped external definitions.
2838 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002839 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002840 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002841
2842 // Write the record containing ext_vector type names.
2843 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002844 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002845
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002846 // Write the record containing VTable uses information.
2847 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002848 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002849
2850 // Write the record containing dynamic classes declarations.
2851 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002852 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002853
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002854 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002855 if (!PendingInstantiations.empty())
2856 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002857
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002858 // Write the record containing declaration references of Sema.
2859 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002860 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002861
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002862 // Write the record containing CUDA-specific declaration references.
2863 if (!CUDASpecialDeclRefs.empty())
2864 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
2865
Douglas Gregor3e1af842009-04-17 22:13:46 +00002866 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002867 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002868 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002869 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002870 Record.push_back(NumLexicalDeclContexts);
2871 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002872 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002873 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002874}
2875
Sebastian Redla4232eb2010-08-18 23:56:21 +00002876void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002877 const char *isysroot) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002878 using namespace llvm;
2879
2880 ASTContext &Context = SemaRef.Context;
2881 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1476ed42010-07-16 16:36:56 +00002882
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002883 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002884 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002885 WriteMetadata(Context, isysroot, "");
Sebastian Redl1476ed42010-07-16 16:36:56 +00002886 if (StatCalls && !isysroot)
2887 WriteStatCache(*StatCalls);
2888 // FIXME: Source manager block should only write new stuff, which could be
2889 // done by tracking the largest ID in the chain
2890 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002891
2892 // The special types are in the chained PCH.
2893
2894 // We don't start with the translation unit, but with its decls that
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002895 // don't come from the chained PCH.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002896 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002897 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl681d7232010-07-27 00:17:23 +00002898 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2899 E = TU->noload_decls_end();
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002900 I != E; ++I) {
Sebastian Redld692af72010-07-27 18:24:41 +00002901 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002902 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redl0b17c612010-08-13 00:28:03 +00002903 else if ((*I)->isChangedSinceDeserialization())
2904 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002905 }
Sebastian Redl681d7232010-07-27 00:17:23 +00002906 // We also need to write a lexical updates block for the TU.
Sebastian Redld692af72010-07-27 18:24:41 +00002907 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002908 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redld692af72010-07-27 18:24:41 +00002909 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2910 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2911 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002912 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redld692af72010-07-27 18:24:41 +00002913 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2914 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002915 NewGlobalDecls.size() * sizeof(KindDeclIDPair));
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00002916 // And a visible updates block for the DeclContexts.
2917 Abv = new llvm::BitCodeAbbrev();
2918 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2919 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2920 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2921 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2922 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2923 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002924
Sebastian Redl083abdf2010-07-27 23:01:28 +00002925 // Build a record containing all of the new tentative definitions in this
2926 // file, in TentativeDefinitions order.
2927 RecordData TentativeDefinitions;
2928 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2929 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2930 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2931 }
2932
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002933 // Build a record containing all of the file scoped decls in this file.
2934 RecordData UnusedFileScopedDecls;
2935 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2936 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2937 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002938 }
2939
Sebastian Redl40566802010-08-05 18:21:25 +00002940 // We write the entire table, overwriting the tables from the chain.
2941 RecordData WeakUndeclaredIdentifiers;
2942 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2943 WeakUndeclaredIdentifiers.push_back(
2944 SemaRef.WeakUndeclaredIdentifiers.size());
2945 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2946 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2947 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2948 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2949 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2950 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2951 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2952 }
2953 }
2954
Sebastian Redl083abdf2010-07-27 23:01:28 +00002955 // Build a record containing all of the locally-scoped external
2956 // declarations in this header file. Generally, this record will be
2957 // empty.
2958 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002959 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl083abdf2010-07-27 23:01:28 +00002960 // nondeterminstic!
2961 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2962 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2963 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2964 TD != TDEnd; ++TD) {
2965 if (TD->second->getPCHLevel() == 0)
2966 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2967 }
2968
2969 // Build a record containing all of the ext_vector declarations.
2970 RecordData ExtVectorDecls;
2971 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2972 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2973 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2974 }
2975
Sebastian Redl40566802010-08-05 18:21:25 +00002976 // Build a record containing all of the VTable uses information.
2977 // We write everything here, because it's too hard to determine whether
2978 // a use is new to this part.
2979 RecordData VTableUses;
2980 if (!SemaRef.VTableUses.empty()) {
2981 VTableUses.push_back(SemaRef.VTableUses.size());
2982 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2983 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2984 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2985 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2986 }
2987 }
2988
2989 // Build a record containing all of dynamic classes declarations.
2990 RecordData DynamicClasses;
2991 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2992 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
2993 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2994
2995 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002996 RecordData PendingInstantiations;
Sebastian Redl40566802010-08-05 18:21:25 +00002997 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00002998 I = SemaRef.PendingInstantiations.begin(),
2999 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl40566802010-08-05 18:21:25 +00003000 if (I->first->getPCHLevel() == 0) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00003001 AddDeclRef(I->first, PendingInstantiations);
3002 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl40566802010-08-05 18:21:25 +00003003 }
3004 }
3005 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3006 "There are local ones at end of translation unit!");
3007
3008 // Build a record containing some declaration references.
3009 // It's not worth the effort to avoid duplication here.
3010 RecordData SemaDeclRefs;
3011 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3012 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3013 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3014 }
3015
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003016 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003017 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00003018 for (DeclsToRewriteTy::iterator
3019 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
3020 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003021 while (!DeclTypesToEmit.empty()) {
3022 DeclOrType DOT = DeclTypesToEmit.front();
3023 DeclTypesToEmit.pop();
3024 if (DOT.isType())
3025 WriteType(DOT.getType());
3026 else
3027 WriteDecl(Context, DOT.getDecl());
3028 }
3029 Stream.ExitBlock();
3030
Sebastian Redl083abdf2010-07-27 23:01:28 +00003031 WritePreprocessor(PP);
Sebastian Redla68340f2010-08-04 22:21:29 +00003032 WriteSelectors(SemaRef);
3033 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003034 WriteIdentifierTable(PP);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003035 WriteFPPragmaOptions(SemaRef.getFPOptions());
3036 WriteOpenCLExtensions(SemaRef);
3037
Sebastian Redl1476ed42010-07-16 16:36:56 +00003038 WriteTypeDeclOffsets();
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00003039 // FIXME: For chained PCH only write the new mappings (we currently
3040 // write all of them again).
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003041 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl083abdf2010-07-27 23:01:28 +00003042
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003043 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl3397c552010-08-18 23:56:27 +00003044 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003045 RecordData FirstLatestDeclIDs;
3046 for (FirstLatestDeclMap::iterator
3047 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
3048 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
3049 "Expected first & second to be in different PCHs");
3050 AddDeclRef(I->first, FirstLatestDeclIDs);
3051 AddDeclRef(I->second, FirstLatestDeclIDs);
3052 }
3053 if (!FirstLatestDeclIDs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003054 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003055
Sebastian Redl083abdf2010-07-27 23:01:28 +00003056 // Write the record containing external, unnamed definitions.
3057 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003058 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003059
3060 // Write the record containing tentative definitions.
3061 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003062 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003063
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003064 // Write the record containing unused file scoped decls.
3065 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003066 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003067
Sebastian Redl40566802010-08-05 18:21:25 +00003068 // Write the record containing weak undeclared identifiers.
3069 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003070 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl40566802010-08-05 18:21:25 +00003071 WeakUndeclaredIdentifiers);
3072
Sebastian Redl083abdf2010-07-27 23:01:28 +00003073 // Write the record containing locally-scoped external definitions.
3074 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003075 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl083abdf2010-07-27 23:01:28 +00003076 LocallyScopedExternalDecls);
3077
3078 // Write the record containing ext_vector type names.
3079 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003080 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003081
Sebastian Redl40566802010-08-05 18:21:25 +00003082 // Write the record containing VTable uses information.
3083 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003084 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl40566802010-08-05 18:21:25 +00003085
3086 // Write the record containing dynamic classes declarations.
3087 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003088 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl40566802010-08-05 18:21:25 +00003089
3090 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003091 if (!PendingInstantiations.empty())
3092 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl40566802010-08-05 18:21:25 +00003093
3094 // Write the record containing declaration references of Sema.
3095 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003096 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003097
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00003098 // Write the updates to DeclContexts.
3099 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3100 I = UpdatedDeclContexts.begin(),
3101 E = UpdatedDeclContexts.end();
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003102 I != E; ++I)
3103 WriteDeclContextVisibleUpdate(*I);
3104
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003105 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003106
Sebastian Redl083abdf2010-07-27 23:01:28 +00003107 Record.clear();
3108 Record.push_back(NumStatements);
3109 Record.push_back(NumMacros);
3110 Record.push_back(NumLexicalDeclContexts);
3111 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003112 WriteDeclReplacementsBlock();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003113 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003114 Stream.ExitBlock();
3115}
3116
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003117void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003118 if (DeclUpdates.empty())
3119 return;
3120
3121 RecordData OffsetsRecord;
3122 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
3123 for (DeclUpdateMap::iterator
3124 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3125 const Decl *D = I->first;
3126 UpdateRecord &URec = I->second;
3127
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003128 if (DeclsToRewrite.count(D))
3129 continue; // The decl will be written completely,no need to store updates.
3130
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003131 uint64_t Offset = Stream.GetCurrentBitNo();
3132 Stream.EmitRecord(DECL_UPDATES, URec);
3133
3134 OffsetsRecord.push_back(GetDeclRef(D));
3135 OffsetsRecord.push_back(Offset);
3136 }
3137 Stream.ExitBlock();
3138 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3139}
3140
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003141void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003142 if (ReplacedDecls.empty())
3143 return;
3144
3145 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003146 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003147 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
3148 Record.push_back(I->first);
3149 Record.push_back(I->second);
3150 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003151 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003152}
3153
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003154void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003155 Record.push_back(Loc.getRawEncoding());
3156}
3157
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003158void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003159 AddSourceLocation(Range.getBegin(), Record);
3160 AddSourceLocation(Range.getEnd(), Record);
3161}
3162
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003163void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003164 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003165 const uint64_t *Words = Value.getRawData();
3166 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003167}
3168
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003169void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003170 Record.push_back(Value.isUnsigned());
3171 AddAPInt(Value, Record);
3172}
3173
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003174void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003175 AddAPInt(Value.bitcastToAPInt(), Record);
3176}
3177
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003178void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003179 Record.push_back(getIdentifierRef(II));
3180}
3181
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003182IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003183 if (II == 0)
3184 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003185
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003186 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003187 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003188 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003189 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003190}
3191
Sebastian Redlf73c93f2010-09-15 19:54:06 +00003192MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003193 if (MD == 0)
3194 return 0;
Sebastian Redlf73c93f2010-09-15 19:54:06 +00003195
3196 MacroID &ID = MacroDefinitions[MD];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003197 if (ID == 0)
Douglas Gregor77424bc2010-10-02 19:29:26 +00003198 ID = NextMacroID++;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003199 return ID;
3200}
3201
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003202void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003203 Record.push_back(getSelectorRef(SelRef));
3204}
3205
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003206SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003207 if (Sel.getAsOpaquePtr() == 0) {
3208 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003209 }
3210
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003211 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003212 if (SID == 0 && Chain) {
3213 // This might trigger a ReadSelector callback, which will set the ID for
3214 // this selector.
3215 Chain->LoadSelector(Sel);
3216 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003217 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003218 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003219 }
Sebastian Redl5d050072010-08-04 17:20:04 +00003220 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003221}
3222
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003223void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00003224 AddDeclRef(Temp->getDestructor(), Record);
3225}
3226
Douglas Gregor7c789c12010-10-29 22:39:52 +00003227void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3228 CXXBaseSpecifier const *BasesEnd,
3229 RecordDataImpl &Record) {
3230 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3231 CXXBaseSpecifiersToWrite.push_back(
3232 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3233 Bases, BasesEnd));
3234 Record.push_back(NextCXXBaseSpecifiersID++);
3235}
3236
Sebastian Redla4232eb2010-08-18 23:56:21 +00003237void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003238 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003239 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003240 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00003241 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003242 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00003243 break;
3244 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003245 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00003246 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00003247 case TemplateArgument::Template:
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003248 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
3249 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003250 break;
3251 case TemplateArgument::TemplateExpansion:
3252 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
3253 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003254 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00003255 break;
John McCall833ca992009-10-29 08:12:44 +00003256 case TemplateArgument::Null:
3257 case TemplateArgument::Integral:
3258 case TemplateArgument::Declaration:
3259 case TemplateArgument::Pack:
3260 break;
3261 }
3262}
3263
Sebastian Redla4232eb2010-08-18 23:56:21 +00003264void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003265 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003266 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003267
3268 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3269 bool InfoHasSameExpr
3270 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3271 Record.push_back(InfoHasSameExpr);
3272 if (InfoHasSameExpr)
3273 return; // Avoid storing the same expr twice.
3274 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003275 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3276 Record);
3277}
3278
Douglas Gregordc355712011-02-25 00:36:19 +00003279void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3280 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00003281 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00003282 AddTypeRef(QualType(), Record);
3283 return;
3284 }
3285
Douglas Gregordc355712011-02-25 00:36:19 +00003286 AddTypeLoc(TInfo->getTypeLoc(), Record);
3287}
3288
3289void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3290 AddTypeRef(TL.getType(), Record);
3291
John McCalla1ee0c52009-10-16 21:56:05 +00003292 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00003293 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003294 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00003295}
3296
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003297void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00003298 Record.push_back(GetOrCreateTypeID(T));
3299}
3300
3301TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003302 return MakeTypeID(T,
3303 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3304}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003305
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003306TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003307 return MakeTypeID(T,
3308 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003309}
3310
3311TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3312 if (T.isNull())
3313 return TypeIdx();
3314 assert(!T.getLocalFastQualifiers());
3315
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00003316 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003317 if (Idx.getIndex() == 0) {
Douglas Gregor366809a2009-04-26 03:49:13 +00003318 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00003319 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003320 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003321 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00003322 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003323 return Idx;
3324}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003325
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003326TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003327 if (T.isNull())
3328 return TypeIdx();
3329 assert(!T.getLocalFastQualifiers());
3330
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003331 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3332 assert(I != TypeIdxs.end() && "Type not emitted!");
3333 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003334}
3335
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003336void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003337 Record.push_back(GetDeclRef(D));
3338}
3339
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003340DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003341 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003342 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003343 }
Douglas Gregor97475832010-10-05 18:37:06 +00003344 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003345 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00003346 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003347 // We haven't seen this declaration before. Give it a new ID and
3348 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003349 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003350 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redl0b17c612010-08-13 00:28:03 +00003351 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3352 // We don't add it to the replacement collection here, because we don't
3353 // have the offset yet.
3354 DeclTypesToEmit.push(const_cast<Decl *>(D));
3355 // Reset the flag, so that we don't add this decl multiple times.
3356 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003357 }
3358
Sebastian Redl681d7232010-07-27 00:17:23 +00003359 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003360}
3361
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003362DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003363 if (D == 0)
3364 return 0;
3365
3366 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3367 return DeclIDs[D];
3368}
3369
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003370void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00003371 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00003372 Record.push_back(Name.getNameKind());
3373 switch (Name.getNameKind()) {
3374 case DeclarationName::Identifier:
3375 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3376 break;
3377
3378 case DeclarationName::ObjCZeroArgSelector:
3379 case DeclarationName::ObjCOneArgSelector:
3380 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003381 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003382 break;
3383
3384 case DeclarationName::CXXConstructorName:
3385 case DeclarationName::CXXDestructorName:
3386 case DeclarationName::CXXConversionFunctionName:
3387 AddTypeRef(Name.getCXXNameType(), Record);
3388 break;
3389
3390 case DeclarationName::CXXOperatorName:
3391 Record.push_back(Name.getCXXOverloadedOperator());
3392 break;
3393
Sean Hunt3e518bd2009-11-29 07:34:05 +00003394 case DeclarationName::CXXLiteralOperatorName:
3395 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3396 break;
3397
Douglas Gregor2cf26342009-04-09 22:27:44 +00003398 case DeclarationName::CXXUsingDirective:
3399 // No extra data to emit
3400 break;
3401 }
3402}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003403
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003404void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003405 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003406 switch (Name.getNameKind()) {
3407 case DeclarationName::CXXConstructorName:
3408 case DeclarationName::CXXDestructorName:
3409 case DeclarationName::CXXConversionFunctionName:
3410 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3411 break;
3412
3413 case DeclarationName::CXXOperatorName:
3414 AddSourceLocation(
3415 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3416 Record);
3417 AddSourceLocation(
3418 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3419 Record);
3420 break;
3421
3422 case DeclarationName::CXXLiteralOperatorName:
3423 AddSourceLocation(
3424 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3425 Record);
3426 break;
3427
3428 case DeclarationName::Identifier:
3429 case DeclarationName::ObjCZeroArgSelector:
3430 case DeclarationName::ObjCOneArgSelector:
3431 case DeclarationName::ObjCMultiArgSelector:
3432 case DeclarationName::CXXUsingDirective:
3433 break;
3434 }
3435}
3436
3437void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003438 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003439 AddDeclarationName(NameInfo.getName(), Record);
3440 AddSourceLocation(NameInfo.getLoc(), Record);
3441 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3442}
3443
3444void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003445 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003446 AddNestedNameSpecifier(Info.NNS, Record);
3447 AddSourceRange(Info.NNSRange, Record);
3448 Record.push_back(Info.NumTemplParamLists);
3449 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3450 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3451}
3452
Sebastian Redla4232eb2010-08-18 23:56:21 +00003453void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003454 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003455 // Nested name specifiers usually aren't too long. I think that 8 would
3456 // typically accomodate the vast majority.
3457 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3458
3459 // Push each of the NNS's onto a stack for serialization in reverse order.
3460 while (NNS) {
3461 NestedNames.push_back(NNS);
3462 NNS = NNS->getPrefix();
3463 }
3464
3465 Record.push_back(NestedNames.size());
3466 while(!NestedNames.empty()) {
3467 NNS = NestedNames.pop_back_val();
3468 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3469 Record.push_back(Kind);
3470 switch (Kind) {
3471 case NestedNameSpecifier::Identifier:
3472 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3473 break;
3474
3475 case NestedNameSpecifier::Namespace:
3476 AddDeclRef(NNS->getAsNamespace(), Record);
3477 break;
3478
Douglas Gregor14aba762011-02-24 02:36:08 +00003479 case NestedNameSpecifier::NamespaceAlias:
3480 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
3481 break;
3482
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003483 case NestedNameSpecifier::TypeSpec:
3484 case NestedNameSpecifier::TypeSpecWithTemplate:
3485 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3486 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3487 break;
3488
3489 case NestedNameSpecifier::Global:
3490 // Don't need to write an associated value.
3491 break;
3492 }
3493 }
3494}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003495
Douglas Gregordc355712011-02-25 00:36:19 +00003496void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
3497 RecordDataImpl &Record) {
3498 // Nested name specifiers usually aren't too long. I think that 8 would
3499 // typically accomodate the vast majority.
3500 llvm::SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
3501
3502 // Push each of the nested-name-specifiers's onto a stack for
3503 // serialization in reverse order.
3504 while (NNS) {
3505 NestedNames.push_back(NNS);
3506 NNS = NNS.getPrefix();
3507 }
3508
3509 Record.push_back(NestedNames.size());
3510 while(!NestedNames.empty()) {
3511 NNS = NestedNames.pop_back_val();
3512 NestedNameSpecifier::SpecifierKind Kind
3513 = NNS.getNestedNameSpecifier()->getKind();
3514 Record.push_back(Kind);
3515 switch (Kind) {
3516 case NestedNameSpecifier::Identifier:
3517 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
3518 AddSourceRange(NNS.getLocalSourceRange(), Record);
3519 break;
3520
3521 case NestedNameSpecifier::Namespace:
3522 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
3523 AddSourceRange(NNS.getLocalSourceRange(), Record);
3524 break;
3525
3526 case NestedNameSpecifier::NamespaceAlias:
3527 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
3528 AddSourceRange(NNS.getLocalSourceRange(), Record);
3529 break;
3530
3531 case NestedNameSpecifier::TypeSpec:
3532 case NestedNameSpecifier::TypeSpecWithTemplate:
3533 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3534 AddTypeLoc(NNS.getTypeLoc(), Record);
3535 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3536 break;
3537
3538 case NestedNameSpecifier::Global:
3539 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3540 break;
3541 }
3542 }
3543}
3544
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003545void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00003546 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003547 Record.push_back(Kind);
3548 switch (Kind) {
3549 case TemplateName::Template:
3550 AddDeclRef(Name.getAsTemplateDecl(), Record);
3551 break;
3552
3553 case TemplateName::OverloadedTemplate: {
3554 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3555 Record.push_back(OvT->size());
3556 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3557 I != E; ++I)
3558 AddDeclRef(*I, Record);
3559 break;
3560 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003561
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003562 case TemplateName::QualifiedTemplate: {
3563 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3564 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3565 Record.push_back(QualT->hasTemplateKeyword());
3566 AddDeclRef(QualT->getTemplateDecl(), Record);
3567 break;
3568 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003569
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003570 case TemplateName::DependentTemplate: {
3571 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3572 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3573 Record.push_back(DepT->isIdentifier());
3574 if (DepT->isIdentifier())
3575 AddIdentifierRef(DepT->getIdentifier(), Record);
3576 else
3577 Record.push_back(DepT->getOperator());
3578 break;
3579 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00003580
3581 case TemplateName::SubstTemplateTemplateParmPack: {
3582 SubstTemplateTemplateParmPackStorage *SubstPack
3583 = Name.getAsSubstTemplateTemplateParmPack();
3584 AddDeclRef(SubstPack->getParameterPack(), Record);
3585 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
3586 break;
3587 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003588 }
3589}
3590
Michael J. Spencer20249a12010-10-21 03:16:25 +00003591void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003592 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003593 Record.push_back(Arg.getKind());
3594 switch (Arg.getKind()) {
3595 case TemplateArgument::Null:
3596 break;
3597 case TemplateArgument::Type:
3598 AddTypeRef(Arg.getAsType(), Record);
3599 break;
3600 case TemplateArgument::Declaration:
3601 AddDeclRef(Arg.getAsDecl(), Record);
3602 break;
3603 case TemplateArgument::Integral:
3604 AddAPSInt(*Arg.getAsIntegral(), Record);
3605 AddTypeRef(Arg.getIntegralType(), Record);
3606 break;
3607 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00003608 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3609 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00003610 case TemplateArgument::TemplateExpansion:
3611 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00003612 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3613 Record.push_back(*NumExpansions + 1);
3614 else
3615 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003616 break;
3617 case TemplateArgument::Expression:
3618 AddStmt(Arg.getAsExpr());
3619 break;
3620 case TemplateArgument::Pack:
3621 Record.push_back(Arg.pack_size());
3622 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3623 I != E; ++I)
3624 AddTemplateArgument(*I, Record);
3625 break;
3626 }
3627}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003628
3629void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003630ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003631 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003632 assert(TemplateParams && "No TemplateParams!");
3633 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3634 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3635 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3636 Record.push_back(TemplateParams->size());
3637 for (TemplateParameterList::const_iterator
3638 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3639 P != PEnd; ++P)
3640 AddDeclRef(*P, Record);
3641}
3642
3643/// \brief Emit a template argument list.
3644void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003645ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003646 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003647 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00003648 Record.push_back(TemplateArgs->size());
3649 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003650 AddTemplateArgument(TemplateArgs->get(i), Record);
3651}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003652
3653
3654void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003655ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003656 Record.push_back(Set.size());
3657 for (UnresolvedSetImpl::const_iterator
3658 I = Set.begin(), E = Set.end(); I != E; ++I) {
3659 AddDeclRef(I.getDecl(), Record);
3660 Record.push_back(I.getAccess());
3661 }
3662}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003663
Sebastian Redla4232eb2010-08-18 23:56:21 +00003664void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003665 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003666 Record.push_back(Base.isVirtual());
3667 Record.push_back(Base.isBaseOfClass());
3668 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00003669 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00003670 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003671 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00003672 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3673 : SourceLocation(),
3674 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003675}
Sebastian Redl30c514c2010-07-14 23:45:08 +00003676
Douglas Gregor7c789c12010-10-29 22:39:52 +00003677void ASTWriter::FlushCXXBaseSpecifiers() {
3678 RecordData Record;
3679 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3680 Record.clear();
3681
3682 // Record the offset of this base-specifier set.
3683 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3684 if (Index == CXXBaseSpecifiersOffsets.size())
3685 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3686 else {
3687 if (Index > CXXBaseSpecifiersOffsets.size())
3688 CXXBaseSpecifiersOffsets.resize(Index + 1);
3689 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3690 }
3691
3692 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3693 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3694 Record.push_back(BEnd - B);
3695 for (; B != BEnd; ++B)
3696 AddCXXBaseSpecifier(*B, Record);
3697 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00003698
3699 // Flush any expressions that were written as part of the base specifiers.
3700 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003701 }
3702
3703 CXXBaseSpecifiersToWrite.clear();
3704}
3705
Sean Huntcbb67482011-01-08 20:30:50 +00003706void ASTWriter::AddCXXCtorInitializers(
3707 const CXXCtorInitializer * const *CtorInitializers,
3708 unsigned NumCtorInitializers,
3709 RecordDataImpl &Record) {
3710 Record.push_back(NumCtorInitializers);
3711 for (unsigned i=0; i != NumCtorInitializers; ++i) {
3712 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003713
3714 Record.push_back(Init->isBaseInitializer());
3715 if (Init->isBaseInitializer()) {
3716 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3717 Record.push_back(Init->isBaseVirtual());
3718 } else {
Francois Pichet00eb3f92010-12-04 09:14:42 +00003719 Record.push_back(Init->isIndirectMemberInitializer());
3720 if (Init->isIndirectMemberInitializer())
3721 AddDeclRef(Init->getIndirectMember(), Record);
3722 else
3723 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003724 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00003725
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003726 AddSourceLocation(Init->getMemberLocation(), Record);
3727 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003728 AddSourceLocation(Init->getLParenLoc(), Record);
3729 AddSourceLocation(Init->getRParenLoc(), Record);
3730 Record.push_back(Init->isWritten());
3731 if (Init->isWritten()) {
3732 Record.push_back(Init->getSourceOrder());
3733 } else {
3734 Record.push_back(Init->getNumArrayIndices());
3735 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3736 AddDeclRef(Init->getArrayIndex(i), Record);
3737 }
3738 }
3739}
3740
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003741void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3742 assert(D->DefinitionData);
3743 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3744 Record.push_back(Data.UserDeclaredConstructor);
3745 Record.push_back(Data.UserDeclaredCopyConstructor);
3746 Record.push_back(Data.UserDeclaredCopyAssignment);
3747 Record.push_back(Data.UserDeclaredDestructor);
3748 Record.push_back(Data.Aggregate);
3749 Record.push_back(Data.PlainOldData);
3750 Record.push_back(Data.Empty);
3751 Record.push_back(Data.Polymorphic);
3752 Record.push_back(Data.Abstract);
3753 Record.push_back(Data.HasTrivialConstructor);
3754 Record.push_back(Data.HasTrivialCopyConstructor);
3755 Record.push_back(Data.HasTrivialCopyAssignment);
3756 Record.push_back(Data.HasTrivialDestructor);
3757 Record.push_back(Data.ComputedVisibleConversions);
3758 Record.push_back(Data.DeclaredDefaultConstructor);
3759 Record.push_back(Data.DeclaredCopyConstructor);
3760 Record.push_back(Data.DeclaredCopyAssignment);
3761 Record.push_back(Data.DeclaredDestructor);
3762
3763 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003764 if (Data.NumBases > 0)
3765 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3766 Record);
3767
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003768 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3769 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003770 if (Data.NumVBases > 0)
3771 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3772 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003773
3774 AddUnresolvedSet(Data.Conversions, Record);
3775 AddUnresolvedSet(Data.VisibleConversions, Record);
3776 // Data.Definition is the owning decl, no need to write it.
3777 AddDeclRef(Data.FirstFriend, Record);
3778}
3779
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003780void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003781 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003782 assert(!Chain && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003783 assert(FirstDeclID == NextDeclID &&
3784 FirstTypeID == NextTypeID &&
3785 FirstIdentID == NextIdentID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00003786 FirstSelectorID == NextSelectorID &&
Douglas Gregor77424bc2010-10-02 19:29:26 +00003787 FirstMacroID == NextMacroID &&
Douglas Gregor7c789c12010-10-29 22:39:52 +00003788 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003789 "Setting chain after writing has started.");
3790 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003791
3792 FirstDeclID += Chain->getTotalNumDecls();
3793 FirstTypeID += Chain->getTotalNumTypes();
3794 FirstIdentID += Chain->getTotalNumIdentifiers();
3795 FirstSelectorID += Chain->getTotalNumSelectors();
3796 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003797 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003798 NextDeclID = FirstDeclID;
3799 NextTypeID = FirstTypeID;
3800 NextIdentID = FirstIdentID;
3801 NextSelectorID = FirstSelectorID;
3802 NextMacroID = FirstMacroID;
Douglas Gregor7c789c12010-10-29 22:39:52 +00003803 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003804}
3805
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003806void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003807 IdentifierIDs[II] = ID;
Douglas Gregor040a8042011-02-11 00:26:14 +00003808 if (II->hasMacroDefinition())
3809 DeserializedMacroNames.push_back(II);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003810}
3811
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003812void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00003813 // Always take the highest-numbered type index. This copes with an interesting
3814 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00003815 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00003816 // keep the higher-numbered entry so that we can properly write it out to
3817 // the AST file.
3818 TypeIdx &StoredIdx = TypeIdxs[T];
3819 if (Idx.getIndex() >= StoredIdx.getIndex())
3820 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003821}
3822
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003823void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1476ed42010-07-16 16:36:56 +00003824 DeclIDs[D] = ID;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003825}
Sebastian Redl5d050072010-08-04 17:20:04 +00003826
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003827void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003828 SelectorIDs[S] = ID;
3829}
Douglas Gregor77424bc2010-10-02 19:29:26 +00003830
Michael J. Spencer20249a12010-10-21 03:16:25 +00003831void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00003832 MacroDefinition *MD) {
3833 MacroDefinitions[MD] = ID;
3834}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003835
3836void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3837 assert(D->isDefinition());
3838 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3839 // We are interested when a PCH decl is modified.
3840 if (RD->getPCHLevel() > 0) {
3841 // A forward reference was mutated into a definition. Rewrite it.
3842 // FIXME: This happens during template instantiation, should we
3843 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00003844 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003845 }
3846
3847 for (CXXRecordDecl::redecl_iterator
3848 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3849 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3850 if (Redecl == RD)
3851 continue;
3852
3853 // We are interested when a PCH decl is modified.
3854 if (Redecl->getPCHLevel() > 0) {
3855 UpdateRecord &Record = DeclUpdates[Redecl];
3856 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3857 assert(Redecl->DefinitionData);
3858 assert(Redecl->DefinitionData->Definition == D);
3859 AddDeclRef(D, Record); // the DefinitionDecl
3860 }
3861 }
3862 }
3863}
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00003864void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3865 // TU and namespaces are handled elsewhere.
3866 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3867 return;
3868
3869 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3870 return; // Not a source decl added to a DeclContext from PCH.
3871
3872 AddUpdatedDeclContext(DC);
3873}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00003874
3875void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3876 assert(D->isImplicit());
3877 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3878 return; // Not a source member added to a class from PCH.
3879 if (!isa<CXXMethodDecl>(D))
3880 return; // We are interested in lazily declared implicit methods.
3881
3882 // A decl coming from PCH was modified.
3883 assert(RD->isDefinition());
3884 UpdateRecord &Record = DeclUpdates[RD];
3885 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3886 AddDeclRef(D, Record);
3887}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00003888
3889void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3890 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00003891 // The specializations set is kept in the canonical template.
3892 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00003893 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3894 return; // Not a source specialization added to a template from PCH.
3895
3896 UpdateRecord &Record = DeclUpdates[TD];
3897 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3898 AddDeclRef(D, Record);
3899}
Douglas Gregor89d99802010-11-30 06:16:57 +00003900
3901ASTSerializationListener::~ASTSerializationListener() { }