blob: ba9032e0d33e0cc299e01aa0da38c022b309a731 [file] [log] [blame]
Sebastian Redld6522cf2010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregoref84c4b2009-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 Redl55c0ad52010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregoref84c4b2009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl1914c6f2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Douglas Gregorf88e35b2010-11-30 06:16:57 +000015#include "clang/Serialization/ASTSerializationListener.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000016#include "ASTCommon.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000017#include "clang/Sema/Sema.h"
18#include "clang/Sema/IdentifierResolver.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclContextInternals.h"
John McCall19c1bfd2010-08-25 05:32:35 +000022#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000023#include "clang/AST/DeclFriend.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000024#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000025#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000026#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000027#include "clang/AST/TypeLocVisitor.h"
Sebastian Redlf5b13462010-08-18 23:57:17 +000028#include "clang/Serialization/ASTReader.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000029#include "clang/Lex/MacroInfo.h"
Douglas Gregoraae92242010-03-19 21:51:54 +000030#include "clang/Lex/PreprocessingRecord.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000031#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000032#include "clang/Lex/HeaderSearch.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000033#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000034#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000035#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000036#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000037#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000038#include "clang/Basic/TargetInfo.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000039#include "clang/Basic/Version.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000040#include "clang/Basic/VersionTuple.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000041#include "llvm/ADT/APFloat.h"
42#include "llvm/ADT/APInt.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000043#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000044#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencer740857f2010-12-21 16:45:57 +000045#include "llvm/Support/FileSystem.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000046#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000047#include "llvm/Support/Path.h"
Chris Lattner225dd6c2009-04-11 18:40:46 +000048#include <cstdio>
Douglas Gregor09b69892011-02-10 17:09:37 +000049#include <string.h>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000050using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000051using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000052
Sebastian Redl3df5a082010-07-30 17:03:48 +000053template <typename T, typename Allocator>
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000054static llvm::StringRef data(const std::vector<T, Allocator> &v) {
55 if (v.empty()) return llvm::StringRef();
56 return llvm::StringRef(reinterpret_cast<const char*>(&v[0]),
57 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000058}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000059
60template <typename T>
61static llvm::StringRef data(const llvm::SmallVectorImpl<T> &v) {
62 return llvm::StringRef(reinterpret_cast<const char*>(v.data()),
63 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000064}
65
Douglas Gregoref84c4b2009-04-09 22:27:44 +000066//===----------------------------------------------------------------------===//
67// Type serialization
68//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000069
Douglas Gregoref84c4b2009-04-09 22:27:44 +000070namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000071 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000072 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000073 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000074
75 public:
76 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000077 TypeCode Code;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000078
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000079 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000080 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000081
82 void VisitArrayType(const ArrayType *T);
83 void VisitFunctionType(const FunctionType *T);
84 void VisitTagType(const TagType *T);
85
86#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
87#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000088#include "clang/AST/TypeNodes.def"
89 };
90}
91
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000092void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000093 assert(false && "Built-in types are never serialized");
94}
95
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000096void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000097 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000098 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000099}
100
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000101void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000102 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000103 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000104}
105
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000106void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000107 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000108 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000109}
110
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000111void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000112 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
113 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000114 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000115}
116
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000117void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000118 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000119 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000120}
121
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000122void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000123 Writer.AddTypeRef(T->getPointeeType(), Record);
124 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000125 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000126}
127
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000128void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000129 Writer.AddTypeRef(T->getElementType(), Record);
130 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000131 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000132}
133
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000134void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000135 VisitArrayType(T);
136 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000137 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000138}
139
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000140void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000141 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000142 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000143}
144
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000145void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000146 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000147 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
148 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000149 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000150 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000151}
152
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000153void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000154 Writer.AddTypeRef(T->getElementType(), Record);
155 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000156 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000157 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000158}
159
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000160void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000161 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000162 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000163}
164
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000165void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000166 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000167 FunctionType::ExtInfo C = T->getExtInfo();
168 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000169 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000170 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000171 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000172 Record.push_back(C.getCC());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000173}
174
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000175void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000176 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000177 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000178}
179
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000180void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000181 VisitFunctionType(T);
182 Record.push_back(T->getNumArgs());
183 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
184 Writer.AddTypeRef(T->getArgType(I), Record);
185 Record.push_back(T->isVariadic());
186 Record.push_back(T->getTypeQuals());
Douglas Gregordb9d6642011-01-26 05:01:58 +0000187 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000188 Record.push_back(T->getExceptionSpecType());
189 if (T->getExceptionSpecType() == EST_Dynamic) {
190 Record.push_back(T->getNumExceptions());
191 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
192 Writer.AddTypeRef(T->getExceptionType(I), Record);
193 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
194 Writer.AddStmt(T->getNoexceptExpr());
195 }
Sebastian Redl539c5062010-08-18 23:57:32 +0000196 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000197}
198
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000199void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000200 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000201 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000202}
John McCallb96ec562009-12-04 22:46:56 +0000203
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000204void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000205 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000206 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
207 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000208 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000209}
210
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000211void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000212 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000213 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000214}
215
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000216void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000217 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000218 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000219}
220
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000221void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson81df7b82009-06-24 19:06:50 +0000222 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000223 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000224}
225
Alexis Hunte852b102011-05-24 22:41:36 +0000226void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
227 Writer.AddTypeRef(T->getBaseType(), Record);
228 Writer.AddTypeRef(T->getUnderlyingType(), Record);
229 Record.push_back(T->getUTTKind());
230 Code = TYPE_UNARY_TRANSFORM;
231}
232
Richard Smith30482bc2011-02-20 03:19:35 +0000233void ASTTypeWriter::VisitAutoType(const AutoType *T) {
234 Writer.AddTypeRef(T->getDeducedType(), Record);
235 Code = TYPE_AUTO;
236}
237
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000238void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000239 Record.push_back(T->isDependentType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000240 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000241 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000242 "Cannot serialize in the middle of a type definition");
243}
244
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000245void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000246 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000247 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000248}
249
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000250void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000251 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000252 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000253}
254
John McCall81904512011-01-06 01:58:22 +0000255void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
256 Writer.AddTypeRef(T->getModifiedType(), Record);
257 Writer.AddTypeRef(T->getEquivalentType(), Record);
258 Record.push_back(T->getAttrKind());
259 Code = TYPE_ATTRIBUTED;
260}
261
Mike Stump11289f42009-09-09 15:08:12 +0000262void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000263ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000264 const SubstTemplateTypeParmType *T) {
265 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
266 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000267 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000268}
269
270void
Douglas Gregorada4b792011-01-14 02:55:32 +0000271ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
272 const SubstTemplateTypeParmPackType *T) {
273 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
274 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
275 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
276}
277
278void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000279ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000280 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000281 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000282 Writer.AddTemplateName(T->getTemplateName(), Record);
283 Record.push_back(T->getNumArgs());
284 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
285 ArgI != ArgE; ++ArgI)
286 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000287 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
288 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000289 : T->getCanonicalTypeInternal(),
290 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000291 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000292}
293
294void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000295ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000296 VisitArrayType(T);
297 Writer.AddStmt(T->getSizeExpr());
298 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000299 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000300}
301
302void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000303ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000304 const DependentSizedExtVectorType *T) {
305 // FIXME: Serialize this type (C++ only)
306 assert(false && "Cannot serialize dependent sized extended vector types");
307}
308
309void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000310ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000311 Record.push_back(T->getDepth());
312 Record.push_back(T->getIndex());
313 Record.push_back(T->isParameterPack());
Chandler Carruth08836322011-05-01 00:51:33 +0000314 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000315 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000316}
317
318void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000319ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000320 Record.push_back(T->getKeyword());
321 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
322 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000323 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
324 : T->getCanonicalTypeInternal(),
325 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000326 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000327}
328
329void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000330ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000331 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000332 Record.push_back(T->getKeyword());
333 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
334 Writer.AddIdentifierRef(T->getIdentifier(), Record);
335 Record.push_back(T->getNumArgs());
336 for (DependentTemplateSpecializationType::iterator
337 I = T->begin(), E = T->end(); I != E; ++I)
338 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000339 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000340}
341
Douglas Gregord2fa7662010-12-20 02:24:11 +0000342void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
343 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000344 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
345 Record.push_back(*NumExpansions + 1);
346 else
347 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000348 Code = TYPE_PACK_EXPANSION;
349}
350
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000351void ASTTypeWriter::VisitParenType(const ParenType *T) {
352 Writer.AddTypeRef(T->getInnerType(), Record);
353 Code = TYPE_PAREN;
354}
355
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000356void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000357 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000358 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
359 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000360 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000361}
362
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000363void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCalle78aac42010-03-10 03:28:59 +0000364 Writer.AddDeclRef(T->getDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000365 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000366 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000367}
368
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000369void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1c283312010-08-11 12:19:30 +0000370 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000371 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000372}
373
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000374void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000375 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000376 Record.push_back(T->getNumProtocols());
John McCall8b07ec22010-05-15 11:32:37 +0000377 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000378 E = T->qual_end(); I != E; ++I)
379 Writer.AddDeclRef(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000380 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000381}
382
Steve Narofffb4330f2009-06-17 22:40:22 +0000383void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000384ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000385 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000386 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000387}
388
John McCall8f115c62009-10-16 21:56:05 +0000389namespace {
390
391class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000392 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000393 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000394
395public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000396 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000397 : Writer(Writer), Record(Record) { }
398
John McCall17001972009-10-18 01:05:36 +0000399#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000400#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000401 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000402#include "clang/AST/TypeLocNodes.def"
403
John McCall17001972009-10-18 01:05:36 +0000404 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
405 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000406};
407
408}
409
John McCall17001972009-10-18 01:05:36 +0000410void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
411 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000412}
John McCall17001972009-10-18 01:05:36 +0000413void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000414 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
415 if (TL.needsExtraLocalData()) {
416 Record.push_back(TL.getWrittenTypeSpec());
417 Record.push_back(TL.getWrittenSignSpec());
418 Record.push_back(TL.getWrittenWidthSpec());
419 Record.push_back(TL.hasModeAttr());
420 }
John McCall8f115c62009-10-16 21:56:05 +0000421}
John McCall17001972009-10-18 01:05:36 +0000422void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
423 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000424}
John McCall17001972009-10-18 01:05:36 +0000425void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
426 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000427}
John McCall17001972009-10-18 01:05:36 +0000428void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
429 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000430}
John McCall17001972009-10-18 01:05:36 +0000431void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
432 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000433}
John McCall17001972009-10-18 01:05:36 +0000434void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
435 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000436}
John McCall17001972009-10-18 01:05:36 +0000437void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
438 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnara509357842011-03-05 14:42:21 +0000439 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000440}
John McCall17001972009-10-18 01:05:36 +0000441void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
442 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
443 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
444 Record.push_back(TL.getSizeExpr() ? 1 : 0);
445 if (TL.getSizeExpr())
446 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000447}
John McCall17001972009-10-18 01:05:36 +0000448void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
449 VisitArrayTypeLoc(TL);
450}
451void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
452 VisitArrayTypeLoc(TL);
453}
454void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
455 VisitArrayTypeLoc(TL);
456}
457void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
458 DependentSizedArrayTypeLoc TL) {
459 VisitArrayTypeLoc(TL);
460}
461void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
462 DependentSizedExtVectorTypeLoc TL) {
463 Writer.AddSourceLocation(TL.getNameLoc(), Record);
464}
465void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
466 Writer.AddSourceLocation(TL.getNameLoc(), Record);
467}
468void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
469 Writer.AddSourceLocation(TL.getNameLoc(), Record);
470}
471void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000472 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
473 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Douglas Gregor7fb25412010-10-01 18:44:50 +0000474 Record.push_back(TL.getTrailingReturn());
John McCall17001972009-10-18 01:05:36 +0000475 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
476 Writer.AddDeclRef(TL.getArg(i), Record);
477}
478void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
479 VisitFunctionTypeLoc(TL);
480}
481void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
482 VisitFunctionTypeLoc(TL);
483}
John McCallb96ec562009-12-04 22:46:56 +0000484void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
485 Writer.AddSourceLocation(TL.getNameLoc(), Record);
486}
John McCall17001972009-10-18 01:05:36 +0000487void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
488 Writer.AddSourceLocation(TL.getNameLoc(), Record);
489}
490void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000491 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
492 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
493 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000494}
495void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000496 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
497 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
498 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
499 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000500}
501void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
502 Writer.AddSourceLocation(TL.getNameLoc(), Record);
503}
Alexis Hunte852b102011-05-24 22:41:36 +0000504void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
505 Writer.AddSourceLocation(TL.getKWLoc(), Record);
506 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
507 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
508 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
509}
Richard Smith30482bc2011-02-20 03:19:35 +0000510void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
511 Writer.AddSourceLocation(TL.getNameLoc(), Record);
512}
John McCall17001972009-10-18 01:05:36 +0000513void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
514 Writer.AddSourceLocation(TL.getNameLoc(), Record);
515}
516void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
517 Writer.AddSourceLocation(TL.getNameLoc(), Record);
518}
John McCall81904512011-01-06 01:58:22 +0000519void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
520 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
521 if (TL.hasAttrOperand()) {
522 SourceRange range = TL.getAttrOperandParensRange();
523 Writer.AddSourceLocation(range.getBegin(), Record);
524 Writer.AddSourceLocation(range.getEnd(), Record);
525 }
526 if (TL.hasAttrExprOperand()) {
527 Expr *operand = TL.getAttrExprOperand();
528 Record.push_back(operand ? 1 : 0);
529 if (operand) Writer.AddStmt(operand);
530 } else if (TL.hasAttrEnumOperand()) {
531 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
532 }
533}
John McCall17001972009-10-18 01:05:36 +0000534void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
535 Writer.AddSourceLocation(TL.getNameLoc(), Record);
536}
John McCallcebee162009-10-18 09:09:24 +0000537void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
538 SubstTemplateTypeParmTypeLoc TL) {
539 Writer.AddSourceLocation(TL.getNameLoc(), Record);
540}
Douglas Gregorada4b792011-01-14 02:55:32 +0000541void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
542 SubstTemplateTypeParmPackTypeLoc TL) {
543 Writer.AddSourceLocation(TL.getNameLoc(), Record);
544}
John McCall17001972009-10-18 01:05:36 +0000545void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
546 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +0000547 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
548 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
549 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
550 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000551 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
552 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000553}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000554void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
555 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
556 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
557}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000558void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000559 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000560 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000561}
John McCalle78aac42010-03-10 03:28:59 +0000562void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
563 Writer.AddSourceLocation(TL.getNameLoc(), Record);
564}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000565void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000566 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000567 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000568 Writer.AddSourceLocation(TL.getNameLoc(), Record);
569}
John McCallc392f372010-06-11 00:33:02 +0000570void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
571 DependentTemplateSpecializationTypeLoc TL) {
572 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000573 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000574 Writer.AddSourceLocation(TL.getNameLoc(), Record);
575 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
576 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
577 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000578 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
579 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000580}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000581void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
582 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
583}
John McCall17001972009-10-18 01:05:36 +0000584void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
585 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000586}
587void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
588 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000589 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
590 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
591 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
592 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000593}
John McCallfc93cf92009-10-22 22:37:11 +0000594void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
595 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000596}
John McCall8f115c62009-10-16 21:56:05 +0000597
Chris Lattner19cea4e2009-04-22 05:57:30 +0000598//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000599// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000600//===----------------------------------------------------------------------===//
601
Chris Lattner28fa4e62009-04-26 22:26:21 +0000602static void EmitBlockID(unsigned ID, const char *Name,
603 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000604 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000605 Record.clear();
606 Record.push_back(ID);
607 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
608
609 // Emit the block name if present.
610 if (Name == 0 || Name[0] == 0) return;
611 Record.clear();
612 while (*Name)
613 Record.push_back(*Name++);
614 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
615}
616
617static void EmitRecordID(unsigned ID, const char *Name,
618 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000619 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000620 Record.clear();
621 Record.push_back(ID);
622 while (*Name)
623 Record.push_back(*Name++);
624 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000625}
626
627static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000628 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000629#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000630 RECORD(STMT_STOP);
631 RECORD(STMT_NULL_PTR);
632 RECORD(STMT_NULL);
633 RECORD(STMT_COMPOUND);
634 RECORD(STMT_CASE);
635 RECORD(STMT_DEFAULT);
636 RECORD(STMT_LABEL);
637 RECORD(STMT_IF);
638 RECORD(STMT_SWITCH);
639 RECORD(STMT_WHILE);
640 RECORD(STMT_DO);
641 RECORD(STMT_FOR);
642 RECORD(STMT_GOTO);
643 RECORD(STMT_INDIRECT_GOTO);
644 RECORD(STMT_CONTINUE);
645 RECORD(STMT_BREAK);
646 RECORD(STMT_RETURN);
647 RECORD(STMT_DECL);
648 RECORD(STMT_ASM);
649 RECORD(EXPR_PREDEFINED);
650 RECORD(EXPR_DECL_REF);
651 RECORD(EXPR_INTEGER_LITERAL);
652 RECORD(EXPR_FLOATING_LITERAL);
653 RECORD(EXPR_IMAGINARY_LITERAL);
654 RECORD(EXPR_STRING_LITERAL);
655 RECORD(EXPR_CHARACTER_LITERAL);
656 RECORD(EXPR_PAREN);
657 RECORD(EXPR_UNARY_OPERATOR);
658 RECORD(EXPR_SIZEOF_ALIGN_OF);
659 RECORD(EXPR_ARRAY_SUBSCRIPT);
660 RECORD(EXPR_CALL);
661 RECORD(EXPR_MEMBER);
662 RECORD(EXPR_BINARY_OPERATOR);
663 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
664 RECORD(EXPR_CONDITIONAL_OPERATOR);
665 RECORD(EXPR_IMPLICIT_CAST);
666 RECORD(EXPR_CSTYLE_CAST);
667 RECORD(EXPR_COMPOUND_LITERAL);
668 RECORD(EXPR_EXT_VECTOR_ELEMENT);
669 RECORD(EXPR_INIT_LIST);
670 RECORD(EXPR_DESIGNATED_INIT);
671 RECORD(EXPR_IMPLICIT_VALUE_INIT);
672 RECORD(EXPR_VA_ARG);
673 RECORD(EXPR_ADDR_LABEL);
674 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000675 RECORD(EXPR_CHOOSE);
676 RECORD(EXPR_GNU_NULL);
677 RECORD(EXPR_SHUFFLE_VECTOR);
678 RECORD(EXPR_BLOCK);
679 RECORD(EXPR_BLOCK_DECL_REF);
Peter Collingbourne91147592011-04-15 00:35:48 +0000680 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000681 RECORD(EXPR_OBJC_STRING_LITERAL);
682 RECORD(EXPR_OBJC_ENCODE);
683 RECORD(EXPR_OBJC_SELECTOR_EXPR);
684 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
685 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
686 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
687 RECORD(EXPR_OBJC_KVC_REF_EXPR);
688 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000689 RECORD(STMT_OBJC_FOR_COLLECTION);
690 RECORD(STMT_OBJC_CATCH);
691 RECORD(STMT_OBJC_FINALLY);
692 RECORD(STMT_OBJC_AT_TRY);
693 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
694 RECORD(STMT_OBJC_AT_THROW);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000695 RECORD(EXPR_CXX_OPERATOR_CALL);
696 RECORD(EXPR_CXX_CONSTRUCT);
697 RECORD(EXPR_CXX_STATIC_CAST);
698 RECORD(EXPR_CXX_DYNAMIC_CAST);
699 RECORD(EXPR_CXX_REINTERPRET_CAST);
700 RECORD(EXPR_CXX_CONST_CAST);
701 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
702 RECORD(EXPR_CXX_BOOL_LITERAL);
703 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000704 RECORD(EXPR_CXX_TYPEID_EXPR);
705 RECORD(EXPR_CXX_TYPEID_TYPE);
706 RECORD(EXPR_CXX_UUIDOF_EXPR);
707 RECORD(EXPR_CXX_UUIDOF_TYPE);
708 RECORD(EXPR_CXX_THIS);
709 RECORD(EXPR_CXX_THROW);
710 RECORD(EXPR_CXX_DEFAULT_ARG);
711 RECORD(EXPR_CXX_BIND_TEMPORARY);
712 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
713 RECORD(EXPR_CXX_NEW);
714 RECORD(EXPR_CXX_DELETE);
715 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
716 RECORD(EXPR_EXPR_WITH_CLEANUPS);
717 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
718 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
719 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
720 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
721 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
722 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
723 RECORD(EXPR_CXX_NOEXCEPT);
724 RECORD(EXPR_OPAQUE_VALUE);
725 RECORD(EXPR_BINARY_TYPE_TRAIT);
726 RECORD(EXPR_PACK_EXPANSION);
727 RECORD(EXPR_SIZEOF_PACK);
728 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000729 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000730#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000731}
Mike Stump11289f42009-09-09 15:08:12 +0000732
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000733void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000734 RecordData Record;
735 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000736
Sebastian Redl539c5062010-08-18 23:57:32 +0000737#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
738#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000739
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000740 // AST Top-Level Block.
Sebastian Redlf1642042010-08-18 23:57:22 +0000741 BLOCK(AST_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000742 RECORD(ORIGINAL_FILE_NAME);
Douglas Gregora3b20262011-05-06 21:43:30 +0000743 RECORD(ORIGINAL_FILE_ID);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000744 RECORD(TYPE_OFFSET);
745 RECORD(DECL_OFFSET);
746 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000747 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000748 RECORD(IDENTIFIER_OFFSET);
749 RECORD(IDENTIFIER_TABLE);
750 RECORD(EXTERNAL_DEFINITIONS);
751 RECORD(SPECIAL_TYPES);
752 RECORD(STATISTICS);
753 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000754 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000755 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
756 RECORD(SELECTOR_OFFSETS);
757 RECORD(METHOD_POOL);
758 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000759 RECORD(SOURCE_LOCATION_OFFSETS);
760 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000761 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000762 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek17437132010-01-22 20:59:36 +0000763 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregoraae92242010-03-19 21:51:54 +0000764 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redl595c5132010-07-08 22:01:51 +0000765 RECORD(CHAINED_METADATA);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000766 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000767 RECORD(TU_UPDATE_LEXICAL);
768 RECORD(REDECLS_UPDATE_LATEST);
769 RECORD(SEMA_DECL_REFS);
770 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
771 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
772 RECORD(DECL_REPLACEMENTS);
773 RECORD(UPDATE_VISIBLE);
774 RECORD(DECL_UPDATE_OFFSETS);
775 RECORD(DECL_UPDATES);
776 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
777 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000778 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000779 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000780 RECORD(FP_PRAGMA_OPTIONS);
781 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +0000782 RECORD(DELEGATING_CTORS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000783
Chris Lattner28fa4e62009-04-26 22:26:21 +0000784 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000785 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000786 RECORD(SM_SLOC_FILE_ENTRY);
787 RECORD(SM_SLOC_BUFFER_ENTRY);
788 RECORD(SM_SLOC_BUFFER_BLOB);
789 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
790 RECORD(SM_LINE_TABLE);
Mike Stump11289f42009-09-09 15:08:12 +0000791
Chris Lattner28fa4e62009-04-26 22:26:21 +0000792 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000793 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000794 RECORD(PP_MACRO_OBJECT_LIKE);
795 RECORD(PP_MACRO_FUNCTION_LIKE);
796 RECORD(PP_TOKEN);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000797
Douglas Gregor12bfa382009-10-17 00:13:19 +0000798 // Decls and Types block.
799 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000800 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000801 RECORD(TYPE_COMPLEX);
802 RECORD(TYPE_POINTER);
803 RECORD(TYPE_BLOCK_POINTER);
804 RECORD(TYPE_LVALUE_REFERENCE);
805 RECORD(TYPE_RVALUE_REFERENCE);
806 RECORD(TYPE_MEMBER_POINTER);
807 RECORD(TYPE_CONSTANT_ARRAY);
808 RECORD(TYPE_INCOMPLETE_ARRAY);
809 RECORD(TYPE_VARIABLE_ARRAY);
810 RECORD(TYPE_VECTOR);
811 RECORD(TYPE_EXT_VECTOR);
812 RECORD(TYPE_FUNCTION_PROTO);
813 RECORD(TYPE_FUNCTION_NO_PROTO);
814 RECORD(TYPE_TYPEDEF);
815 RECORD(TYPE_TYPEOF_EXPR);
816 RECORD(TYPE_TYPEOF);
817 RECORD(TYPE_RECORD);
818 RECORD(TYPE_ENUM);
819 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000820 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000821 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000822 RECORD(TYPE_DECLTYPE);
823 RECORD(TYPE_ELABORATED);
824 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
825 RECORD(TYPE_UNRESOLVED_USING);
826 RECORD(TYPE_INJECTED_CLASS_NAME);
827 RECORD(TYPE_OBJC_OBJECT);
828 RECORD(TYPE_TEMPLATE_TYPE_PARM);
829 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
830 RECORD(TYPE_DEPENDENT_NAME);
831 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
832 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
833 RECORD(TYPE_PAREN);
834 RECORD(TYPE_PACK_EXPANSION);
835 RECORD(TYPE_ATTRIBUTED);
836 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000837 RECORD(DECL_TRANSLATION_UNIT);
838 RECORD(DECL_TYPEDEF);
839 RECORD(DECL_ENUM);
840 RECORD(DECL_RECORD);
841 RECORD(DECL_ENUM_CONSTANT);
842 RECORD(DECL_FUNCTION);
843 RECORD(DECL_OBJC_METHOD);
844 RECORD(DECL_OBJC_INTERFACE);
845 RECORD(DECL_OBJC_PROTOCOL);
846 RECORD(DECL_OBJC_IVAR);
847 RECORD(DECL_OBJC_AT_DEFS_FIELD);
848 RECORD(DECL_OBJC_CLASS);
849 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
850 RECORD(DECL_OBJC_CATEGORY);
851 RECORD(DECL_OBJC_CATEGORY_IMPL);
852 RECORD(DECL_OBJC_IMPLEMENTATION);
853 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
854 RECORD(DECL_OBJC_PROPERTY);
855 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000856 RECORD(DECL_FIELD);
857 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000858 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000859 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000860 RECORD(DECL_FILE_SCOPE_ASM);
861 RECORD(DECL_BLOCK);
862 RECORD(DECL_CONTEXT_LEXICAL);
863 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000864 RECORD(DECL_NAMESPACE);
865 RECORD(DECL_NAMESPACE_ALIAS);
866 RECORD(DECL_USING);
867 RECORD(DECL_USING_SHADOW);
868 RECORD(DECL_USING_DIRECTIVE);
869 RECORD(DECL_UNRESOLVED_USING_VALUE);
870 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
871 RECORD(DECL_LINKAGE_SPEC);
872 RECORD(DECL_CXX_RECORD);
873 RECORD(DECL_CXX_METHOD);
874 RECORD(DECL_CXX_CONSTRUCTOR);
875 RECORD(DECL_CXX_DESTRUCTOR);
876 RECORD(DECL_CXX_CONVERSION);
877 RECORD(DECL_ACCESS_SPEC);
878 RECORD(DECL_FRIEND);
879 RECORD(DECL_FRIEND_TEMPLATE);
880 RECORD(DECL_CLASS_TEMPLATE);
881 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
882 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
883 RECORD(DECL_FUNCTION_TEMPLATE);
884 RECORD(DECL_TEMPLATE_TYPE_PARM);
885 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
886 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
887 RECORD(DECL_STATIC_ASSERT);
888 RECORD(DECL_CXX_BASE_SPECIFIERS);
889 RECORD(DECL_INDIRECTFIELD);
890 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
891
Douglas Gregor03412ba2011-06-03 02:27:19 +0000892 // Statements and Exprs can occur in the Decls and Types block.
893 AddStmtsExprs(Stream, Record);
894
Douglas Gregor92a96f52011-02-08 21:58:10 +0000895 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
896 RECORD(PPD_MACRO_INSTANTIATION);
897 RECORD(PPD_MACRO_DEFINITION);
898 RECORD(PPD_INCLUSION_DIRECTIVE);
899
Chris Lattner28fa4e62009-04-26 22:26:21 +0000900#undef RECORD
901#undef BLOCK
902 Stream.ExitBlock();
903}
904
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000905/// \brief Adjusts the given filename to only write out the portion of the
906/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000907///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000908/// \param Filename the file name to adjust.
909///
910/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
911/// the returned filename will be adjusted by this system root.
912///
913/// \returns either the original filename (if it needs no adjustment) or the
914/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000915static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000916adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
917 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000918
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000919 if (!isysroot)
920 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000921
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000922 // Verify that the filename and the system root have the same prefix.
923 unsigned Pos = 0;
924 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
925 if (Filename[Pos] != isysroot[Pos])
926 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000927
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000928 // We hit the end of the filename before we hit the end of the system root.
929 if (!Filename[Pos])
930 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000931
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000932 // If the file name has a '/' at the current position, skip over the '/'.
933 // We distinguish sysroot-based includes from absolute includes by the
934 // absence of '/' at the beginning of sysroot-based includes.
935 if (Filename[Pos] == '/')
936 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000937
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000938 return Filename + Pos;
939}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000940
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000941/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000942void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot,
943 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000944 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000945
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000946 // Metadata
947 const TargetInfo &Target = Context.Target;
948 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000949 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl539c5062010-08-18 23:57:32 +0000950 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000951 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
952 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000953 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
954 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
955 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000956 // Target triple or chained PCH name
957 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000958 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000959
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000960 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000961 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
962 Record.push_back(VERSION_MAJOR);
963 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000964 Record.push_back(CLANG_VERSION_MAJOR);
965 Record.push_back(CLANG_VERSION_MINOR);
966 Record.push_back(isysroot != 0);
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000967 // FIXME: This writes the absolute path for chained headers.
968 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
969 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump11289f42009-09-09 15:08:12 +0000970
Douglas Gregora3b20262011-05-06 21:43:30 +0000971 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +0000972 SourceManager &SM = Context.getSourceManager();
973 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
974 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000975 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregor45fe0362009-05-12 01:31:05 +0000976 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
977 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
978
Michael J. Spencer740857f2010-12-21 16:45:57 +0000979 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +0000980
Michael J. Spencer740857f2010-12-21 16:45:57 +0000981 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000982
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +0000983 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000984 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000985 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000986 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000987 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000988 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregora3b20262011-05-06 21:43:30 +0000989
990 Record.clear();
991 Record.push_back(SM.getMainFileID().getOpaqueValue());
992 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000993 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000994
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000995 // Original PCH directory
996 if (!OutputFile.empty() && OutputFile != "-") {
997 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
998 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
999 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1000 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1001
1002 llvm::SmallString<128> OutputPath(OutputFile);
1003
1004 llvm::sys::fs::make_absolute(OutputPath);
1005 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1006
1007 RecordData Record;
1008 Record.push_back(ORIGINAL_PCH_DIR);
1009 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1010 }
1011
Ted Kremenek18e066f2010-01-22 22:12:47 +00001012 // Repository branch/version information.
1013 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001014 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenek18e066f2010-01-22 22:12:47 +00001015 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1016 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +00001017 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001018 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +00001019 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
1020 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +00001021}
1022
1023/// \brief Write the LangOptions structure.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001024void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001025 RecordData Record;
1026 Record.push_back(LangOpts.Trigraphs);
1027 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1028 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1029 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1030 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carruthe03aa552010-04-17 20:17:31 +00001031 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor55abb232009-04-10 20:39:37 +00001032 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1033 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1034 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1035 Record.push_back(LangOpts.C99); // C99 Support
Peter Collingbournea686b5f2011-04-15 00:35:23 +00001036 Record.push_back(LangOpts.C1X); // C1X Support
Douglas Gregor55abb232009-04-10 20:39:37 +00001037 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +00001038 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
1039 // already saved elsewhere.
Douglas Gregor55abb232009-04-10 20:39:37 +00001040 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1041 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +00001042 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +00001043
Douglas Gregor55abb232009-04-10 20:39:37 +00001044 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1045 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001046 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian45878032010-02-09 19:31:38 +00001047 // modern abi enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001048 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian45878032010-02-09 19:31:38 +00001049 // modern abi enabled.
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +00001050 Record.push_back(LangOpts.AppleKext); // Apple's kernel extensions ABI
Ted Kremenek1d56c9e2010-12-23 21:35:43 +00001051 Record.push_back(LangOpts.ObjCDefaultSynthProperties); // Objective-C auto-synthesized
1052 // properties enabled.
Douglas Gregora860e6a2011-06-14 23:20:43 +00001053 Record.push_back(LangOpts.ObjCInferRelatedResultType);
Fariborz Jahanian62c56022010-04-22 21:01:59 +00001054 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump11289f42009-09-09 15:08:12 +00001055
Douglas Gregor55abb232009-04-10 20:39:37 +00001056 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +00001057 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1058 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00001059 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +00001060 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Anders Carlssonce8dd3a2011-02-19 23:53:54 +00001061 Record.push_back(LangOpts.ObjCExceptions);
Anders Carlsson6bbd2682011-02-23 03:04:54 +00001062 Record.push_back(LangOpts.CXXExceptions);
1063 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor55abb232009-04-10 20:39:37 +00001064
Douglas Gregordbe39272011-02-01 15:15:22 +00001065 Record.push_back(LangOpts.MSBitfields); // MS-compatible structure layout
Douglas Gregor55abb232009-04-10 20:39:37 +00001066 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1067 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1068 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1069
Chris Lattner258172e2009-04-27 07:35:58 +00001070 // Whether static initializers are protected by locks.
1071 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00001072 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +00001073 Record.push_back(LangOpts.Blocks); // block extension to C
1074 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1075 // they are unused.
1076 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1077 // (modulo the platform support).
1078
Chris Lattner51924e512010-06-26 21:25:03 +00001079 Record.push_back(LangOpts.getSignedOverflowBehavior());
1080 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor55abb232009-04-10 20:39:37 +00001081
1082 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +00001083 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +00001084 // defined.
1085 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1086 // opposed to __DYNAMIC__).
1087 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1088
1089 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1090 // used (instead of C99 semantics).
1091 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Chandler Carruth7ffce732011-04-23 20:05:38 +00001092 Record.push_back(LangOpts.Deprecated); // Should __DEPRECATED be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +00001093 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
1094 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +00001095 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
1096 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +00001097 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Argyrios Kyrtzidisa88942a2011-01-15 02:56:16 +00001098 Record.push_back(LangOpts.ShortEnums); // Should the enum type be equivalent
1099 // to the smallest integer type with
1100 // enough room.
Douglas Gregor55abb232009-04-10 20:39:37 +00001101 Record.push_back(LangOpts.getGCMode());
1102 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +00001103 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +00001104 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00001105 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +00001106 Record.push_back(LangOpts.CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +00001107 Record.push_back(LangOpts.CatchUndefined);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001108 Record.push_back(LangOpts.DefaultFPContract);
Anders Carlsson9cedbef2009-08-22 22:30:33 +00001109 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +00001110 Record.push_back(LangOpts.SpellChecking);
Roman Divacky65b88cd2011-03-01 17:40:53 +00001111 Record.push_back(LangOpts.MRTD);
Sebastian Redl539c5062010-08-18 23:57:32 +00001112 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +00001113}
1114
Douglas Gregora7f71a92009-04-10 03:52:48 +00001115//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00001116// stat cache Serialization
1117//===----------------------------------------------------------------------===//
1118
1119namespace {
1120// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001121class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001122public:
1123 typedef const char * key_type;
1124 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001125
Chris Lattner2a6fa472010-11-23 19:28:12 +00001126 typedef struct stat data_type;
1127 typedef const data_type &data_type_ref;
Douglas Gregorc5046832009-04-27 18:38:38 +00001128
1129 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001130 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001131 }
Mike Stump11289f42009-09-09 15:08:12 +00001132
1133 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +00001134 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1135 data_type_ref Data) {
1136 unsigned StrLen = strlen(path);
1137 clang::io::Emit16(Out, StrLen);
Chris Lattner2a6fa472010-11-23 19:28:12 +00001138 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregorc5046832009-04-27 18:38:38 +00001139 clang::io::Emit8(Out, DataLen);
1140 return std::make_pair(StrLen + 1, DataLen);
1141 }
Mike Stump11289f42009-09-09 15:08:12 +00001142
Douglas Gregorc5046832009-04-27 18:38:38 +00001143 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1144 Out.write(path, KeyLen);
1145 }
Mike Stump11289f42009-09-09 15:08:12 +00001146
Chris Lattner2a6fa472010-11-23 19:28:12 +00001147 void EmitData(llvm::raw_ostream &Out, key_type_ref,
Douglas Gregorc5046832009-04-27 18:38:38 +00001148 data_type_ref Data, unsigned DataLen) {
1149 using namespace clang::io;
1150 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +00001151
Chris Lattner2a6fa472010-11-23 19:28:12 +00001152 Emit32(Out, (uint32_t) Data.st_ino);
1153 Emit32(Out, (uint32_t) Data.st_dev);
1154 Emit16(Out, (uint16_t) Data.st_mode);
1155 Emit64(Out, (uint64_t) Data.st_mtime);
1156 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregorc5046832009-04-27 18:38:38 +00001157
1158 assert(Out.tell() - Start == DataLen && "Wrong data length");
1159 }
1160};
1161} // end anonymous namespace
1162
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001163/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001164void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001165 // Build the on-disk hash table containing information about every
1166 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001167 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +00001168 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001169 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +00001170 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001171 Stat != StatEnd; ++Stat, ++NumStatEntries) {
1172 const char *Filename = Stat->first();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001173 Generator.insert(Filename, Stat->second);
1174 }
Mike Stump11289f42009-09-09 15:08:12 +00001175
Douglas Gregorc5046832009-04-27 18:38:38 +00001176 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001177 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +00001178 uint32_t BucketOffset;
1179 {
1180 llvm::raw_svector_ostream Out(StatCacheData);
1181 // Make sure that no bucket is at offset 0
1182 clang::io::Emit32(Out, 0);
1183 BucketOffset = Generator.Emit(Out);
1184 }
1185
1186 // Create a blob abbreviation
1187 using namespace llvm;
1188 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001189 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +00001190 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1191 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1192 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1193 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1194
1195 // Write the stat cache
1196 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001197 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +00001198 Record.push_back(BucketOffset);
1199 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001200 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +00001201}
1202
1203//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +00001204// Source Manager Serialization
1205//===----------------------------------------------------------------------===//
1206
1207/// \brief Create an abbreviation for the SLocEntry that refers to a
1208/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001209static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001210 using namespace llvm;
1211 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001212 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001213 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1214 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1215 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1216 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001217 // FileEntry fields.
1218 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1219 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora7f71a92009-04-10 03:52:48 +00001220 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001221 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001222}
1223
1224/// \brief Create an abbreviation for the SLocEntry that refers to a
1225/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001226static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001227 using namespace llvm;
1228 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001229 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001230 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1231 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1232 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1233 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1234 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001235 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001236}
1237
1238/// \brief Create an abbreviation for the SLocEntry that refers to a
1239/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001240static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001241 using namespace llvm;
1242 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001243 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001244 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001245 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001246}
1247
1248/// \brief Create an abbreviation for the SLocEntry that refers to an
1249/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001250static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001251 using namespace llvm;
1252 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001253 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001254 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1255 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1256 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1257 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001258 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001259 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001260}
1261
Douglas Gregor09b69892011-02-10 17:09:37 +00001262namespace {
1263 // Trait used for the on-disk hash table of header search information.
1264 class HeaderFileInfoTrait {
1265 ASTWriter &Writer;
1266 HeaderSearch &HS;
1267
1268 public:
1269 HeaderFileInfoTrait(ASTWriter &Writer, HeaderSearch &HS)
1270 : Writer(Writer), HS(HS) { }
1271
1272 typedef const char *key_type;
1273 typedef key_type key_type_ref;
1274
1275 typedef HeaderFileInfo data_type;
1276 typedef const data_type &data_type_ref;
1277
1278 static unsigned ComputeHash(const char *path) {
1279 // The hash is based only on the filename portion of the key, so that the
1280 // reader can match based on filenames when symlinking or excess path
1281 // elements ("foo/../", "../") change the form of the name. However,
1282 // complete path is still the key.
1283 return llvm::HashString(llvm::sys::path::filename(path));
1284 }
1285
1286 std::pair<unsigned,unsigned>
1287 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1288 data_type_ref Data) {
1289 unsigned StrLen = strlen(path);
1290 clang::io::Emit16(Out, StrLen);
1291 unsigned DataLen = 1 + 2 + 4;
1292 clang::io::Emit8(Out, DataLen);
1293 return std::make_pair(StrLen + 1, DataLen);
1294 }
1295
1296 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1297 Out.write(path, KeyLen);
1298 }
1299
1300 void EmitData(llvm::raw_ostream &Out, key_type_ref,
1301 data_type_ref Data, unsigned DataLen) {
1302 using namespace clang::io;
1303 uint64_t Start = Out.tell(); (void)Start;
1304
Douglas Gregor37aa4932011-05-04 00:14:37 +00001305 unsigned char Flags = (Data.isImport << 4)
1306 | (Data.isPragmaOnce << 3)
Douglas Gregor09b69892011-02-10 17:09:37 +00001307 | (Data.DirInfo << 1)
1308 | Data.Resolved;
1309 Emit8(Out, (uint8_t)Flags);
1310 Emit16(Out, (uint16_t) Data.NumIncludes);
1311
1312 if (!Data.ControllingMacro)
1313 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1314 else
1315 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
1316 assert(Out.tell() - Start == DataLen && "Wrong data length");
1317 }
1318 };
1319} // end anonymous namespace
1320
1321/// \brief Write the header search block for the list of files that
1322///
1323/// \param HS The header search structure to save.
1324///
1325/// \param Chain Whether we're creating a chained AST file.
1326void ASTWriter::WriteHeaderSearch(HeaderSearch &HS, const char* isysroot) {
1327 llvm::SmallVector<const FileEntry *, 16> FilesByUID;
1328 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1329
1330 if (FilesByUID.size() > HS.header_file_size())
1331 FilesByUID.resize(HS.header_file_size());
1332
1333 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1334 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
1335 llvm::SmallVector<const char *, 4> SavedStrings;
1336 unsigned NumHeaderSearchEntries = 0;
1337 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1338 const FileEntry *File = FilesByUID[UID];
1339 if (!File)
1340 continue;
1341
1342 const HeaderFileInfo &HFI = HS.header_file_begin()[UID];
1343 if (HFI.External && Chain)
1344 continue;
1345
1346 // Turn the file name into an absolute path, if it isn't already.
1347 const char *Filename = File->getName();
1348 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1349
1350 // If we performed any translation on the file name at all, we need to
1351 // save this string, since the generator will refer to it later.
1352 if (Filename != File->getName()) {
1353 Filename = strdup(Filename);
1354 SavedStrings.push_back(Filename);
1355 }
1356
1357 Generator.insert(Filename, HFI, GeneratorTrait);
1358 ++NumHeaderSearchEntries;
1359 }
1360
1361 // Create the on-disk hash table in a buffer.
1362 llvm::SmallString<4096> TableData;
1363 uint32_t BucketOffset;
1364 {
1365 llvm::raw_svector_ostream Out(TableData);
1366 // Make sure that no bucket is at offset 0
1367 clang::io::Emit32(Out, 0);
1368 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1369 }
1370
1371 // Create a blob abbreviation
1372 using namespace llvm;
1373 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1374 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1375 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1376 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1377 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1378 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1379
1380 // Write the stat cache
1381 RecordData Record;
1382 Record.push_back(HEADER_SEARCH_TABLE);
1383 Record.push_back(BucketOffset);
1384 Record.push_back(NumHeaderSearchEntries);
1385 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1386
1387 // Free all of the strings we had to duplicate.
1388 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1389 free((void*)SavedStrings[I]);
1390}
1391
Douglas Gregora7f71a92009-04-10 03:52:48 +00001392/// \brief Writes the block containing the serialized form of the
1393/// source manager.
1394///
1395/// TODO: We should probably use an on-disk hash table (stored in a
1396/// blob), indexed based on the file name, so that we only create
1397/// entries for files that we actually need. In the common case (no
1398/// errors), we probably won't have to create file entries for any of
1399/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001400void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001401 const Preprocessor &PP,
1402 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001403 RecordData Record;
1404
Chris Lattner0910e3b2009-04-10 17:16:57 +00001405 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001406 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001407
1408 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001409 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1410 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1411 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1412 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001413
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001414 // Write the line table.
1415 if (SourceMgr.hasLineTable()) {
1416 LineTableInfo &LineTable = SourceMgr.getLineTable();
1417
1418 // Emit the file names
1419 Record.push_back(LineTable.getNumFilenames());
1420 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1421 // Emit the file name
1422 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001423 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001424 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1425 Record.push_back(FilenameLen);
1426 if (FilenameLen)
1427 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1428 }
Mike Stump11289f42009-09-09 15:08:12 +00001429
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001430 // Emit the line entries
1431 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1432 L != LEnd; ++L) {
1433 // Emit the file ID
1434 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +00001435
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001436 // Emit the line entries
1437 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +00001438 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001439 LEEnd = L->second.end();
1440 LE != LEEnd; ++LE) {
1441 Record.push_back(LE->FileOffset);
1442 Record.push_back(LE->LineNo);
1443 Record.push_back(LE->FilenameID);
1444 Record.push_back((unsigned)LE->FileKind);
1445 Record.push_back(LE->IncludeOffset);
1446 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001447 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001448 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001449 }
1450
Douglas Gregor258ae542009-04-27 06:38:32 +00001451 // Write out the source location entry table. We skip the first
1452 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001453 std::vector<uint32_t> SLocEntryOffsets;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001454 // Write out the offsets of only source location file entries.
1455 // We will go through them in ASTReader::validateFileEntries().
1456 std::vector<uint32_t> SLocFileEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001457 RecordData PreloadSLocs;
Sebastian Redl5c415f32010-07-22 17:01:13 +00001458 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1459 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1460 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1461 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001462 // Get this source location entry.
1463 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001464
Douglas Gregor258ae542009-04-27 06:38:32 +00001465 // Record the offset of this source-location entry.
1466 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1467
1468 // Figure out which record code to use.
1469 unsigned Code;
1470 if (SLoc->isFile()) {
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001471 if (SLoc->getFile().getContentCache()->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001472 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001473 SLocFileEntryOffsets.push_back(Stream.GetCurrentBitNo());
1474 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001475 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001476 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001477 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001478 Record.clear();
1479 Record.push_back(Code);
1480
1481 Record.push_back(SLoc->getOffset());
1482 if (SLoc->isFile()) {
1483 const SrcMgr::FileInfo &File = SLoc->getFile();
1484 Record.push_back(File.getIncludeLoc().getRawEncoding());
1485 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1486 Record.push_back(File.hasLineDirectives());
1487
1488 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001489 if (Content->OrigEntry) {
1490 assert(Content->OrigEntry == Content->ContentsEntry &&
1491 "Writing to AST an overriden file is not supported");
1492
Douglas Gregor258ae542009-04-27 06:38:32 +00001493 // The source location entry is a file. The blob associated
1494 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001495
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001496 // Emit size/modification time for this file.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001497 Record.push_back(Content->OrigEntry->getSize());
1498 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001499
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001500 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001501 const char *Filename = Content->OrigEntry->getName();
Michael J. Spencer740857f2010-12-21 16:45:57 +00001502 llvm::SmallString<128> FilePath(Filename);
Anders Carlssona4267052011-03-08 16:04:35 +00001503
1504 // Ask the file manager to fixup the relative path for us. This will
1505 // honor the working directory.
1506 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1507
1508 // FIXME: This call to make_absolute shouldn't be necessary, the
1509 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencer740857f2010-12-21 16:45:57 +00001510 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001511 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001512
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001513 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001514 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001515 } else {
1516 // The source location entry is a buffer. The blob associated
1517 // with this entry contains the contents of the buffer.
1518
1519 // We add one to the size so that we capture the trailing NULL
1520 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1521 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001522 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001523 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001524 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001525 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1526 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001527 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001528 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001529 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001530 llvm::StringRef(Buffer->getBufferStart(),
1531 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001532
1533 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl5c415f32010-07-22 17:01:13 +00001534 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001535 }
1536 } else {
1537 // The source location entry is an instantiation.
1538 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1539 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1540 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1541 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1542
1543 // Compute the token length for this macro expansion.
1544 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001545 if (I + 1 != N)
1546 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001547 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1548 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1549 }
1550 }
1551
Douglas Gregor8f45df52009-04-16 22:23:12 +00001552 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001553
1554 if (SLocEntryOffsets.empty())
1555 return;
1556
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001557 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001558 // table is used for lazily loading source-location information.
1559 using namespace llvm;
1560 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001561 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001562 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1563 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1564 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1565 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001566
Douglas Gregor258ae542009-04-27 06:38:32 +00001567 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001568 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001569 Record.push_back(SLocEntryOffsets.size());
Sebastian Redlc1d035f2010-09-22 20:19:08 +00001570 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1571 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001572 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001573
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001574 Abbrev = new BitCodeAbbrev();
1575 Abbrev->Add(BitCodeAbbrevOp(FILE_SOURCE_LOCATION_OFFSETS));
1576 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1577 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1578 unsigned SLocFileOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1579
1580 Record.clear();
1581 Record.push_back(FILE_SOURCE_LOCATION_OFFSETS);
1582 Record.push_back(SLocFileEntryOffsets.size());
1583 Stream.EmitRecordWithBlob(SLocFileOffsetsAbbrev, Record,
1584 data(SLocFileEntryOffsets));
1585
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001586 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001587 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001588 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001589}
1590
Douglas Gregorc5046832009-04-27 18:38:38 +00001591//===----------------------------------------------------------------------===//
1592// Preprocessor Serialization
1593//===----------------------------------------------------------------------===//
1594
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001595static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1596 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1597 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1598 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1599 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1600 return X.first->getName().compare(Y.first->getName());
1601}
1602
Chris Lattnereeffaef2009-04-10 17:15:23 +00001603/// \brief Writes the block containing the serialized form of the
1604/// preprocessor.
1605///
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001606void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001607 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001608
Chris Lattner0af3ba12009-04-13 01:29:17 +00001609 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1610 if (PP.getCounterValue() != 0) {
1611 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001612 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001613 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001614 }
1615
1616 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001617 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001618
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001619 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001620 // FIXME: use diagnostics subsystem for localization etc.
1621 if (PP.SawDateOrTime())
1622 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001623
Douglas Gregor796d76a2010-10-20 22:00:55 +00001624
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001625 // Loop over all the macro definitions that are live at the end of the file,
1626 // emitting each to the PP section.
Douglas Gregoraae92242010-03-19 21:51:54 +00001627 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001628
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001629 // Construct the list of macro definitions that need to be serialized.
1630 llvm::SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
1631 MacrosToEmit;
1632 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor68051a72011-02-11 00:26:14 +00001633 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1634 E = PP.macro_end(Chain == 0);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001635 I != E; ++I) {
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001636 MacroDefinitionsSeen.insert(I->first);
1637 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1638 }
1639
1640 // Sort the set of macro definitions that need to be serialized by the
1641 // name of the macro, to provide a stable ordering.
1642 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1643 &compareMacroDefinitions);
1644
Douglas Gregor68051a72011-02-11 00:26:14 +00001645 // Resolve any identifiers that defined macros at the time they were
1646 // deserialized, adding them to the list of macros to emit (if appropriate).
1647 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1648 IdentifierInfo *Name
1649 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1650 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1651 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1652 }
1653
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001654 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1655 const IdentifierInfo *Name = MacrosToEmit[I].first;
1656 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor68051a72011-02-11 00:26:14 +00001657 if (!MI)
1658 continue;
1659
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001660 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001661 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001662 // Also skip macros from a AST file if we're chaining.
Douglas Gregoreb114da2010-10-01 01:03:07 +00001663
1664 // FIXME: There is a (probably minor) optimization we could do here, if
1665 // the macro comes from the original PCH but the identifier comes from a
1666 // chained PCH, by storing the offset into the original PCH rather than
1667 // writing the macro definition a second time.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001668 if (MI->isBuiltinMacro() ||
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001669 (Chain && Name->isFromAST() && MI->isFromAST()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001670 continue;
1671
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001672 AddIdentifierRef(Name, Record);
1673 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001674 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1675 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001676
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001677 unsigned Code;
1678 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001679 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001680 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001681 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001682
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001683 Record.push_back(MI->isC99Varargs());
1684 Record.push_back(MI->isGNUVarargs());
1685 Record.push_back(MI->getNumArgs());
1686 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1687 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001688 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001689 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001690
Douglas Gregoraae92242010-03-19 21:51:54 +00001691 // If we have a detailed preprocessing record, record the macro definition
1692 // ID that corresponds to this macro.
1693 if (PPRec)
1694 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001695
Douglas Gregor8f45df52009-04-16 22:23:12 +00001696 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001697 Record.clear();
1698
Chris Lattner2199f5b2009-04-10 18:08:30 +00001699 // Emit the tokens array.
1700 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1701 // Note that we know that the preprocessor does not have any annotation
1702 // tokens in it because they are created by the parser, and thus can't be
1703 // in a macro definition.
1704 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001705
Chris Lattner2199f5b2009-04-10 18:08:30 +00001706 Record.push_back(Tok.getLocation().getRawEncoding());
1707 Record.push_back(Tok.getLength());
1708
Chris Lattner2199f5b2009-04-10 18:08:30 +00001709 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1710 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001711 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001712 // FIXME: Should translate token kind to a stable encoding.
1713 Record.push_back(Tok.getKind());
1714 // FIXME: Should translate token flags to a stable encoding.
1715 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001716
Sebastian Redl539c5062010-08-18 23:57:32 +00001717 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001718 Record.clear();
1719 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001720 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001721 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001722 Stream.ExitBlock();
1723
1724 if (PPRec)
1725 WritePreprocessorDetail(*PPRec);
1726}
1727
1728void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
1729 if (PPRec.begin(Chain) == PPRec.end(Chain))
1730 return;
1731
1732 // Enter the preprocessor block.
1733 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001734
Douglas Gregoraae92242010-03-19 21:51:54 +00001735 // If the preprocessor has a preprocessing record, emit it.
1736 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001737 using namespace llvm;
1738
1739 // Set up the abbreviation for
1740 unsigned InclusionAbbrev = 0;
1741 {
1742 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1743 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
1744 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1745 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1746 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1747 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1748 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1749 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1750 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1751 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1752 }
1753
1754 unsigned IndexBase = Chain ? PPRec.getNumPreallocatedEntities() : 0;
1755 RecordData Record;
1756 for (PreprocessingRecord::iterator E = PPRec.begin(Chain),
1757 EEnd = PPRec.end(Chain);
1758 E != EEnd; ++E) {
1759 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001760
Douglas Gregor92a96f52011-02-08 21:58:10 +00001761 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1762 // Record this macro definition's location.
1763 MacroID ID = getMacroDefinitionID(MD);
1764
1765 // Don't write the macro definition if it is from another AST file.
1766 if (ID < FirstMacroID)
Douglas Gregoraae92242010-03-19 21:51:54 +00001767 continue;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001768
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001769 // Notify the serialization listener that we're serializing this entity.
1770 if (SerializationListener)
1771 SerializationListener->SerializedPreprocessedEntity(*E,
Douglas Gregor92a96f52011-02-08 21:58:10 +00001772 Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001773
Douglas Gregor92a96f52011-02-08 21:58:10 +00001774 unsigned Position = ID - FirstMacroID;
1775 if (Position != MacroDefinitionOffsets.size()) {
1776 if (Position > MacroDefinitionOffsets.size())
1777 MacroDefinitionOffsets.resize(Position + 1);
1778
1779 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
1780 } else
1781 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001782
Douglas Gregor92a96f52011-02-08 21:58:10 +00001783 Record.push_back(IndexBase + NumPreprocessingRecords++);
1784 Record.push_back(ID);
1785 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1786 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1787 AddIdentifierRef(MD->getName(), Record);
1788 AddSourceLocation(MD->getLocation(), Record);
1789 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1790 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00001791 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001792
Douglas Gregor92a96f52011-02-08 21:58:10 +00001793 // Notify the serialization listener that we're serializing this entity.
1794 if (SerializationListener)
1795 SerializationListener->SerializedPreprocessedEntity(*E,
1796 Stream.GetCurrentBitNo());
1797
1798 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1799 Record.push_back(IndexBase + NumPreprocessingRecords++);
1800 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1801 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1802 AddIdentifierRef(MI->getName(), Record);
1803 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1804 Stream.EmitRecord(PPD_MACRO_INSTANTIATION, Record);
1805 continue;
1806 }
1807
1808 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1809 Record.push_back(PPD_INCLUSION_DIRECTIVE);
1810 Record.push_back(IndexBase + NumPreprocessingRecords++);
1811 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1812 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1813 Record.push_back(ID->getFileName().size());
1814 Record.push_back(ID->wasInQuotes());
1815 Record.push_back(static_cast<unsigned>(ID->getKind()));
1816 llvm::SmallString<64> Buffer;
1817 Buffer += ID->getFileName();
1818 Buffer += ID->getFile()->getName();
1819 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1820 continue;
1821 }
1822
1823 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1824 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001825 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001826
Douglas Gregoraae92242010-03-19 21:51:54 +00001827 // Write the offsets table for the preprocessing record.
1828 if (NumPreprocessingRecords > 0) {
1829 // Write the offsets table for identifier IDs.
1830 using namespace llvm;
1831 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001832 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregoraae92242010-03-19 21:51:54 +00001833 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1834 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1835 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1836 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001837
Douglas Gregoraae92242010-03-19 21:51:54 +00001838 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001839 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001840 Record.push_back(NumPreprocessingRecords);
1841 Record.push_back(MacroDefinitionOffsets.size());
1842 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001843 data(MacroDefinitionOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00001844 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001845}
1846
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001847void ASTWriter::WritePragmaDiagnosticMappings(const Diagnostic &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001848 RecordData Record;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001849 for (Diagnostic::DiagStatePointsTy::const_iterator
1850 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1851 I != E; ++I) {
1852 const Diagnostic::DiagStatePoint &point = *I;
1853 if (point.Loc.isInvalid())
1854 continue;
1855
1856 Record.push_back(point.Loc.getRawEncoding());
1857 for (Diagnostic::DiagState::iterator
1858 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
1859 unsigned diag = I->first, map = I->second;
1860 if (map & 0x10) { // mapping from a diagnostic pragma.
1861 Record.push_back(diag);
1862 Record.push_back(map & 0x7);
1863 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001864 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001865 Record.push_back(-1); // mark the end of the diag/map pairs for this
1866 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001867 }
1868
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00001869 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001870 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001871}
1872
Anders Carlsson9bb83e82011-03-06 18:41:18 +00001873void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
1874 if (CXXBaseSpecifiersOffsets.empty())
1875 return;
1876
1877 RecordData Record;
1878
1879 // Create a blob abbreviation for the C++ base specifiers offsets.
1880 using namespace llvm;
1881
1882 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1883 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
1884 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
1885 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1886 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1887
1888 // Write the selector offsets table.
1889 Record.clear();
1890 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
1891 Record.push_back(CXXBaseSpecifiersOffsets.size());
1892 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001893 data(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00001894}
1895
Douglas Gregorc5046832009-04-27 18:38:38 +00001896//===----------------------------------------------------------------------===//
1897// Type Serialization
1898//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001899
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001900/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001901void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00001902 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001903 if (Idx.getIndex() == 0) // we haven't seen this type before.
1904 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00001905
Douglas Gregor9b3932c2010-10-05 18:37:06 +00001906 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00001907
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001908 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001909 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001910 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001911 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001912 else if (TypeOffsets.size() < Index) {
1913 TypeOffsets.resize(Index + 1);
1914 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001915 }
1916
1917 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001918
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001919 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001920 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001921
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001922 if (T.hasLocalNonFastQualifiers()) {
1923 Qualifiers Qs = T.getLocalQualifiers();
1924 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001925 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001926 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00001927 } else {
1928 switch (T->getTypeClass()) {
1929 // For all of the concrete, non-dependent types, call the
1930 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001931#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001932 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001933#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001934#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001935 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001936 }
1937
1938 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001939 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001940
1941 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001942 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001943}
1944
Douglas Gregorc5046832009-04-27 18:38:38 +00001945//===----------------------------------------------------------------------===//
1946// Declaration Serialization
1947//===----------------------------------------------------------------------===//
1948
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001949/// \brief Write the block containing all of the declaration IDs
1950/// lexically declared within the given DeclContext.
1951///
1952/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1953/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001954uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001955 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001956 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001957 return 0;
1958
Douglas Gregor8f45df52009-04-16 22:23:12 +00001959 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001960 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001961 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001962 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001963 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1964 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001965 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001966
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001967 ++NumLexicalDeclContexts;
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001968 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001969 return Offset;
1970}
1971
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001972void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001973 using namespace llvm;
1974 RecordData Record;
1975
1976 // Write the type offsets array
1977 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001978 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001979 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1980 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1981 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1982 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001983 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001984 Record.push_back(TypeOffsets.size());
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001985 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001986
1987 // Write the declaration offsets array
1988 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001989 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001990 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1991 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1992 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1993 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001994 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001995 Record.push_back(DeclOffsets.size());
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001996 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001997}
1998
Douglas Gregorc5046832009-04-27 18:38:38 +00001999//===----------------------------------------------------------------------===//
2000// Global Method Pool and Selector Serialization
2001//===----------------------------------------------------------------------===//
2002
Douglas Gregore84a9da2009-04-20 20:36:09 +00002003namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002004// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002005class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002006 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002007
2008public:
2009 typedef Selector key_type;
2010 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002011
Sebastian Redl834bb972010-08-04 17:20:04 +00002012 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002013 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002014 ObjCMethodList Instance, Factory;
2015 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002016 typedef const data_type& data_type_ref;
2017
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002018 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002019
Douglas Gregorc78d3462009-04-24 21:10:55 +00002020 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002021 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002022 }
Mike Stump11289f42009-09-09 15:08:12 +00002023
2024 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00002025 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
2026 data_type_ref Methods) {
2027 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2028 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002029 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2030 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002031 Method = Method->Next)
2032 if (Method->Method)
2033 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002034 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002035 Method = Method->Next)
2036 if (Method->Method)
2037 DataLen += 4;
2038 clang::io::Emit16(Out, DataLen);
2039 return std::make_pair(KeyLen, DataLen);
2040 }
Mike Stump11289f42009-09-09 15:08:12 +00002041
Douglas Gregor95c13f52009-04-25 17:48:32 +00002042 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00002043 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002044 assert((Start >> 32) == 0 && "Selector key offset too large");
2045 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002046 unsigned N = Sel.getNumArgs();
2047 clang::io::Emit16(Out, N);
2048 if (N == 0)
2049 N = 1;
2050 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00002051 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002052 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2053 }
Mike Stump11289f42009-09-09 15:08:12 +00002054
Douglas Gregorc78d3462009-04-24 21:10:55 +00002055 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002056 data_type_ref Methods, unsigned DataLen) {
2057 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00002058 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002059 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002060 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002061 Method = Method->Next)
2062 if (Method->Method)
2063 ++NumInstanceMethods;
2064
2065 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002066 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002067 Method = Method->Next)
2068 if (Method->Method)
2069 ++NumFactoryMethods;
2070
2071 clang::io::Emit16(Out, NumInstanceMethods);
2072 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00002073 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002074 Method = Method->Next)
2075 if (Method->Method)
2076 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002077 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002078 Method = Method->Next)
2079 if (Method->Method)
2080 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002081
2082 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002083 }
2084};
2085} // end anonymous namespace
2086
Sebastian Redla19a67f2010-08-03 21:58:15 +00002087/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002088///
2089/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002090/// in an on-disk hash table indexed by the selector. The hash table also
2091/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002092void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002093 using namespace llvm;
2094
Sebastian Redla19a67f2010-08-03 21:58:15 +00002095 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002096 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002097 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002098 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002099 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002100 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002101 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002102 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002103
Sebastian Redla19a67f2010-08-03 21:58:15 +00002104 // Create the on-disk hash table representation. We walk through every
2105 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002106 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002107 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002108 I = SelectorIDs.begin(), E = SelectorIDs.end();
2109 I != E; ++I) {
2110 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002111 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002112 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002113 I->second,
2114 ObjCMethodList(),
2115 ObjCMethodList()
2116 };
2117 if (F != SemaRef.MethodPool.end()) {
2118 Data.Instance = F->second.first;
2119 Data.Factory = F->second.second;
2120 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002121 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002122 // changed.
2123 if (Chain && I->second < FirstSelectorID) {
2124 // Selector already exists. Did it change?
2125 bool changed = false;
2126 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2127 M = M->Next) {
2128 if (M->Method->getPCHLevel() == 0)
2129 changed = true;
2130 }
2131 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2132 M = M->Next) {
2133 if (M->Method->getPCHLevel() == 0)
2134 changed = true;
2135 }
2136 if (!changed)
2137 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002138 } else if (Data.Instance.Method || Data.Factory.Method) {
2139 // A new method pool entry.
2140 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002141 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002142 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002143 }
2144
Douglas Gregorc78d3462009-04-24 21:10:55 +00002145 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002146 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002147 uint32_t BucketOffset;
2148 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002149 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002150 llvm::raw_svector_ostream Out(MethodPool);
2151 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002152 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002153 BucketOffset = Generator.Emit(Out, Trait);
2154 }
2155
2156 // Create a blob abbreviation
2157 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002158 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002159 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002160 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002161 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2162 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2163
Douglas Gregor95c13f52009-04-25 17:48:32 +00002164 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002165 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002166 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002167 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002168 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002169 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002170
2171 // Create a blob abbreviation for the selector table offsets.
2172 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002173 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002174 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor95c13f52009-04-25 17:48:32 +00002175 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2176 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2177
2178 // Write the selector offsets table.
2179 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002180 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002181 Record.push_back(SelectorOffsets.size());
2182 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002183 data(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002184 }
2185}
2186
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002187/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002188void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002189 using namespace llvm;
2190 if (SemaRef.ReferencedSelectors.empty())
2191 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002192
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002193 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002194
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002195 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002196 // very tricky to fix, and given that @selector shouldn't really appear in
2197 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002198 for (DenseMap<Selector, SourceLocation>::iterator S =
2199 SemaRef.ReferencedSelectors.begin(),
2200 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2201 Selector Sel = (*S).first;
2202 SourceLocation Loc = (*S).second;
2203 AddSelectorRef(Sel, Record);
2204 AddSourceLocation(Loc, Record);
2205 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002206 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002207}
2208
Douglas Gregorc5046832009-04-27 18:38:38 +00002209//===----------------------------------------------------------------------===//
2210// Identifier Table Serialization
2211//===----------------------------------------------------------------------===//
2212
Douglas Gregorc78d3462009-04-24 21:10:55 +00002213namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002214class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002215 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002216 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002217
Douglas Gregor1d583f22009-04-28 21:18:29 +00002218 /// \brief Determines whether this is an "interesting" identifier
2219 /// that needs a full IdentifierInfo structure written into the hash
2220 /// table.
2221 static bool isInterestingIdentifier(const IdentifierInfo *II) {
2222 return II->isPoisoned() ||
2223 II->isExtensionToken() ||
2224 II->hasMacroDefinition() ||
2225 II->getObjCOrBuiltinID() ||
2226 II->getFETokenInfo<void>();
2227 }
2228
Douglas Gregore84a9da2009-04-20 20:36:09 +00002229public:
2230 typedef const IdentifierInfo* key_type;
2231 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002232
Sebastian Redl539c5062010-08-18 23:57:32 +00002233 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002234 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002235
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002236 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00002237 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00002238
2239 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00002240 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00002241 }
Mike Stump11289f42009-09-09 15:08:12 +00002242
2243 std::pair<unsigned,unsigned>
2244 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002245 IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002246 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002247 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
2248 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00002249 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00002250 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00002251 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00002252 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002253 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2254 DEnd = IdentifierResolver::end();
2255 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00002256 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00002257 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00002258 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00002259 // We emit the key length after the data length so that every
2260 // string is preceded by a 16-bit length. This matches the PTH
2261 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00002262 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002263 return std::make_pair(KeyLen, DataLen);
2264 }
Mike Stump11289f42009-09-09 15:08:12 +00002265
2266 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00002267 unsigned KeyLen) {
2268 // Record the location of the key data. This is used when generating
2269 // the mapping from persistent IDs to strings.
2270 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002271 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002272 }
Mike Stump11289f42009-09-09 15:08:12 +00002273
2274 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002275 IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00002276 if (!isInterestingIdentifier(II)) {
2277 clang::io::Emit32(Out, ID << 1);
2278 return;
2279 }
Douglas Gregorb9256522009-04-28 21:32:13 +00002280
Douglas Gregor1d583f22009-04-28 21:18:29 +00002281 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002282 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002283 bool hasMacroDefinition =
2284 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00002285 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00002286 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002287 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
2288 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2289 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00002290 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002291 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00002292 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002293
Douglas Gregorc3366a52009-04-21 23:56:24 +00002294 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00002295 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00002296
Douglas Gregora868bbd2009-04-21 22:25:48 +00002297 // Emit the declaration IDs in reverse order, because the
2298 // IdentifierResolver provides the declarations as they would be
2299 // visible (e.g., the function "stat" would come before the struct
2300 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2301 // adds declarations to the end of the list (so we need to see the
2302 // struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00002303 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump11289f42009-09-09 15:08:12 +00002304 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00002305 IdentifierResolver::end());
2306 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
2307 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00002308 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00002309 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002310 }
2311};
2312} // end anonymous namespace
2313
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002314/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002315///
2316/// The identifier table consists of a blob containing string data
2317/// (the actual identifiers themselves) and a separate "offsets" index
2318/// that maps identifier IDs to locations within the blob.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002319void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002320 using namespace llvm;
2321
2322 // Create and write out the blob that contains the identifier
2323 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002324 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002325 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002326 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump11289f42009-09-09 15:08:12 +00002327
Douglas Gregore6648fb2009-04-28 20:33:11 +00002328 // Look for any identifiers that were named while processing the
2329 // headers, but are otherwise not needed. We add these to the hash
2330 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002331 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00002332 // file.
2333 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2334 IDEnd = PP.getIdentifierTable().end();
2335 ID != IDEnd; ++ID)
2336 getIdentifierRef(ID->second);
2337
Sebastian Redlff4a2952010-07-23 23:49:55 +00002338 // Create the on-disk hash table representation. We only store offsets
2339 // for identifiers that appear here for the first time.
2340 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002341 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002342 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2343 ID != IDEnd; ++ID) {
2344 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002345 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002346 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002347 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002348
Douglas Gregore84a9da2009-04-20 20:36:09 +00002349 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002350 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00002351 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002352 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002353 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002354 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002355 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002356 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002357 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002358 }
2359
2360 // Create a blob abbreviation
2361 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002362 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00002363 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002364 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00002365 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002366
2367 // Write the identifier table
2368 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002369 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002370 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002371 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002372 }
2373
2374 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00002375 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002376 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00002377 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2378 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2379 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2380
2381 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002382 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00002383 Record.push_back(IdentifierOffsets.size());
2384 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002385 data(IdentifierOffsets));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002386}
2387
Douglas Gregorc5046832009-04-27 18:38:38 +00002388//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002389// DeclContext's Name Lookup Table Serialization
2390//===----------------------------------------------------------------------===//
2391
2392namespace {
2393// Trait used for the on-disk hash table used in the method pool.
2394class ASTDeclContextNameLookupTrait {
2395 ASTWriter &Writer;
2396
2397public:
2398 typedef DeclarationName key_type;
2399 typedef key_type key_type_ref;
2400
2401 typedef DeclContext::lookup_result data_type;
2402 typedef const data_type& data_type_ref;
2403
2404 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2405
2406 unsigned ComputeHash(DeclarationName Name) {
2407 llvm::FoldingSetNodeID ID;
2408 ID.AddInteger(Name.getNameKind());
2409
2410 switch (Name.getNameKind()) {
2411 case DeclarationName::Identifier:
2412 ID.AddString(Name.getAsIdentifierInfo()->getName());
2413 break;
2414 case DeclarationName::ObjCZeroArgSelector:
2415 case DeclarationName::ObjCOneArgSelector:
2416 case DeclarationName::ObjCMultiArgSelector:
2417 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2418 break;
2419 case DeclarationName::CXXConstructorName:
2420 case DeclarationName::CXXDestructorName:
2421 case DeclarationName::CXXConversionFunctionName:
2422 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
2423 break;
2424 case DeclarationName::CXXOperatorName:
2425 ID.AddInteger(Name.getCXXOverloadedOperator());
2426 break;
2427 case DeclarationName::CXXLiteralOperatorName:
2428 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2429 case DeclarationName::CXXUsingDirective:
2430 break;
2431 }
2432
2433 return ID.ComputeHash();
2434 }
2435
2436 std::pair<unsigned,unsigned>
2437 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2438 data_type_ref Lookup) {
2439 unsigned KeyLen = 1;
2440 switch (Name.getNameKind()) {
2441 case DeclarationName::Identifier:
2442 case DeclarationName::ObjCZeroArgSelector:
2443 case DeclarationName::ObjCOneArgSelector:
2444 case DeclarationName::ObjCMultiArgSelector:
2445 case DeclarationName::CXXConstructorName:
2446 case DeclarationName::CXXDestructorName:
2447 case DeclarationName::CXXConversionFunctionName:
2448 case DeclarationName::CXXLiteralOperatorName:
2449 KeyLen += 4;
2450 break;
2451 case DeclarationName::CXXOperatorName:
2452 KeyLen += 1;
2453 break;
2454 case DeclarationName::CXXUsingDirective:
2455 break;
2456 }
2457 clang::io::Emit16(Out, KeyLen);
2458
2459 // 2 bytes for num of decls and 4 for each DeclID.
2460 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2461 clang::io::Emit16(Out, DataLen);
2462
2463 return std::make_pair(KeyLen, DataLen);
2464 }
2465
2466 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2467 using namespace clang::io;
2468
2469 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2470 Emit8(Out, Name.getNameKind());
2471 switch (Name.getNameKind()) {
2472 case DeclarationName::Identifier:
2473 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2474 break;
2475 case DeclarationName::ObjCZeroArgSelector:
2476 case DeclarationName::ObjCOneArgSelector:
2477 case DeclarationName::ObjCMultiArgSelector:
2478 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2479 break;
2480 case DeclarationName::CXXConstructorName:
2481 case DeclarationName::CXXDestructorName:
2482 case DeclarationName::CXXConversionFunctionName:
2483 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2484 break;
2485 case DeclarationName::CXXOperatorName:
2486 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2487 Emit8(Out, Name.getCXXOverloadedOperator());
2488 break;
2489 case DeclarationName::CXXLiteralOperatorName:
2490 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2491 break;
2492 case DeclarationName::CXXUsingDirective:
2493 break;
2494 }
2495 }
2496
2497 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2498 data_type Lookup, unsigned DataLen) {
2499 uint64_t Start = Out.tell(); (void)Start;
2500 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2501 for (; Lookup.first != Lookup.second; ++Lookup.first)
2502 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2503
2504 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2505 }
2506};
2507} // end anonymous namespace
2508
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002509/// \brief Write the block containing all of the declaration IDs
2510/// visible from the given DeclContext.
2511///
2512/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002513/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002514uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2515 DeclContext *DC) {
2516 if (DC->getPrimaryContext() != DC)
2517 return 0;
2518
2519 // Since there is no name lookup into functions or methods, don't bother to
2520 // build a visible-declarations table for these entities.
2521 if (DC->isFunctionOrMethod())
2522 return 0;
2523
2524 // If not in C++, we perform name lookup for the translation unit via the
2525 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2526 // FIXME: In C++ we need the visible declarations in order to "see" the
2527 // friend declarations, is there a way to do this without writing the table ?
2528 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2529 return 0;
2530
2531 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00002532 if (DC->hasExternalVisibleStorage())
2533 DC->MaterializeVisibleDeclsFromExternalStorage();
2534 else
2535 DC->lookup(DeclarationName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002536
2537 // Serialize the contents of the mapping used for lookup. Note that,
2538 // although we have two very different code paths, the serialized
2539 // representation is the same for both cases: a declaration name,
2540 // followed by a size, followed by references to the visible
2541 // declarations that have that name.
2542 uint64_t Offset = Stream.GetCurrentBitNo();
2543 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2544 if (!Map || Map->empty())
2545 return 0;
2546
2547 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2548 ASTDeclContextNameLookupTrait Trait(*this);
2549
2550 // Create the on-disk hash table representation.
2551 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2552 D != DEnd; ++D) {
2553 DeclarationName Name = D->first;
2554 DeclContext::lookup_result Result = D->second.getLookupResult();
2555 Generator.insert(Name, Result, Trait);
2556 }
2557
2558 // Create the on-disk hash table in a buffer.
2559 llvm::SmallString<4096> LookupTable;
2560 uint32_t BucketOffset;
2561 {
2562 llvm::raw_svector_ostream Out(LookupTable);
2563 // Make sure that no bucket is at offset 0
2564 clang::io::Emit32(Out, 0);
2565 BucketOffset = Generator.Emit(Out, Trait);
2566 }
2567
2568 // Write the lookup table
2569 RecordData Record;
2570 Record.push_back(DECL_CONTEXT_VISIBLE);
2571 Record.push_back(BucketOffset);
2572 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2573 LookupTable.str());
2574
2575 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2576 ++NumVisibleDeclContexts;
2577 return Offset;
2578}
2579
Sebastian Redla4071b42010-08-24 00:50:09 +00002580/// \brief Write an UPDATE_VISIBLE block for the given context.
2581///
2582/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2583/// DeclContext in a dependent AST file. As such, they only exist for the TU
2584/// (in C++) and for namespaces.
2585void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00002586 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2587 if (!Map || Map->empty())
2588 return;
2589
2590 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2591 ASTDeclContextNameLookupTrait Trait(*this);
2592
2593 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002594 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2595 D != DEnd; ++D) {
2596 DeclarationName Name = D->first;
2597 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002598 // For any name that appears in this table, the results are complete, i.e.
2599 // they overwrite results from previous PCHs. Merging is always a mess.
2600 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002601 }
2602
2603 // Create the on-disk hash table in a buffer.
2604 llvm::SmallString<4096> LookupTable;
2605 uint32_t BucketOffset;
2606 {
2607 llvm::raw_svector_ostream Out(LookupTable);
2608 // Make sure that no bucket is at offset 0
2609 clang::io::Emit32(Out, 0);
2610 BucketOffset = Generator.Emit(Out, Trait);
2611 }
2612
2613 // Write the lookup table
2614 RecordData Record;
2615 Record.push_back(UPDATE_VISIBLE);
2616 Record.push_back(getDeclID(cast<Decl>(DC)));
2617 Record.push_back(BucketOffset);
2618 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2619}
2620
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002621/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2622void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2623 RecordData Record;
2624 Record.push_back(Opts.fp_contract);
2625 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2626}
2627
2628/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2629void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
2630 if (!SemaRef.Context.getLangOptions().OpenCL)
2631 return;
2632
2633 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2634 RecordData Record;
2635#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2636#include "clang/Basic/OpenCLExtensions.def"
2637 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2638}
2639
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002640//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00002641// General Serialization Routines
2642//===----------------------------------------------------------------------===//
2643
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002644/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002645void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00002646 Record.push_back(Attrs.size());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002647 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2648 const Attr * A = *i;
2649 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2650 AddSourceLocation(A->getLocation(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002651
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002652#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00002653
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002654 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002655}
2656
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002657void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002658 Record.push_back(Str.size());
2659 Record.insert(Record.end(), Str.begin(), Str.end());
2660}
2661
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002662void ASTWriter::AddVersionTuple(const VersionTuple &Version,
2663 RecordDataImpl &Record) {
2664 Record.push_back(Version.getMajor());
2665 if (llvm::Optional<unsigned> Minor = Version.getMinor())
2666 Record.push_back(*Minor + 1);
2667 else
2668 Record.push_back(0);
2669 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
2670 Record.push_back(*Subminor + 1);
2671 else
2672 Record.push_back(0);
2673}
2674
Douglas Gregore84a9da2009-04-20 20:36:09 +00002675/// \brief Note that the identifier II occurs at the given offset
2676/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002677void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002678 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002679 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00002680 // up earlier in the chain and thus don't need an offset.
2681 if (ID >= FirstIdentID)
2682 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002683}
2684
Douglas Gregor95c13f52009-04-25 17:48:32 +00002685/// \brief Note that the selector Sel occurs at the given offset
2686/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002687void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00002688 unsigned ID = SelectorIDs[Sel];
2689 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00002690 // Don't record offsets for selectors that are also available in a different
2691 // file.
2692 if (ID < FirstSelectorID)
2693 return;
2694 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002695}
2696
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002697ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00002698 : Stream(Stream), Chain(0), SerializationListener(0),
2699 FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00002700 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002701 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor91096292010-10-02 19:29:26 +00002702 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2703 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002704 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00002705 NumVisibleDeclContexts(0),
2706 FirstCXXBaseSpecifiersID(1), NextCXXBaseSpecifiersID(1),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00002707 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00002708 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
2709 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
2710 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00002711 DeclTypedefAbbrev(0),
2712 DeclVarAbbrev(0), DeclFieldAbbrev(0),
2713 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002714{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002715}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002716
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002717void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002718 const std::string &OutputFile,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002719 const char *isysroot) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002720 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002721 Stream.Emit((unsigned)'C', 8);
2722 Stream.Emit((unsigned)'P', 8);
2723 Stream.Emit((unsigned)'C', 8);
2724 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002725
Chris Lattner28fa4e62009-04-26 22:26:21 +00002726 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002727
Sebastian Redl143413f2010-07-12 22:02:52 +00002728 if (Chain)
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002729 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002730 else
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002731 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile);
Sebastian Redl143413f2010-07-12 22:02:52 +00002732}
2733
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002734void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002735 const char *isysroot,
2736 const std::string &OutputFile) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002737 using namespace llvm;
2738
2739 ASTContext &Context = SemaRef.Context;
2740 Preprocessor &PP = SemaRef.PP;
2741
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002742 // The translation unit is the first declaration we'll emit.
2743 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlff4a2952010-07-23 23:49:55 +00002744 ++NextDeclID;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002745 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002746
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002747 // Make sure that we emit IdentifierInfos (and any attached
2748 // declarations) for builtins.
2749 {
2750 IdentifierTable &Table = PP.getIdentifierTable();
2751 llvm::SmallVector<const char *, 32> BuiltinNames;
2752 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2753 Context.getLangOptions().NoBuiltin);
2754 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2755 getIdentifierRef(&Table.get(BuiltinNames[I]));
2756 }
2757
Chris Lattner0c797362009-09-08 18:19:27 +00002758 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002759 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002760 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002761 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00002762 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2763 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00002764 }
Douglas Gregord4df8652009-04-22 22:02:47 +00002765
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002766 // Build a record containing all of the file scoped decls in this file.
2767 RecordData UnusedFileScopedDecls;
2768 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2769 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002770
Alexis Hunt27a761d2011-05-04 23:29:54 +00002771 RecordData DelegatingCtorDecls;
2772 for (unsigned i=0, e = SemaRef.DelegatingCtorDecls.size(); i != e; ++i)
2773 AddDeclRef(SemaRef.DelegatingCtorDecls[i], DelegatingCtorDecls);
2774
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002775 RecordData WeakUndeclaredIdentifiers;
2776 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2777 WeakUndeclaredIdentifiers.push_back(
2778 SemaRef.WeakUndeclaredIdentifiers.size());
2779 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2780 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2781 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2782 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2783 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2784 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2785 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2786 }
2787 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002788
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002789 // Build a record containing all of the locally-scoped external
2790 // declarations in this header file. Generally, this record will be
2791 // empty.
2792 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002793 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00002794 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002795 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002796 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2797 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2798 TD != TDEnd; ++TD)
2799 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2800
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002801 // Build a record containing all of the ext_vector declarations.
2802 RecordData ExtVectorDecls;
2803 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2804 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2805
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002806 // Build a record containing all of the VTable uses information.
2807 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00002808 if (!SemaRef.VTableUses.empty()) {
2809 VTableUses.push_back(SemaRef.VTableUses.size());
2810 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2811 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2812 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2813 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2814 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002815 }
2816
2817 // Build a record containing all of dynamic classes declarations.
2818 RecordData DynamicClasses;
2819 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2820 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2821
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002822 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002823 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002824 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002825 I = SemaRef.PendingInstantiations.begin(),
2826 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2827 AddDeclRef(I->first, PendingInstantiations);
2828 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002829 }
2830 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2831 "There are local ones at end of translation unit!");
2832
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002833 // Build a record containing some declaration references.
2834 RecordData SemaDeclRefs;
2835 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2836 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2837 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2838 }
2839
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002840 RecordData CUDASpecialDeclRefs;
2841 if (Context.getcudaConfigureCallDecl()) {
2842 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
2843 }
2844
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002845 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002846 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002847 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002848 WriteMetadata(Context, isysroot, OutputFile);
Sebastian Redl143413f2010-07-12 22:02:52 +00002849 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002850 if (StatCalls && !isysroot)
Douglas Gregor11cfd942010-07-12 23:48:14 +00002851 WriteStatCache(*StatCalls);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002852 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc277ad12009-07-18 15:33:26 +00002853 // Write the record of special types.
2854 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002855
Steve Naroffc277ad12009-07-18 15:33:26 +00002856 AddTypeRef(Context.getBuiltinVaListType(), Record);
2857 AddTypeRef(Context.getObjCIdType(), Record);
2858 AddTypeRef(Context.getObjCSelType(), Record);
2859 AddTypeRef(Context.getObjCProtoType(), Record);
2860 AddTypeRef(Context.getObjCClassType(), Record);
2861 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2862 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2863 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002864 AddTypeRef(Context.getjmp_bufType(), Record);
2865 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002866 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2867 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpd0153282009-10-20 02:12:22 +00002868 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002869 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002870 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2871 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002872 Record.push_back(Context.isInt128Installed());
Richard Smith02e85f32011-04-14 22:09:26 +00002873 AddTypeRef(Context.AutoDeductTy, Record);
2874 AddTypeRef(Context.AutoRRefDeductTy, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +00002875 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002876
Douglas Gregor1970d882009-04-26 03:49:13 +00002877 // Keep writing types and declarations until all types and
2878 // declarations have been written.
Douglas Gregor03412ba2011-06-03 02:27:19 +00002879 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002880 WriteDeclsBlockAbbrevs();
2881 while (!DeclTypesToEmit.empty()) {
2882 DeclOrType DOT = DeclTypesToEmit.front();
2883 DeclTypesToEmit.pop();
2884 if (DOT.isType())
2885 WriteType(DOT.getType());
2886 else
2887 WriteDecl(Context, DOT.getDecl());
2888 }
2889 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002890
Douglas Gregor45053152009-10-17 17:25:45 +00002891 WritePreprocessor(PP);
Douglas Gregor09b69892011-02-10 17:09:37 +00002892 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00002893 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002894 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002895 WriteIdentifierTable(PP);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002896 WriteFPPragmaOptions(SemaRef.getFPOptions());
2897 WriteOpenCLExtensions(SemaRef);
Douglas Gregor745ed142009-04-25 18:35:21 +00002898
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002899 WriteTypeDeclOffsets();
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002900 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00002901
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002902 WriteCXXBaseSpecifiersOffsets();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002903
Douglas Gregord4df8652009-04-22 22:02:47 +00002904 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002905 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002906 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002907
2908 // Write the record containing tentative definitions.
2909 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002910 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002911
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002912 // Write the record containing unused file scoped decls.
2913 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002914 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002915
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002916 // Write the record containing weak undeclared identifiers.
2917 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002918 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002919 WeakUndeclaredIdentifiers);
2920
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002921 // Write the record containing locally-scoped external definitions.
2922 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002923 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002924 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002925
2926 // Write the record containing ext_vector type names.
2927 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002928 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002929
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002930 // Write the record containing VTable uses information.
2931 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002932 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002933
2934 // Write the record containing dynamic classes declarations.
2935 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002936 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002937
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002938 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002939 if (!PendingInstantiations.empty())
2940 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002941
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002942 // Write the record containing declaration references of Sema.
2943 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002944 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002945
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002946 // Write the record containing CUDA-specific declaration references.
2947 if (!CUDASpecialDeclRefs.empty())
2948 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00002949
2950 // Write the delegating constructors.
2951 if (!DelegatingCtorDecls.empty())
2952 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002953
Douglas Gregor08f01292009-04-17 22:13:46 +00002954 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002955 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002956 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002957 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002958 Record.push_back(NumLexicalDeclContexts);
2959 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00002960 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002961 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002962}
2963
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002964void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002965 const char *isysroot) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002966 using namespace llvm;
2967
2968 ASTContext &Context = SemaRef.Context;
2969 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002970
Sebastian Redl143413f2010-07-12 22:02:52 +00002971 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002972 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002973 WriteMetadata(Context, isysroot, "");
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002974 if (StatCalls && !isysroot)
2975 WriteStatCache(*StatCalls);
2976 // FIXME: Source manager block should only write new stuff, which could be
2977 // done by tracking the largest ID in the chain
2978 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002979
2980 // The special types are in the chained PCH.
2981
2982 // We don't start with the translation unit, but with its decls that
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002983 // don't come from the chained PCH.
Sebastian Redl143413f2010-07-12 22:02:52 +00002984 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002985 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002986 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2987 E = TU->noload_decls_end();
Sebastian Redl143413f2010-07-12 22:02:52 +00002988 I != E; ++I) {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002989 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002990 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002991 else if ((*I)->isChangedSinceDeserialization())
2992 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl143413f2010-07-12 22:02:52 +00002993 }
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002994 // We also need to write a lexical updates block for the TU.
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002995 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002996 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002997 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2998 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2999 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00003000 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00003001 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00003002 data(NewGlobalDecls));
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003003 // And a visible updates block for the DeclContexts.
3004 Abv = new llvm::BitCodeAbbrev();
3005 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3006 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3007 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3008 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3009 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3010 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl143413f2010-07-12 22:02:52 +00003011
Sebastian Redl98912122010-07-27 23:01:28 +00003012 // Build a record containing all of the new tentative definitions in this
3013 // file, in TentativeDefinitions order.
3014 RecordData TentativeDefinitions;
3015 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
3016 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
3017 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
3018 }
3019
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003020 // Build a record containing all of the file scoped decls in this file.
3021 RecordData UnusedFileScopedDecls;
3022 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
3023 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
3024 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003025 }
3026
Alexis Hunt27a761d2011-05-04 23:29:54 +00003027 // Build a record containing all of the delegating constructor decls in this
3028 // file.
3029 RecordData DelegatingCtorDecls;
3030 for (unsigned i=0, e = SemaRef.DelegatingCtorDecls.size(); i != e; ++i) {
3031 if (SemaRef.DelegatingCtorDecls[i]->getPCHLevel() == 0)
3032 AddDeclRef(SemaRef.DelegatingCtorDecls[i], DelegatingCtorDecls);
3033 }
3034
Sebastian Redl08aca90252010-08-05 18:21:25 +00003035 // We write the entire table, overwriting the tables from the chain.
3036 RecordData WeakUndeclaredIdentifiers;
3037 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
3038 WeakUndeclaredIdentifiers.push_back(
3039 SemaRef.WeakUndeclaredIdentifiers.size());
3040 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
3041 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3042 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3043 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3044 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3045 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3046 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3047 }
3048 }
3049
Sebastian Redl98912122010-07-27 23:01:28 +00003050 // Build a record containing all of the locally-scoped external
3051 // declarations in this header file. Generally, this record will be
3052 // empty.
3053 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003054 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl98912122010-07-27 23:01:28 +00003055 // nondeterminstic!
3056 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
3057 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3058 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
3059 TD != TDEnd; ++TD) {
3060 if (TD->second->getPCHLevel() == 0)
3061 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3062 }
3063
3064 // Build a record containing all of the ext_vector declarations.
3065 RecordData ExtVectorDecls;
3066 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
3067 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
3068 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
3069 }
3070
Sebastian Redl08aca90252010-08-05 18:21:25 +00003071 // Build a record containing all of the VTable uses information.
3072 // We write everything here, because it's too hard to determine whether
3073 // a use is new to this part.
3074 RecordData VTableUses;
3075 if (!SemaRef.VTableUses.empty()) {
3076 VTableUses.push_back(SemaRef.VTableUses.size());
3077 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3078 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3079 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3080 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3081 }
3082 }
3083
3084 // Build a record containing all of dynamic classes declarations.
3085 RecordData DynamicClasses;
3086 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
3087 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
3088 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
3089
3090 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003091 RecordData PendingInstantiations;
Sebastian Redl08aca90252010-08-05 18:21:25 +00003092 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00003093 I = SemaRef.PendingInstantiations.begin(),
3094 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl14afaf02011-04-24 16:27:30 +00003095 AddDeclRef(I->first, PendingInstantiations);
3096 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003097 }
3098 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3099 "There are local ones at end of translation unit!");
3100
3101 // Build a record containing some declaration references.
3102 // It's not worth the effort to avoid duplication here.
3103 RecordData SemaDeclRefs;
3104 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3105 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3106 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3107 }
3108
Douglas Gregor03412ba2011-06-03 02:27:19 +00003109 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Sebastian Redl143413f2010-07-12 22:02:52 +00003110 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003111 for (DeclsToRewriteTy::iterator
3112 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
3113 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl143413f2010-07-12 22:02:52 +00003114 while (!DeclTypesToEmit.empty()) {
3115 DeclOrType DOT = DeclTypesToEmit.front();
3116 DeclTypesToEmit.pop();
3117 if (DOT.isType())
3118 WriteType(DOT.getType());
3119 else
3120 WriteDecl(Context, DOT.getDecl());
3121 }
3122 Stream.ExitBlock();
3123
Sebastian Redl98912122010-07-27 23:01:28 +00003124 WritePreprocessor(PP);
Sebastian Redl51c79d82010-08-04 22:21:29 +00003125 WriteSelectors(SemaRef);
3126 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlff4a2952010-07-23 23:49:55 +00003127 WriteIdentifierTable(PP);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003128 WriteFPPragmaOptions(SemaRef.getFPOptions());
3129 WriteOpenCLExtensions(SemaRef);
3130
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003131 WriteTypeDeclOffsets();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003132 // FIXME: For chained PCH only write the new mappings (we currently
3133 // write all of them again).
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003134 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl98912122010-07-27 23:01:28 +00003135
Anders Carlsson9bb83e82011-03-06 18:41:18 +00003136 WriteCXXBaseSpecifiersOffsets();
3137
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003138 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003139 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003140 RecordData FirstLatestDeclIDs;
3141 for (FirstLatestDeclMap::iterator
3142 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
3143 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
3144 "Expected first & second to be in different PCHs");
3145 AddDeclRef(I->first, FirstLatestDeclIDs);
3146 AddDeclRef(I->second, FirstLatestDeclIDs);
3147 }
3148 if (!FirstLatestDeclIDs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003149 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003150
Sebastian Redl98912122010-07-27 23:01:28 +00003151 // Write the record containing external, unnamed definitions.
3152 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003153 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00003154
3155 // Write the record containing tentative definitions.
3156 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003157 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00003158
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003159 // Write the record containing unused file scoped decls.
3160 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003161 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003162
Sebastian Redl08aca90252010-08-05 18:21:25 +00003163 // Write the record containing weak undeclared identifiers.
3164 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003165 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl08aca90252010-08-05 18:21:25 +00003166 WeakUndeclaredIdentifiers);
3167
Sebastian Redl98912122010-07-27 23:01:28 +00003168 // Write the record containing locally-scoped external definitions.
3169 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003170 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl98912122010-07-27 23:01:28 +00003171 LocallyScopedExternalDecls);
3172
3173 // Write the record containing ext_vector type names.
3174 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003175 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003176
Sebastian Redl08aca90252010-08-05 18:21:25 +00003177 // Write the record containing VTable uses information.
3178 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003179 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003180
3181 // Write the record containing dynamic classes declarations.
3182 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003183 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003184
3185 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003186 if (!PendingInstantiations.empty())
3187 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003188
3189 // Write the record containing declaration references of Sema.
3190 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003191 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003192
3193 // Write the delegating constructors.
3194 if (!DelegatingCtorDecls.empty())
3195 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003196
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003197 // Write the updates to DeclContexts.
3198 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3199 I = UpdatedDeclContexts.begin(),
3200 E = UpdatedDeclContexts.end();
Sebastian Redla4071b42010-08-24 00:50:09 +00003201 I != E; ++I)
3202 WriteDeclContextVisibleUpdate(*I);
3203
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003204 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003205
Sebastian Redl98912122010-07-27 23:01:28 +00003206 Record.clear();
3207 Record.push_back(NumStatements);
3208 Record.push_back(NumMacros);
3209 Record.push_back(NumLexicalDeclContexts);
3210 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003211 WriteDeclReplacementsBlock();
Sebastian Redl539c5062010-08-18 23:57:32 +00003212 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl143413f2010-07-12 22:02:52 +00003213 Stream.ExitBlock();
3214}
3215
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003216void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003217 if (DeclUpdates.empty())
3218 return;
3219
3220 RecordData OffsetsRecord;
Douglas Gregor03412ba2011-06-03 02:27:19 +00003221 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003222 for (DeclUpdateMap::iterator
3223 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3224 const Decl *D = I->first;
3225 UpdateRecord &URec = I->second;
3226
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00003227 if (DeclsToRewrite.count(D))
3228 continue; // The decl will be written completely,no need to store updates.
3229
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003230 uint64_t Offset = Stream.GetCurrentBitNo();
3231 Stream.EmitRecord(DECL_UPDATES, URec);
3232
3233 OffsetsRecord.push_back(GetDeclRef(D));
3234 OffsetsRecord.push_back(Offset);
3235 }
3236 Stream.ExitBlock();
3237 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3238}
3239
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003240void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003241 if (ReplacedDecls.empty())
3242 return;
3243
3244 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003245 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003246 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
3247 Record.push_back(I->first);
3248 Record.push_back(I->second);
3249 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003250 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003251}
3252
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003253void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003254 Record.push_back(Loc.getRawEncoding());
3255}
3256
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003257void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003258 AddSourceLocation(Range.getBegin(), Record);
3259 AddSourceLocation(Range.getEnd(), Record);
3260}
3261
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003262void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003263 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00003264 const uint64_t *Words = Value.getRawData();
3265 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003266}
3267
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003268void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00003269 Record.push_back(Value.isUnsigned());
3270 AddAPInt(Value, Record);
3271}
3272
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003273void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00003274 AddAPInt(Value.bitcastToAPInt(), Record);
3275}
3276
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003277void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003278 Record.push_back(getIdentifierRef(II));
3279}
3280
Sebastian Redl539c5062010-08-18 23:57:32 +00003281IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003282 if (II == 0)
3283 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003284
Sebastian Redl539c5062010-08-18 23:57:32 +00003285 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003286 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00003287 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003288 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003289}
3290
Sebastian Redl50e26582010-09-15 19:54:06 +00003291MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregoraae92242010-03-19 21:51:54 +00003292 if (MD == 0)
3293 return 0;
Sebastian Redl50e26582010-09-15 19:54:06 +00003294
3295 MacroID &ID = MacroDefinitions[MD];
Douglas Gregoraae92242010-03-19 21:51:54 +00003296 if (ID == 0)
Douglas Gregor91096292010-10-02 19:29:26 +00003297 ID = NextMacroID++;
Douglas Gregoraae92242010-03-19 21:51:54 +00003298 return ID;
3299}
3300
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003301void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003302 Record.push_back(getSelectorRef(SelRef));
3303}
3304
Sebastian Redl539c5062010-08-18 23:57:32 +00003305SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003306 if (Sel.getAsOpaquePtr() == 0) {
3307 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00003308 }
3309
Sebastian Redl539c5062010-08-18 23:57:32 +00003310 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00003311 if (SID == 0 && Chain) {
3312 // This might trigger a ReadSelector callback, which will set the ID for
3313 // this selector.
3314 Chain->LoadSelector(Sel);
3315 }
Steve Naroff2ddea052009-04-23 10:39:46 +00003316 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003317 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00003318 }
Sebastian Redl834bb972010-08-04 17:20:04 +00003319 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00003320}
3321
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003322void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00003323 AddDeclRef(Temp->getDestructor(), Record);
3324}
3325
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003326void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3327 CXXBaseSpecifier const *BasesEnd,
3328 RecordDataImpl &Record) {
3329 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3330 CXXBaseSpecifiersToWrite.push_back(
3331 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3332 Bases, BasesEnd));
3333 Record.push_back(NextCXXBaseSpecifiersID++);
3334}
3335
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003336void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003337 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003338 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003339 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00003340 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003341 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00003342 break;
3343 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003344 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00003345 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003346 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00003347 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003348 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003349 break;
3350 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00003351 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003352 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003353 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003354 break;
John McCall0ad16662009-10-29 08:12:44 +00003355 case TemplateArgument::Null:
3356 case TemplateArgument::Integral:
3357 case TemplateArgument::Declaration:
3358 case TemplateArgument::Pack:
3359 break;
3360 }
3361}
3362
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003363void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003364 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003365 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003366
3367 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3368 bool InfoHasSameExpr
3369 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3370 Record.push_back(InfoHasSameExpr);
3371 if (InfoHasSameExpr)
3372 return; // Avoid storing the same expr twice.
3373 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003374 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3375 Record);
3376}
3377
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003378void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3379 RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00003380 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00003381 AddTypeRef(QualType(), Record);
3382 return;
3383 }
3384
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003385 AddTypeLoc(TInfo->getTypeLoc(), Record);
3386}
3387
3388void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3389 AddTypeRef(TL.getType(), Record);
3390
John McCall8f115c62009-10-16 21:56:05 +00003391 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003392 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003393 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00003394}
3395
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003396void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00003397 Record.push_back(GetOrCreateTypeID(T));
3398}
3399
3400TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003401 return MakeTypeID(T,
3402 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3403}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003404
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003405TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003406 return MakeTypeID(T,
3407 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003408}
3409
3410TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3411 if (T.isNull())
3412 return TypeIdx();
3413 assert(!T.getLocalFastQualifiers());
3414
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00003415 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003416 if (Idx.getIndex() == 0) {
Douglas Gregor1970d882009-04-26 03:49:13 +00003417 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00003418 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003419 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003420 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00003421 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003422 return Idx;
3423}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003424
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003425TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003426 if (T.isNull())
3427 return TypeIdx();
3428 assert(!T.getLocalFastQualifiers());
3429
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003430 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3431 assert(I != TypeIdxs.end() && "Type not emitted!");
3432 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003433}
3434
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003435void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003436 Record.push_back(GetDeclRef(D));
3437}
3438
Sebastian Redl539c5062010-08-18 23:57:32 +00003439DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003440 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003441 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003442 }
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003443 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00003444 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00003445 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003446 // We haven't seen this declaration before. Give it a new ID and
3447 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00003448 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003449 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003450 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3451 // We don't add it to the replacement collection here, because we don't
3452 // have the offset yet.
3453 DeclTypesToEmit.push(const_cast<Decl *>(D));
3454 // Reset the flag, so that we don't add this decl multiple times.
3455 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003456 }
3457
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003458 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003459}
3460
Sebastian Redl539c5062010-08-18 23:57:32 +00003461DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00003462 if (D == 0)
3463 return 0;
3464
3465 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3466 return DeclIDs[D];
3467}
3468
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003469void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00003470 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003471 Record.push_back(Name.getNameKind());
3472 switch (Name.getNameKind()) {
3473 case DeclarationName::Identifier:
3474 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3475 break;
3476
3477 case DeclarationName::ObjCZeroArgSelector:
3478 case DeclarationName::ObjCOneArgSelector:
3479 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00003480 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003481 break;
3482
3483 case DeclarationName::CXXConstructorName:
3484 case DeclarationName::CXXDestructorName:
3485 case DeclarationName::CXXConversionFunctionName:
3486 AddTypeRef(Name.getCXXNameType(), Record);
3487 break;
3488
3489 case DeclarationName::CXXOperatorName:
3490 Record.push_back(Name.getCXXOverloadedOperator());
3491 break;
3492
Alexis Hunt3d221f22009-11-29 07:34:05 +00003493 case DeclarationName::CXXLiteralOperatorName:
3494 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3495 break;
3496
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003497 case DeclarationName::CXXUsingDirective:
3498 // No extra data to emit
3499 break;
3500 }
3501}
Chris Lattnerca025db2010-05-07 21:43:38 +00003502
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003503void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003504 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003505 switch (Name.getNameKind()) {
3506 case DeclarationName::CXXConstructorName:
3507 case DeclarationName::CXXDestructorName:
3508 case DeclarationName::CXXConversionFunctionName:
3509 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3510 break;
3511
3512 case DeclarationName::CXXOperatorName:
3513 AddSourceLocation(
3514 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3515 Record);
3516 AddSourceLocation(
3517 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3518 Record);
3519 break;
3520
3521 case DeclarationName::CXXLiteralOperatorName:
3522 AddSourceLocation(
3523 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3524 Record);
3525 break;
3526
3527 case DeclarationName::Identifier:
3528 case DeclarationName::ObjCZeroArgSelector:
3529 case DeclarationName::ObjCOneArgSelector:
3530 case DeclarationName::ObjCMultiArgSelector:
3531 case DeclarationName::CXXUsingDirective:
3532 break;
3533 }
3534}
3535
3536void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003537 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003538 AddDeclarationName(NameInfo.getName(), Record);
3539 AddSourceLocation(NameInfo.getLoc(), Record);
3540 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3541}
3542
3543void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003544 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00003545 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003546 Record.push_back(Info.NumTemplParamLists);
3547 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3548 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3549}
3550
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003551void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003552 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003553 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00003554 // typically accommodate the vast majority.
Chris Lattnerca025db2010-05-07 21:43:38 +00003555 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3556
3557 // Push each of the NNS's onto a stack for serialization in reverse order.
3558 while (NNS) {
3559 NestedNames.push_back(NNS);
3560 NNS = NNS->getPrefix();
3561 }
3562
3563 Record.push_back(NestedNames.size());
3564 while(!NestedNames.empty()) {
3565 NNS = NestedNames.pop_back_val();
3566 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3567 Record.push_back(Kind);
3568 switch (Kind) {
3569 case NestedNameSpecifier::Identifier:
3570 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3571 break;
3572
3573 case NestedNameSpecifier::Namespace:
3574 AddDeclRef(NNS->getAsNamespace(), Record);
3575 break;
3576
Douglas Gregor7b26ff92011-02-24 02:36:08 +00003577 case NestedNameSpecifier::NamespaceAlias:
3578 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
3579 break;
3580
Chris Lattnerca025db2010-05-07 21:43:38 +00003581 case NestedNameSpecifier::TypeSpec:
3582 case NestedNameSpecifier::TypeSpecWithTemplate:
3583 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3584 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3585 break;
3586
3587 case NestedNameSpecifier::Global:
3588 // Don't need to write an associated value.
3589 break;
3590 }
3591 }
3592}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003593
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003594void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
3595 RecordDataImpl &Record) {
3596 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00003597 // typically accommodate the vast majority.
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003598 llvm::SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
3599
3600 // Push each of the nested-name-specifiers's onto a stack for
3601 // serialization in reverse order.
3602 while (NNS) {
3603 NestedNames.push_back(NNS);
3604 NNS = NNS.getPrefix();
3605 }
3606
3607 Record.push_back(NestedNames.size());
3608 while(!NestedNames.empty()) {
3609 NNS = NestedNames.pop_back_val();
3610 NestedNameSpecifier::SpecifierKind Kind
3611 = NNS.getNestedNameSpecifier()->getKind();
3612 Record.push_back(Kind);
3613 switch (Kind) {
3614 case NestedNameSpecifier::Identifier:
3615 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
3616 AddSourceRange(NNS.getLocalSourceRange(), Record);
3617 break;
3618
3619 case NestedNameSpecifier::Namespace:
3620 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
3621 AddSourceRange(NNS.getLocalSourceRange(), Record);
3622 break;
3623
3624 case NestedNameSpecifier::NamespaceAlias:
3625 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
3626 AddSourceRange(NNS.getLocalSourceRange(), Record);
3627 break;
3628
3629 case NestedNameSpecifier::TypeSpec:
3630 case NestedNameSpecifier::TypeSpecWithTemplate:
3631 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3632 AddTypeLoc(NNS.getTypeLoc(), Record);
3633 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3634 break;
3635
3636 case NestedNameSpecifier::Global:
3637 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3638 break;
3639 }
3640 }
3641}
3642
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003643void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003644 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003645 Record.push_back(Kind);
3646 switch (Kind) {
3647 case TemplateName::Template:
3648 AddDeclRef(Name.getAsTemplateDecl(), Record);
3649 break;
3650
3651 case TemplateName::OverloadedTemplate: {
3652 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3653 Record.push_back(OvT->size());
3654 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3655 I != E; ++I)
3656 AddDeclRef(*I, Record);
3657 break;
3658 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003659
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003660 case TemplateName::QualifiedTemplate: {
3661 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3662 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3663 Record.push_back(QualT->hasTemplateKeyword());
3664 AddDeclRef(QualT->getTemplateDecl(), Record);
3665 break;
3666 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003667
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003668 case TemplateName::DependentTemplate: {
3669 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3670 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3671 Record.push_back(DepT->isIdentifier());
3672 if (DepT->isIdentifier())
3673 AddIdentifierRef(DepT->getIdentifier(), Record);
3674 else
3675 Record.push_back(DepT->getOperator());
3676 break;
3677 }
Douglas Gregor5590be02011-01-15 06:45:20 +00003678
3679 case TemplateName::SubstTemplateTemplateParmPack: {
3680 SubstTemplateTemplateParmPackStorage *SubstPack
3681 = Name.getAsSubstTemplateTemplateParmPack();
3682 AddDeclRef(SubstPack->getParameterPack(), Record);
3683 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
3684 break;
3685 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003686 }
3687}
3688
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003689void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003690 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003691 Record.push_back(Arg.getKind());
3692 switch (Arg.getKind()) {
3693 case TemplateArgument::Null:
3694 break;
3695 case TemplateArgument::Type:
3696 AddTypeRef(Arg.getAsType(), Record);
3697 break;
3698 case TemplateArgument::Declaration:
3699 AddDeclRef(Arg.getAsDecl(), Record);
3700 break;
3701 case TemplateArgument::Integral:
3702 AddAPSInt(*Arg.getAsIntegral(), Record);
3703 AddTypeRef(Arg.getIntegralType(), Record);
3704 break;
3705 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00003706 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3707 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003708 case TemplateArgument::TemplateExpansion:
3709 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregore1d60df2011-01-14 23:41:42 +00003710 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3711 Record.push_back(*NumExpansions + 1);
3712 else
3713 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003714 break;
3715 case TemplateArgument::Expression:
3716 AddStmt(Arg.getAsExpr());
3717 break;
3718 case TemplateArgument::Pack:
3719 Record.push_back(Arg.pack_size());
3720 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3721 I != E; ++I)
3722 AddTemplateArgument(*I, Record);
3723 break;
3724 }
3725}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003726
3727void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003728ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003729 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003730 assert(TemplateParams && "No TemplateParams!");
3731 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3732 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3733 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3734 Record.push_back(TemplateParams->size());
3735 for (TemplateParameterList::const_iterator
3736 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3737 P != PEnd; ++P)
3738 AddDeclRef(*P, Record);
3739}
3740
3741/// \brief Emit a template argument list.
3742void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003743ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003744 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003745 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003746 Record.push_back(TemplateArgs->size());
3747 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003748 AddTemplateArgument(TemplateArgs->get(i), Record);
3749}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003750
3751
3752void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003753ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003754 Record.push_back(Set.size());
3755 for (UnresolvedSetImpl::const_iterator
3756 I = Set.begin(), E = Set.end(); I != E; ++I) {
3757 AddDeclRef(I.getDecl(), Record);
3758 Record.push_back(I.getAccess());
3759 }
3760}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003761
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003762void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003763 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003764 Record.push_back(Base.isVirtual());
3765 Record.push_back(Base.isBaseOfClass());
3766 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00003767 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00003768 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003769 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00003770 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3771 : SourceLocation(),
3772 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003773}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003774
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003775void ASTWriter::FlushCXXBaseSpecifiers() {
3776 RecordData Record;
3777 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3778 Record.clear();
3779
3780 // Record the offset of this base-specifier set.
3781 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3782 if (Index == CXXBaseSpecifiersOffsets.size())
3783 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3784 else {
3785 if (Index > CXXBaseSpecifiersOffsets.size())
3786 CXXBaseSpecifiersOffsets.resize(Index + 1);
3787 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3788 }
3789
3790 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3791 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3792 Record.push_back(BEnd - B);
3793 for (; B != BEnd; ++B)
3794 AddCXXBaseSpecifier(*B, Record);
3795 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00003796
3797 // Flush any expressions that were written as part of the base specifiers.
3798 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003799 }
3800
3801 CXXBaseSpecifiersToWrite.clear();
3802}
3803
Alexis Hunt1d792652011-01-08 20:30:50 +00003804void ASTWriter::AddCXXCtorInitializers(
3805 const CXXCtorInitializer * const *CtorInitializers,
3806 unsigned NumCtorInitializers,
3807 RecordDataImpl &Record) {
3808 Record.push_back(NumCtorInitializers);
3809 for (unsigned i=0; i != NumCtorInitializers; ++i) {
3810 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003811
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003812 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00003813 Record.push_back(CTOR_INITIALIZER_BASE);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003814 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3815 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00003816 } else if (Init->isDelegatingInitializer()) {
3817 Record.push_back(CTOR_INITIALIZER_DELEGATING);
3818 AddDeclRef(Init->getTargetConstructor(), Record);
3819 } else if (Init->isMemberInitializer()){
3820 Record.push_back(CTOR_INITIALIZER_MEMBER);
3821 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003822 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00003823 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
3824 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003825 }
Francois Pichetd583da02010-12-04 09:14:42 +00003826
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003827 AddSourceLocation(Init->getMemberLocation(), Record);
3828 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003829 AddSourceLocation(Init->getLParenLoc(), Record);
3830 AddSourceLocation(Init->getRParenLoc(), Record);
3831 Record.push_back(Init->isWritten());
3832 if (Init->isWritten()) {
3833 Record.push_back(Init->getSourceOrder());
3834 } else {
3835 Record.push_back(Init->getNumArrayIndices());
3836 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3837 AddDeclRef(Init->getArrayIndex(i), Record);
3838 }
3839 }
3840}
3841
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003842void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3843 assert(D->DefinitionData);
3844 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3845 Record.push_back(Data.UserDeclaredConstructor);
3846 Record.push_back(Data.UserDeclaredCopyConstructor);
3847 Record.push_back(Data.UserDeclaredCopyAssignment);
3848 Record.push_back(Data.UserDeclaredDestructor);
3849 Record.push_back(Data.Aggregate);
3850 Record.push_back(Data.PlainOldData);
3851 Record.push_back(Data.Empty);
3852 Record.push_back(Data.Polymorphic);
3853 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00003854 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00003855 Record.push_back(Data.HasNoNonEmptyBases);
3856 Record.push_back(Data.HasPrivateFields);
3857 Record.push_back(Data.HasProtectedFields);
3858 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00003859 Record.push_back(Data.HasMutableFields);
Alexis Huntf479f1b2011-05-09 18:22:59 +00003860 Record.push_back(Data.HasTrivialDefaultConstructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00003861 Record.push_back(Data.HasConstExprNonCopyMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003862 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruthad7d4042011-04-23 23:10:33 +00003863 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003864 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruthad7d4042011-04-23 23:10:33 +00003865 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003866 Record.push_back(Data.HasTrivialDestructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00003867 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003868 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00003869 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003870 Record.push_back(Data.DeclaredDefaultConstructor);
3871 Record.push_back(Data.DeclaredCopyConstructor);
3872 Record.push_back(Data.DeclaredCopyAssignment);
3873 Record.push_back(Data.DeclaredDestructor);
3874
3875 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003876 if (Data.NumBases > 0)
3877 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3878 Record);
3879
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003880 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3881 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003882 if (Data.NumVBases > 0)
3883 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3884 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003885
3886 AddUnresolvedSet(Data.Conversions, Record);
3887 AddUnresolvedSet(Data.VisibleConversions, Record);
3888 // Data.Definition is the owning decl, no need to write it.
3889 AddDeclRef(Data.FirstFriend, Record);
3890}
3891
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003892void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00003893 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003894 assert(!Chain && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00003895 assert(FirstDeclID == NextDeclID &&
3896 FirstTypeID == NextTypeID &&
3897 FirstIdentID == NextIdentID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00003898 FirstSelectorID == NextSelectorID &&
Douglas Gregor91096292010-10-02 19:29:26 +00003899 FirstMacroID == NextMacroID &&
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003900 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00003901 "Setting chain after writing has started.");
3902 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003903
3904 FirstDeclID += Chain->getTotalNumDecls();
3905 FirstTypeID += Chain->getTotalNumTypes();
3906 FirstIdentID += Chain->getTotalNumIdentifiers();
3907 FirstSelectorID += Chain->getTotalNumSelectors();
3908 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003909 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003910 NextDeclID = FirstDeclID;
3911 NextTypeID = FirstTypeID;
3912 NextIdentID = FirstIdentID;
3913 NextSelectorID = FirstSelectorID;
3914 NextMacroID = FirstMacroID;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003915 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00003916}
3917
Sebastian Redl539c5062010-08-18 23:57:32 +00003918void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00003919 IdentifierIDs[II] = ID;
Douglas Gregor68051a72011-02-11 00:26:14 +00003920 if (II->hasMacroDefinition())
3921 DeserializedMacroNames.push_back(II);
Sebastian Redlff4a2952010-07-23 23:49:55 +00003922}
3923
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003924void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003925 // Always take the highest-numbered type index. This copes with an interesting
3926 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003927 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003928 // keep the higher-numbered entry so that we can properly write it out to
3929 // the AST file.
3930 TypeIdx &StoredIdx = TypeIdxs[T];
3931 if (Idx.getIndex() >= StoredIdx.getIndex())
3932 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003933}
3934
Sebastian Redl539c5062010-08-18 23:57:32 +00003935void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003936 DeclIDs[D] = ID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003937}
Sebastian Redl834bb972010-08-04 17:20:04 +00003938
Sebastian Redl539c5062010-08-18 23:57:32 +00003939void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003940 SelectorIDs[S] = ID;
3941}
Douglas Gregor91096292010-10-02 19:29:26 +00003942
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003943void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00003944 MacroDefinition *MD) {
3945 MacroDefinitions[MD] = ID;
3946}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003947
3948void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3949 assert(D->isDefinition());
3950 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3951 // We are interested when a PCH decl is modified.
3952 if (RD->getPCHLevel() > 0) {
3953 // A forward reference was mutated into a definition. Rewrite it.
3954 // FIXME: This happens during template instantiation, should we
3955 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003956 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003957 }
3958
3959 for (CXXRecordDecl::redecl_iterator
3960 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3961 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3962 if (Redecl == RD)
3963 continue;
3964
3965 // We are interested when a PCH decl is modified.
3966 if (Redecl->getPCHLevel() > 0) {
3967 UpdateRecord &Record = DeclUpdates[Redecl];
3968 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3969 assert(Redecl->DefinitionData);
3970 assert(Redecl->DefinitionData->Definition == D);
3971 AddDeclRef(D, Record); // the DefinitionDecl
3972 }
3973 }
3974 }
3975}
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003976void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3977 // TU and namespaces are handled elsewhere.
3978 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3979 return;
3980
3981 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3982 return; // Not a source decl added to a DeclContext from PCH.
3983
3984 AddUpdatedDeclContext(DC);
3985}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003986
3987void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3988 assert(D->isImplicit());
3989 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3990 return; // Not a source member added to a class from PCH.
3991 if (!isa<CXXMethodDecl>(D))
3992 return; // We are interested in lazily declared implicit methods.
3993
3994 // A decl coming from PCH was modified.
3995 assert(RD->isDefinition());
3996 UpdateRecord &Record = DeclUpdates[RD];
3997 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3998 AddDeclRef(D, Record);
3999}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004000
4001void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4002 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00004003 // The specializations set is kept in the canonical template.
4004 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004005 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
4006 return; // Not a source specialization added to a template from PCH.
4007
4008 UpdateRecord &Record = DeclUpdates[TD];
4009 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
4010 AddDeclRef(D, Record);
4011}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00004012
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004013void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4014 const FunctionDecl *D) {
4015 // The specializations set is kept in the canonical template.
4016 TD = TD->getCanonicalDecl();
4017 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
4018 return; // Not a source specialization added to a template from PCH.
4019
4020 UpdateRecord &Record = DeclUpdates[TD];
4021 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
4022 AddDeclRef(D, Record);
4023}
4024
Sebastian Redlab238a72011-04-24 16:28:06 +00004025void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
4026 if (D->getPCHLevel() == 0)
4027 return; // Declaration not imported from PCH.
4028
4029 // Implicit decl from a PCH was defined.
4030 // FIXME: Should implicit definition be a separate FunctionDecl?
4031 RewriteDecl(D);
4032}
4033
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004034void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
4035 if (D->getPCHLevel() == 0)
4036 return;
4037
4038 // Since the actual instantiation is delayed, this really means that we need
4039 // to update the instantiation location.
4040 UpdateRecord &Record = DeclUpdates[D];
4041 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4042 AddSourceLocation(
4043 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4044}
4045
Douglas Gregorf88e35b2010-11-30 06:16:57 +00004046ASTSerializationListener::~ASTSerializationListener() { }