blob: 25bff0aba1d5ee60a3fee3b47c1d530c345df7dd [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"
Douglas Gregor925296b2011-07-19 16:10:42 +000048#include <algorithm>
Chris Lattner225dd6c2009-04-11 18:40:46 +000049#include <cstdio>
Douglas Gregor09b69892011-02-10 17:09:37 +000050#include <string.h>
Douglas Gregor925296b2011-07-19 16:10:42 +000051#include <utility>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000052using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000053using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000054
Sebastian Redl3df5a082010-07-30 17:03:48 +000055template <typename T, typename Allocator>
Chris Lattner0e62c1c2011-07-23 10:55:15 +000056static StringRef data(const std::vector<T, Allocator> &v) {
57 if (v.empty()) return StringRef();
58 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000059 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000060}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000061
62template <typename T>
Chris Lattner0e62c1c2011-07-23 10:55:15 +000063static StringRef data(const SmallVectorImpl<T> &v) {
64 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000065 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000066}
67
Douglas Gregoref84c4b2009-04-09 22:27:44 +000068//===----------------------------------------------------------------------===//
69// Type serialization
70//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000071
Douglas Gregoref84c4b2009-04-09 22:27:44 +000072namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000073 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000074 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000075 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000076
77 public:
78 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000079 TypeCode Code;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000080
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000081 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000082 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000083
84 void VisitArrayType(const ArrayType *T);
85 void VisitFunctionType(const FunctionType *T);
86 void VisitTagType(const TagType *T);
87
88#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
89#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000090#include "clang/AST/TypeNodes.def"
91 };
92}
93
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000094void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000095 assert(false && "Built-in types are never serialized");
96}
97
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000098void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000099 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000100 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000101}
102
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000103void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000104 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000105 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000106}
107
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000108void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000109 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000110 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000111}
112
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000113void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000114 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
115 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000116 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000117}
118
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000119void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000120 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000121 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000122}
123
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000124void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000125 Writer.AddTypeRef(T->getPointeeType(), Record);
126 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000127 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000128}
129
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000130void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000131 Writer.AddTypeRef(T->getElementType(), Record);
132 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000133 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000134}
135
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000136void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000137 VisitArrayType(T);
138 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000139 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000140}
141
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000142void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000143 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000144 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000145}
146
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000147void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000148 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000149 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
150 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000151 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000152 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000153}
154
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000155void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000156 Writer.AddTypeRef(T->getElementType(), Record);
157 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000158 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000159 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000160}
161
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000162void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000163 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000164 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000165}
166
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000167void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000168 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000169 FunctionType::ExtInfo C = T->getExtInfo();
170 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000171 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000172 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000173 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000174 Record.push_back(C.getCC());
John McCall31168b02011-06-15 23:02:42 +0000175 Record.push_back(C.getProducesResult());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000176}
177
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000178void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000179 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000180 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000181}
182
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000183void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000184 VisitFunctionType(T);
185 Record.push_back(T->getNumArgs());
186 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
187 Writer.AddTypeRef(T->getArgType(I), Record);
188 Record.push_back(T->isVariadic());
189 Record.push_back(T->getTypeQuals());
Douglas Gregordb9d6642011-01-26 05:01:58 +0000190 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000191 Record.push_back(T->getExceptionSpecType());
192 if (T->getExceptionSpecType() == EST_Dynamic) {
193 Record.push_back(T->getNumExceptions());
194 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
195 Writer.AddTypeRef(T->getExceptionType(I), Record);
196 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
197 Writer.AddStmt(T->getNoexceptExpr());
198 }
Sebastian Redl539c5062010-08-18 23:57:32 +0000199 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000200}
201
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000202void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000203 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000204 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000205}
John McCallb96ec562009-12-04 22:46:56 +0000206
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000207void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000208 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000209 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
210 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000211 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000212}
213
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000214void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000215 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000216 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000217}
218
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000219void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000220 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000221 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000222}
223
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000224void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson81df7b82009-06-24 19:06:50 +0000225 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000226 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000227}
228
Alexis Hunte852b102011-05-24 22:41:36 +0000229void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
230 Writer.AddTypeRef(T->getBaseType(), Record);
231 Writer.AddTypeRef(T->getUnderlyingType(), Record);
232 Record.push_back(T->getUTTKind());
233 Code = TYPE_UNARY_TRANSFORM;
234}
235
Richard Smith30482bc2011-02-20 03:19:35 +0000236void ASTTypeWriter::VisitAutoType(const AutoType *T) {
237 Writer.AddTypeRef(T->getDeducedType(), Record);
238 Code = TYPE_AUTO;
239}
240
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000241void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000242 Record.push_back(T->isDependentType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000243 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000244 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000245 "Cannot serialize in the middle of a type definition");
246}
247
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000248void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000249 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000250 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000251}
252
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000253void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000254 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000255 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000256}
257
John McCall81904512011-01-06 01:58:22 +0000258void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
259 Writer.AddTypeRef(T->getModifiedType(), Record);
260 Writer.AddTypeRef(T->getEquivalentType(), Record);
261 Record.push_back(T->getAttrKind());
262 Code = TYPE_ATTRIBUTED;
263}
264
Mike Stump11289f42009-09-09 15:08:12 +0000265void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000266ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000267 const SubstTemplateTypeParmType *T) {
268 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
269 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000270 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000271}
272
273void
Douglas Gregorada4b792011-01-14 02:55:32 +0000274ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
275 const SubstTemplateTypeParmPackType *T) {
276 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
277 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
278 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
279}
280
281void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000282ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000283 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000284 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000285 Writer.AddTemplateName(T->getTemplateName(), Record);
286 Record.push_back(T->getNumArgs());
287 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
288 ArgI != ArgE; ++ArgI)
289 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000290 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
291 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000292 : T->getCanonicalTypeInternal(),
293 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000294 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000295}
296
297void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000298ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000299 VisitArrayType(T);
300 Writer.AddStmt(T->getSizeExpr());
301 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000302 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000303}
304
305void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000306ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000307 const DependentSizedExtVectorType *T) {
308 // FIXME: Serialize this type (C++ only)
309 assert(false && "Cannot serialize dependent sized extended vector types");
310}
311
312void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000313ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000314 Record.push_back(T->getDepth());
315 Record.push_back(T->getIndex());
316 Record.push_back(T->isParameterPack());
Chandler Carruth08836322011-05-01 00:51:33 +0000317 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000318 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000319}
320
321void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000322ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000323 Record.push_back(T->getKeyword());
324 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
325 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000326 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
327 : T->getCanonicalTypeInternal(),
328 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000329 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000330}
331
332void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000333ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000334 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000335 Record.push_back(T->getKeyword());
336 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
337 Writer.AddIdentifierRef(T->getIdentifier(), Record);
338 Record.push_back(T->getNumArgs());
339 for (DependentTemplateSpecializationType::iterator
340 I = T->begin(), E = T->end(); I != E; ++I)
341 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000342 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000343}
344
Douglas Gregord2fa7662010-12-20 02:24:11 +0000345void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
346 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000347 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
348 Record.push_back(*NumExpansions + 1);
349 else
350 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000351 Code = TYPE_PACK_EXPANSION;
352}
353
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000354void ASTTypeWriter::VisitParenType(const ParenType *T) {
355 Writer.AddTypeRef(T->getInnerType(), Record);
356 Code = TYPE_PAREN;
357}
358
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000359void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000360 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000361 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
362 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000363 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000364}
365
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000366void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCalle78aac42010-03-10 03:28:59 +0000367 Writer.AddDeclRef(T->getDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000368 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000369 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000370}
371
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000372void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1c283312010-08-11 12:19:30 +0000373 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000374 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000375}
376
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000377void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000378 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000379 Record.push_back(T->getNumProtocols());
John McCall8b07ec22010-05-15 11:32:37 +0000380 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000381 E = T->qual_end(); I != E; ++I)
382 Writer.AddDeclRef(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000383 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000384}
385
Steve Narofffb4330f2009-06-17 22:40:22 +0000386void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000387ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000388 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000389 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000390}
391
John McCall8f115c62009-10-16 21:56:05 +0000392namespace {
393
394class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000395 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000396 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000397
398public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000399 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000400 : Writer(Writer), Record(Record) { }
401
John McCall17001972009-10-18 01:05:36 +0000402#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000403#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000404 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000405#include "clang/AST/TypeLocNodes.def"
406
John McCall17001972009-10-18 01:05:36 +0000407 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
408 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000409};
410
411}
412
John McCall17001972009-10-18 01:05:36 +0000413void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
414 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000415}
John McCall17001972009-10-18 01:05:36 +0000416void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000417 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
418 if (TL.needsExtraLocalData()) {
419 Record.push_back(TL.getWrittenTypeSpec());
420 Record.push_back(TL.getWrittenSignSpec());
421 Record.push_back(TL.getWrittenWidthSpec());
422 Record.push_back(TL.hasModeAttr());
423 }
John McCall8f115c62009-10-16 21:56:05 +0000424}
John McCall17001972009-10-18 01:05:36 +0000425void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
426 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000427}
John McCall17001972009-10-18 01:05:36 +0000428void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
429 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000430}
John McCall17001972009-10-18 01:05:36 +0000431void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
432 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000433}
John McCall17001972009-10-18 01:05:36 +0000434void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
435 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000436}
John McCall17001972009-10-18 01:05:36 +0000437void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
438 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000439}
John McCall17001972009-10-18 01:05:36 +0000440void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
441 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnara509357842011-03-05 14:42:21 +0000442 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000443}
John McCall17001972009-10-18 01:05:36 +0000444void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
445 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
446 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
447 Record.push_back(TL.getSizeExpr() ? 1 : 0);
448 if (TL.getSizeExpr())
449 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000450}
John McCall17001972009-10-18 01:05:36 +0000451void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
452 VisitArrayTypeLoc(TL);
453}
454void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
455 VisitArrayTypeLoc(TL);
456}
457void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
458 VisitArrayTypeLoc(TL);
459}
460void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
461 DependentSizedArrayTypeLoc TL) {
462 VisitArrayTypeLoc(TL);
463}
464void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
465 DependentSizedExtVectorTypeLoc TL) {
466 Writer.AddSourceLocation(TL.getNameLoc(), Record);
467}
468void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
469 Writer.AddSourceLocation(TL.getNameLoc(), Record);
470}
471void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
472 Writer.AddSourceLocation(TL.getNameLoc(), Record);
473}
474void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000475 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
476 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Douglas Gregor7fb25412010-10-01 18:44:50 +0000477 Record.push_back(TL.getTrailingReturn());
John McCall17001972009-10-18 01:05:36 +0000478 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
479 Writer.AddDeclRef(TL.getArg(i), Record);
480}
481void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
482 VisitFunctionTypeLoc(TL);
483}
484void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
485 VisitFunctionTypeLoc(TL);
486}
John McCallb96ec562009-12-04 22:46:56 +0000487void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
488 Writer.AddSourceLocation(TL.getNameLoc(), Record);
489}
John McCall17001972009-10-18 01:05:36 +0000490void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
491 Writer.AddSourceLocation(TL.getNameLoc(), Record);
492}
493void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000494 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
495 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
496 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000497}
498void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000499 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
500 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
501 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
502 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000503}
504void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
505 Writer.AddSourceLocation(TL.getNameLoc(), Record);
506}
Alexis Hunte852b102011-05-24 22:41:36 +0000507void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
508 Writer.AddSourceLocation(TL.getKWLoc(), Record);
509 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
510 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
511 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
512}
Richard Smith30482bc2011-02-20 03:19:35 +0000513void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
514 Writer.AddSourceLocation(TL.getNameLoc(), Record);
515}
John McCall17001972009-10-18 01:05:36 +0000516void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
517 Writer.AddSourceLocation(TL.getNameLoc(), Record);
518}
519void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
520 Writer.AddSourceLocation(TL.getNameLoc(), Record);
521}
John McCall81904512011-01-06 01:58:22 +0000522void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
523 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
524 if (TL.hasAttrOperand()) {
525 SourceRange range = TL.getAttrOperandParensRange();
526 Writer.AddSourceLocation(range.getBegin(), Record);
527 Writer.AddSourceLocation(range.getEnd(), Record);
528 }
529 if (TL.hasAttrExprOperand()) {
530 Expr *operand = TL.getAttrExprOperand();
531 Record.push_back(operand ? 1 : 0);
532 if (operand) Writer.AddStmt(operand);
533 } else if (TL.hasAttrEnumOperand()) {
534 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
535 }
536}
John McCall17001972009-10-18 01:05:36 +0000537void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
538 Writer.AddSourceLocation(TL.getNameLoc(), Record);
539}
John McCallcebee162009-10-18 09:09:24 +0000540void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
541 SubstTemplateTypeParmTypeLoc TL) {
542 Writer.AddSourceLocation(TL.getNameLoc(), Record);
543}
Douglas Gregorada4b792011-01-14 02:55:32 +0000544void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
545 SubstTemplateTypeParmPackTypeLoc TL) {
546 Writer.AddSourceLocation(TL.getNameLoc(), Record);
547}
John McCall17001972009-10-18 01:05:36 +0000548void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
549 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +0000550 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
551 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
552 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
553 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000554 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
555 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000556}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000557void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
558 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
559 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
560}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000561void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000562 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000563 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000564}
John McCalle78aac42010-03-10 03:28:59 +0000565void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
566 Writer.AddSourceLocation(TL.getNameLoc(), Record);
567}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000568void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000569 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000570 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000571 Writer.AddSourceLocation(TL.getNameLoc(), Record);
572}
John McCallc392f372010-06-11 00:33:02 +0000573void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
574 DependentTemplateSpecializationTypeLoc TL) {
575 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000576 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000577 Writer.AddSourceLocation(TL.getNameLoc(), Record);
578 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
579 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
580 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000581 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
582 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000583}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000584void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
585 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
586}
John McCall17001972009-10-18 01:05:36 +0000587void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
588 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000589}
590void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
591 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000592 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
593 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
594 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
595 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000596}
John McCallfc93cf92009-10-22 22:37:11 +0000597void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
598 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000599}
John McCall8f115c62009-10-16 21:56:05 +0000600
Chris Lattner19cea4e2009-04-22 05:57:30 +0000601//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000602// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000603//===----------------------------------------------------------------------===//
604
Chris Lattner28fa4e62009-04-26 22:26:21 +0000605static void EmitBlockID(unsigned ID, const char *Name,
606 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000607 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000608 Record.clear();
609 Record.push_back(ID);
610 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
611
612 // Emit the block name if present.
613 if (Name == 0 || Name[0] == 0) return;
614 Record.clear();
615 while (*Name)
616 Record.push_back(*Name++);
617 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
618}
619
620static void EmitRecordID(unsigned ID, const char *Name,
621 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000622 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000623 Record.clear();
624 Record.push_back(ID);
625 while (*Name)
626 Record.push_back(*Name++);
627 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000628}
629
630static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000631 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000632#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000633 RECORD(STMT_STOP);
634 RECORD(STMT_NULL_PTR);
635 RECORD(STMT_NULL);
636 RECORD(STMT_COMPOUND);
637 RECORD(STMT_CASE);
638 RECORD(STMT_DEFAULT);
639 RECORD(STMT_LABEL);
640 RECORD(STMT_IF);
641 RECORD(STMT_SWITCH);
642 RECORD(STMT_WHILE);
643 RECORD(STMT_DO);
644 RECORD(STMT_FOR);
645 RECORD(STMT_GOTO);
646 RECORD(STMT_INDIRECT_GOTO);
647 RECORD(STMT_CONTINUE);
648 RECORD(STMT_BREAK);
649 RECORD(STMT_RETURN);
650 RECORD(STMT_DECL);
651 RECORD(STMT_ASM);
652 RECORD(EXPR_PREDEFINED);
653 RECORD(EXPR_DECL_REF);
654 RECORD(EXPR_INTEGER_LITERAL);
655 RECORD(EXPR_FLOATING_LITERAL);
656 RECORD(EXPR_IMAGINARY_LITERAL);
657 RECORD(EXPR_STRING_LITERAL);
658 RECORD(EXPR_CHARACTER_LITERAL);
659 RECORD(EXPR_PAREN);
660 RECORD(EXPR_UNARY_OPERATOR);
661 RECORD(EXPR_SIZEOF_ALIGN_OF);
662 RECORD(EXPR_ARRAY_SUBSCRIPT);
663 RECORD(EXPR_CALL);
664 RECORD(EXPR_MEMBER);
665 RECORD(EXPR_BINARY_OPERATOR);
666 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
667 RECORD(EXPR_CONDITIONAL_OPERATOR);
668 RECORD(EXPR_IMPLICIT_CAST);
669 RECORD(EXPR_CSTYLE_CAST);
670 RECORD(EXPR_COMPOUND_LITERAL);
671 RECORD(EXPR_EXT_VECTOR_ELEMENT);
672 RECORD(EXPR_INIT_LIST);
673 RECORD(EXPR_DESIGNATED_INIT);
674 RECORD(EXPR_IMPLICIT_VALUE_INIT);
675 RECORD(EXPR_VA_ARG);
676 RECORD(EXPR_ADDR_LABEL);
677 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000678 RECORD(EXPR_CHOOSE);
679 RECORD(EXPR_GNU_NULL);
680 RECORD(EXPR_SHUFFLE_VECTOR);
681 RECORD(EXPR_BLOCK);
682 RECORD(EXPR_BLOCK_DECL_REF);
Peter Collingbourne91147592011-04-15 00:35:48 +0000683 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000684 RECORD(EXPR_OBJC_STRING_LITERAL);
685 RECORD(EXPR_OBJC_ENCODE);
686 RECORD(EXPR_OBJC_SELECTOR_EXPR);
687 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
688 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
689 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
690 RECORD(EXPR_OBJC_KVC_REF_EXPR);
691 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000692 RECORD(STMT_OBJC_FOR_COLLECTION);
693 RECORD(STMT_OBJC_CATCH);
694 RECORD(STMT_OBJC_FINALLY);
695 RECORD(STMT_OBJC_AT_TRY);
696 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
697 RECORD(STMT_OBJC_AT_THROW);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000698 RECORD(EXPR_CXX_OPERATOR_CALL);
699 RECORD(EXPR_CXX_CONSTRUCT);
700 RECORD(EXPR_CXX_STATIC_CAST);
701 RECORD(EXPR_CXX_DYNAMIC_CAST);
702 RECORD(EXPR_CXX_REINTERPRET_CAST);
703 RECORD(EXPR_CXX_CONST_CAST);
704 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
705 RECORD(EXPR_CXX_BOOL_LITERAL);
706 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000707 RECORD(EXPR_CXX_TYPEID_EXPR);
708 RECORD(EXPR_CXX_TYPEID_TYPE);
709 RECORD(EXPR_CXX_UUIDOF_EXPR);
710 RECORD(EXPR_CXX_UUIDOF_TYPE);
711 RECORD(EXPR_CXX_THIS);
712 RECORD(EXPR_CXX_THROW);
713 RECORD(EXPR_CXX_DEFAULT_ARG);
714 RECORD(EXPR_CXX_BIND_TEMPORARY);
715 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
716 RECORD(EXPR_CXX_NEW);
717 RECORD(EXPR_CXX_DELETE);
718 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
719 RECORD(EXPR_EXPR_WITH_CLEANUPS);
720 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
721 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
722 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
723 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
724 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
725 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
726 RECORD(EXPR_CXX_NOEXCEPT);
727 RECORD(EXPR_OPAQUE_VALUE);
728 RECORD(EXPR_BINARY_TYPE_TRAIT);
729 RECORD(EXPR_PACK_EXPANSION);
730 RECORD(EXPR_SIZEOF_PACK);
731 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000732 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000733#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000734}
Mike Stump11289f42009-09-09 15:08:12 +0000735
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000736void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000737 RecordData Record;
738 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000739
Sebastian Redl539c5062010-08-18 23:57:32 +0000740#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
741#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000742
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000743 // AST Top-Level Block.
Sebastian Redlf1642042010-08-18 23:57:22 +0000744 BLOCK(AST_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000745 RECORD(ORIGINAL_FILE_NAME);
Douglas Gregora3b20262011-05-06 21:43:30 +0000746 RECORD(ORIGINAL_FILE_ID);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000747 RECORD(TYPE_OFFSET);
748 RECORD(DECL_OFFSET);
749 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000750 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000751 RECORD(IDENTIFIER_OFFSET);
752 RECORD(IDENTIFIER_TABLE);
753 RECORD(EXTERNAL_DEFINITIONS);
754 RECORD(SPECIAL_TYPES);
755 RECORD(STATISTICS);
756 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000757 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000758 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
759 RECORD(SELECTOR_OFFSETS);
760 RECORD(METHOD_POOL);
761 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000762 RECORD(SOURCE_LOCATION_OFFSETS);
763 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000764 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000765 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek17437132010-01-22 20:59:36 +0000766 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregoraae92242010-03-19 21:51:54 +0000767 RECORD(MACRO_DEFINITION_OFFSETS);
Douglas Gregor29cc6422011-08-17 21:07:30 +0000768 RECORD(IMPORTS);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000769 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000770 RECORD(TU_UPDATE_LEXICAL);
771 RECORD(REDECLS_UPDATE_LATEST);
772 RECORD(SEMA_DECL_REFS);
773 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
774 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
775 RECORD(DECL_REPLACEMENTS);
776 RECORD(UPDATE_VISIBLE);
777 RECORD(DECL_UPDATE_OFFSETS);
778 RECORD(DECL_UPDATES);
779 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
780 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000781 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000782 RECORD(HEADER_SEARCH_TABLE);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000783 RECORD(ORIGINAL_PCH_DIR);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000784 RECORD(FP_PRAGMA_OPTIONS);
785 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +0000786 RECORD(DELEGATING_CTORS);
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000787 RECORD(FILE_SOURCE_LOCATION_OFFSETS);
788 RECORD(KNOWN_NAMESPACES);
Douglas Gregor78d0b572011-08-04 16:39:39 +0000789 RECORD(MODULE_OFFSET_MAP);
790 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregor09b69892011-02-10 17:09:37 +0000791
Chris Lattner28fa4e62009-04-26 22:26:21 +0000792 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000793 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000794 RECORD(SM_SLOC_FILE_ENTRY);
795 RECORD(SM_SLOC_BUFFER_ENTRY);
796 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000797 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump11289f42009-09-09 15:08:12 +0000798
Chris Lattner28fa4e62009-04-26 22:26:21 +0000799 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000800 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000801 RECORD(PP_MACRO_OBJECT_LIKE);
802 RECORD(PP_MACRO_FUNCTION_LIKE);
803 RECORD(PP_TOKEN);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000804
Douglas Gregor12bfa382009-10-17 00:13:19 +0000805 // Decls and Types block.
806 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000807 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000808 RECORD(TYPE_COMPLEX);
809 RECORD(TYPE_POINTER);
810 RECORD(TYPE_BLOCK_POINTER);
811 RECORD(TYPE_LVALUE_REFERENCE);
812 RECORD(TYPE_RVALUE_REFERENCE);
813 RECORD(TYPE_MEMBER_POINTER);
814 RECORD(TYPE_CONSTANT_ARRAY);
815 RECORD(TYPE_INCOMPLETE_ARRAY);
816 RECORD(TYPE_VARIABLE_ARRAY);
817 RECORD(TYPE_VECTOR);
818 RECORD(TYPE_EXT_VECTOR);
819 RECORD(TYPE_FUNCTION_PROTO);
820 RECORD(TYPE_FUNCTION_NO_PROTO);
821 RECORD(TYPE_TYPEDEF);
822 RECORD(TYPE_TYPEOF_EXPR);
823 RECORD(TYPE_TYPEOF);
824 RECORD(TYPE_RECORD);
825 RECORD(TYPE_ENUM);
826 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000827 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000828 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000829 RECORD(TYPE_DECLTYPE);
830 RECORD(TYPE_ELABORATED);
831 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
832 RECORD(TYPE_UNRESOLVED_USING);
833 RECORD(TYPE_INJECTED_CLASS_NAME);
834 RECORD(TYPE_OBJC_OBJECT);
835 RECORD(TYPE_TEMPLATE_TYPE_PARM);
836 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
837 RECORD(TYPE_DEPENDENT_NAME);
838 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
839 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
840 RECORD(TYPE_PAREN);
841 RECORD(TYPE_PACK_EXPANSION);
842 RECORD(TYPE_ATTRIBUTED);
843 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000844 RECORD(DECL_TYPEDEF);
845 RECORD(DECL_ENUM);
846 RECORD(DECL_RECORD);
847 RECORD(DECL_ENUM_CONSTANT);
848 RECORD(DECL_FUNCTION);
849 RECORD(DECL_OBJC_METHOD);
850 RECORD(DECL_OBJC_INTERFACE);
851 RECORD(DECL_OBJC_PROTOCOL);
852 RECORD(DECL_OBJC_IVAR);
853 RECORD(DECL_OBJC_AT_DEFS_FIELD);
854 RECORD(DECL_OBJC_CLASS);
855 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
856 RECORD(DECL_OBJC_CATEGORY);
857 RECORD(DECL_OBJC_CATEGORY_IMPL);
858 RECORD(DECL_OBJC_IMPLEMENTATION);
859 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
860 RECORD(DECL_OBJC_PROPERTY);
861 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000862 RECORD(DECL_FIELD);
863 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000864 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000865 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000866 RECORD(DECL_FILE_SCOPE_ASM);
867 RECORD(DECL_BLOCK);
868 RECORD(DECL_CONTEXT_LEXICAL);
869 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000870 RECORD(DECL_NAMESPACE);
871 RECORD(DECL_NAMESPACE_ALIAS);
872 RECORD(DECL_USING);
873 RECORD(DECL_USING_SHADOW);
874 RECORD(DECL_USING_DIRECTIVE);
875 RECORD(DECL_UNRESOLVED_USING_VALUE);
876 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
877 RECORD(DECL_LINKAGE_SPEC);
878 RECORD(DECL_CXX_RECORD);
879 RECORD(DECL_CXX_METHOD);
880 RECORD(DECL_CXX_CONSTRUCTOR);
881 RECORD(DECL_CXX_DESTRUCTOR);
882 RECORD(DECL_CXX_CONVERSION);
883 RECORD(DECL_ACCESS_SPEC);
884 RECORD(DECL_FRIEND);
885 RECORD(DECL_FRIEND_TEMPLATE);
886 RECORD(DECL_CLASS_TEMPLATE);
887 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
888 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
889 RECORD(DECL_FUNCTION_TEMPLATE);
890 RECORD(DECL_TEMPLATE_TYPE_PARM);
891 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
892 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
893 RECORD(DECL_STATIC_ASSERT);
894 RECORD(DECL_CXX_BASE_SPECIFIERS);
895 RECORD(DECL_INDIRECTFIELD);
896 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
897
Douglas Gregor03412ba2011-06-03 02:27:19 +0000898 // Statements and Exprs can occur in the Decls and Types block.
899 AddStmtsExprs(Stream, Record);
900
Douglas Gregor92a96f52011-02-08 21:58:10 +0000901 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000902 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +0000903 RECORD(PPD_MACRO_DEFINITION);
904 RECORD(PPD_INCLUSION_DIRECTIVE);
905
Chris Lattner28fa4e62009-04-26 22:26:21 +0000906#undef RECORD
907#undef BLOCK
908 Stream.ExitBlock();
909}
910
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000911/// \brief Adjusts the given filename to only write out the portion of the
912/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000913///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000914/// \param Filename the file name to adjust.
915///
916/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
917/// the returned filename will be adjusted by this system root.
918///
919/// \returns either the original filename (if it needs no adjustment) or the
920/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000921static const char *
Douglas Gregorc567ba22011-07-22 16:35:34 +0000922adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000923 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000924
Douglas Gregorc567ba22011-07-22 16:35:34 +0000925 if (isysroot.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000926 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000927
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000928 // Verify that the filename and the system root have the same prefix.
929 unsigned Pos = 0;
Douglas Gregorc567ba22011-07-22 16:35:34 +0000930 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000931 if (Filename[Pos] != isysroot[Pos])
932 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000933
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000934 // We hit the end of the filename before we hit the end of the system root.
935 if (!Filename[Pos])
936 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000937
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000938 // If the file name has a '/' at the current position, skip over the '/'.
939 // We distinguish sysroot-based includes from absolute includes by the
940 // absence of '/' at the beginning of sysroot-based includes.
941 if (Filename[Pos] == '/')
942 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000943
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000944 return Filename + Pos;
945}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000946
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000947/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Douglas Gregorc567ba22011-07-22 16:35:34 +0000948void ASTWriter::WriteMetadata(ASTContext &Context, StringRef isysroot,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000949 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000950 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000951
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000952 // Metadata
953 const TargetInfo &Target = Context.Target;
954 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Douglas Gregor29cc6422011-08-17 21:07:30 +0000955 MetaAbbrev->Add(BitCodeAbbrevOp(METADATA));
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000956 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
957 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000958 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
959 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
960 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Douglas Gregor29cc6422011-08-17 21:07:30 +0000961 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000962 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000963
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000964 RecordData Record;
Douglas Gregor29cc6422011-08-17 21:07:30 +0000965 Record.push_back(METADATA);
Sebastian Redl539c5062010-08-18 23:57:32 +0000966 Record.push_back(VERSION_MAJOR);
967 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000968 Record.push_back(CLANG_VERSION_MAJOR);
969 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregorc567ba22011-07-22 16:35:34 +0000970 Record.push_back(!isysroot.empty());
Douglas Gregor29cc6422011-08-17 21:07:30 +0000971 const std::string &Triple = Target.getTriple().getTriple();
972 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, Triple);
973
974 if (Chain) {
Douglas Gregor29cc6422011-08-17 21:07:30 +0000975 serialization::ModuleManager &Mgr = Chain->getModuleManager();
976 llvm::SmallVector<char, 128> ModulePaths;
977 Record.clear();
Douglas Gregordf0c1512011-08-18 04:12:04 +0000978
979 for (ModuleManager::ModuleIterator M = Mgr.begin(), MEnd = Mgr.end();
980 M != MEnd; ++M) {
981 // Skip modules that weren't directly imported.
982 if (!(*M)->isDirectlyImported())
983 continue;
984
985 Record.push_back((unsigned)(*M)->Kind); // FIXME: Stable encoding
986 // FIXME: Write import location, once it matters.
987 // FIXME: This writes the absolute path for AST files we depend on.
988 const std::string &FileName = (*M)->FileName;
989 Record.push_back(FileName.size());
990 Record.append(FileName.begin(), FileName.end());
991 }
Douglas Gregor29cc6422011-08-17 21:07:30 +0000992 Stream.EmitRecord(IMPORTS, Record);
993 }
Mike Stump11289f42009-09-09 15:08:12 +0000994
Douglas Gregora3b20262011-05-06 21:43:30 +0000995 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +0000996 SourceManager &SM = Context.getSourceManager();
997 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
998 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000999 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregor45fe0362009-05-12 01:31:05 +00001000 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1001 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1002
Michael J. Spencer740857f2010-12-21 16:45:57 +00001003 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +00001004
Michael J. Spencer740857f2010-12-21 16:45:57 +00001005 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001006
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001007 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001008 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001009 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001010 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001011 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001012 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregora3b20262011-05-06 21:43:30 +00001013
1014 Record.clear();
1015 Record.push_back(SM.getMainFileID().getOpaqueValue());
1016 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001017 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001018
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001019 // Original PCH directory
1020 if (!OutputFile.empty() && OutputFile != "-") {
1021 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1022 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1023 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1024 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1025
1026 llvm::SmallString<128> OutputPath(OutputFile);
1027
1028 llvm::sys::fs::make_absolute(OutputPath);
1029 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1030
1031 RecordData Record;
1032 Record.push_back(ORIGINAL_PCH_DIR);
1033 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1034 }
1035
Ted Kremenek18e066f2010-01-22 22:12:47 +00001036 // Repository branch/version information.
1037 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001038 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenek18e066f2010-01-22 22:12:47 +00001039 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1040 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +00001041 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001042 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +00001043 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
1044 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +00001045}
1046
1047/// \brief Write the LangOptions structure.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001048void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001049 RecordData Record;
1050 Record.push_back(LangOpts.Trigraphs);
1051 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1052 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1053 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1054 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carruthe03aa552010-04-17 20:17:31 +00001055 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor55abb232009-04-10 20:39:37 +00001056 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1057 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1058 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1059 Record.push_back(LangOpts.C99); // C99 Support
Peter Collingbournea686b5f2011-04-15 00:35:23 +00001060 Record.push_back(LangOpts.C1X); // C1X Support
Douglas Gregor55abb232009-04-10 20:39:37 +00001061 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +00001062 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
1063 // already saved elsewhere.
Douglas Gregor55abb232009-04-10 20:39:37 +00001064 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1065 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +00001066 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +00001067
Douglas Gregor55abb232009-04-10 20:39:37 +00001068 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1069 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001070 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian45878032010-02-09 19:31:38 +00001071 // modern abi enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001072 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian45878032010-02-09 19:31:38 +00001073 // modern abi enabled.
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +00001074 Record.push_back(LangOpts.AppleKext); // Apple's kernel extensions ABI
Ted Kremenek1d56c9e2010-12-23 21:35:43 +00001075 Record.push_back(LangOpts.ObjCDefaultSynthProperties); // Objective-C auto-synthesized
1076 // properties enabled.
Douglas Gregora860e6a2011-06-14 23:20:43 +00001077 Record.push_back(LangOpts.ObjCInferRelatedResultType);
Fariborz Jahanian62c56022010-04-22 21:01:59 +00001078 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump11289f42009-09-09 15:08:12 +00001079
Douglas Gregor55abb232009-04-10 20:39:37 +00001080 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +00001081 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1082 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00001083 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +00001084 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Anders Carlssonce8dd3a2011-02-19 23:53:54 +00001085 Record.push_back(LangOpts.ObjCExceptions);
Anders Carlsson6bbd2682011-02-23 03:04:54 +00001086 Record.push_back(LangOpts.CXXExceptions);
1087 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor55abb232009-04-10 20:39:37 +00001088
Douglas Gregordbe39272011-02-01 15:15:22 +00001089 Record.push_back(LangOpts.MSBitfields); // MS-compatible structure layout
Douglas Gregor55abb232009-04-10 20:39:37 +00001090 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1091 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1092 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1093
Chris Lattner258172e2009-04-27 07:35:58 +00001094 // Whether static initializers are protected by locks.
1095 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00001096 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +00001097 Record.push_back(LangOpts.Blocks); // block extension to C
1098 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1099 // they are unused.
1100 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1101 // (modulo the platform support).
1102
Chris Lattner51924e512010-06-26 21:25:03 +00001103 Record.push_back(LangOpts.getSignedOverflowBehavior());
1104 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor55abb232009-04-10 20:39:37 +00001105
1106 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +00001107 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +00001108 // defined.
1109 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1110 // opposed to __DYNAMIC__).
1111 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1112
1113 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1114 // used (instead of C99 semantics).
1115 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Chandler Carruth7ffce732011-04-23 20:05:38 +00001116 Record.push_back(LangOpts.Deprecated); // Should __DEPRECATED be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +00001117 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
1118 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +00001119 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
1120 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +00001121 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Argyrios Kyrtzidisa88942a2011-01-15 02:56:16 +00001122 Record.push_back(LangOpts.ShortEnums); // Should the enum type be equivalent
1123 // to the smallest integer type with
1124 // enough room.
Douglas Gregor55abb232009-04-10 20:39:37 +00001125 Record.push_back(LangOpts.getGCMode());
1126 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +00001127 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +00001128 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00001129 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +00001130 Record.push_back(LangOpts.CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +00001131 Record.push_back(LangOpts.CatchUndefined);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001132 Record.push_back(LangOpts.DefaultFPContract);
Anders Carlsson9cedbef2009-08-22 22:30:33 +00001133 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +00001134 Record.push_back(LangOpts.SpellChecking);
Roman Divacky65b88cd2011-03-01 17:40:53 +00001135 Record.push_back(LangOpts.MRTD);
John McCall31168b02011-06-15 23:02:42 +00001136 Record.push_back(LangOpts.ObjCAutoRefCount);
Sebastian Redl539c5062010-08-18 23:57:32 +00001137 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +00001138}
1139
Douglas Gregora7f71a92009-04-10 03:52:48 +00001140//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00001141// stat cache Serialization
1142//===----------------------------------------------------------------------===//
1143
1144namespace {
1145// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001146class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001147public:
1148 typedef const char * key_type;
1149 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001150
Chris Lattner2a6fa472010-11-23 19:28:12 +00001151 typedef struct stat data_type;
1152 typedef const data_type &data_type_ref;
Douglas Gregorc5046832009-04-27 18:38:38 +00001153
1154 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001155 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001156 }
Mike Stump11289f42009-09-09 15:08:12 +00001157
1158 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001159 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorc5046832009-04-27 18:38:38 +00001160 data_type_ref Data) {
1161 unsigned StrLen = strlen(path);
1162 clang::io::Emit16(Out, StrLen);
Chris Lattner2a6fa472010-11-23 19:28:12 +00001163 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregorc5046832009-04-27 18:38:38 +00001164 clang::io::Emit8(Out, DataLen);
1165 return std::make_pair(StrLen + 1, DataLen);
1166 }
Mike Stump11289f42009-09-09 15:08:12 +00001167
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001168 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001169 Out.write(path, KeyLen);
1170 }
Mike Stump11289f42009-09-09 15:08:12 +00001171
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001172 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorc5046832009-04-27 18:38:38 +00001173 data_type_ref Data, unsigned DataLen) {
1174 using namespace clang::io;
1175 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +00001176
Chris Lattner2a6fa472010-11-23 19:28:12 +00001177 Emit32(Out, (uint32_t) Data.st_ino);
1178 Emit32(Out, (uint32_t) Data.st_dev);
1179 Emit16(Out, (uint16_t) Data.st_mode);
1180 Emit64(Out, (uint64_t) Data.st_mtime);
1181 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregorc5046832009-04-27 18:38:38 +00001182
1183 assert(Out.tell() - Start == DataLen && "Wrong data length");
1184 }
1185};
1186} // end anonymous namespace
1187
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001188/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001189void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001190 // Build the on-disk hash table containing information about every
1191 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001192 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +00001193 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001194 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +00001195 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001196 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001197 StringRef Filename = Stat->first();
Chris Lattnerd386df42011-07-14 18:24:21 +00001198 Generator.insert(Filename.data(), Stat->second);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001199 }
Mike Stump11289f42009-09-09 15:08:12 +00001200
Douglas Gregorc5046832009-04-27 18:38:38 +00001201 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001202 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +00001203 uint32_t BucketOffset;
1204 {
1205 llvm::raw_svector_ostream Out(StatCacheData);
1206 // Make sure that no bucket is at offset 0
1207 clang::io::Emit32(Out, 0);
1208 BucketOffset = Generator.Emit(Out);
1209 }
1210
1211 // Create a blob abbreviation
1212 using namespace llvm;
1213 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001214 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +00001215 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1216 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1217 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1218 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1219
1220 // Write the stat cache
1221 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001222 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +00001223 Record.push_back(BucketOffset);
1224 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001225 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +00001226}
1227
1228//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +00001229// Source Manager Serialization
1230//===----------------------------------------------------------------------===//
1231
1232/// \brief Create an abbreviation for the SLocEntry that refers to a
1233/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001234static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001235 using namespace llvm;
1236 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001237 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001238 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1239 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1240 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1241 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001242 // FileEntry fields.
1243 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1244 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001245 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Douglas Gregora7f71a92009-04-10 03:52:48 +00001246 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001247 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001248}
1249
1250/// \brief Create an abbreviation for the SLocEntry that refers to a
1251/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001252static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001253 using namespace llvm;
1254 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001255 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001256 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1257 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1258 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1259 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1260 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001261 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001262}
1263
1264/// \brief Create an abbreviation for the SLocEntry that refers to a
1265/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001266static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001267 using namespace llvm;
1268 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001269 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001270 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001271 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001272}
1273
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001274/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1275/// expansion.
1276static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001277 using namespace llvm;
1278 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001279 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001280 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1281 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1282 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1283 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001284 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001285 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001286}
1287
Douglas Gregor09b69892011-02-10 17:09:37 +00001288namespace {
1289 // Trait used for the on-disk hash table of header search information.
1290 class HeaderFileInfoTrait {
1291 ASTWriter &Writer;
1292 HeaderSearch &HS;
1293
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001294 // Keep track of the framework names we've used during serialization.
1295 SmallVector<char, 128> FrameworkStringData;
1296 llvm::StringMap<unsigned> FrameworkNameOffset;
1297
Douglas Gregor09b69892011-02-10 17:09:37 +00001298 public:
1299 HeaderFileInfoTrait(ASTWriter &Writer, HeaderSearch &HS)
1300 : Writer(Writer), HS(HS) { }
1301
1302 typedef const char *key_type;
1303 typedef key_type key_type_ref;
1304
1305 typedef HeaderFileInfo data_type;
1306 typedef const data_type &data_type_ref;
1307
1308 static unsigned ComputeHash(const char *path) {
1309 // The hash is based only on the filename portion of the key, so that the
1310 // reader can match based on filenames when symlinking or excess path
1311 // elements ("foo/../", "../") change the form of the name. However,
1312 // complete path is still the key.
1313 return llvm::HashString(llvm::sys::path::filename(path));
1314 }
1315
1316 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001317 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor09b69892011-02-10 17:09:37 +00001318 data_type_ref Data) {
1319 unsigned StrLen = strlen(path);
1320 clang::io::Emit16(Out, StrLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001321 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregor09b69892011-02-10 17:09:37 +00001322 clang::io::Emit8(Out, DataLen);
1323 return std::make_pair(StrLen + 1, DataLen);
1324 }
1325
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001326 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor09b69892011-02-10 17:09:37 +00001327 Out.write(path, KeyLen);
1328 }
1329
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001330 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor09b69892011-02-10 17:09:37 +00001331 data_type_ref Data, unsigned DataLen) {
1332 using namespace clang::io;
1333 uint64_t Start = Out.tell(); (void)Start;
1334
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001335 unsigned char Flags = (Data.isImport << 5)
1336 | (Data.isPragmaOnce << 4)
1337 | (Data.DirInfo << 2)
1338 | (Data.Resolved << 1)
1339 | Data.IndexHeaderMapHeader;
Douglas Gregor09b69892011-02-10 17:09:37 +00001340 Emit8(Out, (uint8_t)Flags);
1341 Emit16(Out, (uint16_t) Data.NumIncludes);
1342
1343 if (!Data.ControllingMacro)
1344 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1345 else
1346 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001347
1348 unsigned Offset = 0;
1349 if (!Data.Framework.empty()) {
1350 // If this header refers into a framework, save the framework name.
1351 llvm::StringMap<unsigned>::iterator Pos
1352 = FrameworkNameOffset.find(Data.Framework);
1353 if (Pos == FrameworkNameOffset.end()) {
1354 Offset = FrameworkStringData.size() + 1;
1355 FrameworkStringData.append(Data.Framework.begin(),
1356 Data.Framework.end());
1357 FrameworkStringData.push_back(0);
1358
1359 FrameworkNameOffset[Data.Framework] = Offset;
1360 } else
1361 Offset = Pos->second;
1362 }
1363 Emit32(Out, Offset);
1364
Douglas Gregor09b69892011-02-10 17:09:37 +00001365 assert(Out.tell() - Start == DataLen && "Wrong data length");
1366 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001367
1368 const char *strings_begin() const { return FrameworkStringData.begin(); }
1369 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001370 };
1371} // end anonymous namespace
1372
1373/// \brief Write the header search block for the list of files that
1374///
1375/// \param HS The header search structure to save.
1376///
1377/// \param Chain Whether we're creating a chained AST file.
Douglas Gregorc567ba22011-07-22 16:35:34 +00001378void ASTWriter::WriteHeaderSearch(HeaderSearch &HS, StringRef isysroot) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001379 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001380 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1381
1382 if (FilesByUID.size() > HS.header_file_size())
1383 FilesByUID.resize(HS.header_file_size());
1384
1385 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1386 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001387 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001388 unsigned NumHeaderSearchEntries = 0;
1389 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1390 const FileEntry *File = FilesByUID[UID];
1391 if (!File)
1392 continue;
1393
1394 const HeaderFileInfo &HFI = HS.header_file_begin()[UID];
1395 if (HFI.External && Chain)
1396 continue;
1397
1398 // Turn the file name into an absolute path, if it isn't already.
1399 const char *Filename = File->getName();
1400 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1401
1402 // If we performed any translation on the file name at all, we need to
1403 // save this string, since the generator will refer to it later.
1404 if (Filename != File->getName()) {
1405 Filename = strdup(Filename);
1406 SavedStrings.push_back(Filename);
1407 }
1408
1409 Generator.insert(Filename, HFI, GeneratorTrait);
1410 ++NumHeaderSearchEntries;
1411 }
1412
1413 // Create the on-disk hash table in a buffer.
1414 llvm::SmallString<4096> TableData;
1415 uint32_t BucketOffset;
1416 {
1417 llvm::raw_svector_ostream Out(TableData);
1418 // Make sure that no bucket is at offset 0
1419 clang::io::Emit32(Out, 0);
1420 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1421 }
1422
1423 // Create a blob abbreviation
1424 using namespace llvm;
1425 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1426 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1427 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1428 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001429 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001430 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1431 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1432
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001433 // Write the header search table
Douglas Gregor09b69892011-02-10 17:09:37 +00001434 RecordData Record;
1435 Record.push_back(HEADER_SEARCH_TABLE);
1436 Record.push_back(BucketOffset);
1437 Record.push_back(NumHeaderSearchEntries);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001438 Record.push_back(TableData.size());
1439 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregor09b69892011-02-10 17:09:37 +00001440 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1441
1442 // Free all of the strings we had to duplicate.
1443 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1444 free((void*)SavedStrings[I]);
1445}
1446
Douglas Gregora7f71a92009-04-10 03:52:48 +00001447/// \brief Writes the block containing the serialized form of the
1448/// source manager.
1449///
1450/// TODO: We should probably use an on-disk hash table (stored in a
1451/// blob), indexed based on the file name, so that we only create
1452/// entries for files that we actually need. In the common case (no
1453/// errors), we probably won't have to create file entries for any of
1454/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001455void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001456 const Preprocessor &PP,
Douglas Gregorc567ba22011-07-22 16:35:34 +00001457 StringRef isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001458 RecordData Record;
1459
Chris Lattner0910e3b2009-04-10 17:16:57 +00001460 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001461 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001462
1463 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001464 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1465 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1466 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001467 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001468
Douglas Gregor258ae542009-04-27 06:38:32 +00001469 // Write out the source location entry table. We skip the first
1470 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001471 std::vector<uint32_t> SLocEntryOffsets;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001472 // Write out the offsets of only source location file entries.
1473 // We will go through them in ASTReader::validateFileEntries().
1474 std::vector<uint32_t> SLocFileEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001475 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001476 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1477 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001478 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001479 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001480 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001481
Douglas Gregor258ae542009-04-27 06:38:32 +00001482 // Record the offset of this source-location entry.
1483 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1484
1485 // Figure out which record code to use.
1486 unsigned Code;
1487 if (SLoc->isFile()) {
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001488 if (SLoc->getFile().getContentCache()->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001489 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001490 SLocFileEntryOffsets.push_back(Stream.GetCurrentBitNo());
1491 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001492 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001493 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001494 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001495 Record.clear();
1496 Record.push_back(Code);
1497
Douglas Gregor925296b2011-07-19 16:10:42 +00001498 // Starting offset of this entry within this module, so skip the dummy.
1499 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001500 if (SLoc->isFile()) {
1501 const SrcMgr::FileInfo &File = SLoc->getFile();
1502 Record.push_back(File.getIncludeLoc().getRawEncoding());
1503 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1504 Record.push_back(File.hasLineDirectives());
1505
1506 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001507 if (Content->OrigEntry) {
1508 assert(Content->OrigEntry == Content->ContentsEntry &&
1509 "Writing to AST an overriden file is not supported");
1510
Douglas Gregor258ae542009-04-27 06:38:32 +00001511 // The source location entry is a file. The blob associated
1512 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001513
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001514 // Emit size/modification time for this file.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001515 Record.push_back(Content->OrigEntry->getSize());
1516 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001517
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001518 Record.push_back(File.NumCreatedFIDs);
1519
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001520 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001521 const char *Filename = Content->OrigEntry->getName();
Michael J. Spencer740857f2010-12-21 16:45:57 +00001522 llvm::SmallString<128> FilePath(Filename);
Anders Carlssona4267052011-03-08 16:04:35 +00001523
1524 // Ask the file manager to fixup the relative path for us. This will
1525 // honor the working directory.
1526 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1527
1528 // FIXME: This call to make_absolute shouldn't be necessary, the
1529 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencer740857f2010-12-21 16:45:57 +00001530 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001531 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001532
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001533 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001534 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001535 } else {
1536 // The source location entry is a buffer. The blob associated
1537 // with this entry contains the contents of the buffer.
1538
1539 // We add one to the size so that we capture the trailing NULL
1540 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1541 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001542 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001543 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001544 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001545 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001546 StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001547 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001548 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001549 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001550 StringRef(Buffer->getBufferStart(),
Daniel Dunbar8100d012009-08-24 09:31:37 +00001551 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001552
Douglas Gregor925296b2011-07-19 16:10:42 +00001553 if (strcmp(Name, "<built-in>") == 0) {
1554 PreloadSLocs.push_back(SLocEntryOffsets.size());
1555 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001556 }
1557 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001558 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001559 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth73ee5d72011-07-26 04:41:47 +00001560 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1561 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisa1d943a2011-08-17 00:31:14 +00001562 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1563 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor258ae542009-04-27 06:38:32 +00001564
1565 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001566 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001567 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001568 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001569 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001570 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001571 }
1572 }
1573
Douglas Gregor8f45df52009-04-16 22:23:12 +00001574 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001575
1576 if (SLocEntryOffsets.empty())
1577 return;
1578
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001579 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001580 // table is used for lazily loading source-location information.
1581 using namespace llvm;
1582 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001583 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001584 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00001585 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00001586 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1587 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001588
Douglas Gregor258ae542009-04-27 06:38:32 +00001589 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001590 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001591 Record.push_back(SLocEntryOffsets.size());
Douglas Gregor925296b2011-07-19 16:10:42 +00001592 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001593 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001594
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001595 Abbrev = new BitCodeAbbrev();
1596 Abbrev->Add(BitCodeAbbrevOp(FILE_SOURCE_LOCATION_OFFSETS));
1597 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1598 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1599 unsigned SLocFileOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1600
1601 Record.clear();
1602 Record.push_back(FILE_SOURCE_LOCATION_OFFSETS);
1603 Record.push_back(SLocFileEntryOffsets.size());
1604 Stream.EmitRecordWithBlob(SLocFileOffsetsAbbrev, Record,
1605 data(SLocFileEntryOffsets));
1606
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001607 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001608 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001609 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00001610
1611 // Write the line table. It depends on remapping working, so it must come
1612 // after the source location offsets.
1613 if (SourceMgr.hasLineTable()) {
1614 LineTableInfo &LineTable = SourceMgr.getLineTable();
1615
1616 Record.clear();
1617 // Emit the file names
1618 Record.push_back(LineTable.getNumFilenames());
1619 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1620 // Emit the file name
1621 const char *Filename = LineTable.getFilename(I);
1622 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1623 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1624 Record.push_back(FilenameLen);
1625 if (FilenameLen)
1626 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1627 }
1628
1629 // Emit the line entries
1630 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1631 L != LEnd; ++L) {
1632 // Only emit entries for local files.
1633 if (L->first < 0)
1634 continue;
1635
1636 // Emit the file ID
1637 Record.push_back(L->first);
1638
1639 // Emit the line entries
1640 Record.push_back(L->second.size());
1641 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1642 LEEnd = L->second.end();
1643 LE != LEEnd; ++LE) {
1644 Record.push_back(LE->FileOffset);
1645 Record.push_back(LE->LineNo);
1646 Record.push_back(LE->FilenameID);
1647 Record.push_back((unsigned)LE->FileKind);
1648 Record.push_back(LE->IncludeOffset);
1649 }
1650 }
1651 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1652 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001653}
1654
Douglas Gregorc5046832009-04-27 18:38:38 +00001655//===----------------------------------------------------------------------===//
1656// Preprocessor Serialization
1657//===----------------------------------------------------------------------===//
1658
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001659static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1660 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1661 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1662 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1663 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1664 return X.first->getName().compare(Y.first->getName());
1665}
1666
Chris Lattnereeffaef2009-04-10 17:15:23 +00001667/// \brief Writes the block containing the serialized form of the
1668/// preprocessor.
1669///
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001670void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001671 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001672
Chris Lattner0af3ba12009-04-13 01:29:17 +00001673 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1674 if (PP.getCounterValue() != 0) {
1675 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001676 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001677 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001678 }
1679
1680 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001681 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001682
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001683 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001684 // FIXME: use diagnostics subsystem for localization etc.
1685 if (PP.SawDateOrTime())
1686 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001687
Douglas Gregor796d76a2010-10-20 22:00:55 +00001688
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001689 // Loop over all the macro definitions that are live at the end of the file,
1690 // emitting each to the PP section.
Douglas Gregoraae92242010-03-19 21:51:54 +00001691 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001692
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001693 // Construct the list of macro definitions that need to be serialized.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001694 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001695 MacrosToEmit;
1696 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor68051a72011-02-11 00:26:14 +00001697 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1698 E = PP.macro_end(Chain == 0);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001699 I != E; ++I) {
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001700 MacroDefinitionsSeen.insert(I->first);
1701 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1702 }
1703
1704 // Sort the set of macro definitions that need to be serialized by the
1705 // name of the macro, to provide a stable ordering.
1706 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1707 &compareMacroDefinitions);
1708
Douglas Gregor68051a72011-02-11 00:26:14 +00001709 // Resolve any identifiers that defined macros at the time they were
1710 // deserialized, adding them to the list of macros to emit (if appropriate).
1711 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1712 IdentifierInfo *Name
1713 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1714 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1715 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1716 }
1717
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001718 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1719 const IdentifierInfo *Name = MacrosToEmit[I].first;
1720 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor68051a72011-02-11 00:26:14 +00001721 if (!MI)
1722 continue;
1723
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001724 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001725 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001726 // Also skip macros from a AST file if we're chaining.
Douglas Gregoreb114da2010-10-01 01:03:07 +00001727
1728 // FIXME: There is a (probably minor) optimization we could do here, if
1729 // the macro comes from the original PCH but the identifier comes from a
1730 // chained PCH, by storing the offset into the original PCH rather than
1731 // writing the macro definition a second time.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001732 if (MI->isBuiltinMacro() ||
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001733 (Chain && Name->isFromAST() && MI->isFromAST()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001734 continue;
1735
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001736 AddIdentifierRef(Name, Record);
1737 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001738 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1739 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001740
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001741 unsigned Code;
1742 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001743 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001744 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001745 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001746
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001747 Record.push_back(MI->isC99Varargs());
1748 Record.push_back(MI->isGNUVarargs());
1749 Record.push_back(MI->getNumArgs());
1750 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1751 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001752 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001753 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001754
Douglas Gregoraae92242010-03-19 21:51:54 +00001755 // If we have a detailed preprocessing record, record the macro definition
1756 // ID that corresponds to this macro.
1757 if (PPRec)
1758 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001759
Douglas Gregor8f45df52009-04-16 22:23:12 +00001760 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001761 Record.clear();
1762
Chris Lattner2199f5b2009-04-10 18:08:30 +00001763 // Emit the tokens array.
1764 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1765 // Note that we know that the preprocessor does not have any annotation
1766 // tokens in it because they are created by the parser, and thus can't be
1767 // in a macro definition.
1768 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001769
Chris Lattner2199f5b2009-04-10 18:08:30 +00001770 Record.push_back(Tok.getLocation().getRawEncoding());
1771 Record.push_back(Tok.getLength());
1772
Chris Lattner2199f5b2009-04-10 18:08:30 +00001773 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1774 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001775 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001776 // FIXME: Should translate token kind to a stable encoding.
1777 Record.push_back(Tok.getKind());
1778 // FIXME: Should translate token flags to a stable encoding.
1779 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001780
Sebastian Redl539c5062010-08-18 23:57:32 +00001781 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001782 Record.clear();
1783 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001784 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001785 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001786 Stream.ExitBlock();
1787
1788 if (PPRec)
1789 WritePreprocessorDetail(*PPRec);
1790}
1791
1792void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
1793 if (PPRec.begin(Chain) == PPRec.end(Chain))
1794 return;
1795
1796 // Enter the preprocessor block.
1797 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001798
Douglas Gregoraae92242010-03-19 21:51:54 +00001799 // If the preprocessor has a preprocessing record, emit it.
1800 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001801 using namespace llvm;
1802
1803 // Set up the abbreviation for
1804 unsigned InclusionAbbrev = 0;
1805 {
1806 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1807 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
1808 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1809 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1810 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1811 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1812 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1813 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1814 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1815 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1816 }
1817
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001818 unsigned FirstPreprocessorEntityID
1819 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1820 + NUM_PREDEF_PP_ENTITY_IDS;
1821 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001822 RecordData Record;
Douglas Gregord32f0352011-07-22 06:10:01 +00001823 uint64_t BitsInChain = Chain? Chain->TotalModulesSizeInBits : 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001824 for (PreprocessingRecord::iterator E = PPRec.begin(Chain),
1825 EEnd = PPRec.end(Chain);
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001826 E != EEnd;
1827 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001828 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001829
Douglas Gregor92a96f52011-02-08 21:58:10 +00001830 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1831 // Record this macro definition's location.
1832 MacroID ID = getMacroDefinitionID(MD);
1833
1834 // Don't write the macro definition if it is from another AST file.
1835 if (ID < FirstMacroID)
Douglas Gregoraae92242010-03-19 21:51:54 +00001836 continue;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001837
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001838 // Notify the serialization listener that we're serializing this entity.
1839 if (SerializationListener)
1840 SerializationListener->SerializedPreprocessedEntity(*E,
Douglas Gregord32f0352011-07-22 06:10:01 +00001841 BitsInChain + Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001842
Douglas Gregor92a96f52011-02-08 21:58:10 +00001843 unsigned Position = ID - FirstMacroID;
1844 if (Position != MacroDefinitionOffsets.size()) {
1845 if (Position > MacroDefinitionOffsets.size())
1846 MacroDefinitionOffsets.resize(Position + 1);
1847
1848 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
1849 } else
1850 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001851
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001852 Record.push_back(NextPreprocessorEntityID);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001853 Record.push_back(ID);
1854 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1855 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1856 AddIdentifierRef(MD->getName(), Record);
1857 AddSourceLocation(MD->getLocation(), Record);
1858 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1859 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00001860 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001861
Douglas Gregor92a96f52011-02-08 21:58:10 +00001862 // Notify the serialization listener that we're serializing this entity.
1863 if (SerializationListener)
1864 SerializationListener->SerializedPreprocessedEntity(*E,
Douglas Gregord32f0352011-07-22 06:10:01 +00001865 BitsInChain + Stream.GetCurrentBitNo());
Douglas Gregor92a96f52011-02-08 21:58:10 +00001866
Chandler Carrutha88a22182011-07-14 08:20:46 +00001867 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001868 Record.push_back(NextPreprocessorEntityID);
Chandler Carrutha88a22182011-07-14 08:20:46 +00001869 AddSourceLocation(ME->getSourceRange().getBegin(), Record);
1870 AddSourceLocation(ME->getSourceRange().getEnd(), Record);
1871 AddIdentifierRef(ME->getName(), Record);
1872 Record.push_back(getMacroDefinitionID(ME->getDefinition()));
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001873 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001874 continue;
1875 }
1876
1877 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1878 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001879 Record.push_back(NextPreprocessorEntityID);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001880 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1881 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1882 Record.push_back(ID->getFileName().size());
1883 Record.push_back(ID->wasInQuotes());
1884 Record.push_back(static_cast<unsigned>(ID->getKind()));
1885 llvm::SmallString<64> Buffer;
1886 Buffer += ID->getFileName();
1887 Buffer += ID->getFile()->getName();
1888 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1889 continue;
1890 }
1891
1892 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1893 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001894 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001895
Douglas Gregoraae92242010-03-19 21:51:54 +00001896 // Write the offsets table for the preprocessing record.
1897 if (NumPreprocessingRecords > 0) {
1898 // Write the offsets table for identifier IDs.
1899 using namespace llvm;
1900 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001901 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregoraae92242010-03-19 21:51:54 +00001902 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001903 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00001904 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
Douglas Gregora863b4b2011-08-04 16:36:56 +00001905 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first macro def
Douglas Gregoraae92242010-03-19 21:51:54 +00001906 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1907 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001908
Douglas Gregoraae92242010-03-19 21:51:54 +00001909 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001910 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001911 Record.push_back(NumPreprocessingRecords);
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001912 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001913 Record.push_back(MacroDefinitionOffsets.size());
Douglas Gregora863b4b2011-08-04 16:36:56 +00001914 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001915 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001916 data(MacroDefinitionOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00001917 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001918}
1919
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001920void ASTWriter::WritePragmaDiagnosticMappings(const Diagnostic &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001921 RecordData Record;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001922 for (Diagnostic::DiagStatePointsTy::const_iterator
1923 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1924 I != E; ++I) {
1925 const Diagnostic::DiagStatePoint &point = *I;
1926 if (point.Loc.isInvalid())
1927 continue;
1928
1929 Record.push_back(point.Loc.getRawEncoding());
1930 for (Diagnostic::DiagState::iterator
1931 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
1932 unsigned diag = I->first, map = I->second;
1933 if (map & 0x10) { // mapping from a diagnostic pragma.
1934 Record.push_back(diag);
1935 Record.push_back(map & 0x7);
1936 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001937 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001938 Record.push_back(-1); // mark the end of the diag/map pairs for this
1939 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001940 }
1941
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00001942 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001943 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001944}
1945
Anders Carlsson9bb83e82011-03-06 18:41:18 +00001946void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
1947 if (CXXBaseSpecifiersOffsets.empty())
1948 return;
1949
1950 RecordData Record;
1951
1952 // Create a blob abbreviation for the C++ base specifiers offsets.
1953 using namespace llvm;
1954
1955 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1956 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
1957 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
1958 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1959 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1960
Douglas Gregorc27b2872011-08-04 00:01:48 +00001961 // Write the base specifier offsets table.
Anders Carlsson9bb83e82011-03-06 18:41:18 +00001962 Record.clear();
1963 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
1964 Record.push_back(CXXBaseSpecifiersOffsets.size());
1965 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001966 data(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00001967}
1968
Douglas Gregorc5046832009-04-27 18:38:38 +00001969//===----------------------------------------------------------------------===//
1970// Type Serialization
1971//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001972
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001973/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001974void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00001975 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001976 if (Idx.getIndex() == 0) // we haven't seen this type before.
1977 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00001978
Douglas Gregor9b3932c2010-10-05 18:37:06 +00001979 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00001980
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001981 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001982 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001983 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001984 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001985 else if (TypeOffsets.size() < Index) {
1986 TypeOffsets.resize(Index + 1);
1987 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001988 }
1989
1990 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001991
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001992 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001993 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001994
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001995 if (T.hasLocalNonFastQualifiers()) {
1996 Qualifiers Qs = T.getLocalQualifiers();
1997 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001998 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001999 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00002000 } else {
2001 switch (T->getTypeClass()) {
2002 // For all of the concrete, non-dependent types, call the
2003 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002004#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00002005 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002006#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002007#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00002008 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002009 }
2010
2011 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002012 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002013
2014 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002015 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002016}
2017
Douglas Gregorc5046832009-04-27 18:38:38 +00002018//===----------------------------------------------------------------------===//
2019// Declaration Serialization
2020//===----------------------------------------------------------------------===//
2021
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002022/// \brief Write the block containing all of the declaration IDs
2023/// lexically declared within the given DeclContext.
2024///
2025/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2026/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002027uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002028 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002029 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002030 return 0;
2031
Douglas Gregor8f45df52009-04-16 22:23:12 +00002032 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002033 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002034 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002035 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002036 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2037 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002038 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002039
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002040 ++NumLexicalDeclContexts;
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002041 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002042 return Offset;
2043}
2044
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002045void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002046 using namespace llvm;
2047 RecordData Record;
2048
2049 // Write the type offsets array
2050 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002051 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002052 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002053 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002054 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2055 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2056 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002057 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002058 Record.push_back(TypeOffsets.size());
Douglas Gregor5204bde2011-08-02 16:26:37 +00002059 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002060 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002061
2062 // Write the declaration offsets array
2063 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002064 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002065 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002066 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002067 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2068 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2069 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002070 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002071 Record.push_back(DeclOffsets.size());
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002072 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002073 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002074}
2075
Douglas Gregorc5046832009-04-27 18:38:38 +00002076//===----------------------------------------------------------------------===//
2077// Global Method Pool and Selector Serialization
2078//===----------------------------------------------------------------------===//
2079
Douglas Gregore84a9da2009-04-20 20:36:09 +00002080namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002081// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002082class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002083 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002084
2085public:
2086 typedef Selector key_type;
2087 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002088
Sebastian Redl834bb972010-08-04 17:20:04 +00002089 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002090 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002091 ObjCMethodList Instance, Factory;
2092 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002093 typedef const data_type& data_type_ref;
2094
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002095 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002096
Douglas Gregorc78d3462009-04-24 21:10:55 +00002097 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002098 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002099 }
Mike Stump11289f42009-09-09 15:08:12 +00002100
2101 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002102 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002103 data_type_ref Methods) {
2104 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2105 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002106 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2107 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002108 Method = Method->Next)
2109 if (Method->Method)
2110 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002111 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002112 Method = Method->Next)
2113 if (Method->Method)
2114 DataLen += 4;
2115 clang::io::Emit16(Out, DataLen);
2116 return std::make_pair(KeyLen, DataLen);
2117 }
Mike Stump11289f42009-09-09 15:08:12 +00002118
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002119 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00002120 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002121 assert((Start >> 32) == 0 && "Selector key offset too large");
2122 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002123 unsigned N = Sel.getNumArgs();
2124 clang::io::Emit16(Out, N);
2125 if (N == 0)
2126 N = 1;
2127 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00002128 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002129 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2130 }
Mike Stump11289f42009-09-09 15:08:12 +00002131
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002132 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002133 data_type_ref Methods, unsigned DataLen) {
2134 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00002135 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002136 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002137 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002138 Method = Method->Next)
2139 if (Method->Method)
2140 ++NumInstanceMethods;
2141
2142 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002143 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002144 Method = Method->Next)
2145 if (Method->Method)
2146 ++NumFactoryMethods;
2147
2148 clang::io::Emit16(Out, NumInstanceMethods);
2149 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00002150 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002151 Method = Method->Next)
2152 if (Method->Method)
2153 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002154 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002155 Method = Method->Next)
2156 if (Method->Method)
2157 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002158
2159 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002160 }
2161};
2162} // end anonymous namespace
2163
Sebastian Redla19a67f2010-08-03 21:58:15 +00002164/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002165///
2166/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002167/// in an on-disk hash table indexed by the selector. The hash table also
2168/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002169void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002170 using namespace llvm;
2171
Sebastian Redla19a67f2010-08-03 21:58:15 +00002172 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002173 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002174 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002175 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002176 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002177 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002178 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002179 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002180
Sebastian Redla19a67f2010-08-03 21:58:15 +00002181 // Create the on-disk hash table representation. We walk through every
2182 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002183 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002184 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002185 I = SelectorIDs.begin(), E = SelectorIDs.end();
2186 I != E; ++I) {
2187 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002188 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002189 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002190 I->second,
2191 ObjCMethodList(),
2192 ObjCMethodList()
2193 };
2194 if (F != SemaRef.MethodPool.end()) {
2195 Data.Instance = F->second.first;
2196 Data.Factory = F->second.second;
2197 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002198 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002199 // changed.
2200 if (Chain && I->second < FirstSelectorID) {
2201 // Selector already exists. Did it change?
2202 bool changed = false;
2203 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2204 M = M->Next) {
2205 if (M->Method->getPCHLevel() == 0)
2206 changed = true;
2207 }
2208 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2209 M = M->Next) {
2210 if (M->Method->getPCHLevel() == 0)
2211 changed = true;
2212 }
2213 if (!changed)
2214 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002215 } else if (Data.Instance.Method || Data.Factory.Method) {
2216 // A new method pool entry.
2217 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002218 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002219 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002220 }
2221
Douglas Gregorc78d3462009-04-24 21:10:55 +00002222 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002223 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002224 uint32_t BucketOffset;
2225 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002226 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002227 llvm::raw_svector_ostream Out(MethodPool);
2228 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002229 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002230 BucketOffset = Generator.Emit(Out, Trait);
2231 }
2232
2233 // Create a blob abbreviation
2234 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002235 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002236 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002237 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002238 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2239 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2240
Douglas Gregor95c13f52009-04-25 17:48:32 +00002241 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002242 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002243 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002244 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002245 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002246 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002247
2248 // Create a blob abbreviation for the selector table offsets.
2249 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002250 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002251 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002252 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00002253 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2254 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2255
2256 // Write the selector offsets table.
2257 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002258 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002259 Record.push_back(SelectorOffsets.size());
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002260 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002261 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002262 data(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002263 }
2264}
2265
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002266/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002267void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002268 using namespace llvm;
2269 if (SemaRef.ReferencedSelectors.empty())
2270 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002271
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002272 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002273
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002274 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002275 // very tricky to fix, and given that @selector shouldn't really appear in
2276 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002277 for (DenseMap<Selector, SourceLocation>::iterator S =
2278 SemaRef.ReferencedSelectors.begin(),
2279 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2280 Selector Sel = (*S).first;
2281 SourceLocation Loc = (*S).second;
2282 AddSelectorRef(Sel, Record);
2283 AddSourceLocation(Loc, Record);
2284 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002285 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002286}
2287
Douglas Gregorc5046832009-04-27 18:38:38 +00002288//===----------------------------------------------------------------------===//
2289// Identifier Table Serialization
2290//===----------------------------------------------------------------------===//
2291
Douglas Gregorc78d3462009-04-24 21:10:55 +00002292namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002293class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002294 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002295 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002296
Douglas Gregor1d583f22009-04-28 21:18:29 +00002297 /// \brief Determines whether this is an "interesting" identifier
2298 /// that needs a full IdentifierInfo structure written into the hash
2299 /// table.
2300 static bool isInterestingIdentifier(const IdentifierInfo *II) {
2301 return II->isPoisoned() ||
2302 II->isExtensionToken() ||
2303 II->hasMacroDefinition() ||
2304 II->getObjCOrBuiltinID() ||
2305 II->getFETokenInfo<void>();
2306 }
2307
Douglas Gregore84a9da2009-04-20 20:36:09 +00002308public:
2309 typedef const IdentifierInfo* key_type;
2310 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002311
Sebastian Redl539c5062010-08-18 23:57:32 +00002312 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002313 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002314
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002315 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00002316 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00002317
2318 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00002319 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00002320 }
Mike Stump11289f42009-09-09 15:08:12 +00002321
2322 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002323 EmitKeyDataLength(raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002324 IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002325 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002326 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
2327 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00002328 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00002329 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00002330 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00002331 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002332 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2333 DEnd = IdentifierResolver::end();
2334 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00002335 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00002336 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00002337 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00002338 // We emit the key length after the data length so that every
2339 // string is preceded by a 16-bit length. This matches the PTH
2340 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00002341 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002342 return std::make_pair(KeyLen, DataLen);
2343 }
Mike Stump11289f42009-09-09 15:08:12 +00002344
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002345 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00002346 unsigned KeyLen) {
2347 // Record the location of the key data. This is used when generating
2348 // the mapping from persistent IDs to strings.
2349 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002350 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002351 }
Mike Stump11289f42009-09-09 15:08:12 +00002352
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002353 void EmitData(raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002354 IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00002355 if (!isInterestingIdentifier(II)) {
2356 clang::io::Emit32(Out, ID << 1);
2357 return;
2358 }
Douglas Gregorb9256522009-04-28 21:32:13 +00002359
Douglas Gregor1d583f22009-04-28 21:18:29 +00002360 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002361 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002362 bool hasMacroDefinition =
2363 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00002364 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00002365 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002366 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
2367 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2368 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00002369 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002370 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00002371 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002372
Douglas Gregorc3366a52009-04-21 23:56:24 +00002373 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00002374 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00002375
Douglas Gregora868bbd2009-04-21 22:25:48 +00002376 // Emit the declaration IDs in reverse order, because the
2377 // IdentifierResolver provides the declarations as they would be
2378 // visible (e.g., the function "stat" would come before the struct
2379 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2380 // adds declarations to the end of the list (so we need to see the
2381 // struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00002382 // Only emit declarations that aren't from a chained PCH, though.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002383 SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00002384 IdentifierResolver::end());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002385 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregora868bbd2009-04-21 22:25:48 +00002386 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00002387 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00002388 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002389 }
2390};
2391} // end anonymous namespace
2392
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002393/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002394///
2395/// The identifier table consists of a blob containing string data
2396/// (the actual identifiers themselves) and a separate "offsets" index
2397/// that maps identifier IDs to locations within the blob.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002398void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002399 using namespace llvm;
2400
2401 // Create and write out the blob that contains the identifier
2402 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002403 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002404 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002405 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump11289f42009-09-09 15:08:12 +00002406
Douglas Gregore6648fb2009-04-28 20:33:11 +00002407 // Look for any identifiers that were named while processing the
2408 // headers, but are otherwise not needed. We add these to the hash
2409 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002410 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00002411 // file.
2412 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2413 IDEnd = PP.getIdentifierTable().end();
2414 ID != IDEnd; ++ID)
2415 getIdentifierRef(ID->second);
2416
Sebastian Redlff4a2952010-07-23 23:49:55 +00002417 // Create the on-disk hash table representation. We only store offsets
2418 // for identifiers that appear here for the first time.
2419 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002420 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002421 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2422 ID != IDEnd; ++ID) {
2423 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002424 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002425 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002426 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002427
Douglas Gregore84a9da2009-04-20 20:36:09 +00002428 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002429 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00002430 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002431 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002432 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002433 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002434 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002435 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002436 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002437 }
2438
2439 // Create a blob abbreviation
2440 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002441 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00002442 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002443 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00002444 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002445
2446 // Write the identifier table
2447 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002448 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002449 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002450 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002451 }
2452
2453 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00002454 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002455 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00002456 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002457 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00002458 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2459 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2460
2461 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002462 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00002463 Record.push_back(IdentifierOffsets.size());
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002464 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor0e149972009-04-25 19:10:14 +00002465 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002466 data(IdentifierOffsets));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002467}
2468
Douglas Gregorc5046832009-04-27 18:38:38 +00002469//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002470// DeclContext's Name Lookup Table Serialization
2471//===----------------------------------------------------------------------===//
2472
2473namespace {
2474// Trait used for the on-disk hash table used in the method pool.
2475class ASTDeclContextNameLookupTrait {
2476 ASTWriter &Writer;
2477
2478public:
2479 typedef DeclarationName key_type;
2480 typedef key_type key_type_ref;
2481
2482 typedef DeclContext::lookup_result data_type;
2483 typedef const data_type& data_type_ref;
2484
2485 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2486
2487 unsigned ComputeHash(DeclarationName Name) {
2488 llvm::FoldingSetNodeID ID;
2489 ID.AddInteger(Name.getNameKind());
2490
2491 switch (Name.getNameKind()) {
2492 case DeclarationName::Identifier:
2493 ID.AddString(Name.getAsIdentifierInfo()->getName());
2494 break;
2495 case DeclarationName::ObjCZeroArgSelector:
2496 case DeclarationName::ObjCOneArgSelector:
2497 case DeclarationName::ObjCMultiArgSelector:
2498 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2499 break;
2500 case DeclarationName::CXXConstructorName:
2501 case DeclarationName::CXXDestructorName:
2502 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002503 break;
2504 case DeclarationName::CXXOperatorName:
2505 ID.AddInteger(Name.getCXXOverloadedOperator());
2506 break;
2507 case DeclarationName::CXXLiteralOperatorName:
2508 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2509 case DeclarationName::CXXUsingDirective:
2510 break;
2511 }
2512
2513 return ID.ComputeHash();
2514 }
2515
2516 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002517 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002518 data_type_ref Lookup) {
2519 unsigned KeyLen = 1;
2520 switch (Name.getNameKind()) {
2521 case DeclarationName::Identifier:
2522 case DeclarationName::ObjCZeroArgSelector:
2523 case DeclarationName::ObjCOneArgSelector:
2524 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002525 case DeclarationName::CXXLiteralOperatorName:
2526 KeyLen += 4;
2527 break;
2528 case DeclarationName::CXXOperatorName:
2529 KeyLen += 1;
2530 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002531 case DeclarationName::CXXConstructorName:
2532 case DeclarationName::CXXDestructorName:
2533 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002534 case DeclarationName::CXXUsingDirective:
2535 break;
2536 }
2537 clang::io::Emit16(Out, KeyLen);
2538
2539 // 2 bytes for num of decls and 4 for each DeclID.
2540 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2541 clang::io::Emit16(Out, DataLen);
2542
2543 return std::make_pair(KeyLen, DataLen);
2544 }
2545
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002546 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002547 using namespace clang::io;
2548
2549 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2550 Emit8(Out, Name.getNameKind());
2551 switch (Name.getNameKind()) {
2552 case DeclarationName::Identifier:
2553 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2554 break;
2555 case DeclarationName::ObjCZeroArgSelector:
2556 case DeclarationName::ObjCOneArgSelector:
2557 case DeclarationName::ObjCMultiArgSelector:
2558 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2559 break;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002560 case DeclarationName::CXXOperatorName:
2561 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2562 Emit8(Out, Name.getCXXOverloadedOperator());
2563 break;
2564 case DeclarationName::CXXLiteralOperatorName:
2565 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2566 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002567 case DeclarationName::CXXConstructorName:
2568 case DeclarationName::CXXDestructorName:
2569 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002570 case DeclarationName::CXXUsingDirective:
2571 break;
2572 }
2573 }
2574
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002575 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002576 data_type Lookup, unsigned DataLen) {
2577 uint64_t Start = Out.tell(); (void)Start;
2578 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2579 for (; Lookup.first != Lookup.second; ++Lookup.first)
2580 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2581
2582 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2583 }
2584};
2585} // end anonymous namespace
2586
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002587/// \brief Write the block containing all of the declaration IDs
2588/// visible from the given DeclContext.
2589///
2590/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002591/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002592uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2593 DeclContext *DC) {
2594 if (DC->getPrimaryContext() != DC)
2595 return 0;
2596
2597 // Since there is no name lookup into functions or methods, don't bother to
2598 // build a visible-declarations table for these entities.
2599 if (DC->isFunctionOrMethod())
2600 return 0;
2601
2602 // If not in C++, we perform name lookup for the translation unit via the
2603 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2604 // FIXME: In C++ we need the visible declarations in order to "see" the
2605 // friend declarations, is there a way to do this without writing the table ?
2606 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2607 return 0;
2608
2609 // Force the DeclContext to build a its name-lookup table.
Douglas Gregora3e59b42011-08-24 21:56:08 +00002610 if (!DC->hasExternalVisibleStorage())
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00002611 DC->lookup(DeclarationName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002612
2613 // Serialize the contents of the mapping used for lookup. Note that,
2614 // although we have two very different code paths, the serialized
2615 // representation is the same for both cases: a declaration name,
2616 // followed by a size, followed by references to the visible
2617 // declarations that have that name.
2618 uint64_t Offset = Stream.GetCurrentBitNo();
2619 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2620 if (!Map || Map->empty())
2621 return 0;
2622
2623 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2624 ASTDeclContextNameLookupTrait Trait(*this);
2625
2626 // Create the on-disk hash table representation.
Douglas Gregor05ef9312011-08-30 20:49:19 +00002627 DeclarationName ConversionName;
2628 llvm::SmallVector<NamedDecl *, 4> ConversionDecls;
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002629 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2630 D != DEnd; ++D) {
2631 DeclarationName Name = D->first;
2632 DeclContext::lookup_result Result = D->second.getLookupResult();
Douglas Gregor05ef9312011-08-30 20:49:19 +00002633 if (Result.first != Result.second) {
2634 if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
2635 // Hash all conversion function names to the same name. The actual
2636 // type information in conversion function name is not used in the
2637 // key (since such type information is not stable across different
2638 // modules), so the intended effect is to coalesce all of the conversion
2639 // functions under a single key.
2640 if (!ConversionName)
2641 ConversionName = Name;
2642 ConversionDecls.append(Result.first, Result.second);
2643 continue;
2644 }
2645
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00002646 Generator.insert(Name, Result, Trait);
Douglas Gregor05ef9312011-08-30 20:49:19 +00002647 }
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002648 }
2649
Douglas Gregor05ef9312011-08-30 20:49:19 +00002650 // Add the conversion functions
2651 if (!ConversionDecls.empty()) {
2652 Generator.insert(ConversionName,
2653 DeclContext::lookup_result(ConversionDecls.begin(),
2654 ConversionDecls.end()),
2655 Trait);
2656 }
2657
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002658 // Create the on-disk hash table in a buffer.
2659 llvm::SmallString<4096> LookupTable;
2660 uint32_t BucketOffset;
2661 {
2662 llvm::raw_svector_ostream Out(LookupTable);
2663 // Make sure that no bucket is at offset 0
2664 clang::io::Emit32(Out, 0);
2665 BucketOffset = Generator.Emit(Out, Trait);
2666 }
2667
2668 // Write the lookup table
2669 RecordData Record;
2670 Record.push_back(DECL_CONTEXT_VISIBLE);
2671 Record.push_back(BucketOffset);
2672 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2673 LookupTable.str());
2674
2675 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2676 ++NumVisibleDeclContexts;
2677 return Offset;
2678}
2679
Sebastian Redla4071b42010-08-24 00:50:09 +00002680/// \brief Write an UPDATE_VISIBLE block for the given context.
2681///
2682/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2683/// DeclContext in a dependent AST file. As such, they only exist for the TU
2684/// (in C++) and for namespaces.
2685void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00002686 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2687 if (!Map || Map->empty())
2688 return;
2689
2690 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2691 ASTDeclContextNameLookupTrait Trait(*this);
2692
2693 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002694 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2695 D != DEnd; ++D) {
2696 DeclarationName Name = D->first;
2697 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002698 // For any name that appears in this table, the results are complete, i.e.
2699 // they overwrite results from previous PCHs. Merging is always a mess.
Argyrios Kyrtzidisd3497db2011-08-30 19:43:23 +00002700 if (Result.first != Result.second)
2701 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002702 }
2703
2704 // Create the on-disk hash table in a buffer.
2705 llvm::SmallString<4096> LookupTable;
2706 uint32_t BucketOffset;
2707 {
2708 llvm::raw_svector_ostream Out(LookupTable);
2709 // Make sure that no bucket is at offset 0
2710 clang::io::Emit32(Out, 0);
2711 BucketOffset = Generator.Emit(Out, Trait);
2712 }
2713
2714 // Write the lookup table
2715 RecordData Record;
2716 Record.push_back(UPDATE_VISIBLE);
2717 Record.push_back(getDeclID(cast<Decl>(DC)));
2718 Record.push_back(BucketOffset);
2719 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2720}
2721
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002722/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2723void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2724 RecordData Record;
2725 Record.push_back(Opts.fp_contract);
2726 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2727}
2728
2729/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2730void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
2731 if (!SemaRef.Context.getLangOptions().OpenCL)
2732 return;
2733
2734 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2735 RecordData Record;
2736#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2737#include "clang/Basic/OpenCLExtensions.def"
2738 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2739}
2740
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002741//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00002742// General Serialization Routines
2743//===----------------------------------------------------------------------===//
2744
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002745/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002746void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00002747 Record.push_back(Attrs.size());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002748 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2749 const Attr * A = *i;
2750 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2751 AddSourceLocation(A->getLocation(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002752
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002753#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00002754
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002755 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002756}
2757
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002758void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002759 Record.push_back(Str.size());
2760 Record.insert(Record.end(), Str.begin(), Str.end());
2761}
2762
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002763void ASTWriter::AddVersionTuple(const VersionTuple &Version,
2764 RecordDataImpl &Record) {
2765 Record.push_back(Version.getMajor());
2766 if (llvm::Optional<unsigned> Minor = Version.getMinor())
2767 Record.push_back(*Minor + 1);
2768 else
2769 Record.push_back(0);
2770 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
2771 Record.push_back(*Subminor + 1);
2772 else
2773 Record.push_back(0);
2774}
2775
Douglas Gregore84a9da2009-04-20 20:36:09 +00002776/// \brief Note that the identifier II occurs at the given offset
2777/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002778void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002779 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002780 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00002781 // up earlier in the chain and thus don't need an offset.
2782 if (ID >= FirstIdentID)
2783 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002784}
2785
Douglas Gregor95c13f52009-04-25 17:48:32 +00002786/// \brief Note that the selector Sel occurs at the given offset
2787/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002788void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00002789 unsigned ID = SelectorIDs[Sel];
2790 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00002791 // Don't record offsets for selectors that are also available in a different
2792 // file.
2793 if (ID < FirstSelectorID)
2794 return;
2795 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002796}
2797
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002798ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregoreda8e122011-08-09 15:13:55 +00002799 : Stream(Stream), Context(0), Chain(0), SerializationListener(0),
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002800 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00002801 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002802 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002803 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregora863b4b2011-08-04 16:36:56 +00002804 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor91096292010-10-02 19:29:26 +00002805 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002806 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00002807 NumVisibleDeclContexts(0),
Douglas Gregorc27b2872011-08-04 00:01:48 +00002808 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00002809 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00002810 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
2811 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
2812 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00002813 DeclTypedefAbbrev(0),
2814 DeclVarAbbrev(0), DeclFieldAbbrev(0),
2815 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002816{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002817}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002818
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002819void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002820 const std::string &OutputFile,
Douglas Gregorc567ba22011-07-22 16:35:34 +00002821 StringRef isysroot) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002822 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002823 Stream.Emit((unsigned)'C', 8);
2824 Stream.Emit((unsigned)'P', 8);
2825 Stream.Emit((unsigned)'C', 8);
2826 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002827
Chris Lattner28fa4e62009-04-26 22:26:21 +00002828 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002829
Douglas Gregoreda8e122011-08-09 15:13:55 +00002830 Context = &SemaRef.Context;
Douglas Gregor851443c2011-08-12 01:39:19 +00002831 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile);
Douglas Gregoreda8e122011-08-09 15:13:55 +00002832 Context = 0;
Sebastian Redl143413f2010-07-12 22:02:52 +00002833}
2834
Douglas Gregora94a1542011-07-27 21:45:57 +00002835template<typename Vector>
2836static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
2837 ASTWriter::RecordData &Record) {
2838 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
2839 I != E; ++I) {
2840 Writer.AddDeclRef(*I, Record);
2841 }
2842}
2843
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002844void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregorc567ba22011-07-22 16:35:34 +00002845 StringRef isysroot,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002846 const std::string &OutputFile) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002847 using namespace llvm;
2848
2849 ASTContext &Context = SemaRef.Context;
2850 Preprocessor &PP = SemaRef.PP;
2851
Douglas Gregordab42432011-08-12 00:15:20 +00002852 // Set up predefined declaration IDs.
2853 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor3ea72692011-08-12 05:46:01 +00002854 if (Context.ObjCIdDecl)
2855 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor52e02802011-08-12 06:17:30 +00002856 if (Context.ObjCSelDecl)
2857 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor0a586182011-08-12 05:59:41 +00002858 if (Context.ObjCClassDecl)
2859 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregor801c99d2011-08-12 06:49:56 +00002860 if (Context.Int128Decl)
2861 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
2862 if (Context.UInt128Decl)
2863 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregor0a586182011-08-12 05:59:41 +00002864
Douglas Gregor851443c2011-08-12 01:39:19 +00002865 if (!Chain) {
2866 // Make sure that we emit IdentifierInfos (and any attached
2867 // declarations) for builtins. We don't need to do this when we're
2868 // emitting chained PCH files, because all of the builtins will be
2869 // in the original PCH file.
2870 // FIXME: Modules won't like this at all.
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002871 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002872 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002873 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2874 Context.getLangOptions().NoBuiltin);
2875 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2876 getIdentifierRef(&Table.get(BuiltinNames[I]));
2877 }
2878
Chris Lattner0c797362009-09-08 18:19:27 +00002879 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002880 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002881 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002882 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00002883 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00002884
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002885 // Build a record containing all of the file scoped decls in this file.
2886 RecordData UnusedFileScopedDecls;
Douglas Gregora94a1542011-07-27 21:45:57 +00002887 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
2888 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002889
Douglas Gregor851443c2011-08-12 01:39:19 +00002890 // Build a record containing all of the delegating constructors we still need
2891 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00002892 RecordData DelegatingCtorDecls;
Douglas Gregorbae31202011-07-27 21:57:17 +00002893 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00002894
Douglas Gregor851443c2011-08-12 01:39:19 +00002895 // Write the set of weak, undeclared identifiers. We always write the
2896 // entire table, since later PCH files in a PCH chain are only interested in
2897 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002898 RecordData WeakUndeclaredIdentifiers;
2899 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00002900 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002901 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2902 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2903 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2904 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2905 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2906 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2907 }
2908 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002909
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002910 // Build a record containing all of the locally-scoped external
2911 // declarations in this header file. Generally, this record will be
2912 // empty.
2913 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002914 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00002915 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002916 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002917 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2918 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregordc5c9582011-07-28 14:20:37 +00002919 TD != TDEnd; ++TD) {
2920 if (TD->second->getPCHLevel() == 0)
2921 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2922 }
2923
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002924 // Build a record containing all of the ext_vector declarations.
2925 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00002926 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002927
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002928 // Build a record containing all of the VTable uses information.
2929 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00002930 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00002931 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2932 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2933 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2934 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2935 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002936 }
2937
2938 // Build a record containing all of dynamic classes declarations.
2939 RecordData DynamicClasses;
Douglas Gregor32002192011-07-28 00:53:40 +00002940 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002941
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002942 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002943 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002944 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002945 I = SemaRef.PendingInstantiations.begin(),
2946 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2947 AddDeclRef(I->first, PendingInstantiations);
2948 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002949 }
2950 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2951 "There are local ones at end of translation unit!");
2952
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002953 // Build a record containing some declaration references.
2954 RecordData SemaDeclRefs;
2955 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2956 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2957 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2958 }
2959
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002960 RecordData CUDASpecialDeclRefs;
2961 if (Context.getcudaConfigureCallDecl()) {
2962 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
2963 }
2964
Douglas Gregorc2fa1692011-06-28 16:20:02 +00002965 // Build a record containing all of the known namespaces.
2966 RecordData KnownNamespaces;
2967 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
2968 I = SemaRef.KnownNamespaces.begin(),
2969 IEnd = SemaRef.KnownNamespaces.end();
2970 I != IEnd; ++I) {
2971 if (!I->second)
2972 AddDeclRef(I->first, KnownNamespaces);
2973 }
2974
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002975 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002976 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002977 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002978 WriteMetadata(Context, isysroot, OutputFile);
Sebastian Redl143413f2010-07-12 22:02:52 +00002979 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregorc567ba22011-07-22 16:35:34 +00002980 if (StatCalls && isysroot.empty())
Douglas Gregor11cfd942010-07-12 23:48:14 +00002981 WriteStatCache(*StatCalls);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002982 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Douglas Gregor2df17cb2011-08-01 16:54:33 +00002983
Douglas Gregor851443c2011-08-12 01:39:19 +00002984 if (Chain) {
2985 // Write the mapping information describing our module dependencies and how
2986 // each of those modules were mapped into our own offset/ID space, so that
2987 // the reader can build the appropriate mapping to its own offset/ID space.
2988 // The map consists solely of a blob with the following format:
2989 // *(module-name-len:i16 module-name:len*i8
2990 // source-location-offset:i32
2991 // identifier-id:i32
2992 // preprocessed-entity-id:i32
2993 // macro-definition-id:i32
2994 // selector-id:i32
2995 // declaration-id:i32
2996 // c++-base-specifiers-id:i32
2997 // type-id:i32)
2998 //
2999 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3000 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3001 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3002 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
3003 llvm::SmallString<2048> Buffer;
3004 {
3005 llvm::raw_svector_ostream Out(Buffer);
3006 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
3007 MEnd = Chain->ModuleMgr.end();
3008 M != MEnd; ++M) {
3009 StringRef FileName = (*M)->FileName;
3010 io::Emit16(Out, FileName.size());
3011 Out.write(FileName.data(), FileName.size());
3012 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3013 io::Emit32(Out, (*M)->BaseIdentifierID);
3014 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
3015 io::Emit32(Out, (*M)->BaseMacroDefinitionID);
3016 io::Emit32(Out, (*M)->BaseSelectorID);
3017 io::Emit32(Out, (*M)->BaseDeclID);
3018 io::Emit32(Out, (*M)->BaseTypeIndex);
3019 }
3020 }
3021 Record.clear();
3022 Record.push_back(MODULE_OFFSET_MAP);
3023 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3024 Buffer.data(), Buffer.size());
3025 }
3026
3027 // Create a lexical update block containing all of the declarations in the
3028 // translation unit that do not come from other AST files.
3029 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3030 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3031 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3032 E = TU->noload_decls_end();
3033 I != E; ++I) {
3034 if ((*I)->getPCHLevel() == 0)
3035 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
3036 else if ((*I)->isChangedSinceDeserialization())
3037 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
3038 }
3039
3040 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3041 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3042 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3043 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3044 Record.clear();
3045 Record.push_back(TU_UPDATE_LEXICAL);
3046 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3047 data(NewGlobalDecls));
3048
3049 // And a visible updates block for the translation unit.
3050 Abv = new llvm::BitCodeAbbrev();
3051 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3052 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3053 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3054 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3055 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3056 WriteDeclContextVisibleUpdate(TU);
3057
3058 // If the translation unit has an anonymous namespace, and we don't already
3059 // have an update block for it, write it as an update block.
3060 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3061 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3062 if (Record.empty()) {
3063 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
3064 AddDeclRef(NS, Record);
3065 }
3066 }
3067
Douglas Gregor5204bde2011-08-02 16:26:37 +00003068 // Form the record of special types.
3069 RecordData SpecialTypes;
3070 AddTypeRef(Context.getBuiltinVaListType(), SpecialTypes);
Douglas Gregor09c4aa82011-08-11 22:04:35 +00003071 AddTypeRef(Context.ObjCProtoType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00003072 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00003073 AddTypeRef(Context.getFILEType(), SpecialTypes);
3074 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3075 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3076 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3077 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00003078 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Douglas Gregordab42432011-08-12 00:15:20 +00003079
Douglas Gregor1970d882009-04-26 03:49:13 +00003080 // Keep writing types and declarations until all types and
3081 // declarations have been written.
Douglas Gregor03412ba2011-06-03 02:27:19 +00003082 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003083 WriteDeclsBlockAbbrevs();
Douglas Gregor851443c2011-08-12 01:39:19 +00003084 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3085 E = DeclsToRewrite.end();
3086 I != E; ++I)
3087 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor12bfa382009-10-17 00:13:19 +00003088 while (!DeclTypesToEmit.empty()) {
3089 DeclOrType DOT = DeclTypesToEmit.front();
3090 DeclTypesToEmit.pop();
3091 if (DOT.isType())
3092 WriteType(DOT.getType());
3093 else
3094 WriteDecl(Context, DOT.getDecl());
3095 }
3096 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003097
Douglas Gregor45053152009-10-17 17:25:45 +00003098 WritePreprocessor(PP);
Douglas Gregor09b69892011-02-10 17:09:37 +00003099 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00003100 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003101 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00003102 WriteIdentifierTable(PP);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003103 WriteFPPragmaOptions(SemaRef.getFPOptions());
3104 WriteOpenCLExtensions(SemaRef);
Douglas Gregor745ed142009-04-25 18:35:21 +00003105
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003106 WriteTypeDeclOffsets();
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003107 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00003108
Anders Carlsson9bb83e82011-03-06 18:41:18 +00003109 WriteCXXBaseSpecifiersOffsets();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003110
Douglas Gregor5204bde2011-08-02 16:26:37 +00003111 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3112
Douglas Gregor851443c2011-08-12 01:39:19 +00003113 /// Build a record containing first declarations from a chained PCH and the
3114 /// most recent declarations in this AST that they point to.
3115 RecordData FirstLatestDeclIDs;
3116 for (FirstLatestDeclMap::iterator I = FirstLatestDecls.begin(),
3117 E = FirstLatestDecls.end();
3118 I != E; ++I) {
3119 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
3120 "Expected first & second to be in different PCHs");
3121 AddDeclRef(I->first, FirstLatestDeclIDs);
3122 AddDeclRef(I->second, FirstLatestDeclIDs);
3123 }
3124
3125 if (!FirstLatestDeclIDs.empty())
3126 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
3127
Douglas Gregord4df8652009-04-22 22:02:47 +00003128 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003129 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003130 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00003131
3132 // Write the record containing tentative definitions.
3133 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003134 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003135
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003136 // Write the record containing unused file scoped decls.
3137 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003138 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003139
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003140 // Write the record containing weak undeclared identifiers.
3141 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003142 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003143 WeakUndeclaredIdentifiers);
3144
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003145 // Write the record containing locally-scoped external definitions.
3146 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003147 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003148 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003149
3150 // Write the record containing ext_vector type names.
3151 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003152 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00003153
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003154 // Write the record containing VTable uses information.
3155 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003156 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003157
3158 // Write the record containing dynamic classes declarations.
3159 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003160 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003161
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003162 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003163 if (!PendingInstantiations.empty())
3164 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003165
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003166 // Write the record containing declaration references of Sema.
3167 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003168 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003169
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003170 // Write the record containing CUDA-specific declaration references.
3171 if (!CUDASpecialDeclRefs.empty())
3172 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003173
3174 // Write the delegating constructors.
3175 if (!DelegatingCtorDecls.empty())
3176 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003177
Douglas Gregorc2fa1692011-06-28 16:20:02 +00003178 // Write the known namespaces.
3179 if (!KnownNamespaces.empty())
3180 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3181
Douglas Gregor851443c2011-08-12 01:39:19 +00003182 // Write the visible updates to DeclContexts.
3183 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3184 I = UpdatedDeclContexts.begin(),
3185 E = UpdatedDeclContexts.end();
3186 I != E; ++I)
3187 WriteDeclContextVisibleUpdate(*I);
3188
Douglas Gregordab42432011-08-12 00:15:20 +00003189 WriteDeclUpdatesBlocks();
Douglas Gregor851443c2011-08-12 01:39:19 +00003190 WriteDeclReplacementsBlock();
Douglas Gregordab42432011-08-12 00:15:20 +00003191
Douglas Gregor08f01292009-04-17 22:13:46 +00003192 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00003193 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00003194 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00003195 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003196 Record.push_back(NumLexicalDeclContexts);
3197 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00003198 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00003199 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003200}
3201
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003202void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003203 if (DeclUpdates.empty())
3204 return;
3205
3206 RecordData OffsetsRecord;
Douglas Gregor03412ba2011-06-03 02:27:19 +00003207 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003208 for (DeclUpdateMap::iterator
3209 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3210 const Decl *D = I->first;
3211 UpdateRecord &URec = I->second;
3212
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00003213 if (DeclsToRewrite.count(D))
3214 continue; // The decl will be written completely,no need to store updates.
3215
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003216 uint64_t Offset = Stream.GetCurrentBitNo();
3217 Stream.EmitRecord(DECL_UPDATES, URec);
3218
3219 OffsetsRecord.push_back(GetDeclRef(D));
3220 OffsetsRecord.push_back(Offset);
3221 }
3222 Stream.ExitBlock();
3223 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3224}
3225
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003226void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003227 if (ReplacedDecls.empty())
3228 return;
3229
3230 RecordData Record;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003231 for (SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003232 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
3233 Record.push_back(I->first);
3234 Record.push_back(I->second);
3235 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003236 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003237}
3238
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003239void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003240 Record.push_back(Loc.getRawEncoding());
3241}
3242
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003243void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003244 AddSourceLocation(Range.getBegin(), Record);
3245 AddSourceLocation(Range.getEnd(), Record);
3246}
3247
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003248void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003249 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00003250 const uint64_t *Words = Value.getRawData();
3251 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003252}
3253
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003254void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00003255 Record.push_back(Value.isUnsigned());
3256 AddAPInt(Value, Record);
3257}
3258
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003259void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00003260 AddAPInt(Value.bitcastToAPInt(), Record);
3261}
3262
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003263void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003264 Record.push_back(getIdentifierRef(II));
3265}
3266
Sebastian Redl539c5062010-08-18 23:57:32 +00003267IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003268 if (II == 0)
3269 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003270
Sebastian Redl539c5062010-08-18 23:57:32 +00003271 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003272 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00003273 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003274 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003275}
3276
Sebastian Redl50e26582010-09-15 19:54:06 +00003277MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregoraae92242010-03-19 21:51:54 +00003278 if (MD == 0)
3279 return 0;
Sebastian Redl50e26582010-09-15 19:54:06 +00003280
3281 MacroID &ID = MacroDefinitions[MD];
Douglas Gregoraae92242010-03-19 21:51:54 +00003282 if (ID == 0)
Douglas Gregor91096292010-10-02 19:29:26 +00003283 ID = NextMacroID++;
Douglas Gregoraae92242010-03-19 21:51:54 +00003284 return ID;
3285}
3286
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003287void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003288 Record.push_back(getSelectorRef(SelRef));
3289}
3290
Sebastian Redl539c5062010-08-18 23:57:32 +00003291SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003292 if (Sel.getAsOpaquePtr() == 0) {
3293 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00003294 }
3295
Sebastian Redl539c5062010-08-18 23:57:32 +00003296 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00003297 if (SID == 0 && Chain) {
3298 // This might trigger a ReadSelector callback, which will set the ID for
3299 // this selector.
3300 Chain->LoadSelector(Sel);
3301 }
Steve Naroff2ddea052009-04-23 10:39:46 +00003302 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003303 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00003304 }
Sebastian Redl834bb972010-08-04 17:20:04 +00003305 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00003306}
3307
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003308void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00003309 AddDeclRef(Temp->getDestructor(), Record);
3310}
3311
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003312void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3313 CXXBaseSpecifier const *BasesEnd,
3314 RecordDataImpl &Record) {
3315 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3316 CXXBaseSpecifiersToWrite.push_back(
3317 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3318 Bases, BasesEnd));
3319 Record.push_back(NextCXXBaseSpecifiersID++);
3320}
3321
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003322void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003323 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003324 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003325 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00003326 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003327 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00003328 break;
3329 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003330 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00003331 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003332 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00003333 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003334 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003335 break;
3336 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00003337 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003338 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003339 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003340 break;
John McCall0ad16662009-10-29 08:12:44 +00003341 case TemplateArgument::Null:
3342 case TemplateArgument::Integral:
3343 case TemplateArgument::Declaration:
3344 case TemplateArgument::Pack:
3345 break;
3346 }
3347}
3348
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003349void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003350 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003351 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003352
3353 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3354 bool InfoHasSameExpr
3355 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3356 Record.push_back(InfoHasSameExpr);
3357 if (InfoHasSameExpr)
3358 return; // Avoid storing the same expr twice.
3359 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003360 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3361 Record);
3362}
3363
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003364void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3365 RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00003366 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00003367 AddTypeRef(QualType(), Record);
3368 return;
3369 }
3370
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003371 AddTypeLoc(TInfo->getTypeLoc(), Record);
3372}
3373
3374void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3375 AddTypeRef(TL.getType(), Record);
3376
John McCall8f115c62009-10-16 21:56:05 +00003377 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003378 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003379 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00003380}
3381
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003382void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00003383 Record.push_back(GetOrCreateTypeID(T));
3384}
3385
Douglas Gregoreda8e122011-08-09 15:13:55 +00003386TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
3387 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003388 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3389}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003390
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003391TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregoreda8e122011-08-09 15:13:55 +00003392 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003393 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003394}
3395
3396TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3397 if (T.isNull())
3398 return TypeIdx();
3399 assert(!T.getLocalFastQualifiers());
3400
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00003401 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003402 if (Idx.getIndex() == 0) {
Douglas Gregor1970d882009-04-26 03:49:13 +00003403 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00003404 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003405 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003406 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00003407 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003408 return Idx;
3409}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003410
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003411TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003412 if (T.isNull())
3413 return TypeIdx();
3414 assert(!T.getLocalFastQualifiers());
3415
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003416 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3417 assert(I != TypeIdxs.end() && "Type not emitted!");
3418 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003419}
3420
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003421void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003422 Record.push_back(GetDeclRef(D));
3423}
3424
Sebastian Redl539c5062010-08-18 23:57:32 +00003425DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003426 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003427 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003428 }
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003429 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00003430 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00003431 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003432 // We haven't seen this declaration before. Give it a new ID and
3433 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00003434 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003435 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003436 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3437 // We don't add it to the replacement collection here, because we don't
3438 // have the offset yet.
3439 DeclTypesToEmit.push(const_cast<Decl *>(D));
3440 // Reset the flag, so that we don't add this decl multiple times.
3441 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003442 }
3443
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003444 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003445}
3446
Sebastian Redl539c5062010-08-18 23:57:32 +00003447DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00003448 if (D == 0)
3449 return 0;
3450
3451 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3452 return DeclIDs[D];
3453}
3454
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003455void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00003456 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003457 Record.push_back(Name.getNameKind());
3458 switch (Name.getNameKind()) {
3459 case DeclarationName::Identifier:
3460 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3461 break;
3462
3463 case DeclarationName::ObjCZeroArgSelector:
3464 case DeclarationName::ObjCOneArgSelector:
3465 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00003466 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003467 break;
3468
3469 case DeclarationName::CXXConstructorName:
3470 case DeclarationName::CXXDestructorName:
3471 case DeclarationName::CXXConversionFunctionName:
3472 AddTypeRef(Name.getCXXNameType(), Record);
3473 break;
3474
3475 case DeclarationName::CXXOperatorName:
3476 Record.push_back(Name.getCXXOverloadedOperator());
3477 break;
3478
Alexis Hunt3d221f22009-11-29 07:34:05 +00003479 case DeclarationName::CXXLiteralOperatorName:
3480 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3481 break;
3482
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003483 case DeclarationName::CXXUsingDirective:
3484 // No extra data to emit
3485 break;
3486 }
3487}
Chris Lattnerca025db2010-05-07 21:43:38 +00003488
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003489void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003490 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003491 switch (Name.getNameKind()) {
3492 case DeclarationName::CXXConstructorName:
3493 case DeclarationName::CXXDestructorName:
3494 case DeclarationName::CXXConversionFunctionName:
3495 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3496 break;
3497
3498 case DeclarationName::CXXOperatorName:
3499 AddSourceLocation(
3500 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3501 Record);
3502 AddSourceLocation(
3503 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3504 Record);
3505 break;
3506
3507 case DeclarationName::CXXLiteralOperatorName:
3508 AddSourceLocation(
3509 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3510 Record);
3511 break;
3512
3513 case DeclarationName::Identifier:
3514 case DeclarationName::ObjCZeroArgSelector:
3515 case DeclarationName::ObjCOneArgSelector:
3516 case DeclarationName::ObjCMultiArgSelector:
3517 case DeclarationName::CXXUsingDirective:
3518 break;
3519 }
3520}
3521
3522void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003523 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003524 AddDeclarationName(NameInfo.getName(), Record);
3525 AddSourceLocation(NameInfo.getLoc(), Record);
3526 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3527}
3528
3529void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003530 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00003531 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003532 Record.push_back(Info.NumTemplParamLists);
3533 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3534 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3535}
3536
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003537void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003538 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003539 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00003540 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003541 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00003542
3543 // Push each of the NNS's onto a stack for serialization in reverse order.
3544 while (NNS) {
3545 NestedNames.push_back(NNS);
3546 NNS = NNS->getPrefix();
3547 }
3548
3549 Record.push_back(NestedNames.size());
3550 while(!NestedNames.empty()) {
3551 NNS = NestedNames.pop_back_val();
3552 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3553 Record.push_back(Kind);
3554 switch (Kind) {
3555 case NestedNameSpecifier::Identifier:
3556 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3557 break;
3558
3559 case NestedNameSpecifier::Namespace:
3560 AddDeclRef(NNS->getAsNamespace(), Record);
3561 break;
3562
Douglas Gregor7b26ff92011-02-24 02:36:08 +00003563 case NestedNameSpecifier::NamespaceAlias:
3564 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
3565 break;
3566
Chris Lattnerca025db2010-05-07 21:43:38 +00003567 case NestedNameSpecifier::TypeSpec:
3568 case NestedNameSpecifier::TypeSpecWithTemplate:
3569 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3570 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3571 break;
3572
3573 case NestedNameSpecifier::Global:
3574 // Don't need to write an associated value.
3575 break;
3576 }
3577 }
3578}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003579
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003580void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
3581 RecordDataImpl &Record) {
3582 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00003583 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003584 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003585
3586 // Push each of the nested-name-specifiers's onto a stack for
3587 // serialization in reverse order.
3588 while (NNS) {
3589 NestedNames.push_back(NNS);
3590 NNS = NNS.getPrefix();
3591 }
3592
3593 Record.push_back(NestedNames.size());
3594 while(!NestedNames.empty()) {
3595 NNS = NestedNames.pop_back_val();
3596 NestedNameSpecifier::SpecifierKind Kind
3597 = NNS.getNestedNameSpecifier()->getKind();
3598 Record.push_back(Kind);
3599 switch (Kind) {
3600 case NestedNameSpecifier::Identifier:
3601 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
3602 AddSourceRange(NNS.getLocalSourceRange(), Record);
3603 break;
3604
3605 case NestedNameSpecifier::Namespace:
3606 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
3607 AddSourceRange(NNS.getLocalSourceRange(), Record);
3608 break;
3609
3610 case NestedNameSpecifier::NamespaceAlias:
3611 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
3612 AddSourceRange(NNS.getLocalSourceRange(), Record);
3613 break;
3614
3615 case NestedNameSpecifier::TypeSpec:
3616 case NestedNameSpecifier::TypeSpecWithTemplate:
3617 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3618 AddTypeLoc(NNS.getTypeLoc(), Record);
3619 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3620 break;
3621
3622 case NestedNameSpecifier::Global:
3623 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3624 break;
3625 }
3626 }
3627}
3628
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003629void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003630 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003631 Record.push_back(Kind);
3632 switch (Kind) {
3633 case TemplateName::Template:
3634 AddDeclRef(Name.getAsTemplateDecl(), Record);
3635 break;
3636
3637 case TemplateName::OverloadedTemplate: {
3638 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3639 Record.push_back(OvT->size());
3640 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3641 I != E; ++I)
3642 AddDeclRef(*I, Record);
3643 break;
3644 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003645
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003646 case TemplateName::QualifiedTemplate: {
3647 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3648 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3649 Record.push_back(QualT->hasTemplateKeyword());
3650 AddDeclRef(QualT->getTemplateDecl(), Record);
3651 break;
3652 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003653
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003654 case TemplateName::DependentTemplate: {
3655 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3656 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3657 Record.push_back(DepT->isIdentifier());
3658 if (DepT->isIdentifier())
3659 AddIdentifierRef(DepT->getIdentifier(), Record);
3660 else
3661 Record.push_back(DepT->getOperator());
3662 break;
3663 }
John McCalld9dfe3a2011-06-30 08:33:18 +00003664
3665 case TemplateName::SubstTemplateTemplateParm: {
3666 SubstTemplateTemplateParmStorage *subst
3667 = Name.getAsSubstTemplateTemplateParm();
3668 AddDeclRef(subst->getParameter(), Record);
3669 AddTemplateName(subst->getReplacement(), Record);
3670 break;
3671 }
Douglas Gregor5590be02011-01-15 06:45:20 +00003672
3673 case TemplateName::SubstTemplateTemplateParmPack: {
3674 SubstTemplateTemplateParmPackStorage *SubstPack
3675 = Name.getAsSubstTemplateTemplateParmPack();
3676 AddDeclRef(SubstPack->getParameterPack(), Record);
3677 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
3678 break;
3679 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003680 }
3681}
3682
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003683void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003684 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003685 Record.push_back(Arg.getKind());
3686 switch (Arg.getKind()) {
3687 case TemplateArgument::Null:
3688 break;
3689 case TemplateArgument::Type:
3690 AddTypeRef(Arg.getAsType(), Record);
3691 break;
3692 case TemplateArgument::Declaration:
3693 AddDeclRef(Arg.getAsDecl(), Record);
3694 break;
3695 case TemplateArgument::Integral:
3696 AddAPSInt(*Arg.getAsIntegral(), Record);
3697 AddTypeRef(Arg.getIntegralType(), Record);
3698 break;
3699 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00003700 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3701 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003702 case TemplateArgument::TemplateExpansion:
3703 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregore1d60df2011-01-14 23:41:42 +00003704 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3705 Record.push_back(*NumExpansions + 1);
3706 else
3707 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003708 break;
3709 case TemplateArgument::Expression:
3710 AddStmt(Arg.getAsExpr());
3711 break;
3712 case TemplateArgument::Pack:
3713 Record.push_back(Arg.pack_size());
3714 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3715 I != E; ++I)
3716 AddTemplateArgument(*I, Record);
3717 break;
3718 }
3719}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003720
3721void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003722ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003723 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003724 assert(TemplateParams && "No TemplateParams!");
3725 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3726 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3727 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3728 Record.push_back(TemplateParams->size());
3729 for (TemplateParameterList::const_iterator
3730 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3731 P != PEnd; ++P)
3732 AddDeclRef(*P, Record);
3733}
3734
3735/// \brief Emit a template argument list.
3736void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003737ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003738 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003739 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003740 Record.push_back(TemplateArgs->size());
3741 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003742 AddTemplateArgument(TemplateArgs->get(i), Record);
3743}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003744
3745
3746void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003747ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003748 Record.push_back(Set.size());
3749 for (UnresolvedSetImpl::const_iterator
3750 I = Set.begin(), E = Set.end(); I != E; ++I) {
3751 AddDeclRef(I.getDecl(), Record);
3752 Record.push_back(I.getAccess());
3753 }
3754}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003755
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003756void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003757 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003758 Record.push_back(Base.isVirtual());
3759 Record.push_back(Base.isBaseOfClass());
3760 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00003761 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00003762 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003763 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00003764 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3765 : SourceLocation(),
3766 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003767}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003768
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003769void ASTWriter::FlushCXXBaseSpecifiers() {
3770 RecordData Record;
3771 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3772 Record.clear();
3773
3774 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00003775 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003776 if (Index == CXXBaseSpecifiersOffsets.size())
3777 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3778 else {
3779 if (Index > CXXBaseSpecifiersOffsets.size())
3780 CXXBaseSpecifiersOffsets.resize(Index + 1);
3781 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3782 }
3783
3784 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3785 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3786 Record.push_back(BEnd - B);
3787 for (; B != BEnd; ++B)
3788 AddCXXBaseSpecifier(*B, Record);
3789 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00003790
3791 // Flush any expressions that were written as part of the base specifiers.
3792 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003793 }
3794
3795 CXXBaseSpecifiersToWrite.clear();
3796}
3797
Alexis Hunt1d792652011-01-08 20:30:50 +00003798void ASTWriter::AddCXXCtorInitializers(
3799 const CXXCtorInitializer * const *CtorInitializers,
3800 unsigned NumCtorInitializers,
3801 RecordDataImpl &Record) {
3802 Record.push_back(NumCtorInitializers);
3803 for (unsigned i=0; i != NumCtorInitializers; ++i) {
3804 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003805
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003806 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00003807 Record.push_back(CTOR_INITIALIZER_BASE);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003808 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3809 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00003810 } else if (Init->isDelegatingInitializer()) {
3811 Record.push_back(CTOR_INITIALIZER_DELEGATING);
3812 AddDeclRef(Init->getTargetConstructor(), Record);
3813 } else if (Init->isMemberInitializer()){
3814 Record.push_back(CTOR_INITIALIZER_MEMBER);
3815 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003816 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00003817 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
3818 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003819 }
Francois Pichetd583da02010-12-04 09:14:42 +00003820
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003821 AddSourceLocation(Init->getMemberLocation(), Record);
3822 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003823 AddSourceLocation(Init->getLParenLoc(), Record);
3824 AddSourceLocation(Init->getRParenLoc(), Record);
3825 Record.push_back(Init->isWritten());
3826 if (Init->isWritten()) {
3827 Record.push_back(Init->getSourceOrder());
3828 } else {
3829 Record.push_back(Init->getNumArrayIndices());
3830 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3831 AddDeclRef(Init->getArrayIndex(i), Record);
3832 }
3833 }
3834}
3835
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003836void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3837 assert(D->DefinitionData);
3838 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3839 Record.push_back(Data.UserDeclaredConstructor);
3840 Record.push_back(Data.UserDeclaredCopyConstructor);
3841 Record.push_back(Data.UserDeclaredCopyAssignment);
3842 Record.push_back(Data.UserDeclaredDestructor);
3843 Record.push_back(Data.Aggregate);
3844 Record.push_back(Data.PlainOldData);
3845 Record.push_back(Data.Empty);
3846 Record.push_back(Data.Polymorphic);
3847 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00003848 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00003849 Record.push_back(Data.HasNoNonEmptyBases);
3850 Record.push_back(Data.HasPrivateFields);
3851 Record.push_back(Data.HasProtectedFields);
3852 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00003853 Record.push_back(Data.HasMutableFields);
Alexis Huntf479f1b2011-05-09 18:22:59 +00003854 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith111af8d2011-08-10 18:11:37 +00003855 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003856 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruthad7d4042011-04-23 23:10:33 +00003857 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003858 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruthad7d4042011-04-23 23:10:33 +00003859 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003860 Record.push_back(Data.HasTrivialDestructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00003861 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003862 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00003863 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003864 Record.push_back(Data.DeclaredDefaultConstructor);
3865 Record.push_back(Data.DeclaredCopyConstructor);
3866 Record.push_back(Data.DeclaredCopyAssignment);
3867 Record.push_back(Data.DeclaredDestructor);
Sebastian Redlb7448632011-08-31 13:59:56 +00003868 Record.push_back(Data.FailedImplicitMoveConstructor);
3869 Record.push_back(Data.FailedImplicitMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003870
3871 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003872 if (Data.NumBases > 0)
3873 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3874 Record);
3875
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003876 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3877 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003878 if (Data.NumVBases > 0)
3879 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3880 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003881
3882 AddUnresolvedSet(Data.Conversions, Record);
3883 AddUnresolvedSet(Data.VisibleConversions, Record);
3884 // Data.Definition is the owning decl, no need to write it.
3885 AddDeclRef(Data.FirstFriend, Record);
3886}
3887
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003888void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00003889 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00003890 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00003891 assert(FirstDeclID == NextDeclID &&
3892 FirstTypeID == NextTypeID &&
3893 FirstIdentID == NextIdentID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00003894 FirstSelectorID == NextSelectorID &&
Douglas Gregor91096292010-10-02 19:29:26 +00003895 FirstMacroID == NextMacroID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00003896 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00003897
Sebastian Redl07a89a82010-07-30 00:29:29 +00003898 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003899
Douglas Gregordf0c1512011-08-18 04:12:04 +00003900 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
3901 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
3902 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
3903 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
3904 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacroDefinitions();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003905 NextDeclID = FirstDeclID;
3906 NextTypeID = FirstTypeID;
3907 NextIdentID = FirstIdentID;
3908 NextSelectorID = FirstSelectorID;
3909 NextMacroID = FirstMacroID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00003910}
3911
Sebastian Redl539c5062010-08-18 23:57:32 +00003912void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00003913 IdentifierIDs[II] = ID;
Douglas Gregor68051a72011-02-11 00:26:14 +00003914 if (II->hasMacroDefinition())
3915 DeserializedMacroNames.push_back(II);
Sebastian Redlff4a2952010-07-23 23:49:55 +00003916}
3917
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003918void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003919 // Always take the highest-numbered type index. This copes with an interesting
3920 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003921 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003922 // keep the higher-numbered entry so that we can properly write it out to
3923 // the AST file.
3924 TypeIdx &StoredIdx = TypeIdxs[T];
3925 if (Idx.getIndex() >= StoredIdx.getIndex())
3926 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003927}
3928
Sebastian Redl539c5062010-08-18 23:57:32 +00003929void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003930 DeclIDs[D] = ID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003931}
Sebastian Redl834bb972010-08-04 17:20:04 +00003932
Sebastian Redl539c5062010-08-18 23:57:32 +00003933void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003934 SelectorIDs[S] = ID;
3935}
Douglas Gregor91096292010-10-02 19:29:26 +00003936
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003937void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00003938 MacroDefinition *MD) {
3939 MacroDefinitions[MD] = ID;
3940}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003941
3942void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3943 assert(D->isDefinition());
3944 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3945 // We are interested when a PCH decl is modified.
3946 if (RD->getPCHLevel() > 0) {
3947 // A forward reference was mutated into a definition. Rewrite it.
3948 // FIXME: This happens during template instantiation, should we
3949 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003950 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003951 }
3952
3953 for (CXXRecordDecl::redecl_iterator
3954 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3955 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3956 if (Redecl == RD)
3957 continue;
3958
3959 // We are interested when a PCH decl is modified.
3960 if (Redecl->getPCHLevel() > 0) {
3961 UpdateRecord &Record = DeclUpdates[Redecl];
3962 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3963 assert(Redecl->DefinitionData);
3964 assert(Redecl->DefinitionData->Definition == D);
3965 AddDeclRef(D, Record); // the DefinitionDecl
3966 }
3967 }
3968 }
3969}
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003970void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3971 // TU and namespaces are handled elsewhere.
3972 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3973 return;
3974
3975 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3976 return; // Not a source decl added to a DeclContext from PCH.
3977
3978 AddUpdatedDeclContext(DC);
3979}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003980
3981void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3982 assert(D->isImplicit());
3983 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3984 return; // Not a source member added to a class from PCH.
3985 if (!isa<CXXMethodDecl>(D))
3986 return; // We are interested in lazily declared implicit methods.
3987
3988 // A decl coming from PCH was modified.
3989 assert(RD->isDefinition());
3990 UpdateRecord &Record = DeclUpdates[RD];
3991 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3992 AddDeclRef(D, Record);
3993}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003994
3995void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3996 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00003997 // The specializations set is kept in the canonical template.
3998 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003999 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
4000 return; // Not a source specialization added to a template from PCH.
4001
4002 UpdateRecord &Record = DeclUpdates[TD];
4003 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
4004 AddDeclRef(D, Record);
4005}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00004006
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004007void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4008 const FunctionDecl *D) {
4009 // The specializations set is kept in the canonical template.
4010 TD = TD->getCanonicalDecl();
4011 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
4012 return; // Not a source specialization added to a template from PCH.
4013
4014 UpdateRecord &Record = DeclUpdates[TD];
4015 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
4016 AddDeclRef(D, Record);
4017}
4018
Sebastian Redlab238a72011-04-24 16:28:06 +00004019void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
4020 if (D->getPCHLevel() == 0)
4021 return; // Declaration not imported from PCH.
4022
4023 // Implicit decl from a PCH was defined.
4024 // FIXME: Should implicit definition be a separate FunctionDecl?
4025 RewriteDecl(D);
4026}
4027
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004028void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
4029 if (D->getPCHLevel() == 0)
4030 return;
4031
4032 // Since the actual instantiation is delayed, this really means that we need
4033 // to update the instantiation location.
4034 UpdateRecord &Record = DeclUpdates[D];
4035 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4036 AddSourceLocation(
4037 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4038}
4039
Douglas Gregorf88e35b2010-11-30 06:16:57 +00004040ASTSerializationListener::~ASTSerializationListener() { }