blob: acd05006c3404f321195380ef0f1f40289980673 [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);
1137 Record.push_back(LangOpts.ObjCInferRelatedReturnType);
Sebastian Redl539c5062010-08-18 23:57:32 +00001138 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +00001139}
1140
Douglas Gregora7f71a92009-04-10 03:52:48 +00001141//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00001142// stat cache Serialization
1143//===----------------------------------------------------------------------===//
1144
1145namespace {
1146// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001147class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001148public:
1149 typedef const char * key_type;
1150 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001151
Chris Lattner2a6fa472010-11-23 19:28:12 +00001152 typedef struct stat data_type;
1153 typedef const data_type &data_type_ref;
Douglas Gregorc5046832009-04-27 18:38:38 +00001154
1155 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001156 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001157 }
Mike Stump11289f42009-09-09 15:08:12 +00001158
1159 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001160 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorc5046832009-04-27 18:38:38 +00001161 data_type_ref Data) {
1162 unsigned StrLen = strlen(path);
1163 clang::io::Emit16(Out, StrLen);
Chris Lattner2a6fa472010-11-23 19:28:12 +00001164 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregorc5046832009-04-27 18:38:38 +00001165 clang::io::Emit8(Out, DataLen);
1166 return std::make_pair(StrLen + 1, DataLen);
1167 }
Mike Stump11289f42009-09-09 15:08:12 +00001168
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001169 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001170 Out.write(path, KeyLen);
1171 }
Mike Stump11289f42009-09-09 15:08:12 +00001172
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001173 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorc5046832009-04-27 18:38:38 +00001174 data_type_ref Data, unsigned DataLen) {
1175 using namespace clang::io;
1176 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +00001177
Chris Lattner2a6fa472010-11-23 19:28:12 +00001178 Emit32(Out, (uint32_t) Data.st_ino);
1179 Emit32(Out, (uint32_t) Data.st_dev);
1180 Emit16(Out, (uint16_t) Data.st_mode);
1181 Emit64(Out, (uint64_t) Data.st_mtime);
1182 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregorc5046832009-04-27 18:38:38 +00001183
1184 assert(Out.tell() - Start == DataLen && "Wrong data length");
1185 }
1186};
1187} // end anonymous namespace
1188
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001189/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001190void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001191 // Build the on-disk hash table containing information about every
1192 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001193 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +00001194 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001195 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +00001196 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001197 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001198 StringRef Filename = Stat->first();
Chris Lattnerd386df42011-07-14 18:24:21 +00001199 Generator.insert(Filename.data(), Stat->second);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001200 }
Mike Stump11289f42009-09-09 15:08:12 +00001201
Douglas Gregorc5046832009-04-27 18:38:38 +00001202 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001203 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +00001204 uint32_t BucketOffset;
1205 {
1206 llvm::raw_svector_ostream Out(StatCacheData);
1207 // Make sure that no bucket is at offset 0
1208 clang::io::Emit32(Out, 0);
1209 BucketOffset = Generator.Emit(Out);
1210 }
1211
1212 // Create a blob abbreviation
1213 using namespace llvm;
1214 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001215 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +00001216 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1217 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1218 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1219 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1220
1221 // Write the stat cache
1222 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001223 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +00001224 Record.push_back(BucketOffset);
1225 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001226 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +00001227}
1228
1229//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +00001230// Source Manager Serialization
1231//===----------------------------------------------------------------------===//
1232
1233/// \brief Create an abbreviation for the SLocEntry that refers to a
1234/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001235static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001236 using namespace llvm;
1237 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001238 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001239 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1240 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1241 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1242 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001243 // FileEntry fields.
1244 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1245 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001246 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
Douglas Gregora7f71a92009-04-10 03:52:48 +00001247 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001248 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001249}
1250
1251/// \brief Create an abbreviation for the SLocEntry that refers to a
1252/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001253static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001254 using namespace llvm;
1255 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001256 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001257 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1258 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1259 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1260 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1261 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001262 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001263}
1264
1265/// \brief Create an abbreviation for the SLocEntry that refers to a
1266/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001267static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001268 using namespace llvm;
1269 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001270 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001271 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001272 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001273}
1274
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001275/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1276/// expansion.
1277static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001278 using namespace llvm;
1279 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001280 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001281 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1282 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1283 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1284 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001285 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001286 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001287}
1288
Douglas Gregor09b69892011-02-10 17:09:37 +00001289namespace {
1290 // Trait used for the on-disk hash table of header search information.
1291 class HeaderFileInfoTrait {
1292 ASTWriter &Writer;
1293 HeaderSearch &HS;
1294
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001295 // Keep track of the framework names we've used during serialization.
1296 SmallVector<char, 128> FrameworkStringData;
1297 llvm::StringMap<unsigned> FrameworkNameOffset;
1298
Douglas Gregor09b69892011-02-10 17:09:37 +00001299 public:
1300 HeaderFileInfoTrait(ASTWriter &Writer, HeaderSearch &HS)
1301 : Writer(Writer), HS(HS) { }
1302
1303 typedef const char *key_type;
1304 typedef key_type key_type_ref;
1305
1306 typedef HeaderFileInfo data_type;
1307 typedef const data_type &data_type_ref;
1308
1309 static unsigned ComputeHash(const char *path) {
1310 // The hash is based only on the filename portion of the key, so that the
1311 // reader can match based on filenames when symlinking or excess path
1312 // elements ("foo/../", "../") change the form of the name. However,
1313 // complete path is still the key.
1314 return llvm::HashString(llvm::sys::path::filename(path));
1315 }
1316
1317 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001318 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor09b69892011-02-10 17:09:37 +00001319 data_type_ref Data) {
1320 unsigned StrLen = strlen(path);
1321 clang::io::Emit16(Out, StrLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001322 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregor09b69892011-02-10 17:09:37 +00001323 clang::io::Emit8(Out, DataLen);
1324 return std::make_pair(StrLen + 1, DataLen);
1325 }
1326
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001327 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor09b69892011-02-10 17:09:37 +00001328 Out.write(path, KeyLen);
1329 }
1330
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001331 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor09b69892011-02-10 17:09:37 +00001332 data_type_ref Data, unsigned DataLen) {
1333 using namespace clang::io;
1334 uint64_t Start = Out.tell(); (void)Start;
1335
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001336 unsigned char Flags = (Data.isImport << 5)
1337 | (Data.isPragmaOnce << 4)
1338 | (Data.DirInfo << 2)
1339 | (Data.Resolved << 1)
1340 | Data.IndexHeaderMapHeader;
Douglas Gregor09b69892011-02-10 17:09:37 +00001341 Emit8(Out, (uint8_t)Flags);
1342 Emit16(Out, (uint16_t) Data.NumIncludes);
1343
1344 if (!Data.ControllingMacro)
1345 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1346 else
1347 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001348
1349 unsigned Offset = 0;
1350 if (!Data.Framework.empty()) {
1351 // If this header refers into a framework, save the framework name.
1352 llvm::StringMap<unsigned>::iterator Pos
1353 = FrameworkNameOffset.find(Data.Framework);
1354 if (Pos == FrameworkNameOffset.end()) {
1355 Offset = FrameworkStringData.size() + 1;
1356 FrameworkStringData.append(Data.Framework.begin(),
1357 Data.Framework.end());
1358 FrameworkStringData.push_back(0);
1359
1360 FrameworkNameOffset[Data.Framework] = Offset;
1361 } else
1362 Offset = Pos->second;
1363 }
1364 Emit32(Out, Offset);
1365
Douglas Gregor09b69892011-02-10 17:09:37 +00001366 assert(Out.tell() - Start == DataLen && "Wrong data length");
1367 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001368
1369 const char *strings_begin() const { return FrameworkStringData.begin(); }
1370 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001371 };
1372} // end anonymous namespace
1373
1374/// \brief Write the header search block for the list of files that
1375///
1376/// \param HS The header search structure to save.
1377///
1378/// \param Chain Whether we're creating a chained AST file.
Douglas Gregorc567ba22011-07-22 16:35:34 +00001379void ASTWriter::WriteHeaderSearch(HeaderSearch &HS, StringRef isysroot) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001380 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001381 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1382
1383 if (FilesByUID.size() > HS.header_file_size())
1384 FilesByUID.resize(HS.header_file_size());
1385
1386 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1387 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001388 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001389 unsigned NumHeaderSearchEntries = 0;
1390 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1391 const FileEntry *File = FilesByUID[UID];
1392 if (!File)
1393 continue;
1394
1395 const HeaderFileInfo &HFI = HS.header_file_begin()[UID];
1396 if (HFI.External && Chain)
1397 continue;
1398
1399 // Turn the file name into an absolute path, if it isn't already.
1400 const char *Filename = File->getName();
1401 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1402
1403 // If we performed any translation on the file name at all, we need to
1404 // save this string, since the generator will refer to it later.
1405 if (Filename != File->getName()) {
1406 Filename = strdup(Filename);
1407 SavedStrings.push_back(Filename);
1408 }
1409
1410 Generator.insert(Filename, HFI, GeneratorTrait);
1411 ++NumHeaderSearchEntries;
1412 }
1413
1414 // Create the on-disk hash table in a buffer.
1415 llvm::SmallString<4096> TableData;
1416 uint32_t BucketOffset;
1417 {
1418 llvm::raw_svector_ostream Out(TableData);
1419 // Make sure that no bucket is at offset 0
1420 clang::io::Emit32(Out, 0);
1421 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1422 }
1423
1424 // Create a blob abbreviation
1425 using namespace llvm;
1426 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1427 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1428 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1429 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001430 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001431 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1432 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1433
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001434 // Write the header search table
Douglas Gregor09b69892011-02-10 17:09:37 +00001435 RecordData Record;
1436 Record.push_back(HEADER_SEARCH_TABLE);
1437 Record.push_back(BucketOffset);
1438 Record.push_back(NumHeaderSearchEntries);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001439 Record.push_back(TableData.size());
1440 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregor09b69892011-02-10 17:09:37 +00001441 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1442
1443 // Free all of the strings we had to duplicate.
1444 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1445 free((void*)SavedStrings[I]);
1446}
1447
Douglas Gregora7f71a92009-04-10 03:52:48 +00001448/// \brief Writes the block containing the serialized form of the
1449/// source manager.
1450///
1451/// TODO: We should probably use an on-disk hash table (stored in a
1452/// blob), indexed based on the file name, so that we only create
1453/// entries for files that we actually need. In the common case (no
1454/// errors), we probably won't have to create file entries for any of
1455/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001456void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001457 const Preprocessor &PP,
Douglas Gregorc567ba22011-07-22 16:35:34 +00001458 StringRef isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001459 RecordData Record;
1460
Chris Lattner0910e3b2009-04-10 17:16:57 +00001461 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001462 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001463
1464 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001465 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1466 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1467 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001468 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001469
Douglas Gregor258ae542009-04-27 06:38:32 +00001470 // Write out the source location entry table. We skip the first
1471 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001472 std::vector<uint32_t> SLocEntryOffsets;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001473 // Write out the offsets of only source location file entries.
1474 // We will go through them in ASTReader::validateFileEntries().
1475 std::vector<uint32_t> SLocFileEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001476 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001477 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1478 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001479 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001480 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001481 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001482
Douglas Gregor258ae542009-04-27 06:38:32 +00001483 // Record the offset of this source-location entry.
1484 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1485
1486 // Figure out which record code to use.
1487 unsigned Code;
1488 if (SLoc->isFile()) {
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001489 if (SLoc->getFile().getContentCache()->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001490 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001491 SLocFileEntryOffsets.push_back(Stream.GetCurrentBitNo());
1492 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001493 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001494 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001495 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001496 Record.clear();
1497 Record.push_back(Code);
1498
Douglas Gregor925296b2011-07-19 16:10:42 +00001499 // Starting offset of this entry within this module, so skip the dummy.
1500 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001501 if (SLoc->isFile()) {
1502 const SrcMgr::FileInfo &File = SLoc->getFile();
1503 Record.push_back(File.getIncludeLoc().getRawEncoding());
1504 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1505 Record.push_back(File.hasLineDirectives());
1506
1507 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001508 if (Content->OrigEntry) {
1509 assert(Content->OrigEntry == Content->ContentsEntry &&
1510 "Writing to AST an overriden file is not supported");
1511
Douglas Gregor258ae542009-04-27 06:38:32 +00001512 // The source location entry is a file. The blob associated
1513 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001514
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001515 // Emit size/modification time for this file.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001516 Record.push_back(Content->OrigEntry->getSize());
1517 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001518
Argyrios Kyrtzidis61ef3db2011-08-21 23:33:04 +00001519 Record.push_back(File.NumCreatedFIDs);
1520
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001521 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001522 const char *Filename = Content->OrigEntry->getName();
Michael J. Spencer740857f2010-12-21 16:45:57 +00001523 llvm::SmallString<128> FilePath(Filename);
Anders Carlssona4267052011-03-08 16:04:35 +00001524
1525 // Ask the file manager to fixup the relative path for us. This will
1526 // honor the working directory.
1527 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1528
1529 // FIXME: This call to make_absolute shouldn't be necessary, the
1530 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencer740857f2010-12-21 16:45:57 +00001531 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001532 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001533
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001534 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001535 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001536 } else {
1537 // The source location entry is a buffer. The blob associated
1538 // with this entry contains the contents of the buffer.
1539
1540 // We add one to the size so that we capture the trailing NULL
1541 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1542 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001543 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001544 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001545 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001546 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001547 StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001548 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001549 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001550 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001551 StringRef(Buffer->getBufferStart(),
Daniel Dunbar8100d012009-08-24 09:31:37 +00001552 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001553
Douglas Gregor925296b2011-07-19 16:10:42 +00001554 if (strcmp(Name, "<built-in>") == 0) {
1555 PreloadSLocs.push_back(SLocEntryOffsets.size());
1556 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001557 }
1558 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001559 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001560 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth73ee5d72011-07-26 04:41:47 +00001561 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1562 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
Argyrios Kyrtzidisa1d943a2011-08-17 00:31:14 +00001563 Record.push_back(Expansion.isMacroArgExpansion() ? 0
1564 : Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor258ae542009-04-27 06:38:32 +00001565
1566 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001567 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001568 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001569 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001570 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001571 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001572 }
1573 }
1574
Douglas Gregor8f45df52009-04-16 22:23:12 +00001575 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001576
1577 if (SLocEntryOffsets.empty())
1578 return;
1579
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001580 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001581 // table is used for lazily loading source-location information.
1582 using namespace llvm;
1583 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001584 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001585 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00001586 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00001587 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1588 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001589
Douglas Gregor258ae542009-04-27 06:38:32 +00001590 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001591 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001592 Record.push_back(SLocEntryOffsets.size());
Douglas Gregor925296b2011-07-19 16:10:42 +00001593 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001594 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001595
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001596 Abbrev = new BitCodeAbbrev();
1597 Abbrev->Add(BitCodeAbbrevOp(FILE_SOURCE_LOCATION_OFFSETS));
1598 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1599 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1600 unsigned SLocFileOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1601
1602 Record.clear();
1603 Record.push_back(FILE_SOURCE_LOCATION_OFFSETS);
1604 Record.push_back(SLocFileEntryOffsets.size());
1605 Stream.EmitRecordWithBlob(SLocFileOffsetsAbbrev, Record,
1606 data(SLocFileEntryOffsets));
1607
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001608 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001609 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001610 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00001611
1612 // Write the line table. It depends on remapping working, so it must come
1613 // after the source location offsets.
1614 if (SourceMgr.hasLineTable()) {
1615 LineTableInfo &LineTable = SourceMgr.getLineTable();
1616
1617 Record.clear();
1618 // Emit the file names
1619 Record.push_back(LineTable.getNumFilenames());
1620 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1621 // Emit the file name
1622 const char *Filename = LineTable.getFilename(I);
1623 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1624 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1625 Record.push_back(FilenameLen);
1626 if (FilenameLen)
1627 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1628 }
1629
1630 // Emit the line entries
1631 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1632 L != LEnd; ++L) {
1633 // Only emit entries for local files.
1634 if (L->first < 0)
1635 continue;
1636
1637 // Emit the file ID
1638 Record.push_back(L->first);
1639
1640 // Emit the line entries
1641 Record.push_back(L->second.size());
1642 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1643 LEEnd = L->second.end();
1644 LE != LEEnd; ++LE) {
1645 Record.push_back(LE->FileOffset);
1646 Record.push_back(LE->LineNo);
1647 Record.push_back(LE->FilenameID);
1648 Record.push_back((unsigned)LE->FileKind);
1649 Record.push_back(LE->IncludeOffset);
1650 }
1651 }
1652 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1653 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001654}
1655
Douglas Gregorc5046832009-04-27 18:38:38 +00001656//===----------------------------------------------------------------------===//
1657// Preprocessor Serialization
1658//===----------------------------------------------------------------------===//
1659
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001660static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1661 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1662 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1663 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1664 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1665 return X.first->getName().compare(Y.first->getName());
1666}
1667
Chris Lattnereeffaef2009-04-10 17:15:23 +00001668/// \brief Writes the block containing the serialized form of the
1669/// preprocessor.
1670///
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001671void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001672 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001673
Chris Lattner0af3ba12009-04-13 01:29:17 +00001674 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1675 if (PP.getCounterValue() != 0) {
1676 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001677 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001678 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001679 }
1680
1681 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001682 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001683
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001684 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001685 // FIXME: use diagnostics subsystem for localization etc.
1686 if (PP.SawDateOrTime())
1687 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001688
Douglas Gregor796d76a2010-10-20 22:00:55 +00001689
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001690 // Loop over all the macro definitions that are live at the end of the file,
1691 // emitting each to the PP section.
Douglas Gregoraae92242010-03-19 21:51:54 +00001692 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001693
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001694 // Construct the list of macro definitions that need to be serialized.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001695 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001696 MacrosToEmit;
1697 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor68051a72011-02-11 00:26:14 +00001698 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1699 E = PP.macro_end(Chain == 0);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001700 I != E; ++I) {
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001701 MacroDefinitionsSeen.insert(I->first);
1702 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1703 }
1704
1705 // Sort the set of macro definitions that need to be serialized by the
1706 // name of the macro, to provide a stable ordering.
1707 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1708 &compareMacroDefinitions);
1709
Douglas Gregor68051a72011-02-11 00:26:14 +00001710 // Resolve any identifiers that defined macros at the time they were
1711 // deserialized, adding them to the list of macros to emit (if appropriate).
1712 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1713 IdentifierInfo *Name
1714 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1715 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1716 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1717 }
1718
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001719 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1720 const IdentifierInfo *Name = MacrosToEmit[I].first;
1721 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor68051a72011-02-11 00:26:14 +00001722 if (!MI)
1723 continue;
1724
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001725 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001726 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001727 // Also skip macros from a AST file if we're chaining.
Douglas Gregoreb114da2010-10-01 01:03:07 +00001728
1729 // FIXME: There is a (probably minor) optimization we could do here, if
1730 // the macro comes from the original PCH but the identifier comes from a
1731 // chained PCH, by storing the offset into the original PCH rather than
1732 // writing the macro definition a second time.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001733 if (MI->isBuiltinMacro() ||
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001734 (Chain && Name->isFromAST() && MI->isFromAST()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001735 continue;
1736
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001737 AddIdentifierRef(Name, Record);
1738 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001739 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1740 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001741
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001742 unsigned Code;
1743 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001744 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001745 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001746 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001747
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001748 Record.push_back(MI->isC99Varargs());
1749 Record.push_back(MI->isGNUVarargs());
1750 Record.push_back(MI->getNumArgs());
1751 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1752 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001753 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001754 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001755
Douglas Gregoraae92242010-03-19 21:51:54 +00001756 // If we have a detailed preprocessing record, record the macro definition
1757 // ID that corresponds to this macro.
1758 if (PPRec)
1759 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001760
Douglas Gregor8f45df52009-04-16 22:23:12 +00001761 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001762 Record.clear();
1763
Chris Lattner2199f5b2009-04-10 18:08:30 +00001764 // Emit the tokens array.
1765 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1766 // Note that we know that the preprocessor does not have any annotation
1767 // tokens in it because they are created by the parser, and thus can't be
1768 // in a macro definition.
1769 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001770
Chris Lattner2199f5b2009-04-10 18:08:30 +00001771 Record.push_back(Tok.getLocation().getRawEncoding());
1772 Record.push_back(Tok.getLength());
1773
Chris Lattner2199f5b2009-04-10 18:08:30 +00001774 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1775 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001776 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001777 // FIXME: Should translate token kind to a stable encoding.
1778 Record.push_back(Tok.getKind());
1779 // FIXME: Should translate token flags to a stable encoding.
1780 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001781
Sebastian Redl539c5062010-08-18 23:57:32 +00001782 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001783 Record.clear();
1784 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001785 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001786 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001787 Stream.ExitBlock();
1788
1789 if (PPRec)
1790 WritePreprocessorDetail(*PPRec);
1791}
1792
1793void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
1794 if (PPRec.begin(Chain) == PPRec.end(Chain))
1795 return;
1796
1797 // Enter the preprocessor block.
1798 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001799
Douglas Gregoraae92242010-03-19 21:51:54 +00001800 // If the preprocessor has a preprocessing record, emit it.
1801 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001802 using namespace llvm;
1803
1804 // Set up the abbreviation for
1805 unsigned InclusionAbbrev = 0;
1806 {
1807 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1808 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
1809 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1810 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1811 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1812 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1813 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1814 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1815 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1816 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1817 }
1818
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001819 unsigned FirstPreprocessorEntityID
1820 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1821 + NUM_PREDEF_PP_ENTITY_IDS;
1822 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001823 RecordData Record;
Douglas Gregord32f0352011-07-22 06:10:01 +00001824 uint64_t BitsInChain = Chain? Chain->TotalModulesSizeInBits : 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001825 for (PreprocessingRecord::iterator E = PPRec.begin(Chain),
1826 EEnd = PPRec.end(Chain);
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001827 E != EEnd;
1828 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001829 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001830
Douglas Gregor92a96f52011-02-08 21:58:10 +00001831 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1832 // Record this macro definition's location.
1833 MacroID ID = getMacroDefinitionID(MD);
1834
1835 // Don't write the macro definition if it is from another AST file.
1836 if (ID < FirstMacroID)
Douglas Gregoraae92242010-03-19 21:51:54 +00001837 continue;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001838
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001839 // Notify the serialization listener that we're serializing this entity.
1840 if (SerializationListener)
1841 SerializationListener->SerializedPreprocessedEntity(*E,
Douglas Gregord32f0352011-07-22 06:10:01 +00001842 BitsInChain + Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001843
Douglas Gregor92a96f52011-02-08 21:58:10 +00001844 unsigned Position = ID - FirstMacroID;
1845 if (Position != MacroDefinitionOffsets.size()) {
1846 if (Position > MacroDefinitionOffsets.size())
1847 MacroDefinitionOffsets.resize(Position + 1);
1848
1849 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
1850 } else
1851 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001852
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001853 Record.push_back(NextPreprocessorEntityID);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001854 Record.push_back(ID);
1855 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1856 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1857 AddIdentifierRef(MD->getName(), Record);
1858 AddSourceLocation(MD->getLocation(), Record);
1859 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1860 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00001861 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001862
Douglas Gregor92a96f52011-02-08 21:58:10 +00001863 // Notify the serialization listener that we're serializing this entity.
1864 if (SerializationListener)
1865 SerializationListener->SerializedPreprocessedEntity(*E,
Douglas Gregord32f0352011-07-22 06:10:01 +00001866 BitsInChain + Stream.GetCurrentBitNo());
Douglas Gregor92a96f52011-02-08 21:58:10 +00001867
Chandler Carrutha88a22182011-07-14 08:20:46 +00001868 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001869 Record.push_back(NextPreprocessorEntityID);
Chandler Carrutha88a22182011-07-14 08:20:46 +00001870 AddSourceLocation(ME->getSourceRange().getBegin(), Record);
1871 AddSourceLocation(ME->getSourceRange().getEnd(), Record);
1872 AddIdentifierRef(ME->getName(), Record);
1873 Record.push_back(getMacroDefinitionID(ME->getDefinition()));
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001874 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001875 continue;
1876 }
1877
1878 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1879 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor0d4b4312011-08-04 17:06:18 +00001880 Record.push_back(NextPreprocessorEntityID);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001881 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1882 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1883 Record.push_back(ID->getFileName().size());
1884 Record.push_back(ID->wasInQuotes());
1885 Record.push_back(static_cast<unsigned>(ID->getKind()));
1886 llvm::SmallString<64> Buffer;
1887 Buffer += ID->getFileName();
1888 Buffer += ID->getFile()->getName();
1889 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1890 continue;
1891 }
1892
1893 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1894 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001895 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001896
Douglas Gregoraae92242010-03-19 21:51:54 +00001897 // Write the offsets table for the preprocessing record.
1898 if (NumPreprocessingRecords > 0) {
1899 // Write the offsets table for identifier IDs.
1900 using namespace llvm;
1901 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001902 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregoraae92242010-03-19 21:51:54 +00001903 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001904 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregoraae92242010-03-19 21:51:54 +00001905 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
Douglas Gregora863b4b2011-08-04 16:36:56 +00001906 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first macro def
Douglas Gregoraae92242010-03-19 21:51:54 +00001907 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1908 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001909
Douglas Gregoraae92242010-03-19 21:51:54 +00001910 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001911 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001912 Record.push_back(NumPreprocessingRecords);
Douglas Gregor2f555fc2011-08-04 18:56:47 +00001913 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001914 Record.push_back(MacroDefinitionOffsets.size());
Douglas Gregora863b4b2011-08-04 16:36:56 +00001915 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001916 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001917 data(MacroDefinitionOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00001918 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001919}
1920
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001921void ASTWriter::WritePragmaDiagnosticMappings(const Diagnostic &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001922 RecordData Record;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001923 for (Diagnostic::DiagStatePointsTy::const_iterator
1924 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1925 I != E; ++I) {
1926 const Diagnostic::DiagStatePoint &point = *I;
1927 if (point.Loc.isInvalid())
1928 continue;
1929
1930 Record.push_back(point.Loc.getRawEncoding());
1931 for (Diagnostic::DiagState::iterator
1932 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
1933 unsigned diag = I->first, map = I->second;
1934 if (map & 0x10) { // mapping from a diagnostic pragma.
1935 Record.push_back(diag);
1936 Record.push_back(map & 0x7);
1937 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001938 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001939 Record.push_back(-1); // mark the end of the diag/map pairs for this
1940 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001941 }
1942
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00001943 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001944 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001945}
1946
Anders Carlsson9bb83e82011-03-06 18:41:18 +00001947void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
1948 if (CXXBaseSpecifiersOffsets.empty())
1949 return;
1950
1951 RecordData Record;
1952
1953 // Create a blob abbreviation for the C++ base specifiers offsets.
1954 using namespace llvm;
1955
1956 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1957 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
1958 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
1959 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1960 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1961
Douglas Gregorc27b2872011-08-04 00:01:48 +00001962 // Write the base specifier offsets table.
Anders Carlsson9bb83e82011-03-06 18:41:18 +00001963 Record.clear();
1964 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
1965 Record.push_back(CXXBaseSpecifiersOffsets.size());
1966 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001967 data(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00001968}
1969
Douglas Gregorc5046832009-04-27 18:38:38 +00001970//===----------------------------------------------------------------------===//
1971// Type Serialization
1972//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001973
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001974/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001975void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00001976 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001977 if (Idx.getIndex() == 0) // we haven't seen this type before.
1978 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00001979
Douglas Gregor9b3932c2010-10-05 18:37:06 +00001980 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00001981
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001982 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001983 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001984 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001985 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001986 else if (TypeOffsets.size() < Index) {
1987 TypeOffsets.resize(Index + 1);
1988 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001989 }
1990
1991 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001992
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001993 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001994 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001995
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001996 if (T.hasLocalNonFastQualifiers()) {
1997 Qualifiers Qs = T.getLocalQualifiers();
1998 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001999 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00002000 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00002001 } else {
2002 switch (T->getTypeClass()) {
2003 // For all of the concrete, non-dependent types, call the
2004 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002005#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00002006 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002007#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002008#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00002009 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002010 }
2011
2012 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002013 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00002014
2015 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002016 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002017}
2018
Douglas Gregorc5046832009-04-27 18:38:38 +00002019//===----------------------------------------------------------------------===//
2020// Declaration Serialization
2021//===----------------------------------------------------------------------===//
2022
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002023/// \brief Write the block containing all of the declaration IDs
2024/// lexically declared within the given DeclContext.
2025///
2026/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2027/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002028uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002029 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002030 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002031 return 0;
2032
Douglas Gregor8f45df52009-04-16 22:23:12 +00002033 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002034 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002035 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002036 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002037 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2038 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002039 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002040
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002041 ++NumLexicalDeclContexts;
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002042 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002043 return Offset;
2044}
2045
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002046void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002047 using namespace llvm;
2048 RecordData Record;
2049
2050 // Write the type offsets array
2051 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002052 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002053 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002054 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002055 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2056 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2057 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002058 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002059 Record.push_back(TypeOffsets.size());
Douglas Gregor5204bde2011-08-02 16:26:37 +00002060 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002061 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002062
2063 // Write the declaration offsets array
2064 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002065 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002066 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002067 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002068 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2069 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2070 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002071 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002072 Record.push_back(DeclOffsets.size());
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002073 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002074 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002075}
2076
Douglas Gregorc5046832009-04-27 18:38:38 +00002077//===----------------------------------------------------------------------===//
2078// Global Method Pool and Selector Serialization
2079//===----------------------------------------------------------------------===//
2080
Douglas Gregore84a9da2009-04-20 20:36:09 +00002081namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002082// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002083class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002084 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002085
2086public:
2087 typedef Selector key_type;
2088 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002089
Sebastian Redl834bb972010-08-04 17:20:04 +00002090 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002091 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002092 ObjCMethodList Instance, Factory;
2093 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002094 typedef const data_type& data_type_ref;
2095
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002096 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002097
Douglas Gregorc78d3462009-04-24 21:10:55 +00002098 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002099 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002100 }
Mike Stump11289f42009-09-09 15:08:12 +00002101
2102 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002103 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002104 data_type_ref Methods) {
2105 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2106 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002107 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2108 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002109 Method = Method->Next)
2110 if (Method->Method)
2111 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002112 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002113 Method = Method->Next)
2114 if (Method->Method)
2115 DataLen += 4;
2116 clang::io::Emit16(Out, DataLen);
2117 return std::make_pair(KeyLen, DataLen);
2118 }
Mike Stump11289f42009-09-09 15:08:12 +00002119
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002120 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00002121 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002122 assert((Start >> 32) == 0 && "Selector key offset too large");
2123 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002124 unsigned N = Sel.getNumArgs();
2125 clang::io::Emit16(Out, N);
2126 if (N == 0)
2127 N = 1;
2128 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00002129 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002130 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2131 }
Mike Stump11289f42009-09-09 15:08:12 +00002132
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002133 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002134 data_type_ref Methods, unsigned DataLen) {
2135 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00002136 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002137 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002138 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002139 Method = Method->Next)
2140 if (Method->Method)
2141 ++NumInstanceMethods;
2142
2143 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002144 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002145 Method = Method->Next)
2146 if (Method->Method)
2147 ++NumFactoryMethods;
2148
2149 clang::io::Emit16(Out, NumInstanceMethods);
2150 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00002151 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002152 Method = Method->Next)
2153 if (Method->Method)
2154 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002155 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002156 Method = Method->Next)
2157 if (Method->Method)
2158 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002159
2160 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002161 }
2162};
2163} // end anonymous namespace
2164
Sebastian Redla19a67f2010-08-03 21:58:15 +00002165/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002166///
2167/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002168/// in an on-disk hash table indexed by the selector. The hash table also
2169/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002170void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002171 using namespace llvm;
2172
Sebastian Redla19a67f2010-08-03 21:58:15 +00002173 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002174 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002175 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002176 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002177 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002178 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002179 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002180 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002181
Sebastian Redla19a67f2010-08-03 21:58:15 +00002182 // Create the on-disk hash table representation. We walk through every
2183 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002184 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002185 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002186 I = SelectorIDs.begin(), E = SelectorIDs.end();
2187 I != E; ++I) {
2188 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002189 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002190 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002191 I->second,
2192 ObjCMethodList(),
2193 ObjCMethodList()
2194 };
2195 if (F != SemaRef.MethodPool.end()) {
2196 Data.Instance = F->second.first;
2197 Data.Factory = F->second.second;
2198 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002199 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002200 // changed.
2201 if (Chain && I->second < FirstSelectorID) {
2202 // Selector already exists. Did it change?
2203 bool changed = false;
2204 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2205 M = M->Next) {
2206 if (M->Method->getPCHLevel() == 0)
2207 changed = true;
2208 }
2209 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2210 M = M->Next) {
2211 if (M->Method->getPCHLevel() == 0)
2212 changed = true;
2213 }
2214 if (!changed)
2215 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002216 } else if (Data.Instance.Method || Data.Factory.Method) {
2217 // A new method pool entry.
2218 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002219 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002220 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002221 }
2222
Douglas Gregorc78d3462009-04-24 21:10:55 +00002223 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002224 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002225 uint32_t BucketOffset;
2226 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002227 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002228 llvm::raw_svector_ostream Out(MethodPool);
2229 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002230 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002231 BucketOffset = Generator.Emit(Out, Trait);
2232 }
2233
2234 // Create a blob abbreviation
2235 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002236 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002237 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002238 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002239 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2240 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2241
Douglas Gregor95c13f52009-04-25 17:48:32 +00002242 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002243 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002244 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002245 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002246 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002247 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002248
2249 // Create a blob abbreviation for the selector table offsets.
2250 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002251 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002252 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002253 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00002254 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2255 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2256
2257 // Write the selector offsets table.
2258 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002259 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002260 Record.push_back(SelectorOffsets.size());
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002261 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002262 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002263 data(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002264 }
2265}
2266
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002267/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002268void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002269 using namespace llvm;
2270 if (SemaRef.ReferencedSelectors.empty())
2271 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002272
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002273 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002274
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002275 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002276 // very tricky to fix, and given that @selector shouldn't really appear in
2277 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002278 for (DenseMap<Selector, SourceLocation>::iterator S =
2279 SemaRef.ReferencedSelectors.begin(),
2280 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2281 Selector Sel = (*S).first;
2282 SourceLocation Loc = (*S).second;
2283 AddSelectorRef(Sel, Record);
2284 AddSourceLocation(Loc, Record);
2285 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002286 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002287}
2288
Douglas Gregorc5046832009-04-27 18:38:38 +00002289//===----------------------------------------------------------------------===//
2290// Identifier Table Serialization
2291//===----------------------------------------------------------------------===//
2292
Douglas Gregorc78d3462009-04-24 21:10:55 +00002293namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002294class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002295 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002296 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002297
Douglas Gregor1d583f22009-04-28 21:18:29 +00002298 /// \brief Determines whether this is an "interesting" identifier
2299 /// that needs a full IdentifierInfo structure written into the hash
2300 /// table.
2301 static bool isInterestingIdentifier(const IdentifierInfo *II) {
2302 return II->isPoisoned() ||
2303 II->isExtensionToken() ||
2304 II->hasMacroDefinition() ||
2305 II->getObjCOrBuiltinID() ||
2306 II->getFETokenInfo<void>();
2307 }
2308
Douglas Gregore84a9da2009-04-20 20:36:09 +00002309public:
2310 typedef const IdentifierInfo* key_type;
2311 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002312
Sebastian Redl539c5062010-08-18 23:57:32 +00002313 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002314 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002315
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002316 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00002317 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00002318
2319 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00002320 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00002321 }
Mike Stump11289f42009-09-09 15:08:12 +00002322
2323 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002324 EmitKeyDataLength(raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002325 IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002326 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002327 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
2328 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00002329 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00002330 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00002331 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00002332 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002333 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2334 DEnd = IdentifierResolver::end();
2335 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00002336 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00002337 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00002338 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00002339 // We emit the key length after the data length so that every
2340 // string is preceded by a 16-bit length. This matches the PTH
2341 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00002342 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002343 return std::make_pair(KeyLen, DataLen);
2344 }
Mike Stump11289f42009-09-09 15:08:12 +00002345
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002346 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00002347 unsigned KeyLen) {
2348 // Record the location of the key data. This is used when generating
2349 // the mapping from persistent IDs to strings.
2350 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002351 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002352 }
Mike Stump11289f42009-09-09 15:08:12 +00002353
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002354 void EmitData(raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002355 IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00002356 if (!isInterestingIdentifier(II)) {
2357 clang::io::Emit32(Out, ID << 1);
2358 return;
2359 }
Douglas Gregorb9256522009-04-28 21:32:13 +00002360
Douglas Gregor1d583f22009-04-28 21:18:29 +00002361 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002362 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002363 bool hasMacroDefinition =
2364 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00002365 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00002366 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002367 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
2368 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2369 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00002370 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002371 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00002372 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002373
Douglas Gregorc3366a52009-04-21 23:56:24 +00002374 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00002375 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00002376
Douglas Gregora868bbd2009-04-21 22:25:48 +00002377 // Emit the declaration IDs in reverse order, because the
2378 // IdentifierResolver provides the declarations as they would be
2379 // visible (e.g., the function "stat" would come before the struct
2380 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2381 // adds declarations to the end of the list (so we need to see the
2382 // struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00002383 // Only emit declarations that aren't from a chained PCH, though.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002384 SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00002385 IdentifierResolver::end());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002386 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregora868bbd2009-04-21 22:25:48 +00002387 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00002388 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00002389 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002390 }
2391};
2392} // end anonymous namespace
2393
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002394/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002395///
2396/// The identifier table consists of a blob containing string data
2397/// (the actual identifiers themselves) and a separate "offsets" index
2398/// that maps identifier IDs to locations within the blob.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002399void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002400 using namespace llvm;
2401
2402 // Create and write out the blob that contains the identifier
2403 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002404 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002405 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002406 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump11289f42009-09-09 15:08:12 +00002407
Douglas Gregore6648fb2009-04-28 20:33:11 +00002408 // Look for any identifiers that were named while processing the
2409 // headers, but are otherwise not needed. We add these to the hash
2410 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002411 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00002412 // file.
2413 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2414 IDEnd = PP.getIdentifierTable().end();
2415 ID != IDEnd; ++ID)
2416 getIdentifierRef(ID->second);
2417
Sebastian Redlff4a2952010-07-23 23:49:55 +00002418 // Create the on-disk hash table representation. We only store offsets
2419 // for identifiers that appear here for the first time.
2420 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002421 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002422 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2423 ID != IDEnd; ++ID) {
2424 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002425 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002426 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002427 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002428
Douglas Gregore84a9da2009-04-20 20:36:09 +00002429 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002430 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00002431 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002432 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002433 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002434 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002435 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002436 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002437 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002438 }
2439
2440 // Create a blob abbreviation
2441 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002442 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00002443 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002444 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00002445 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002446
2447 // Write the identifier table
2448 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002449 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002450 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002451 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002452 }
2453
2454 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00002455 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002456 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00002457 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002458 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00002459 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2460 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2461
2462 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002463 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00002464 Record.push_back(IdentifierOffsets.size());
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002465 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor0e149972009-04-25 19:10:14 +00002466 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002467 data(IdentifierOffsets));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002468}
2469
Douglas Gregorc5046832009-04-27 18:38:38 +00002470//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002471// DeclContext's Name Lookup Table Serialization
2472//===----------------------------------------------------------------------===//
2473
2474namespace {
2475// Trait used for the on-disk hash table used in the method pool.
2476class ASTDeclContextNameLookupTrait {
2477 ASTWriter &Writer;
2478
2479public:
2480 typedef DeclarationName key_type;
2481 typedef key_type key_type_ref;
2482
2483 typedef DeclContext::lookup_result data_type;
2484 typedef const data_type& data_type_ref;
2485
2486 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2487
2488 unsigned ComputeHash(DeclarationName Name) {
2489 llvm::FoldingSetNodeID ID;
2490 ID.AddInteger(Name.getNameKind());
2491
2492 switch (Name.getNameKind()) {
2493 case DeclarationName::Identifier:
2494 ID.AddString(Name.getAsIdentifierInfo()->getName());
2495 break;
2496 case DeclarationName::ObjCZeroArgSelector:
2497 case DeclarationName::ObjCOneArgSelector:
2498 case DeclarationName::ObjCMultiArgSelector:
2499 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2500 break;
2501 case DeclarationName::CXXConstructorName:
2502 case DeclarationName::CXXDestructorName:
2503 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002504 break;
2505 case DeclarationName::CXXOperatorName:
2506 ID.AddInteger(Name.getCXXOverloadedOperator());
2507 break;
2508 case DeclarationName::CXXLiteralOperatorName:
2509 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2510 case DeclarationName::CXXUsingDirective:
2511 break;
2512 }
2513
2514 return ID.ComputeHash();
2515 }
2516
2517 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002518 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002519 data_type_ref Lookup) {
2520 unsigned KeyLen = 1;
2521 switch (Name.getNameKind()) {
2522 case DeclarationName::Identifier:
2523 case DeclarationName::ObjCZeroArgSelector:
2524 case DeclarationName::ObjCOneArgSelector:
2525 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002526 case DeclarationName::CXXLiteralOperatorName:
2527 KeyLen += 4;
2528 break;
2529 case DeclarationName::CXXOperatorName:
2530 KeyLen += 1;
2531 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002532 case DeclarationName::CXXConstructorName:
2533 case DeclarationName::CXXDestructorName:
2534 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002535 case DeclarationName::CXXUsingDirective:
2536 break;
2537 }
2538 clang::io::Emit16(Out, KeyLen);
2539
2540 // 2 bytes for num of decls and 4 for each DeclID.
2541 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2542 clang::io::Emit16(Out, DataLen);
2543
2544 return std::make_pair(KeyLen, DataLen);
2545 }
2546
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002547 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002548 using namespace clang::io;
2549
2550 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2551 Emit8(Out, Name.getNameKind());
2552 switch (Name.getNameKind()) {
2553 case DeclarationName::Identifier:
2554 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2555 break;
2556 case DeclarationName::ObjCZeroArgSelector:
2557 case DeclarationName::ObjCOneArgSelector:
2558 case DeclarationName::ObjCMultiArgSelector:
2559 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2560 break;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002561 case DeclarationName::CXXOperatorName:
2562 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2563 Emit8(Out, Name.getCXXOverloadedOperator());
2564 break;
2565 case DeclarationName::CXXLiteralOperatorName:
2566 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2567 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002568 case DeclarationName::CXXConstructorName:
2569 case DeclarationName::CXXDestructorName:
2570 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002571 case DeclarationName::CXXUsingDirective:
2572 break;
2573 }
2574 }
2575
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002576 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002577 data_type Lookup, unsigned DataLen) {
2578 uint64_t Start = Out.tell(); (void)Start;
2579 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2580 for (; Lookup.first != Lookup.second; ++Lookup.first)
2581 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2582
2583 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2584 }
2585};
2586} // end anonymous namespace
2587
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002588/// \brief Write the block containing all of the declaration IDs
2589/// visible from the given DeclContext.
2590///
2591/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002592/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002593uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2594 DeclContext *DC) {
2595 if (DC->getPrimaryContext() != DC)
2596 return 0;
2597
2598 // Since there is no name lookup into functions or methods, don't bother to
2599 // build a visible-declarations table for these entities.
2600 if (DC->isFunctionOrMethod())
2601 return 0;
2602
2603 // If not in C++, we perform name lookup for the translation unit via the
2604 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2605 // FIXME: In C++ we need the visible declarations in order to "see" the
2606 // friend declarations, is there a way to do this without writing the table ?
2607 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2608 return 0;
2609
2610 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00002611 if (DC->hasExternalVisibleStorage())
2612 DC->MaterializeVisibleDeclsFromExternalStorage();
2613 else
2614 DC->lookup(DeclarationName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002615
2616 // Serialize the contents of the mapping used for lookup. Note that,
2617 // although we have two very different code paths, the serialized
2618 // representation is the same for both cases: a declaration name,
2619 // followed by a size, followed by references to the visible
2620 // declarations that have that name.
2621 uint64_t Offset = Stream.GetCurrentBitNo();
2622 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2623 if (!Map || Map->empty())
2624 return 0;
2625
2626 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2627 ASTDeclContextNameLookupTrait Trait(*this);
2628
2629 // Create the on-disk hash table representation.
2630 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2631 D != DEnd; ++D) {
2632 DeclarationName Name = D->first;
2633 DeclContext::lookup_result Result = D->second.getLookupResult();
2634 Generator.insert(Name, Result, Trait);
2635 }
2636
2637 // Create the on-disk hash table in a buffer.
2638 llvm::SmallString<4096> LookupTable;
2639 uint32_t BucketOffset;
2640 {
2641 llvm::raw_svector_ostream Out(LookupTable);
2642 // Make sure that no bucket is at offset 0
2643 clang::io::Emit32(Out, 0);
2644 BucketOffset = Generator.Emit(Out, Trait);
2645 }
2646
2647 // Write the lookup table
2648 RecordData Record;
2649 Record.push_back(DECL_CONTEXT_VISIBLE);
2650 Record.push_back(BucketOffset);
2651 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2652 LookupTable.str());
2653
2654 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2655 ++NumVisibleDeclContexts;
2656 return Offset;
2657}
2658
Sebastian Redla4071b42010-08-24 00:50:09 +00002659/// \brief Write an UPDATE_VISIBLE block for the given context.
2660///
2661/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2662/// DeclContext in a dependent AST file. As such, they only exist for the TU
2663/// (in C++) and for namespaces.
2664void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00002665 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2666 if (!Map || Map->empty())
2667 return;
2668
2669 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2670 ASTDeclContextNameLookupTrait Trait(*this);
2671
2672 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002673 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2674 D != DEnd; ++D) {
2675 DeclarationName Name = D->first;
2676 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002677 // For any name that appears in this table, the results are complete, i.e.
2678 // they overwrite results from previous PCHs. Merging is always a mess.
2679 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002680 }
2681
2682 // Create the on-disk hash table in a buffer.
2683 llvm::SmallString<4096> LookupTable;
2684 uint32_t BucketOffset;
2685 {
2686 llvm::raw_svector_ostream Out(LookupTable);
2687 // Make sure that no bucket is at offset 0
2688 clang::io::Emit32(Out, 0);
2689 BucketOffset = Generator.Emit(Out, Trait);
2690 }
2691
2692 // Write the lookup table
2693 RecordData Record;
2694 Record.push_back(UPDATE_VISIBLE);
2695 Record.push_back(getDeclID(cast<Decl>(DC)));
2696 Record.push_back(BucketOffset);
2697 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2698}
2699
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002700/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2701void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2702 RecordData Record;
2703 Record.push_back(Opts.fp_contract);
2704 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2705}
2706
2707/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2708void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
2709 if (!SemaRef.Context.getLangOptions().OpenCL)
2710 return;
2711
2712 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2713 RecordData Record;
2714#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2715#include "clang/Basic/OpenCLExtensions.def"
2716 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2717}
2718
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002719//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00002720// General Serialization Routines
2721//===----------------------------------------------------------------------===//
2722
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002723/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002724void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00002725 Record.push_back(Attrs.size());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002726 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2727 const Attr * A = *i;
2728 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2729 AddSourceLocation(A->getLocation(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002730
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002731#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00002732
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002733 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002734}
2735
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002736void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002737 Record.push_back(Str.size());
2738 Record.insert(Record.end(), Str.begin(), Str.end());
2739}
2740
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002741void ASTWriter::AddVersionTuple(const VersionTuple &Version,
2742 RecordDataImpl &Record) {
2743 Record.push_back(Version.getMajor());
2744 if (llvm::Optional<unsigned> Minor = Version.getMinor())
2745 Record.push_back(*Minor + 1);
2746 else
2747 Record.push_back(0);
2748 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
2749 Record.push_back(*Subminor + 1);
2750 else
2751 Record.push_back(0);
2752}
2753
Douglas Gregore84a9da2009-04-20 20:36:09 +00002754/// \brief Note that the identifier II occurs at the given offset
2755/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002756void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002757 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002758 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00002759 // up earlier in the chain and thus don't need an offset.
2760 if (ID >= FirstIdentID)
2761 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002762}
2763
Douglas Gregor95c13f52009-04-25 17:48:32 +00002764/// \brief Note that the selector Sel occurs at the given offset
2765/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002766void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00002767 unsigned ID = SelectorIDs[Sel];
2768 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00002769 // Don't record offsets for selectors that are also available in a different
2770 // file.
2771 if (ID < FirstSelectorID)
2772 return;
2773 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002774}
2775
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002776ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregoreda8e122011-08-09 15:13:55 +00002777 : Stream(Stream), Context(0), Chain(0), SerializationListener(0),
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002778 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00002779 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002780 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002781 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregora863b4b2011-08-04 16:36:56 +00002782 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor91096292010-10-02 19:29:26 +00002783 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002784 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00002785 NumVisibleDeclContexts(0),
Douglas Gregorc27b2872011-08-04 00:01:48 +00002786 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00002787 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00002788 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
2789 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
2790 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00002791 DeclTypedefAbbrev(0),
2792 DeclVarAbbrev(0), DeclFieldAbbrev(0),
2793 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002794{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002795}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002796
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002797void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002798 const std::string &OutputFile,
Douglas Gregorc567ba22011-07-22 16:35:34 +00002799 StringRef isysroot) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002800 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002801 Stream.Emit((unsigned)'C', 8);
2802 Stream.Emit((unsigned)'P', 8);
2803 Stream.Emit((unsigned)'C', 8);
2804 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002805
Chris Lattner28fa4e62009-04-26 22:26:21 +00002806 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002807
Douglas Gregoreda8e122011-08-09 15:13:55 +00002808 Context = &SemaRef.Context;
Douglas Gregor851443c2011-08-12 01:39:19 +00002809 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile);
Douglas Gregoreda8e122011-08-09 15:13:55 +00002810 Context = 0;
Sebastian Redl143413f2010-07-12 22:02:52 +00002811}
2812
Douglas Gregora94a1542011-07-27 21:45:57 +00002813template<typename Vector>
2814static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
2815 ASTWriter::RecordData &Record) {
2816 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
2817 I != E; ++I) {
2818 Writer.AddDeclRef(*I, Record);
2819 }
2820}
2821
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002822void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregorc567ba22011-07-22 16:35:34 +00002823 StringRef isysroot,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002824 const std::string &OutputFile) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002825 using namespace llvm;
2826
2827 ASTContext &Context = SemaRef.Context;
2828 Preprocessor &PP = SemaRef.PP;
2829
Douglas Gregordab42432011-08-12 00:15:20 +00002830 // Set up predefined declaration IDs.
2831 DeclIDs[Context.getTranslationUnitDecl()] = PREDEF_DECL_TRANSLATION_UNIT_ID;
Douglas Gregor3ea72692011-08-12 05:46:01 +00002832 if (Context.ObjCIdDecl)
2833 DeclIDs[Context.ObjCIdDecl] = PREDEF_DECL_OBJC_ID_ID;
Douglas Gregor52e02802011-08-12 06:17:30 +00002834 if (Context.ObjCSelDecl)
2835 DeclIDs[Context.ObjCSelDecl] = PREDEF_DECL_OBJC_SEL_ID;
Douglas Gregor0a586182011-08-12 05:59:41 +00002836 if (Context.ObjCClassDecl)
2837 DeclIDs[Context.ObjCClassDecl] = PREDEF_DECL_OBJC_CLASS_ID;
Douglas Gregor801c99d2011-08-12 06:49:56 +00002838 if (Context.Int128Decl)
2839 DeclIDs[Context.Int128Decl] = PREDEF_DECL_INT_128_ID;
2840 if (Context.UInt128Decl)
2841 DeclIDs[Context.UInt128Decl] = PREDEF_DECL_UNSIGNED_INT_128_ID;
Douglas Gregor0a586182011-08-12 05:59:41 +00002842
Douglas Gregor851443c2011-08-12 01:39:19 +00002843 if (!Chain) {
2844 // Make sure that we emit IdentifierInfos (and any attached
2845 // declarations) for builtins. We don't need to do this when we're
2846 // emitting chained PCH files, because all of the builtins will be
2847 // in the original PCH file.
2848 // FIXME: Modules won't like this at all.
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002849 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002850 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002851 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2852 Context.getLangOptions().NoBuiltin);
2853 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2854 getIdentifierRef(&Table.get(BuiltinNames[I]));
2855 }
2856
Chris Lattner0c797362009-09-08 18:19:27 +00002857 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002858 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002859 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002860 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00002861 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00002862
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002863 // Build a record containing all of the file scoped decls in this file.
2864 RecordData UnusedFileScopedDecls;
Douglas Gregora94a1542011-07-27 21:45:57 +00002865 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
2866 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002867
Douglas Gregor851443c2011-08-12 01:39:19 +00002868 // Build a record containing all of the delegating constructors we still need
2869 // to resolve.
Alexis Hunt27a761d2011-05-04 23:29:54 +00002870 RecordData DelegatingCtorDecls;
Douglas Gregorbae31202011-07-27 21:57:17 +00002871 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00002872
Douglas Gregor851443c2011-08-12 01:39:19 +00002873 // Write the set of weak, undeclared identifiers. We always write the
2874 // entire table, since later PCH files in a PCH chain are only interested in
2875 // the results at the end of the chain.
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002876 RecordData WeakUndeclaredIdentifiers;
2877 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00002878 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002879 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2880 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2881 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2882 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2883 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2884 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2885 }
2886 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002887
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002888 // Build a record containing all of the locally-scoped external
2889 // declarations in this header file. Generally, this record will be
2890 // empty.
2891 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002892 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00002893 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002894 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002895 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2896 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregordc5c9582011-07-28 14:20:37 +00002897 TD != TDEnd; ++TD) {
2898 if (TD->second->getPCHLevel() == 0)
2899 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2900 }
2901
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002902 // Build a record containing all of the ext_vector declarations.
2903 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00002904 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002905
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002906 // Build a record containing all of the VTable uses information.
2907 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00002908 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00002909 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2910 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2911 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2912 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2913 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002914 }
2915
2916 // Build a record containing all of dynamic classes declarations.
2917 RecordData DynamicClasses;
Douglas Gregor32002192011-07-28 00:53:40 +00002918 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002919
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002920 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002921 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002922 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002923 I = SemaRef.PendingInstantiations.begin(),
2924 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2925 AddDeclRef(I->first, PendingInstantiations);
2926 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002927 }
2928 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2929 "There are local ones at end of translation unit!");
2930
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002931 // Build a record containing some declaration references.
2932 RecordData SemaDeclRefs;
2933 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2934 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2935 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2936 }
2937
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002938 RecordData CUDASpecialDeclRefs;
2939 if (Context.getcudaConfigureCallDecl()) {
2940 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
2941 }
2942
Douglas Gregorc2fa1692011-06-28 16:20:02 +00002943 // Build a record containing all of the known namespaces.
2944 RecordData KnownNamespaces;
2945 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
2946 I = SemaRef.KnownNamespaces.begin(),
2947 IEnd = SemaRef.KnownNamespaces.end();
2948 I != IEnd; ++I) {
2949 if (!I->second)
2950 AddDeclRef(I->first, KnownNamespaces);
2951 }
2952
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002953 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002954 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002955 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002956 WriteMetadata(Context, isysroot, OutputFile);
Sebastian Redl143413f2010-07-12 22:02:52 +00002957 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregorc567ba22011-07-22 16:35:34 +00002958 if (StatCalls && isysroot.empty())
Douglas Gregor11cfd942010-07-12 23:48:14 +00002959 WriteStatCache(*StatCalls);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002960 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Douglas Gregor2df17cb2011-08-01 16:54:33 +00002961
Douglas Gregor851443c2011-08-12 01:39:19 +00002962 if (Chain) {
2963 // Write the mapping information describing our module dependencies and how
2964 // each of those modules were mapped into our own offset/ID space, so that
2965 // the reader can build the appropriate mapping to its own offset/ID space.
2966 // The map consists solely of a blob with the following format:
2967 // *(module-name-len:i16 module-name:len*i8
2968 // source-location-offset:i32
2969 // identifier-id:i32
2970 // preprocessed-entity-id:i32
2971 // macro-definition-id:i32
2972 // selector-id:i32
2973 // declaration-id:i32
2974 // c++-base-specifiers-id:i32
2975 // type-id:i32)
2976 //
2977 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2978 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
2979 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2980 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
2981 llvm::SmallString<2048> Buffer;
2982 {
2983 llvm::raw_svector_ostream Out(Buffer);
2984 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
2985 MEnd = Chain->ModuleMgr.end();
2986 M != MEnd; ++M) {
2987 StringRef FileName = (*M)->FileName;
2988 io::Emit16(Out, FileName.size());
2989 Out.write(FileName.data(), FileName.size());
2990 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
2991 io::Emit32(Out, (*M)->BaseIdentifierID);
2992 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
2993 io::Emit32(Out, (*M)->BaseMacroDefinitionID);
2994 io::Emit32(Out, (*M)->BaseSelectorID);
2995 io::Emit32(Out, (*M)->BaseDeclID);
2996 io::Emit32(Out, (*M)->BaseTypeIndex);
2997 }
2998 }
2999 Record.clear();
3000 Record.push_back(MODULE_OFFSET_MAP);
3001 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
3002 Buffer.data(), Buffer.size());
3003 }
3004
3005 // Create a lexical update block containing all of the declarations in the
3006 // translation unit that do not come from other AST files.
3007 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
3008 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
3009 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3010 E = TU->noload_decls_end();
3011 I != E; ++I) {
3012 if ((*I)->getPCHLevel() == 0)
3013 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
3014 else if ((*I)->isChangedSinceDeserialization())
3015 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
3016 }
3017
3018 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
3019 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
3020 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3021 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3022 Record.clear();
3023 Record.push_back(TU_UPDATE_LEXICAL);
3024 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
3025 data(NewGlobalDecls));
3026
3027 // And a visible updates block for the translation unit.
3028 Abv = new llvm::BitCodeAbbrev();
3029 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3030 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3031 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3032 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3033 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3034 WriteDeclContextVisibleUpdate(TU);
3035
3036 // If the translation unit has an anonymous namespace, and we don't already
3037 // have an update block for it, write it as an update block.
3038 if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
3039 ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
3040 if (Record.empty()) {
3041 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
3042 AddDeclRef(NS, Record);
3043 }
3044 }
3045
Douglas Gregor5204bde2011-08-02 16:26:37 +00003046 // Form the record of special types.
3047 RecordData SpecialTypes;
3048 AddTypeRef(Context.getBuiltinVaListType(), SpecialTypes);
Douglas Gregor09c4aa82011-08-11 22:04:35 +00003049 AddTypeRef(Context.ObjCProtoType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00003050 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00003051 AddTypeRef(Context.getFILEType(), SpecialTypes);
3052 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
3053 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
3054 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
3055 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
Douglas Gregor5204bde2011-08-02 16:26:37 +00003056 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
Douglas Gregordab42432011-08-12 00:15:20 +00003057
Douglas Gregor1970d882009-04-26 03:49:13 +00003058 // Keep writing types and declarations until all types and
3059 // declarations have been written.
Douglas Gregor03412ba2011-06-03 02:27:19 +00003060 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003061 WriteDeclsBlockAbbrevs();
Douglas Gregor851443c2011-08-12 01:39:19 +00003062 for (DeclsToRewriteTy::iterator I = DeclsToRewrite.begin(),
3063 E = DeclsToRewrite.end();
3064 I != E; ++I)
3065 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Douglas Gregor12bfa382009-10-17 00:13:19 +00003066 while (!DeclTypesToEmit.empty()) {
3067 DeclOrType DOT = DeclTypesToEmit.front();
3068 DeclTypesToEmit.pop();
3069 if (DOT.isType())
3070 WriteType(DOT.getType());
3071 else
3072 WriteDecl(Context, DOT.getDecl());
3073 }
3074 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003075
Douglas Gregor45053152009-10-17 17:25:45 +00003076 WritePreprocessor(PP);
Douglas Gregor09b69892011-02-10 17:09:37 +00003077 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00003078 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00003079 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00003080 WriteIdentifierTable(PP);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003081 WriteFPPragmaOptions(SemaRef.getFPOptions());
3082 WriteOpenCLExtensions(SemaRef);
Douglas Gregor745ed142009-04-25 18:35:21 +00003083
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003084 WriteTypeDeclOffsets();
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003085 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00003086
Anders Carlsson9bb83e82011-03-06 18:41:18 +00003087 WriteCXXBaseSpecifiersOffsets();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003088
Douglas Gregor5204bde2011-08-02 16:26:37 +00003089 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
3090
Douglas Gregor851443c2011-08-12 01:39:19 +00003091 /// Build a record containing first declarations from a chained PCH and the
3092 /// most recent declarations in this AST that they point to.
3093 RecordData FirstLatestDeclIDs;
3094 for (FirstLatestDeclMap::iterator I = FirstLatestDecls.begin(),
3095 E = FirstLatestDecls.end();
3096 I != E; ++I) {
3097 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
3098 "Expected first & second to be in different PCHs");
3099 AddDeclRef(I->first, FirstLatestDeclIDs);
3100 AddDeclRef(I->second, FirstLatestDeclIDs);
3101 }
3102
3103 if (!FirstLatestDeclIDs.empty())
3104 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
3105
Douglas Gregord4df8652009-04-22 22:02:47 +00003106 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00003107 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003108 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00003109
3110 // Write the record containing tentative definitions.
3111 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003112 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003113
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003114 // Write the record containing unused file scoped decls.
3115 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003116 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003117
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003118 // Write the record containing weak undeclared identifiers.
3119 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003120 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00003121 WeakUndeclaredIdentifiers);
3122
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003123 // Write the record containing locally-scoped external definitions.
3124 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003125 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00003126 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00003127
3128 // Write the record containing ext_vector type names.
3129 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003130 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00003131
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003132 // Write the record containing VTable uses information.
3133 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003134 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003135
3136 // Write the record containing dynamic classes declarations.
3137 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003138 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003139
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003140 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003141 if (!PendingInstantiations.empty())
3142 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003143
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003144 // Write the record containing declaration references of Sema.
3145 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003146 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003147
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003148 // Write the record containing CUDA-specific declaration references.
3149 if (!CUDASpecialDeclRefs.empty())
3150 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003151
3152 // Write the delegating constructors.
3153 if (!DelegatingCtorDecls.empty())
3154 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003155
Douglas Gregorc2fa1692011-06-28 16:20:02 +00003156 // Write the known namespaces.
3157 if (!KnownNamespaces.empty())
3158 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3159
Douglas Gregor851443c2011-08-12 01:39:19 +00003160 // Write the visible updates to DeclContexts.
3161 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3162 I = UpdatedDeclContexts.begin(),
3163 E = UpdatedDeclContexts.end();
3164 I != E; ++I)
3165 WriteDeclContextVisibleUpdate(*I);
3166
Douglas Gregordab42432011-08-12 00:15:20 +00003167 WriteDeclUpdatesBlocks();
Douglas Gregor851443c2011-08-12 01:39:19 +00003168 WriteDeclReplacementsBlock();
Douglas Gregordab42432011-08-12 00:15:20 +00003169
Douglas Gregor08f01292009-04-17 22:13:46 +00003170 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00003171 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00003172 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00003173 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003174 Record.push_back(NumLexicalDeclContexts);
3175 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00003176 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00003177 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003178}
3179
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003180void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003181 if (DeclUpdates.empty())
3182 return;
3183
3184 RecordData OffsetsRecord;
Douglas Gregor03412ba2011-06-03 02:27:19 +00003185 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003186 for (DeclUpdateMap::iterator
3187 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3188 const Decl *D = I->first;
3189 UpdateRecord &URec = I->second;
3190
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00003191 if (DeclsToRewrite.count(D))
3192 continue; // The decl will be written completely,no need to store updates.
3193
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003194 uint64_t Offset = Stream.GetCurrentBitNo();
3195 Stream.EmitRecord(DECL_UPDATES, URec);
3196
3197 OffsetsRecord.push_back(GetDeclRef(D));
3198 OffsetsRecord.push_back(Offset);
3199 }
3200 Stream.ExitBlock();
3201 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3202}
3203
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003204void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003205 if (ReplacedDecls.empty())
3206 return;
3207
3208 RecordData Record;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003209 for (SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003210 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
3211 Record.push_back(I->first);
3212 Record.push_back(I->second);
3213 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003214 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003215}
3216
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003217void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003218 Record.push_back(Loc.getRawEncoding());
3219}
3220
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003221void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003222 AddSourceLocation(Range.getBegin(), Record);
3223 AddSourceLocation(Range.getEnd(), Record);
3224}
3225
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003226void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003227 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00003228 const uint64_t *Words = Value.getRawData();
3229 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003230}
3231
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003232void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00003233 Record.push_back(Value.isUnsigned());
3234 AddAPInt(Value, Record);
3235}
3236
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003237void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00003238 AddAPInt(Value.bitcastToAPInt(), Record);
3239}
3240
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003241void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003242 Record.push_back(getIdentifierRef(II));
3243}
3244
Sebastian Redl539c5062010-08-18 23:57:32 +00003245IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003246 if (II == 0)
3247 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003248
Sebastian Redl539c5062010-08-18 23:57:32 +00003249 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003250 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00003251 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003252 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003253}
3254
Sebastian Redl50e26582010-09-15 19:54:06 +00003255MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregoraae92242010-03-19 21:51:54 +00003256 if (MD == 0)
3257 return 0;
Sebastian Redl50e26582010-09-15 19:54:06 +00003258
3259 MacroID &ID = MacroDefinitions[MD];
Douglas Gregoraae92242010-03-19 21:51:54 +00003260 if (ID == 0)
Douglas Gregor91096292010-10-02 19:29:26 +00003261 ID = NextMacroID++;
Douglas Gregoraae92242010-03-19 21:51:54 +00003262 return ID;
3263}
3264
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003265void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003266 Record.push_back(getSelectorRef(SelRef));
3267}
3268
Sebastian Redl539c5062010-08-18 23:57:32 +00003269SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003270 if (Sel.getAsOpaquePtr() == 0) {
3271 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00003272 }
3273
Sebastian Redl539c5062010-08-18 23:57:32 +00003274 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00003275 if (SID == 0 && Chain) {
3276 // This might trigger a ReadSelector callback, which will set the ID for
3277 // this selector.
3278 Chain->LoadSelector(Sel);
3279 }
Steve Naroff2ddea052009-04-23 10:39:46 +00003280 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003281 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00003282 }
Sebastian Redl834bb972010-08-04 17:20:04 +00003283 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00003284}
3285
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003286void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00003287 AddDeclRef(Temp->getDestructor(), Record);
3288}
3289
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003290void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3291 CXXBaseSpecifier const *BasesEnd,
3292 RecordDataImpl &Record) {
3293 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3294 CXXBaseSpecifiersToWrite.push_back(
3295 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3296 Bases, BasesEnd));
3297 Record.push_back(NextCXXBaseSpecifiersID++);
3298}
3299
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003300void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003301 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003302 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003303 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00003304 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003305 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00003306 break;
3307 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003308 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00003309 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003310 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00003311 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003312 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003313 break;
3314 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00003315 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003316 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003317 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003318 break;
John McCall0ad16662009-10-29 08:12:44 +00003319 case TemplateArgument::Null:
3320 case TemplateArgument::Integral:
3321 case TemplateArgument::Declaration:
3322 case TemplateArgument::Pack:
3323 break;
3324 }
3325}
3326
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003327void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003328 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003329 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003330
3331 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3332 bool InfoHasSameExpr
3333 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3334 Record.push_back(InfoHasSameExpr);
3335 if (InfoHasSameExpr)
3336 return; // Avoid storing the same expr twice.
3337 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003338 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3339 Record);
3340}
3341
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003342void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3343 RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00003344 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00003345 AddTypeRef(QualType(), Record);
3346 return;
3347 }
3348
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003349 AddTypeLoc(TInfo->getTypeLoc(), Record);
3350}
3351
3352void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3353 AddTypeRef(TL.getType(), Record);
3354
John McCall8f115c62009-10-16 21:56:05 +00003355 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003356 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003357 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00003358}
3359
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003360void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00003361 Record.push_back(GetOrCreateTypeID(T));
3362}
3363
Douglas Gregoreda8e122011-08-09 15:13:55 +00003364TypeID ASTWriter::GetOrCreateTypeID( QualType T) {
3365 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003366 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3367}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003368
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003369TypeID ASTWriter::getTypeID(QualType T) const {
Douglas Gregoreda8e122011-08-09 15:13:55 +00003370 return MakeTypeID(*Context, T,
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003371 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003372}
3373
3374TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3375 if (T.isNull())
3376 return TypeIdx();
3377 assert(!T.getLocalFastQualifiers());
3378
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00003379 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003380 if (Idx.getIndex() == 0) {
Douglas Gregor1970d882009-04-26 03:49:13 +00003381 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00003382 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003383 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003384 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00003385 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003386 return Idx;
3387}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003388
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003389TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003390 if (T.isNull())
3391 return TypeIdx();
3392 assert(!T.getLocalFastQualifiers());
3393
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003394 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3395 assert(I != TypeIdxs.end() && "Type not emitted!");
3396 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003397}
3398
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003399void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003400 Record.push_back(GetDeclRef(D));
3401}
3402
Sebastian Redl539c5062010-08-18 23:57:32 +00003403DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003404 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003405 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003406 }
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003407 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00003408 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00003409 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003410 // We haven't seen this declaration before. Give it a new ID and
3411 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00003412 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003413 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003414 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3415 // We don't add it to the replacement collection here, because we don't
3416 // have the offset yet.
3417 DeclTypesToEmit.push(const_cast<Decl *>(D));
3418 // Reset the flag, so that we don't add this decl multiple times.
3419 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003420 }
3421
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003422 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003423}
3424
Sebastian Redl539c5062010-08-18 23:57:32 +00003425DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00003426 if (D == 0)
3427 return 0;
3428
3429 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3430 return DeclIDs[D];
3431}
3432
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003433void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00003434 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003435 Record.push_back(Name.getNameKind());
3436 switch (Name.getNameKind()) {
3437 case DeclarationName::Identifier:
3438 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3439 break;
3440
3441 case DeclarationName::ObjCZeroArgSelector:
3442 case DeclarationName::ObjCOneArgSelector:
3443 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00003444 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003445 break;
3446
3447 case DeclarationName::CXXConstructorName:
3448 case DeclarationName::CXXDestructorName:
3449 case DeclarationName::CXXConversionFunctionName:
3450 AddTypeRef(Name.getCXXNameType(), Record);
3451 break;
3452
3453 case DeclarationName::CXXOperatorName:
3454 Record.push_back(Name.getCXXOverloadedOperator());
3455 break;
3456
Alexis Hunt3d221f22009-11-29 07:34:05 +00003457 case DeclarationName::CXXLiteralOperatorName:
3458 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3459 break;
3460
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003461 case DeclarationName::CXXUsingDirective:
3462 // No extra data to emit
3463 break;
3464 }
3465}
Chris Lattnerca025db2010-05-07 21:43:38 +00003466
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003467void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003468 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003469 switch (Name.getNameKind()) {
3470 case DeclarationName::CXXConstructorName:
3471 case DeclarationName::CXXDestructorName:
3472 case DeclarationName::CXXConversionFunctionName:
3473 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3474 break;
3475
3476 case DeclarationName::CXXOperatorName:
3477 AddSourceLocation(
3478 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3479 Record);
3480 AddSourceLocation(
3481 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3482 Record);
3483 break;
3484
3485 case DeclarationName::CXXLiteralOperatorName:
3486 AddSourceLocation(
3487 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3488 Record);
3489 break;
3490
3491 case DeclarationName::Identifier:
3492 case DeclarationName::ObjCZeroArgSelector:
3493 case DeclarationName::ObjCOneArgSelector:
3494 case DeclarationName::ObjCMultiArgSelector:
3495 case DeclarationName::CXXUsingDirective:
3496 break;
3497 }
3498}
3499
3500void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003501 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003502 AddDeclarationName(NameInfo.getName(), Record);
3503 AddSourceLocation(NameInfo.getLoc(), Record);
3504 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3505}
3506
3507void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003508 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00003509 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003510 Record.push_back(Info.NumTemplParamLists);
3511 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3512 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3513}
3514
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003515void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003516 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003517 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00003518 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003519 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00003520
3521 // Push each of the NNS's onto a stack for serialization in reverse order.
3522 while (NNS) {
3523 NestedNames.push_back(NNS);
3524 NNS = NNS->getPrefix();
3525 }
3526
3527 Record.push_back(NestedNames.size());
3528 while(!NestedNames.empty()) {
3529 NNS = NestedNames.pop_back_val();
3530 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3531 Record.push_back(Kind);
3532 switch (Kind) {
3533 case NestedNameSpecifier::Identifier:
3534 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3535 break;
3536
3537 case NestedNameSpecifier::Namespace:
3538 AddDeclRef(NNS->getAsNamespace(), Record);
3539 break;
3540
Douglas Gregor7b26ff92011-02-24 02:36:08 +00003541 case NestedNameSpecifier::NamespaceAlias:
3542 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
3543 break;
3544
Chris Lattnerca025db2010-05-07 21:43:38 +00003545 case NestedNameSpecifier::TypeSpec:
3546 case NestedNameSpecifier::TypeSpecWithTemplate:
3547 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3548 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3549 break;
3550
3551 case NestedNameSpecifier::Global:
3552 // Don't need to write an associated value.
3553 break;
3554 }
3555 }
3556}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003557
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003558void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
3559 RecordDataImpl &Record) {
3560 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00003561 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003562 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003563
3564 // Push each of the nested-name-specifiers's onto a stack for
3565 // serialization in reverse order.
3566 while (NNS) {
3567 NestedNames.push_back(NNS);
3568 NNS = NNS.getPrefix();
3569 }
3570
3571 Record.push_back(NestedNames.size());
3572 while(!NestedNames.empty()) {
3573 NNS = NestedNames.pop_back_val();
3574 NestedNameSpecifier::SpecifierKind Kind
3575 = NNS.getNestedNameSpecifier()->getKind();
3576 Record.push_back(Kind);
3577 switch (Kind) {
3578 case NestedNameSpecifier::Identifier:
3579 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
3580 AddSourceRange(NNS.getLocalSourceRange(), Record);
3581 break;
3582
3583 case NestedNameSpecifier::Namespace:
3584 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
3585 AddSourceRange(NNS.getLocalSourceRange(), Record);
3586 break;
3587
3588 case NestedNameSpecifier::NamespaceAlias:
3589 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
3590 AddSourceRange(NNS.getLocalSourceRange(), Record);
3591 break;
3592
3593 case NestedNameSpecifier::TypeSpec:
3594 case NestedNameSpecifier::TypeSpecWithTemplate:
3595 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3596 AddTypeLoc(NNS.getTypeLoc(), Record);
3597 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3598 break;
3599
3600 case NestedNameSpecifier::Global:
3601 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3602 break;
3603 }
3604 }
3605}
3606
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003607void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003608 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003609 Record.push_back(Kind);
3610 switch (Kind) {
3611 case TemplateName::Template:
3612 AddDeclRef(Name.getAsTemplateDecl(), Record);
3613 break;
3614
3615 case TemplateName::OverloadedTemplate: {
3616 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3617 Record.push_back(OvT->size());
3618 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3619 I != E; ++I)
3620 AddDeclRef(*I, Record);
3621 break;
3622 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003623
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003624 case TemplateName::QualifiedTemplate: {
3625 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3626 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3627 Record.push_back(QualT->hasTemplateKeyword());
3628 AddDeclRef(QualT->getTemplateDecl(), Record);
3629 break;
3630 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003631
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003632 case TemplateName::DependentTemplate: {
3633 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3634 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3635 Record.push_back(DepT->isIdentifier());
3636 if (DepT->isIdentifier())
3637 AddIdentifierRef(DepT->getIdentifier(), Record);
3638 else
3639 Record.push_back(DepT->getOperator());
3640 break;
3641 }
John McCalld9dfe3a2011-06-30 08:33:18 +00003642
3643 case TemplateName::SubstTemplateTemplateParm: {
3644 SubstTemplateTemplateParmStorage *subst
3645 = Name.getAsSubstTemplateTemplateParm();
3646 AddDeclRef(subst->getParameter(), Record);
3647 AddTemplateName(subst->getReplacement(), Record);
3648 break;
3649 }
Douglas Gregor5590be02011-01-15 06:45:20 +00003650
3651 case TemplateName::SubstTemplateTemplateParmPack: {
3652 SubstTemplateTemplateParmPackStorage *SubstPack
3653 = Name.getAsSubstTemplateTemplateParmPack();
3654 AddDeclRef(SubstPack->getParameterPack(), Record);
3655 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
3656 break;
3657 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003658 }
3659}
3660
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003661void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003662 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003663 Record.push_back(Arg.getKind());
3664 switch (Arg.getKind()) {
3665 case TemplateArgument::Null:
3666 break;
3667 case TemplateArgument::Type:
3668 AddTypeRef(Arg.getAsType(), Record);
3669 break;
3670 case TemplateArgument::Declaration:
3671 AddDeclRef(Arg.getAsDecl(), Record);
3672 break;
3673 case TemplateArgument::Integral:
3674 AddAPSInt(*Arg.getAsIntegral(), Record);
3675 AddTypeRef(Arg.getIntegralType(), Record);
3676 break;
3677 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00003678 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3679 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003680 case TemplateArgument::TemplateExpansion:
3681 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregore1d60df2011-01-14 23:41:42 +00003682 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3683 Record.push_back(*NumExpansions + 1);
3684 else
3685 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003686 break;
3687 case TemplateArgument::Expression:
3688 AddStmt(Arg.getAsExpr());
3689 break;
3690 case TemplateArgument::Pack:
3691 Record.push_back(Arg.pack_size());
3692 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3693 I != E; ++I)
3694 AddTemplateArgument(*I, Record);
3695 break;
3696 }
3697}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003698
3699void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003700ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003701 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003702 assert(TemplateParams && "No TemplateParams!");
3703 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3704 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3705 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3706 Record.push_back(TemplateParams->size());
3707 for (TemplateParameterList::const_iterator
3708 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3709 P != PEnd; ++P)
3710 AddDeclRef(*P, Record);
3711}
3712
3713/// \brief Emit a template argument list.
3714void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003715ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003716 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003717 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003718 Record.push_back(TemplateArgs->size());
3719 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003720 AddTemplateArgument(TemplateArgs->get(i), Record);
3721}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003722
3723
3724void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003725ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003726 Record.push_back(Set.size());
3727 for (UnresolvedSetImpl::const_iterator
3728 I = Set.begin(), E = Set.end(); I != E; ++I) {
3729 AddDeclRef(I.getDecl(), Record);
3730 Record.push_back(I.getAccess());
3731 }
3732}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003733
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003734void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003735 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003736 Record.push_back(Base.isVirtual());
3737 Record.push_back(Base.isBaseOfClass());
3738 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00003739 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00003740 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003741 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00003742 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3743 : SourceLocation(),
3744 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003745}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003746
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003747void ASTWriter::FlushCXXBaseSpecifiers() {
3748 RecordData Record;
3749 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3750 Record.clear();
3751
3752 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00003753 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003754 if (Index == CXXBaseSpecifiersOffsets.size())
3755 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3756 else {
3757 if (Index > CXXBaseSpecifiersOffsets.size())
3758 CXXBaseSpecifiersOffsets.resize(Index + 1);
3759 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3760 }
3761
3762 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3763 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3764 Record.push_back(BEnd - B);
3765 for (; B != BEnd; ++B)
3766 AddCXXBaseSpecifier(*B, Record);
3767 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00003768
3769 // Flush any expressions that were written as part of the base specifiers.
3770 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003771 }
3772
3773 CXXBaseSpecifiersToWrite.clear();
3774}
3775
Alexis Hunt1d792652011-01-08 20:30:50 +00003776void ASTWriter::AddCXXCtorInitializers(
3777 const CXXCtorInitializer * const *CtorInitializers,
3778 unsigned NumCtorInitializers,
3779 RecordDataImpl &Record) {
3780 Record.push_back(NumCtorInitializers);
3781 for (unsigned i=0; i != NumCtorInitializers; ++i) {
3782 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003783
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003784 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00003785 Record.push_back(CTOR_INITIALIZER_BASE);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003786 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3787 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00003788 } else if (Init->isDelegatingInitializer()) {
3789 Record.push_back(CTOR_INITIALIZER_DELEGATING);
3790 AddDeclRef(Init->getTargetConstructor(), Record);
3791 } else if (Init->isMemberInitializer()){
3792 Record.push_back(CTOR_INITIALIZER_MEMBER);
3793 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003794 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00003795 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
3796 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003797 }
Francois Pichetd583da02010-12-04 09:14:42 +00003798
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003799 AddSourceLocation(Init->getMemberLocation(), Record);
3800 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003801 AddSourceLocation(Init->getLParenLoc(), Record);
3802 AddSourceLocation(Init->getRParenLoc(), Record);
3803 Record.push_back(Init->isWritten());
3804 if (Init->isWritten()) {
3805 Record.push_back(Init->getSourceOrder());
3806 } else {
3807 Record.push_back(Init->getNumArrayIndices());
3808 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3809 AddDeclRef(Init->getArrayIndex(i), Record);
3810 }
3811 }
3812}
3813
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003814void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3815 assert(D->DefinitionData);
3816 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3817 Record.push_back(Data.UserDeclaredConstructor);
3818 Record.push_back(Data.UserDeclaredCopyConstructor);
3819 Record.push_back(Data.UserDeclaredCopyAssignment);
3820 Record.push_back(Data.UserDeclaredDestructor);
3821 Record.push_back(Data.Aggregate);
3822 Record.push_back(Data.PlainOldData);
3823 Record.push_back(Data.Empty);
3824 Record.push_back(Data.Polymorphic);
3825 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00003826 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00003827 Record.push_back(Data.HasNoNonEmptyBases);
3828 Record.push_back(Data.HasPrivateFields);
3829 Record.push_back(Data.HasProtectedFields);
3830 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00003831 Record.push_back(Data.HasMutableFields);
Alexis Huntf479f1b2011-05-09 18:22:59 +00003832 Record.push_back(Data.HasTrivialDefaultConstructor);
Richard Smith111af8d2011-08-10 18:11:37 +00003833 Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003834 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruthad7d4042011-04-23 23:10:33 +00003835 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003836 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruthad7d4042011-04-23 23:10:33 +00003837 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003838 Record.push_back(Data.HasTrivialDestructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00003839 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003840 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00003841 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003842 Record.push_back(Data.DeclaredDefaultConstructor);
3843 Record.push_back(Data.DeclaredCopyConstructor);
3844 Record.push_back(Data.DeclaredCopyAssignment);
3845 Record.push_back(Data.DeclaredDestructor);
3846
3847 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003848 if (Data.NumBases > 0)
3849 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3850 Record);
3851
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003852 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3853 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003854 if (Data.NumVBases > 0)
3855 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3856 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003857
3858 AddUnresolvedSet(Data.Conversions, Record);
3859 AddUnresolvedSet(Data.VisibleConversions, Record);
3860 // Data.Definition is the owning decl, no need to write it.
3861 AddDeclRef(Data.FirstFriend, Record);
3862}
3863
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003864void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00003865 assert(Reader && "Cannot remove chain");
Douglas Gregordf0c1512011-08-18 04:12:04 +00003866 assert((!Chain || Chain == Reader) && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00003867 assert(FirstDeclID == NextDeclID &&
3868 FirstTypeID == NextTypeID &&
3869 FirstIdentID == NextIdentID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00003870 FirstSelectorID == NextSelectorID &&
Douglas Gregor91096292010-10-02 19:29:26 +00003871 FirstMacroID == NextMacroID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00003872 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00003873
Sebastian Redl07a89a82010-07-30 00:29:29 +00003874 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003875
Douglas Gregordf0c1512011-08-18 04:12:04 +00003876 FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
3877 FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
3878 FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
3879 FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
3880 FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacroDefinitions();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003881 NextDeclID = FirstDeclID;
3882 NextTypeID = FirstTypeID;
3883 NextIdentID = FirstIdentID;
3884 NextSelectorID = FirstSelectorID;
3885 NextMacroID = FirstMacroID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00003886}
3887
Sebastian Redl539c5062010-08-18 23:57:32 +00003888void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00003889 IdentifierIDs[II] = ID;
Douglas Gregor68051a72011-02-11 00:26:14 +00003890 if (II->hasMacroDefinition())
3891 DeserializedMacroNames.push_back(II);
Sebastian Redlff4a2952010-07-23 23:49:55 +00003892}
3893
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003894void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003895 // Always take the highest-numbered type index. This copes with an interesting
3896 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003897 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003898 // keep the higher-numbered entry so that we can properly write it out to
3899 // the AST file.
3900 TypeIdx &StoredIdx = TypeIdxs[T];
3901 if (Idx.getIndex() >= StoredIdx.getIndex())
3902 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003903}
3904
Sebastian Redl539c5062010-08-18 23:57:32 +00003905void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003906 DeclIDs[D] = ID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003907}
Sebastian Redl834bb972010-08-04 17:20:04 +00003908
Sebastian Redl539c5062010-08-18 23:57:32 +00003909void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003910 SelectorIDs[S] = ID;
3911}
Douglas Gregor91096292010-10-02 19:29:26 +00003912
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003913void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00003914 MacroDefinition *MD) {
3915 MacroDefinitions[MD] = ID;
3916}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003917
3918void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3919 assert(D->isDefinition());
3920 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3921 // We are interested when a PCH decl is modified.
3922 if (RD->getPCHLevel() > 0) {
3923 // A forward reference was mutated into a definition. Rewrite it.
3924 // FIXME: This happens during template instantiation, should we
3925 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003926 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003927 }
3928
3929 for (CXXRecordDecl::redecl_iterator
3930 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3931 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3932 if (Redecl == RD)
3933 continue;
3934
3935 // We are interested when a PCH decl is modified.
3936 if (Redecl->getPCHLevel() > 0) {
3937 UpdateRecord &Record = DeclUpdates[Redecl];
3938 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3939 assert(Redecl->DefinitionData);
3940 assert(Redecl->DefinitionData->Definition == D);
3941 AddDeclRef(D, Record); // the DefinitionDecl
3942 }
3943 }
3944 }
3945}
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003946void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3947 // TU and namespaces are handled elsewhere.
3948 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3949 return;
3950
3951 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3952 return; // Not a source decl added to a DeclContext from PCH.
3953
3954 AddUpdatedDeclContext(DC);
3955}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003956
3957void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3958 assert(D->isImplicit());
3959 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3960 return; // Not a source member added to a class from PCH.
3961 if (!isa<CXXMethodDecl>(D))
3962 return; // We are interested in lazily declared implicit methods.
3963
3964 // A decl coming from PCH was modified.
3965 assert(RD->isDefinition());
3966 UpdateRecord &Record = DeclUpdates[RD];
3967 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3968 AddDeclRef(D, Record);
3969}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003970
3971void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3972 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00003973 // The specializations set is kept in the canonical template.
3974 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003975 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3976 return; // Not a source specialization added to a template from PCH.
3977
3978 UpdateRecord &Record = DeclUpdates[TD];
3979 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3980 AddDeclRef(D, Record);
3981}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003982
Sebastian Redl9ab988f2011-04-14 14:07:59 +00003983void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
3984 const FunctionDecl *D) {
3985 // The specializations set is kept in the canonical template.
3986 TD = TD->getCanonicalDecl();
3987 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3988 return; // Not a source specialization added to a template from PCH.
3989
3990 UpdateRecord &Record = DeclUpdates[TD];
3991 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3992 AddDeclRef(D, Record);
3993}
3994
Sebastian Redlab238a72011-04-24 16:28:06 +00003995void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
3996 if (D->getPCHLevel() == 0)
3997 return; // Declaration not imported from PCH.
3998
3999 // Implicit decl from a PCH was defined.
4000 // FIXME: Should implicit definition be a separate FunctionDecl?
4001 RewriteDecl(D);
4002}
4003
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004004void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
4005 if (D->getPCHLevel() == 0)
4006 return;
4007
4008 // Since the actual instantiation is delayed, this really means that we need
4009 // to update the instantiation location.
4010 UpdateRecord &Record = DeclUpdates[D];
4011 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4012 AddSourceLocation(
4013 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4014}
4015
Douglas Gregorf88e35b2010-11-30 06:16:57 +00004016ASTSerializationListener::~ASTSerializationListener() { }