blob: caf5836efc799528cd3b229ed11798fd66aa4aa0 [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);
Sebastian Redl595c5132010-07-08 22:01:51 +0000768 RECORD(CHAINED_METADATA);
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_TRANSLATION_UNIT);
845 RECORD(DECL_TYPEDEF);
846 RECORD(DECL_ENUM);
847 RECORD(DECL_RECORD);
848 RECORD(DECL_ENUM_CONSTANT);
849 RECORD(DECL_FUNCTION);
850 RECORD(DECL_OBJC_METHOD);
851 RECORD(DECL_OBJC_INTERFACE);
852 RECORD(DECL_OBJC_PROTOCOL);
853 RECORD(DECL_OBJC_IVAR);
854 RECORD(DECL_OBJC_AT_DEFS_FIELD);
855 RECORD(DECL_OBJC_CLASS);
856 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
857 RECORD(DECL_OBJC_CATEGORY);
858 RECORD(DECL_OBJC_CATEGORY_IMPL);
859 RECORD(DECL_OBJC_IMPLEMENTATION);
860 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
861 RECORD(DECL_OBJC_PROPERTY);
862 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000863 RECORD(DECL_FIELD);
864 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000865 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000866 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000867 RECORD(DECL_FILE_SCOPE_ASM);
868 RECORD(DECL_BLOCK);
869 RECORD(DECL_CONTEXT_LEXICAL);
870 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000871 RECORD(DECL_NAMESPACE);
872 RECORD(DECL_NAMESPACE_ALIAS);
873 RECORD(DECL_USING);
874 RECORD(DECL_USING_SHADOW);
875 RECORD(DECL_USING_DIRECTIVE);
876 RECORD(DECL_UNRESOLVED_USING_VALUE);
877 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
878 RECORD(DECL_LINKAGE_SPEC);
879 RECORD(DECL_CXX_RECORD);
880 RECORD(DECL_CXX_METHOD);
881 RECORD(DECL_CXX_CONSTRUCTOR);
882 RECORD(DECL_CXX_DESTRUCTOR);
883 RECORD(DECL_CXX_CONVERSION);
884 RECORD(DECL_ACCESS_SPEC);
885 RECORD(DECL_FRIEND);
886 RECORD(DECL_FRIEND_TEMPLATE);
887 RECORD(DECL_CLASS_TEMPLATE);
888 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
889 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
890 RECORD(DECL_FUNCTION_TEMPLATE);
891 RECORD(DECL_TEMPLATE_TYPE_PARM);
892 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
893 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
894 RECORD(DECL_STATIC_ASSERT);
895 RECORD(DECL_CXX_BASE_SPECIFIERS);
896 RECORD(DECL_INDIRECTFIELD);
897 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
898
Douglas Gregor03412ba2011-06-03 02:27:19 +0000899 // Statements and Exprs can occur in the Decls and Types block.
900 AddStmtsExprs(Stream, Record);
901
Douglas Gregor92a96f52011-02-08 21:58:10 +0000902 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +0000903 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor92a96f52011-02-08 21:58:10 +0000904 RECORD(PPD_MACRO_DEFINITION);
905 RECORD(PPD_INCLUSION_DIRECTIVE);
906
Chris Lattner28fa4e62009-04-26 22:26:21 +0000907#undef RECORD
908#undef BLOCK
909 Stream.ExitBlock();
910}
911
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000912/// \brief Adjusts the given filename to only write out the portion of the
913/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000914///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000915/// \param Filename the file name to adjust.
916///
917/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
918/// the returned filename will be adjusted by this system root.
919///
920/// \returns either the original filename (if it needs no adjustment) or the
921/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000922static const char *
Douglas Gregorc567ba22011-07-22 16:35:34 +0000923adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000924 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000925
Douglas Gregorc567ba22011-07-22 16:35:34 +0000926 if (isysroot.empty())
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000927 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000928
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000929 // Verify that the filename and the system root have the same prefix.
930 unsigned Pos = 0;
Douglas Gregorc567ba22011-07-22 16:35:34 +0000931 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000932 if (Filename[Pos] != isysroot[Pos])
933 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000934
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000935 // We hit the end of the filename before we hit the end of the system root.
936 if (!Filename[Pos])
937 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000938
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000939 // If the file name has a '/' at the current position, skip over the '/'.
940 // We distinguish sysroot-based includes from absolute includes by the
941 // absence of '/' at the beginning of sysroot-based includes.
942 if (Filename[Pos] == '/')
943 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000944
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000945 return Filename + Pos;
946}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000947
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000948/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Douglas Gregorc567ba22011-07-22 16:35:34 +0000949void ASTWriter::WriteMetadata(ASTContext &Context, StringRef isysroot,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000950 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000951 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000952
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000953 // Metadata
954 const TargetInfo &Target = Context.Target;
955 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000956 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl539c5062010-08-18 23:57:32 +0000957 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000958 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
959 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000960 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
961 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
962 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000963 // Target triple or chained PCH name
964 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000965 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000966
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000967 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000968 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
969 Record.push_back(VERSION_MAJOR);
970 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000971 Record.push_back(CLANG_VERSION_MAJOR);
972 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregorc567ba22011-07-22 16:35:34 +0000973 Record.push_back(!isysroot.empty());
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000974 // FIXME: This writes the absolute path for chained headers.
Jonathan D. Turnerb2b08232011-07-26 18:21:30 +0000975 const std::string &BlobStr =
976 Chain ? Chain->getFileName() : Target.getTriple().getTriple();
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000977 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump11289f42009-09-09 15:08:12 +0000978
Douglas Gregora3b20262011-05-06 21:43:30 +0000979 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +0000980 SourceManager &SM = Context.getSourceManager();
981 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
982 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000983 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregor45fe0362009-05-12 01:31:05 +0000984 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
985 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
986
Michael J. Spencer740857f2010-12-21 16:45:57 +0000987 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +0000988
Michael J. Spencer740857f2010-12-21 16:45:57 +0000989 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000990
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +0000991 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000992 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000993 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000994 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000995 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000996 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregora3b20262011-05-06 21:43:30 +0000997
998 Record.clear();
999 Record.push_back(SM.getMainFileID().getOpaqueValue());
1000 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
Douglas Gregor45fe0362009-05-12 01:31:05 +00001001 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001002
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00001003 // Original PCH directory
1004 if (!OutputFile.empty() && OutputFile != "-") {
1005 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1006 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1007 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1008 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1009
1010 llvm::SmallString<128> OutputPath(OutputFile);
1011
1012 llvm::sys::fs::make_absolute(OutputPath);
1013 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1014
1015 RecordData Record;
1016 Record.push_back(ORIGINAL_PCH_DIR);
1017 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1018 }
1019
Ted Kremenek18e066f2010-01-22 22:12:47 +00001020 // Repository branch/version information.
1021 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001022 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenek18e066f2010-01-22 22:12:47 +00001023 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1024 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +00001025 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001026 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +00001027 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
1028 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +00001029}
1030
1031/// \brief Write the LangOptions structure.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001032void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001033 RecordData Record;
1034 Record.push_back(LangOpts.Trigraphs);
1035 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1036 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1037 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1038 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carruthe03aa552010-04-17 20:17:31 +00001039 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor55abb232009-04-10 20:39:37 +00001040 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1041 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1042 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1043 Record.push_back(LangOpts.C99); // C99 Support
Peter Collingbournea686b5f2011-04-15 00:35:23 +00001044 Record.push_back(LangOpts.C1X); // C1X Support
Douglas Gregor55abb232009-04-10 20:39:37 +00001045 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +00001046 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
1047 // already saved elsewhere.
Douglas Gregor55abb232009-04-10 20:39:37 +00001048 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1049 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +00001050 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +00001051
Douglas Gregor55abb232009-04-10 20:39:37 +00001052 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1053 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001054 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian45878032010-02-09 19:31:38 +00001055 // modern abi enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001056 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian45878032010-02-09 19:31:38 +00001057 // modern abi enabled.
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +00001058 Record.push_back(LangOpts.AppleKext); // Apple's kernel extensions ABI
Ted Kremenek1d56c9e2010-12-23 21:35:43 +00001059 Record.push_back(LangOpts.ObjCDefaultSynthProperties); // Objective-C auto-synthesized
1060 // properties enabled.
Douglas Gregora860e6a2011-06-14 23:20:43 +00001061 Record.push_back(LangOpts.ObjCInferRelatedResultType);
Fariborz Jahanian62c56022010-04-22 21:01:59 +00001062 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump11289f42009-09-09 15:08:12 +00001063
Douglas Gregor55abb232009-04-10 20:39:37 +00001064 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +00001065 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1066 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00001067 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +00001068 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Anders Carlssonce8dd3a2011-02-19 23:53:54 +00001069 Record.push_back(LangOpts.ObjCExceptions);
Anders Carlsson6bbd2682011-02-23 03:04:54 +00001070 Record.push_back(LangOpts.CXXExceptions);
1071 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor55abb232009-04-10 20:39:37 +00001072
Douglas Gregordbe39272011-02-01 15:15:22 +00001073 Record.push_back(LangOpts.MSBitfields); // MS-compatible structure layout
Douglas Gregor55abb232009-04-10 20:39:37 +00001074 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1075 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1076 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1077
Chris Lattner258172e2009-04-27 07:35:58 +00001078 // Whether static initializers are protected by locks.
1079 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00001080 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +00001081 Record.push_back(LangOpts.Blocks); // block extension to C
1082 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1083 // they are unused.
1084 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1085 // (modulo the platform support).
1086
Chris Lattner51924e512010-06-26 21:25:03 +00001087 Record.push_back(LangOpts.getSignedOverflowBehavior());
1088 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor55abb232009-04-10 20:39:37 +00001089
1090 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +00001091 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +00001092 // defined.
1093 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1094 // opposed to __DYNAMIC__).
1095 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1096
1097 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1098 // used (instead of C99 semantics).
1099 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Chandler Carruth7ffce732011-04-23 20:05:38 +00001100 Record.push_back(LangOpts.Deprecated); // Should __DEPRECATED be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +00001101 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
1102 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +00001103 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
1104 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +00001105 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Argyrios Kyrtzidisa88942a2011-01-15 02:56:16 +00001106 Record.push_back(LangOpts.ShortEnums); // Should the enum type be equivalent
1107 // to the smallest integer type with
1108 // enough room.
Douglas Gregor55abb232009-04-10 20:39:37 +00001109 Record.push_back(LangOpts.getGCMode());
1110 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +00001111 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +00001112 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00001113 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +00001114 Record.push_back(LangOpts.CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +00001115 Record.push_back(LangOpts.CatchUndefined);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001116 Record.push_back(LangOpts.DefaultFPContract);
Anders Carlsson9cedbef2009-08-22 22:30:33 +00001117 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +00001118 Record.push_back(LangOpts.SpellChecking);
Roman Divacky65b88cd2011-03-01 17:40:53 +00001119 Record.push_back(LangOpts.MRTD);
John McCall31168b02011-06-15 23:02:42 +00001120 Record.push_back(LangOpts.ObjCAutoRefCount);
1121 Record.push_back(LangOpts.ObjCInferRelatedReturnType);
Sebastian Redl539c5062010-08-18 23:57:32 +00001122 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +00001123}
1124
Douglas Gregora7f71a92009-04-10 03:52:48 +00001125//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00001126// stat cache Serialization
1127//===----------------------------------------------------------------------===//
1128
1129namespace {
1130// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001131class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001132public:
1133 typedef const char * key_type;
1134 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001135
Chris Lattner2a6fa472010-11-23 19:28:12 +00001136 typedef struct stat data_type;
1137 typedef const data_type &data_type_ref;
Douglas Gregorc5046832009-04-27 18:38:38 +00001138
1139 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001140 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001141 }
Mike Stump11289f42009-09-09 15:08:12 +00001142
1143 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001144 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorc5046832009-04-27 18:38:38 +00001145 data_type_ref Data) {
1146 unsigned StrLen = strlen(path);
1147 clang::io::Emit16(Out, StrLen);
Chris Lattner2a6fa472010-11-23 19:28:12 +00001148 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregorc5046832009-04-27 18:38:38 +00001149 clang::io::Emit8(Out, DataLen);
1150 return std::make_pair(StrLen + 1, DataLen);
1151 }
Mike Stump11289f42009-09-09 15:08:12 +00001152
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001153 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001154 Out.write(path, KeyLen);
1155 }
Mike Stump11289f42009-09-09 15:08:12 +00001156
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001157 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorc5046832009-04-27 18:38:38 +00001158 data_type_ref Data, unsigned DataLen) {
1159 using namespace clang::io;
1160 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +00001161
Chris Lattner2a6fa472010-11-23 19:28:12 +00001162 Emit32(Out, (uint32_t) Data.st_ino);
1163 Emit32(Out, (uint32_t) Data.st_dev);
1164 Emit16(Out, (uint16_t) Data.st_mode);
1165 Emit64(Out, (uint64_t) Data.st_mtime);
1166 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregorc5046832009-04-27 18:38:38 +00001167
1168 assert(Out.tell() - Start == DataLen && "Wrong data length");
1169 }
1170};
1171} // end anonymous namespace
1172
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001173/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001174void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001175 // Build the on-disk hash table containing information about every
1176 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001177 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +00001178 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001179 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +00001180 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001181 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001182 StringRef Filename = Stat->first();
Chris Lattnerd386df42011-07-14 18:24:21 +00001183 Generator.insert(Filename.data(), Stat->second);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001184 }
Mike Stump11289f42009-09-09 15:08:12 +00001185
Douglas Gregorc5046832009-04-27 18:38:38 +00001186 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001187 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +00001188 uint32_t BucketOffset;
1189 {
1190 llvm::raw_svector_ostream Out(StatCacheData);
1191 // Make sure that no bucket is at offset 0
1192 clang::io::Emit32(Out, 0);
1193 BucketOffset = Generator.Emit(Out);
1194 }
1195
1196 // Create a blob abbreviation
1197 using namespace llvm;
1198 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001199 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +00001200 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1201 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1202 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1203 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1204
1205 // Write the stat cache
1206 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001207 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +00001208 Record.push_back(BucketOffset);
1209 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001210 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +00001211}
1212
1213//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +00001214// Source Manager Serialization
1215//===----------------------------------------------------------------------===//
1216
1217/// \brief Create an abbreviation for the SLocEntry that refers to a
1218/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001219static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001220 using namespace llvm;
1221 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001222 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001223 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1224 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1225 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1226 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001227 // FileEntry fields.
1228 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1229 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora7f71a92009-04-10 03:52:48 +00001230 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001231 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001232}
1233
1234/// \brief Create an abbreviation for the SLocEntry that refers to a
1235/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001236static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001237 using namespace llvm;
1238 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001239 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001240 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1241 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1242 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1243 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1244 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001245 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001246}
1247
1248/// \brief Create an abbreviation for the SLocEntry that refers to a
1249/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001250static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001251 using namespace llvm;
1252 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001253 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001254 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001255 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001256}
1257
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001258/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1259/// expansion.
1260static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001261 using namespace llvm;
1262 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001263 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001264 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1265 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1266 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1267 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001268 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001269 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001270}
1271
Douglas Gregor09b69892011-02-10 17:09:37 +00001272namespace {
1273 // Trait used for the on-disk hash table of header search information.
1274 class HeaderFileInfoTrait {
1275 ASTWriter &Writer;
1276 HeaderSearch &HS;
1277
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001278 // Keep track of the framework names we've used during serialization.
1279 SmallVector<char, 128> FrameworkStringData;
1280 llvm::StringMap<unsigned> FrameworkNameOffset;
1281
Douglas Gregor09b69892011-02-10 17:09:37 +00001282 public:
1283 HeaderFileInfoTrait(ASTWriter &Writer, HeaderSearch &HS)
1284 : Writer(Writer), HS(HS) { }
1285
1286 typedef const char *key_type;
1287 typedef key_type key_type_ref;
1288
1289 typedef HeaderFileInfo data_type;
1290 typedef const data_type &data_type_ref;
1291
1292 static unsigned ComputeHash(const char *path) {
1293 // The hash is based only on the filename portion of the key, so that the
1294 // reader can match based on filenames when symlinking or excess path
1295 // elements ("foo/../", "../") change the form of the name. However,
1296 // complete path is still the key.
1297 return llvm::HashString(llvm::sys::path::filename(path));
1298 }
1299
1300 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001301 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor09b69892011-02-10 17:09:37 +00001302 data_type_ref Data) {
1303 unsigned StrLen = strlen(path);
1304 clang::io::Emit16(Out, StrLen);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001305 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregor09b69892011-02-10 17:09:37 +00001306 clang::io::Emit8(Out, DataLen);
1307 return std::make_pair(StrLen + 1, DataLen);
1308 }
1309
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001310 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor09b69892011-02-10 17:09:37 +00001311 Out.write(path, KeyLen);
1312 }
1313
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001314 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor09b69892011-02-10 17:09:37 +00001315 data_type_ref Data, unsigned DataLen) {
1316 using namespace clang::io;
1317 uint64_t Start = Out.tell(); (void)Start;
1318
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001319 unsigned char Flags = (Data.isImport << 5)
1320 | (Data.isPragmaOnce << 4)
1321 | (Data.DirInfo << 2)
1322 | (Data.Resolved << 1)
1323 | Data.IndexHeaderMapHeader;
Douglas Gregor09b69892011-02-10 17:09:37 +00001324 Emit8(Out, (uint8_t)Flags);
1325 Emit16(Out, (uint16_t) Data.NumIncludes);
1326
1327 if (!Data.ControllingMacro)
1328 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1329 else
1330 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001331
1332 unsigned Offset = 0;
1333 if (!Data.Framework.empty()) {
1334 // If this header refers into a framework, save the framework name.
1335 llvm::StringMap<unsigned>::iterator Pos
1336 = FrameworkNameOffset.find(Data.Framework);
1337 if (Pos == FrameworkNameOffset.end()) {
1338 Offset = FrameworkStringData.size() + 1;
1339 FrameworkStringData.append(Data.Framework.begin(),
1340 Data.Framework.end());
1341 FrameworkStringData.push_back(0);
1342
1343 FrameworkNameOffset[Data.Framework] = Offset;
1344 } else
1345 Offset = Pos->second;
1346 }
1347 Emit32(Out, Offset);
1348
Douglas Gregor09b69892011-02-10 17:09:37 +00001349 assert(Out.tell() - Start == DataLen && "Wrong data length");
1350 }
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001351
1352 const char *strings_begin() const { return FrameworkStringData.begin(); }
1353 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregor09b69892011-02-10 17:09:37 +00001354 };
1355} // end anonymous namespace
1356
1357/// \brief Write the header search block for the list of files that
1358///
1359/// \param HS The header search structure to save.
1360///
1361/// \param Chain Whether we're creating a chained AST file.
Douglas Gregorc567ba22011-07-22 16:35:34 +00001362void ASTWriter::WriteHeaderSearch(HeaderSearch &HS, StringRef isysroot) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001363 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregor09b69892011-02-10 17:09:37 +00001364 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1365
1366 if (FilesByUID.size() > HS.header_file_size())
1367 FilesByUID.resize(HS.header_file_size());
1368
1369 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1370 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001371 SmallVector<const char *, 4> SavedStrings;
Douglas Gregor09b69892011-02-10 17:09:37 +00001372 unsigned NumHeaderSearchEntries = 0;
1373 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1374 const FileEntry *File = FilesByUID[UID];
1375 if (!File)
1376 continue;
1377
1378 const HeaderFileInfo &HFI = HS.header_file_begin()[UID];
1379 if (HFI.External && Chain)
1380 continue;
1381
1382 // Turn the file name into an absolute path, if it isn't already.
1383 const char *Filename = File->getName();
1384 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1385
1386 // If we performed any translation on the file name at all, we need to
1387 // save this string, since the generator will refer to it later.
1388 if (Filename != File->getName()) {
1389 Filename = strdup(Filename);
1390 SavedStrings.push_back(Filename);
1391 }
1392
1393 Generator.insert(Filename, HFI, GeneratorTrait);
1394 ++NumHeaderSearchEntries;
1395 }
1396
1397 // Create the on-disk hash table in a buffer.
1398 llvm::SmallString<4096> TableData;
1399 uint32_t BucketOffset;
1400 {
1401 llvm::raw_svector_ostream Out(TableData);
1402 // Make sure that no bucket is at offset 0
1403 clang::io::Emit32(Out, 0);
1404 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1405 }
1406
1407 // Create a blob abbreviation
1408 using namespace llvm;
1409 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1410 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1411 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1412 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001413 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor09b69892011-02-10 17:09:37 +00001414 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1415 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1416
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001417 // Write the header search table
Douglas Gregor09b69892011-02-10 17:09:37 +00001418 RecordData Record;
1419 Record.push_back(HEADER_SEARCH_TABLE);
1420 Record.push_back(BucketOffset);
1421 Record.push_back(NumHeaderSearchEntries);
Douglas Gregor4b123cb2011-07-28 04:50:02 +00001422 Record.push_back(TableData.size());
1423 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregor09b69892011-02-10 17:09:37 +00001424 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1425
1426 // Free all of the strings we had to duplicate.
1427 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1428 free((void*)SavedStrings[I]);
1429}
1430
Douglas Gregora7f71a92009-04-10 03:52:48 +00001431/// \brief Writes the block containing the serialized form of the
1432/// source manager.
1433///
1434/// TODO: We should probably use an on-disk hash table (stored in a
1435/// blob), indexed based on the file name, so that we only create
1436/// entries for files that we actually need. In the common case (no
1437/// errors), we probably won't have to create file entries for any of
1438/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001439void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001440 const Preprocessor &PP,
Douglas Gregorc567ba22011-07-22 16:35:34 +00001441 StringRef isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001442 RecordData Record;
1443
Chris Lattner0910e3b2009-04-10 17:16:57 +00001444 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001445 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001446
1447 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001448 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1449 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1450 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001451 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001452
Douglas Gregor258ae542009-04-27 06:38:32 +00001453 // Write out the source location entry table. We skip the first
1454 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001455 std::vector<uint32_t> SLocEntryOffsets;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001456 // Write out the offsets of only source location file entries.
1457 // We will go through them in ASTReader::validateFileEntries().
1458 std::vector<uint32_t> SLocFileEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001459 RecordData PreloadSLocs;
Douglas Gregor925296b2011-07-19 16:10:42 +00001460 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1461 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl5c415f32010-07-22 17:01:13 +00001462 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001463 // Get this source location entry.
Douglas Gregor925296b2011-07-19 16:10:42 +00001464 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001465
Douglas Gregor258ae542009-04-27 06:38:32 +00001466 // Record the offset of this source-location entry.
1467 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1468
1469 // Figure out which record code to use.
1470 unsigned Code;
1471 if (SLoc->isFile()) {
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001472 if (SLoc->getFile().getContentCache()->OrigEntry) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001473 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001474 SLocFileEntryOffsets.push_back(Stream.GetCurrentBitNo());
1475 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001476 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001477 } else
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001478 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001479 Record.clear();
1480 Record.push_back(Code);
1481
Douglas Gregor925296b2011-07-19 16:10:42 +00001482 // Starting offset of this entry within this module, so skip the dummy.
1483 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor258ae542009-04-27 06:38:32 +00001484 if (SLoc->isFile()) {
1485 const SrcMgr::FileInfo &File = SLoc->getFile();
1486 Record.push_back(File.getIncludeLoc().getRawEncoding());
1487 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1488 Record.push_back(File.hasLineDirectives());
1489
1490 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001491 if (Content->OrigEntry) {
1492 assert(Content->OrigEntry == Content->ContentsEntry &&
1493 "Writing to AST an overriden file is not supported");
1494
Douglas Gregor258ae542009-04-27 06:38:32 +00001495 // The source location entry is a file. The blob associated
1496 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001497
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001498 // Emit size/modification time for this file.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001499 Record.push_back(Content->OrigEntry->getSize());
1500 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001501
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001502 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001503 const char *Filename = Content->OrigEntry->getName();
Michael J. Spencer740857f2010-12-21 16:45:57 +00001504 llvm::SmallString<128> FilePath(Filename);
Anders Carlssona4267052011-03-08 16:04:35 +00001505
1506 // Ask the file manager to fixup the relative path for us. This will
1507 // honor the working directory.
1508 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1509
1510 // FIXME: This call to make_absolute shouldn't be necessary, the
1511 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencer740857f2010-12-21 16:45:57 +00001512 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001513 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001514
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001515 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001516 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001517 } else {
1518 // The source location entry is a buffer. The blob associated
1519 // with this entry contains the contents of the buffer.
1520
1521 // We add one to the size so that we capture the trailing NULL
1522 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1523 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001524 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001525 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001526 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001527 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001528 StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001529 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001530 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001531 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001532 StringRef(Buffer->getBufferStart(),
Daniel Dunbar8100d012009-08-24 09:31:37 +00001533 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001534
Douglas Gregor925296b2011-07-19 16:10:42 +00001535 if (strcmp(Name, "<built-in>") == 0) {
1536 PreloadSLocs.push_back(SLocEntryOffsets.size());
1537 }
Douglas Gregor258ae542009-04-27 06:38:32 +00001538 }
1539 } else {
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001540 // The source location entry is a macro expansion.
Chandler Carruthee4c1d12011-07-26 04:56:51 +00001541 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth73ee5d72011-07-26 04:41:47 +00001542 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1543 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
1544 Record.push_back(Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor258ae542009-04-27 06:38:32 +00001545
1546 // Compute the token length for this macro expansion.
Douglas Gregor925296b2011-07-19 16:10:42 +00001547 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001548 if (I + 1 != N)
Douglas Gregor925296b2011-07-19 16:10:42 +00001549 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001550 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001551 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor258ae542009-04-27 06:38:32 +00001552 }
1553 }
1554
Douglas Gregor8f45df52009-04-16 22:23:12 +00001555 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001556
1557 if (SLocEntryOffsets.empty())
1558 return;
1559
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001560 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001561 // table is used for lazily loading source-location information.
1562 using namespace llvm;
1563 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001564 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001565 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregor925296b2011-07-19 16:10:42 +00001566 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor258ae542009-04-27 06:38:32 +00001567 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1568 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001569
Douglas Gregor258ae542009-04-27 06:38:32 +00001570 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001571 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001572 Record.push_back(SLocEntryOffsets.size());
Douglas Gregor925296b2011-07-19 16:10:42 +00001573 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001574 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001575
Argyrios Kyrtzidis92dd4662011-06-02 20:01:46 +00001576 Abbrev = new BitCodeAbbrev();
1577 Abbrev->Add(BitCodeAbbrevOp(FILE_SOURCE_LOCATION_OFFSETS));
1578 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1579 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1580 unsigned SLocFileOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1581
1582 Record.clear();
1583 Record.push_back(FILE_SOURCE_LOCATION_OFFSETS);
1584 Record.push_back(SLocFileEntryOffsets.size());
1585 Stream.EmitRecordWithBlob(SLocFileOffsetsAbbrev, Record,
1586 data(SLocFileEntryOffsets));
1587
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001588 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001589 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001590 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor925296b2011-07-19 16:10:42 +00001591
1592 // Write the line table. It depends on remapping working, so it must come
1593 // after the source location offsets.
1594 if (SourceMgr.hasLineTable()) {
1595 LineTableInfo &LineTable = SourceMgr.getLineTable();
1596
1597 Record.clear();
1598 // Emit the file names
1599 Record.push_back(LineTable.getNumFilenames());
1600 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1601 // Emit the file name
1602 const char *Filename = LineTable.getFilename(I);
1603 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1604 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1605 Record.push_back(FilenameLen);
1606 if (FilenameLen)
1607 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1608 }
1609
1610 // Emit the line entries
1611 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1612 L != LEnd; ++L) {
1613 // Only emit entries for local files.
1614 if (L->first < 0)
1615 continue;
1616
1617 // Emit the file ID
1618 Record.push_back(L->first);
1619
1620 // Emit the line entries
1621 Record.push_back(L->second.size());
1622 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1623 LEEnd = L->second.end();
1624 LE != LEEnd; ++LE) {
1625 Record.push_back(LE->FileOffset);
1626 Record.push_back(LE->LineNo);
1627 Record.push_back(LE->FilenameID);
1628 Record.push_back((unsigned)LE->FileKind);
1629 Record.push_back(LE->IncludeOffset);
1630 }
1631 }
1632 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1633 }
Douglas Gregora7f71a92009-04-10 03:52:48 +00001634}
1635
Douglas Gregorc5046832009-04-27 18:38:38 +00001636//===----------------------------------------------------------------------===//
1637// Preprocessor Serialization
1638//===----------------------------------------------------------------------===//
1639
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001640static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1641 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1642 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1643 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1644 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1645 return X.first->getName().compare(Y.first->getName());
1646}
1647
Chris Lattnereeffaef2009-04-10 17:15:23 +00001648/// \brief Writes the block containing the serialized form of the
1649/// preprocessor.
1650///
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001651void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001652 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001653
Chris Lattner0af3ba12009-04-13 01:29:17 +00001654 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1655 if (PP.getCounterValue() != 0) {
1656 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001657 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001658 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001659 }
1660
1661 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001662 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001663
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001664 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001665 // FIXME: use diagnostics subsystem for localization etc.
1666 if (PP.SawDateOrTime())
1667 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001668
Douglas Gregor796d76a2010-10-20 22:00:55 +00001669
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001670 // Loop over all the macro definitions that are live at the end of the file,
1671 // emitting each to the PP section.
Douglas Gregoraae92242010-03-19 21:51:54 +00001672 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001673
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001674 // Construct the list of macro definitions that need to be serialized.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001675 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001676 MacrosToEmit;
1677 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor68051a72011-02-11 00:26:14 +00001678 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1679 E = PP.macro_end(Chain == 0);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001680 I != E; ++I) {
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001681 MacroDefinitionsSeen.insert(I->first);
1682 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1683 }
1684
1685 // Sort the set of macro definitions that need to be serialized by the
1686 // name of the macro, to provide a stable ordering.
1687 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1688 &compareMacroDefinitions);
1689
Douglas Gregor68051a72011-02-11 00:26:14 +00001690 // Resolve any identifiers that defined macros at the time they were
1691 // deserialized, adding them to the list of macros to emit (if appropriate).
1692 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1693 IdentifierInfo *Name
1694 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1695 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1696 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1697 }
1698
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001699 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1700 const IdentifierInfo *Name = MacrosToEmit[I].first;
1701 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor68051a72011-02-11 00:26:14 +00001702 if (!MI)
1703 continue;
1704
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001705 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001706 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001707 // Also skip macros from a AST file if we're chaining.
Douglas Gregoreb114da2010-10-01 01:03:07 +00001708
1709 // FIXME: There is a (probably minor) optimization we could do here, if
1710 // the macro comes from the original PCH but the identifier comes from a
1711 // chained PCH, by storing the offset into the original PCH rather than
1712 // writing the macro definition a second time.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001713 if (MI->isBuiltinMacro() ||
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001714 (Chain && Name->isFromAST() && MI->isFromAST()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001715 continue;
1716
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001717 AddIdentifierRef(Name, Record);
1718 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001719 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1720 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001721
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001722 unsigned Code;
1723 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001724 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001725 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001726 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001727
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001728 Record.push_back(MI->isC99Varargs());
1729 Record.push_back(MI->isGNUVarargs());
1730 Record.push_back(MI->getNumArgs());
1731 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1732 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001733 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001734 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001735
Douglas Gregoraae92242010-03-19 21:51:54 +00001736 // If we have a detailed preprocessing record, record the macro definition
1737 // ID that corresponds to this macro.
1738 if (PPRec)
1739 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001740
Douglas Gregor8f45df52009-04-16 22:23:12 +00001741 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001742 Record.clear();
1743
Chris Lattner2199f5b2009-04-10 18:08:30 +00001744 // Emit the tokens array.
1745 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1746 // Note that we know that the preprocessor does not have any annotation
1747 // tokens in it because they are created by the parser, and thus can't be
1748 // in a macro definition.
1749 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001750
Chris Lattner2199f5b2009-04-10 18:08:30 +00001751 Record.push_back(Tok.getLocation().getRawEncoding());
1752 Record.push_back(Tok.getLength());
1753
Chris Lattner2199f5b2009-04-10 18:08:30 +00001754 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1755 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001756 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001757 // FIXME: Should translate token kind to a stable encoding.
1758 Record.push_back(Tok.getKind());
1759 // FIXME: Should translate token flags to a stable encoding.
1760 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001761
Sebastian Redl539c5062010-08-18 23:57:32 +00001762 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001763 Record.clear();
1764 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001765 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001766 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001767 Stream.ExitBlock();
1768
1769 if (PPRec)
1770 WritePreprocessorDetail(*PPRec);
1771}
1772
1773void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
1774 if (PPRec.begin(Chain) == PPRec.end(Chain))
1775 return;
1776
1777 // Enter the preprocessor block.
1778 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001779
Douglas Gregoraae92242010-03-19 21:51:54 +00001780 // If the preprocessor has a preprocessing record, emit it.
1781 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001782 using namespace llvm;
1783
1784 // Set up the abbreviation for
1785 unsigned InclusionAbbrev = 0;
1786 {
1787 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1788 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
1789 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1790 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1791 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1792 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1793 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1794 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1795 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1796 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1797 }
1798
Douglas Gregor4a9c39a2011-07-21 00:47:40 +00001799 unsigned IndexBase = Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001800 RecordData Record;
Douglas Gregord32f0352011-07-22 06:10:01 +00001801 uint64_t BitsInChain = Chain? Chain->TotalModulesSizeInBits : 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001802 for (PreprocessingRecord::iterator E = PPRec.begin(Chain),
1803 EEnd = PPRec.end(Chain);
1804 E != EEnd; ++E) {
1805 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001806
Douglas Gregor92a96f52011-02-08 21:58:10 +00001807 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1808 // Record this macro definition's location.
1809 MacroID ID = getMacroDefinitionID(MD);
1810
1811 // Don't write the macro definition if it is from another AST file.
1812 if (ID < FirstMacroID)
Douglas Gregoraae92242010-03-19 21:51:54 +00001813 continue;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001814
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001815 // Notify the serialization listener that we're serializing this entity.
1816 if (SerializationListener)
1817 SerializationListener->SerializedPreprocessedEntity(*E,
Douglas Gregord32f0352011-07-22 06:10:01 +00001818 BitsInChain + Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001819
Douglas Gregor92a96f52011-02-08 21:58:10 +00001820 unsigned Position = ID - FirstMacroID;
1821 if (Position != MacroDefinitionOffsets.size()) {
1822 if (Position > MacroDefinitionOffsets.size())
1823 MacroDefinitionOffsets.resize(Position + 1);
1824
1825 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
1826 } else
1827 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001828
Douglas Gregor92a96f52011-02-08 21:58:10 +00001829 Record.push_back(IndexBase + NumPreprocessingRecords++);
1830 Record.push_back(ID);
1831 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1832 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1833 AddIdentifierRef(MD->getName(), Record);
1834 AddSourceLocation(MD->getLocation(), Record);
1835 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1836 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00001837 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001838
Douglas Gregor92a96f52011-02-08 21:58:10 +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 Gregor92a96f52011-02-08 21:58:10 +00001843
Chandler Carrutha88a22182011-07-14 08:20:46 +00001844 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Douglas Gregor92a96f52011-02-08 21:58:10 +00001845 Record.push_back(IndexBase + NumPreprocessingRecords++);
Chandler Carrutha88a22182011-07-14 08:20:46 +00001846 AddSourceLocation(ME->getSourceRange().getBegin(), Record);
1847 AddSourceLocation(ME->getSourceRange().getEnd(), Record);
1848 AddIdentifierRef(ME->getName(), Record);
1849 Record.push_back(getMacroDefinitionID(ME->getDefinition()));
Chandler Carruthf92ac9e2011-07-15 07:25:21 +00001850 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor92a96f52011-02-08 21:58:10 +00001851 continue;
1852 }
1853
1854 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1855 Record.push_back(PPD_INCLUSION_DIRECTIVE);
1856 Record.push_back(IndexBase + NumPreprocessingRecords++);
1857 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1858 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1859 Record.push_back(ID->getFileName().size());
1860 Record.push_back(ID->wasInQuotes());
1861 Record.push_back(static_cast<unsigned>(ID->getKind()));
1862 llvm::SmallString<64> Buffer;
1863 Buffer += ID->getFileName();
1864 Buffer += ID->getFile()->getName();
1865 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1866 continue;
1867 }
1868
1869 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1870 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001871 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001872
Douglas Gregoraae92242010-03-19 21:51:54 +00001873 // Write the offsets table for the preprocessing record.
1874 if (NumPreprocessingRecords > 0) {
1875 // Write the offsets table for identifier IDs.
1876 using namespace llvm;
1877 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001878 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregoraae92242010-03-19 21:51:54 +00001879 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1880 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
Douglas Gregora863b4b2011-08-04 16:36:56 +00001881 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first macro def
Douglas Gregoraae92242010-03-19 21:51:54 +00001882 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1883 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001884
Douglas Gregoraae92242010-03-19 21:51:54 +00001885 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001886 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001887 Record.push_back(NumPreprocessingRecords);
1888 Record.push_back(MacroDefinitionOffsets.size());
Douglas Gregora863b4b2011-08-04 16:36:56 +00001889 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001890 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001891 data(MacroDefinitionOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00001892 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001893}
1894
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001895void ASTWriter::WritePragmaDiagnosticMappings(const Diagnostic &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001896 RecordData Record;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001897 for (Diagnostic::DiagStatePointsTy::const_iterator
1898 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1899 I != E; ++I) {
1900 const Diagnostic::DiagStatePoint &point = *I;
1901 if (point.Loc.isInvalid())
1902 continue;
1903
1904 Record.push_back(point.Loc.getRawEncoding());
1905 for (Diagnostic::DiagState::iterator
1906 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
1907 unsigned diag = I->first, map = I->second;
1908 if (map & 0x10) { // mapping from a diagnostic pragma.
1909 Record.push_back(diag);
1910 Record.push_back(map & 0x7);
1911 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001912 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001913 Record.push_back(-1); // mark the end of the diag/map pairs for this
1914 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001915 }
1916
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00001917 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001918 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001919}
1920
Anders Carlsson9bb83e82011-03-06 18:41:18 +00001921void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
1922 if (CXXBaseSpecifiersOffsets.empty())
1923 return;
1924
1925 RecordData Record;
1926
1927 // Create a blob abbreviation for the C++ base specifiers offsets.
1928 using namespace llvm;
1929
1930 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1931 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
1932 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
1933 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1934 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1935
Douglas Gregorc27b2872011-08-04 00:01:48 +00001936 // Write the base specifier offsets table.
Anders Carlsson9bb83e82011-03-06 18:41:18 +00001937 Record.clear();
1938 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
1939 Record.push_back(CXXBaseSpecifiersOffsets.size());
1940 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001941 data(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00001942}
1943
Douglas Gregorc5046832009-04-27 18:38:38 +00001944//===----------------------------------------------------------------------===//
1945// Type Serialization
1946//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001947
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001948/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001949void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00001950 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001951 if (Idx.getIndex() == 0) // we haven't seen this type before.
1952 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00001953
Douglas Gregor9b3932c2010-10-05 18:37:06 +00001954 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00001955
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001956 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001957 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001958 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001959 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001960 else if (TypeOffsets.size() < Index) {
1961 TypeOffsets.resize(Index + 1);
1962 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001963 }
1964
1965 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001966
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001967 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001968 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001969
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001970 if (T.hasLocalNonFastQualifiers()) {
1971 Qualifiers Qs = T.getLocalQualifiers();
1972 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001973 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001974 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00001975 } else {
1976 switch (T->getTypeClass()) {
1977 // For all of the concrete, non-dependent types, call the
1978 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001979#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001980 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001981#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001982#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001983 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001984 }
1985
1986 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001987 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001988
1989 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001990 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001991}
1992
Douglas Gregorc5046832009-04-27 18:38:38 +00001993//===----------------------------------------------------------------------===//
1994// Declaration Serialization
1995//===----------------------------------------------------------------------===//
1996
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001997/// \brief Write the block containing all of the declaration IDs
1998/// lexically declared within the given DeclContext.
1999///
2000/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2001/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002002uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002003 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002004 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002005 return 0;
2006
Douglas Gregor8f45df52009-04-16 22:23:12 +00002007 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002008 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002009 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002010 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002011 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2012 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002013 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002014
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002015 ++NumLexicalDeclContexts;
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002016 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002017 return Offset;
2018}
2019
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002020void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002021 using namespace llvm;
2022 RecordData Record;
2023
2024 // Write the type offsets array
2025 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002026 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002027 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregor5204bde2011-08-02 16:26:37 +00002028 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002029 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2030 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2031 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002032 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002033 Record.push_back(TypeOffsets.size());
Douglas Gregor5204bde2011-08-02 16:26:37 +00002034 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002035 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002036
2037 // Write the declaration offsets array
2038 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002039 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002040 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregorf7180622011-08-03 15:48:04 +00002041 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002042 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2043 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2044 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002045 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002046 Record.push_back(DeclOffsets.size());
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002047 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002048 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002049}
2050
Douglas Gregorc5046832009-04-27 18:38:38 +00002051//===----------------------------------------------------------------------===//
2052// Global Method Pool and Selector Serialization
2053//===----------------------------------------------------------------------===//
2054
Douglas Gregore84a9da2009-04-20 20:36:09 +00002055namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002056// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002057class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002058 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002059
2060public:
2061 typedef Selector key_type;
2062 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002063
Sebastian Redl834bb972010-08-04 17:20:04 +00002064 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00002065 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00002066 ObjCMethodList Instance, Factory;
2067 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00002068 typedef const data_type& data_type_ref;
2069
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002070 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002071
Douglas Gregorc78d3462009-04-24 21:10:55 +00002072 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002073 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002074 }
Mike Stump11289f42009-09-09 15:08:12 +00002075
2076 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002077 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002078 data_type_ref Methods) {
2079 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2080 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002081 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2082 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002083 Method = Method->Next)
2084 if (Method->Method)
2085 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002086 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002087 Method = Method->Next)
2088 if (Method->Method)
2089 DataLen += 4;
2090 clang::io::Emit16(Out, DataLen);
2091 return std::make_pair(KeyLen, DataLen);
2092 }
Mike Stump11289f42009-09-09 15:08:12 +00002093
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002094 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00002095 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002096 assert((Start >> 32) == 0 && "Selector key offset too large");
2097 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002098 unsigned N = Sel.getNumArgs();
2099 clang::io::Emit16(Out, N);
2100 if (N == 0)
2101 N = 1;
2102 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00002103 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002104 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2105 }
Mike Stump11289f42009-09-09 15:08:12 +00002106
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002107 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002108 data_type_ref Methods, unsigned DataLen) {
2109 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00002110 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002111 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002112 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002113 Method = Method->Next)
2114 if (Method->Method)
2115 ++NumInstanceMethods;
2116
2117 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002118 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002119 Method = Method->Next)
2120 if (Method->Method)
2121 ++NumFactoryMethods;
2122
2123 clang::io::Emit16(Out, NumInstanceMethods);
2124 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00002125 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002126 Method = Method->Next)
2127 if (Method->Method)
2128 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002129 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002130 Method = Method->Next)
2131 if (Method->Method)
2132 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002133
2134 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002135 }
2136};
2137} // end anonymous namespace
2138
Sebastian Redla19a67f2010-08-03 21:58:15 +00002139/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002140///
2141/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002142/// in an on-disk hash table indexed by the selector. The hash table also
2143/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002144void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002145 using namespace llvm;
2146
Sebastian Redla19a67f2010-08-03 21:58:15 +00002147 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002148 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002149 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002150 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002151 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002152 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002153 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002154 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002155
Sebastian Redla19a67f2010-08-03 21:58:15 +00002156 // Create the on-disk hash table representation. We walk through every
2157 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002158 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002159 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002160 I = SelectorIDs.begin(), E = SelectorIDs.end();
2161 I != E; ++I) {
2162 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002163 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002164 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002165 I->second,
2166 ObjCMethodList(),
2167 ObjCMethodList()
2168 };
2169 if (F != SemaRef.MethodPool.end()) {
2170 Data.Instance = F->second.first;
2171 Data.Factory = F->second.second;
2172 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002173 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002174 // changed.
2175 if (Chain && I->second < FirstSelectorID) {
2176 // Selector already exists. Did it change?
2177 bool changed = false;
2178 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2179 M = M->Next) {
2180 if (M->Method->getPCHLevel() == 0)
2181 changed = true;
2182 }
2183 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2184 M = M->Next) {
2185 if (M->Method->getPCHLevel() == 0)
2186 changed = true;
2187 }
2188 if (!changed)
2189 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002190 } else if (Data.Instance.Method || Data.Factory.Method) {
2191 // A new method pool entry.
2192 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002193 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002194 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002195 }
2196
Douglas Gregorc78d3462009-04-24 21:10:55 +00002197 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002198 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002199 uint32_t BucketOffset;
2200 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002201 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002202 llvm::raw_svector_ostream Out(MethodPool);
2203 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002204 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002205 BucketOffset = Generator.Emit(Out, Trait);
2206 }
2207
2208 // Create a blob abbreviation
2209 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002210 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002211 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002212 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002213 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2214 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2215
Douglas Gregor95c13f52009-04-25 17:48:32 +00002216 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002217 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002218 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002219 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002220 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002221 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002222
2223 // Create a blob abbreviation for the selector table offsets.
2224 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002225 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002226 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002227 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor95c13f52009-04-25 17:48:32 +00002228 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2229 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2230
2231 // Write the selector offsets table.
2232 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002233 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002234 Record.push_back(SelectorOffsets.size());
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002235 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002236 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002237 data(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002238 }
2239}
2240
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002241/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002242void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002243 using namespace llvm;
2244 if (SemaRef.ReferencedSelectors.empty())
2245 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002246
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002247 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002248
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002249 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002250 // very tricky to fix, and given that @selector shouldn't really appear in
2251 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002252 for (DenseMap<Selector, SourceLocation>::iterator S =
2253 SemaRef.ReferencedSelectors.begin(),
2254 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2255 Selector Sel = (*S).first;
2256 SourceLocation Loc = (*S).second;
2257 AddSelectorRef(Sel, Record);
2258 AddSourceLocation(Loc, Record);
2259 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002260 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002261}
2262
Douglas Gregorc5046832009-04-27 18:38:38 +00002263//===----------------------------------------------------------------------===//
2264// Identifier Table Serialization
2265//===----------------------------------------------------------------------===//
2266
Douglas Gregorc78d3462009-04-24 21:10:55 +00002267namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002268class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002269 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002270 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002271
Douglas Gregor1d583f22009-04-28 21:18:29 +00002272 /// \brief Determines whether this is an "interesting" identifier
2273 /// that needs a full IdentifierInfo structure written into the hash
2274 /// table.
2275 static bool isInterestingIdentifier(const IdentifierInfo *II) {
2276 return II->isPoisoned() ||
2277 II->isExtensionToken() ||
2278 II->hasMacroDefinition() ||
2279 II->getObjCOrBuiltinID() ||
2280 II->getFETokenInfo<void>();
2281 }
2282
Douglas Gregore84a9da2009-04-20 20:36:09 +00002283public:
2284 typedef const IdentifierInfo* key_type;
2285 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002286
Sebastian Redl539c5062010-08-18 23:57:32 +00002287 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002288 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002289
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002290 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00002291 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00002292
2293 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00002294 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00002295 }
Mike Stump11289f42009-09-09 15:08:12 +00002296
2297 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002298 EmitKeyDataLength(raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002299 IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002300 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002301 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
2302 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00002303 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00002304 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00002305 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00002306 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002307 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2308 DEnd = IdentifierResolver::end();
2309 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00002310 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00002311 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00002312 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00002313 // We emit the key length after the data length so that every
2314 // string is preceded by a 16-bit length. This matches the PTH
2315 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00002316 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002317 return std::make_pair(KeyLen, DataLen);
2318 }
Mike Stump11289f42009-09-09 15:08:12 +00002319
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002320 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00002321 unsigned KeyLen) {
2322 // Record the location of the key data. This is used when generating
2323 // the mapping from persistent IDs to strings.
2324 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002325 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002326 }
Mike Stump11289f42009-09-09 15:08:12 +00002327
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002328 void EmitData(raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002329 IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00002330 if (!isInterestingIdentifier(II)) {
2331 clang::io::Emit32(Out, ID << 1);
2332 return;
2333 }
Douglas Gregorb9256522009-04-28 21:32:13 +00002334
Douglas Gregor1d583f22009-04-28 21:18:29 +00002335 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002336 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002337 bool hasMacroDefinition =
2338 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00002339 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00002340 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002341 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
2342 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2343 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00002344 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002345 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00002346 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002347
Douglas Gregorc3366a52009-04-21 23:56:24 +00002348 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00002349 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00002350
Douglas Gregora868bbd2009-04-21 22:25:48 +00002351 // Emit the declaration IDs in reverse order, because the
2352 // IdentifierResolver provides the declarations as they would be
2353 // visible (e.g., the function "stat" would come before the struct
2354 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2355 // adds declarations to the end of the list (so we need to see the
2356 // struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00002357 // Only emit declarations that aren't from a chained PCH, though.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002358 SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00002359 IdentifierResolver::end());
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002360 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregora868bbd2009-04-21 22:25:48 +00002361 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00002362 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00002363 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002364 }
2365};
2366} // end anonymous namespace
2367
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002368/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002369///
2370/// The identifier table consists of a blob containing string data
2371/// (the actual identifiers themselves) and a separate "offsets" index
2372/// that maps identifier IDs to locations within the blob.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002373void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002374 using namespace llvm;
2375
2376 // Create and write out the blob that contains the identifier
2377 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002378 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002379 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002380 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump11289f42009-09-09 15:08:12 +00002381
Douglas Gregore6648fb2009-04-28 20:33:11 +00002382 // Look for any identifiers that were named while processing the
2383 // headers, but are otherwise not needed. We add these to the hash
2384 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002385 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00002386 // file.
2387 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2388 IDEnd = PP.getIdentifierTable().end();
2389 ID != IDEnd; ++ID)
2390 getIdentifierRef(ID->second);
2391
Sebastian Redlff4a2952010-07-23 23:49:55 +00002392 // Create the on-disk hash table representation. We only store offsets
2393 // for identifiers that appear here for the first time.
2394 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002395 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002396 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2397 ID != IDEnd; ++ID) {
2398 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002399 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002400 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002401 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002402
Douglas Gregore84a9da2009-04-20 20:36:09 +00002403 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002404 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00002405 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002406 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002407 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002408 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002409 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002410 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002411 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002412 }
2413
2414 // Create a blob abbreviation
2415 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002416 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00002417 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002418 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00002419 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002420
2421 // Write the identifier table
2422 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002423 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002424 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002425 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002426 }
2427
2428 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00002429 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002430 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00002431 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002432 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor0e149972009-04-25 19:10:14 +00002433 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2434 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2435
2436 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002437 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00002438 Record.push_back(IdentifierOffsets.size());
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002439 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor0e149972009-04-25 19:10:14 +00002440 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002441 data(IdentifierOffsets));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002442}
2443
Douglas Gregorc5046832009-04-27 18:38:38 +00002444//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002445// DeclContext's Name Lookup Table Serialization
2446//===----------------------------------------------------------------------===//
2447
2448namespace {
2449// Trait used for the on-disk hash table used in the method pool.
2450class ASTDeclContextNameLookupTrait {
2451 ASTWriter &Writer;
2452
2453public:
2454 typedef DeclarationName key_type;
2455 typedef key_type key_type_ref;
2456
2457 typedef DeclContext::lookup_result data_type;
2458 typedef const data_type& data_type_ref;
2459
2460 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2461
2462 unsigned ComputeHash(DeclarationName Name) {
2463 llvm::FoldingSetNodeID ID;
2464 ID.AddInteger(Name.getNameKind());
2465
2466 switch (Name.getNameKind()) {
2467 case DeclarationName::Identifier:
2468 ID.AddString(Name.getAsIdentifierInfo()->getName());
2469 break;
2470 case DeclarationName::ObjCZeroArgSelector:
2471 case DeclarationName::ObjCOneArgSelector:
2472 case DeclarationName::ObjCMultiArgSelector:
2473 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2474 break;
2475 case DeclarationName::CXXConstructorName:
2476 case DeclarationName::CXXDestructorName:
2477 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002478 break;
2479 case DeclarationName::CXXOperatorName:
2480 ID.AddInteger(Name.getCXXOverloadedOperator());
2481 break;
2482 case DeclarationName::CXXLiteralOperatorName:
2483 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2484 case DeclarationName::CXXUsingDirective:
2485 break;
2486 }
2487
2488 return ID.ComputeHash();
2489 }
2490
2491 std::pair<unsigned,unsigned>
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002492 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002493 data_type_ref Lookup) {
2494 unsigned KeyLen = 1;
2495 switch (Name.getNameKind()) {
2496 case DeclarationName::Identifier:
2497 case DeclarationName::ObjCZeroArgSelector:
2498 case DeclarationName::ObjCOneArgSelector:
2499 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002500 case DeclarationName::CXXLiteralOperatorName:
2501 KeyLen += 4;
2502 break;
2503 case DeclarationName::CXXOperatorName:
2504 KeyLen += 1;
2505 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002506 case DeclarationName::CXXConstructorName:
2507 case DeclarationName::CXXDestructorName:
2508 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002509 case DeclarationName::CXXUsingDirective:
2510 break;
2511 }
2512 clang::io::Emit16(Out, KeyLen);
2513
2514 // 2 bytes for num of decls and 4 for each DeclID.
2515 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2516 clang::io::Emit16(Out, DataLen);
2517
2518 return std::make_pair(KeyLen, DataLen);
2519 }
2520
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002521 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002522 using namespace clang::io;
2523
2524 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2525 Emit8(Out, Name.getNameKind());
2526 switch (Name.getNameKind()) {
2527 case DeclarationName::Identifier:
2528 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2529 break;
2530 case DeclarationName::ObjCZeroArgSelector:
2531 case DeclarationName::ObjCOneArgSelector:
2532 case DeclarationName::ObjCMultiArgSelector:
2533 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2534 break;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002535 case DeclarationName::CXXOperatorName:
2536 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2537 Emit8(Out, Name.getCXXOverloadedOperator());
2538 break;
2539 case DeclarationName::CXXLiteralOperatorName:
2540 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2541 break;
Douglas Gregor3b65ed02011-08-02 18:32:54 +00002542 case DeclarationName::CXXConstructorName:
2543 case DeclarationName::CXXDestructorName:
2544 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002545 case DeclarationName::CXXUsingDirective:
2546 break;
2547 }
2548 }
2549
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002550 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002551 data_type Lookup, unsigned DataLen) {
2552 uint64_t Start = Out.tell(); (void)Start;
2553 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2554 for (; Lookup.first != Lookup.second; ++Lookup.first)
2555 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2556
2557 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2558 }
2559};
2560} // end anonymous namespace
2561
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002562/// \brief Write the block containing all of the declaration IDs
2563/// visible from the given DeclContext.
2564///
2565/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002566/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002567uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2568 DeclContext *DC) {
2569 if (DC->getPrimaryContext() != DC)
2570 return 0;
2571
2572 // Since there is no name lookup into functions or methods, don't bother to
2573 // build a visible-declarations table for these entities.
2574 if (DC->isFunctionOrMethod())
2575 return 0;
2576
2577 // If not in C++, we perform name lookup for the translation unit via the
2578 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2579 // FIXME: In C++ we need the visible declarations in order to "see" the
2580 // friend declarations, is there a way to do this without writing the table ?
2581 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2582 return 0;
2583
2584 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00002585 if (DC->hasExternalVisibleStorage())
2586 DC->MaterializeVisibleDeclsFromExternalStorage();
2587 else
2588 DC->lookup(DeclarationName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002589
2590 // Serialize the contents of the mapping used for lookup. Note that,
2591 // although we have two very different code paths, the serialized
2592 // representation is the same for both cases: a declaration name,
2593 // followed by a size, followed by references to the visible
2594 // declarations that have that name.
2595 uint64_t Offset = Stream.GetCurrentBitNo();
2596 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2597 if (!Map || Map->empty())
2598 return 0;
2599
2600 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2601 ASTDeclContextNameLookupTrait Trait(*this);
2602
2603 // Create the on-disk hash table representation.
2604 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2605 D != DEnd; ++D) {
2606 DeclarationName Name = D->first;
2607 DeclContext::lookup_result Result = D->second.getLookupResult();
2608 Generator.insert(Name, Result, Trait);
2609 }
2610
2611 // Create the on-disk hash table in a buffer.
2612 llvm::SmallString<4096> LookupTable;
2613 uint32_t BucketOffset;
2614 {
2615 llvm::raw_svector_ostream Out(LookupTable);
2616 // Make sure that no bucket is at offset 0
2617 clang::io::Emit32(Out, 0);
2618 BucketOffset = Generator.Emit(Out, Trait);
2619 }
2620
2621 // Write the lookup table
2622 RecordData Record;
2623 Record.push_back(DECL_CONTEXT_VISIBLE);
2624 Record.push_back(BucketOffset);
2625 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2626 LookupTable.str());
2627
2628 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2629 ++NumVisibleDeclContexts;
2630 return Offset;
2631}
2632
Sebastian Redla4071b42010-08-24 00:50:09 +00002633/// \brief Write an UPDATE_VISIBLE block for the given context.
2634///
2635/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2636/// DeclContext in a dependent AST file. As such, they only exist for the TU
2637/// (in C++) and for namespaces.
2638void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00002639 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2640 if (!Map || Map->empty())
2641 return;
2642
2643 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2644 ASTDeclContextNameLookupTrait Trait(*this);
2645
2646 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002647 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2648 D != DEnd; ++D) {
2649 DeclarationName Name = D->first;
2650 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002651 // For any name that appears in this table, the results are complete, i.e.
2652 // they overwrite results from previous PCHs. Merging is always a mess.
2653 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002654 }
2655
2656 // Create the on-disk hash table in a buffer.
2657 llvm::SmallString<4096> LookupTable;
2658 uint32_t BucketOffset;
2659 {
2660 llvm::raw_svector_ostream Out(LookupTable);
2661 // Make sure that no bucket is at offset 0
2662 clang::io::Emit32(Out, 0);
2663 BucketOffset = Generator.Emit(Out, Trait);
2664 }
2665
2666 // Write the lookup table
2667 RecordData Record;
2668 Record.push_back(UPDATE_VISIBLE);
2669 Record.push_back(getDeclID(cast<Decl>(DC)));
2670 Record.push_back(BucketOffset);
2671 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2672}
2673
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002674/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2675void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2676 RecordData Record;
2677 Record.push_back(Opts.fp_contract);
2678 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2679}
2680
2681/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2682void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
2683 if (!SemaRef.Context.getLangOptions().OpenCL)
2684 return;
2685
2686 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2687 RecordData Record;
2688#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2689#include "clang/Basic/OpenCLExtensions.def"
2690 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2691}
2692
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002693//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00002694// General Serialization Routines
2695//===----------------------------------------------------------------------===//
2696
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002697/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002698void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00002699 Record.push_back(Attrs.size());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002700 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2701 const Attr * A = *i;
2702 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2703 AddSourceLocation(A->getLocation(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002704
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002705#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00002706
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002707 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002708}
2709
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002710void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002711 Record.push_back(Str.size());
2712 Record.insert(Record.end(), Str.begin(), Str.end());
2713}
2714
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002715void ASTWriter::AddVersionTuple(const VersionTuple &Version,
2716 RecordDataImpl &Record) {
2717 Record.push_back(Version.getMajor());
2718 if (llvm::Optional<unsigned> Minor = Version.getMinor())
2719 Record.push_back(*Minor + 1);
2720 else
2721 Record.push_back(0);
2722 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
2723 Record.push_back(*Subminor + 1);
2724 else
2725 Record.push_back(0);
2726}
2727
Douglas Gregore84a9da2009-04-20 20:36:09 +00002728/// \brief Note that the identifier II occurs at the given offset
2729/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002730void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002731 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002732 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00002733 // up earlier in the chain and thus don't need an offset.
2734 if (ID >= FirstIdentID)
2735 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002736}
2737
Douglas Gregor95c13f52009-04-25 17:48:32 +00002738/// \brief Note that the selector Sel occurs at the given offset
2739/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002740void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00002741 unsigned ID = SelectorIDs[Sel];
2742 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00002743 // Don't record offsets for selectors that are also available in a different
2744 // file.
2745 if (ID < FirstSelectorID)
2746 return;
2747 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002748}
2749
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002750ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00002751 : Stream(Stream), Chain(0), SerializationListener(0),
Douglas Gregor6f8912e2011-08-03 16:05:40 +00002752 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00002753 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregor1ab036c2011-08-03 21:49:18 +00002754 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
Douglas Gregor8f364fb2011-08-03 23:28:44 +00002755 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregora863b4b2011-08-04 16:36:56 +00002756 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor91096292010-10-02 19:29:26 +00002757 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002758 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00002759 NumVisibleDeclContexts(0),
Douglas Gregorc27b2872011-08-04 00:01:48 +00002760 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00002761 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregor03412ba2011-06-03 02:27:19 +00002762 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
2763 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
2764 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner205c7d52011-06-03 23:11:16 +00002765 DeclTypedefAbbrev(0),
2766 DeclVarAbbrev(0), DeclFieldAbbrev(0),
2767 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002768{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002769}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002770
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002771void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002772 const std::string &OutputFile,
Douglas Gregorc567ba22011-07-22 16:35:34 +00002773 StringRef isysroot) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002774 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002775 Stream.Emit((unsigned)'C', 8);
2776 Stream.Emit((unsigned)'P', 8);
2777 Stream.Emit((unsigned)'C', 8);
2778 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002779
Chris Lattner28fa4e62009-04-26 22:26:21 +00002780 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002781
Sebastian Redl143413f2010-07-12 22:02:52 +00002782 if (Chain)
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002783 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002784 else
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002785 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile);
Sebastian Redl143413f2010-07-12 22:02:52 +00002786}
2787
Douglas Gregora94a1542011-07-27 21:45:57 +00002788template<typename Vector>
2789static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
2790 ASTWriter::RecordData &Record) {
2791 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
2792 I != E; ++I) {
2793 Writer.AddDeclRef(*I, Record);
2794 }
2795}
2796
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002797void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregorc567ba22011-07-22 16:35:34 +00002798 StringRef isysroot,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002799 const std::string &OutputFile) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002800 using namespace llvm;
2801
2802 ASTContext &Context = SemaRef.Context;
2803 Preprocessor &PP = SemaRef.PP;
2804
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002805 // The translation unit is the first declaration we'll emit.
2806 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlff4a2952010-07-23 23:49:55 +00002807 ++NextDeclID;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002808 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002809
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002810 // Make sure that we emit IdentifierInfos (and any attached
2811 // declarations) for builtins.
2812 {
2813 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002814 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002815 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2816 Context.getLangOptions().NoBuiltin);
2817 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2818 getIdentifierRef(&Table.get(BuiltinNames[I]));
2819 }
2820
Chris Lattner0c797362009-09-08 18:19:27 +00002821 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002822 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002823 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002824 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00002825 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00002826
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002827 // Build a record containing all of the file scoped decls in this file.
2828 RecordData UnusedFileScopedDecls;
Douglas Gregora94a1542011-07-27 21:45:57 +00002829 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
2830 UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002831
Alexis Hunt27a761d2011-05-04 23:29:54 +00002832 RecordData DelegatingCtorDecls;
Douglas Gregorbae31202011-07-27 21:57:17 +00002833 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00002834
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002835 RecordData WeakUndeclaredIdentifiers;
2836 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00002837 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002838 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2839 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2840 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2841 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2842 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2843 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2844 }
2845 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002846
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002847 // Build a record containing all of the locally-scoped external
2848 // declarations in this header file. Generally, this record will be
2849 // empty.
2850 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002851 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00002852 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002853 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002854 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2855 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregordc5c9582011-07-28 14:20:37 +00002856 TD != TDEnd; ++TD) {
2857 if (TD->second->getPCHLevel() == 0)
2858 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2859 }
2860
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002861 // Build a record containing all of the ext_vector declarations.
2862 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00002863 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002864
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002865 // Build a record containing all of the VTable uses information.
2866 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00002867 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00002868 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2869 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2870 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2871 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2872 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002873 }
2874
2875 // Build a record containing all of dynamic classes declarations.
2876 RecordData DynamicClasses;
Douglas Gregor32002192011-07-28 00:53:40 +00002877 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002878
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002879 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002880 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002881 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002882 I = SemaRef.PendingInstantiations.begin(),
2883 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2884 AddDeclRef(I->first, PendingInstantiations);
2885 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002886 }
2887 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2888 "There are local ones at end of translation unit!");
2889
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002890 // Build a record containing some declaration references.
2891 RecordData SemaDeclRefs;
2892 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2893 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2894 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2895 }
2896
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002897 RecordData CUDASpecialDeclRefs;
2898 if (Context.getcudaConfigureCallDecl()) {
2899 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
2900 }
2901
Douglas Gregorc2fa1692011-06-28 16:20:02 +00002902 // Build a record containing all of the known namespaces.
2903 RecordData KnownNamespaces;
2904 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
2905 I = SemaRef.KnownNamespaces.begin(),
2906 IEnd = SemaRef.KnownNamespaces.end();
2907 I != IEnd; ++I) {
2908 if (!I->second)
2909 AddDeclRef(I->first, KnownNamespaces);
2910 }
2911
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002912 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002913 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002914 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002915 WriteMetadata(Context, isysroot, OutputFile);
Sebastian Redl143413f2010-07-12 22:02:52 +00002916 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregorc567ba22011-07-22 16:35:34 +00002917 if (StatCalls && isysroot.empty())
Douglas Gregor11cfd942010-07-12 23:48:14 +00002918 WriteStatCache(*StatCalls);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002919 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Douglas Gregor2df17cb2011-08-01 16:54:33 +00002920
Douglas Gregor5204bde2011-08-02 16:26:37 +00002921 // Form the record of special types.
2922 RecordData SpecialTypes;
2923 AddTypeRef(Context.getBuiltinVaListType(), SpecialTypes);
2924 AddTypeRef(Context.getObjCIdType(), SpecialTypes);
2925 AddTypeRef(Context.getObjCSelType(), SpecialTypes);
2926 AddTypeRef(Context.getObjCProtoType(), SpecialTypes);
2927 AddTypeRef(Context.getObjCClassType(), SpecialTypes);
2928 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
2929 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), SpecialTypes);
2930 AddTypeRef(Context.getFILEType(), SpecialTypes);
2931 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
2932 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
2933 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
2934 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
2935 AddTypeRef(Context.getRawBlockdescriptorType(), SpecialTypes);
2936 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), SpecialTypes);
2937 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
2938 AddTypeRef(Context.getRawNSConstantStringType(), SpecialTypes);
2939 SpecialTypes.push_back(Context.isInt128Installed());
2940 AddTypeRef(Context.AutoDeductTy, SpecialTypes);
2941 AddTypeRef(Context.AutoRRefDeductTy, SpecialTypes);
Mike Stump11289f42009-09-09 15:08:12 +00002942
Douglas Gregor1970d882009-04-26 03:49:13 +00002943 // Keep writing types and declarations until all types and
2944 // declarations have been written.
Douglas Gregor03412ba2011-06-03 02:27:19 +00002945 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002946 WriteDeclsBlockAbbrevs();
2947 while (!DeclTypesToEmit.empty()) {
2948 DeclOrType DOT = DeclTypesToEmit.front();
2949 DeclTypesToEmit.pop();
2950 if (DOT.isType())
2951 WriteType(DOT.getType());
2952 else
2953 WriteDecl(Context, DOT.getDecl());
2954 }
2955 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002956
Douglas Gregor45053152009-10-17 17:25:45 +00002957 WritePreprocessor(PP);
Douglas Gregor09b69892011-02-10 17:09:37 +00002958 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00002959 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002960 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002961 WriteIdentifierTable(PP);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002962 WriteFPPragmaOptions(SemaRef.getFPOptions());
2963 WriteOpenCLExtensions(SemaRef);
Douglas Gregor745ed142009-04-25 18:35:21 +00002964
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002965 WriteTypeDeclOffsets();
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002966 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00002967
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002968 WriteCXXBaseSpecifiersOffsets();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002969
Douglas Gregor5204bde2011-08-02 16:26:37 +00002970 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
2971
Douglas Gregord4df8652009-04-22 22:02:47 +00002972 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002973 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002974 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002975
2976 // Write the record containing tentative definitions.
2977 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002978 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002979
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002980 // Write the record containing unused file scoped decls.
2981 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002982 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002983
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002984 // Write the record containing weak undeclared identifiers.
2985 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002986 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002987 WeakUndeclaredIdentifiers);
2988
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002989 // Write the record containing locally-scoped external definitions.
2990 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002991 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002992 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002993
2994 // Write the record containing ext_vector type names.
2995 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002996 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002997
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002998 // Write the record containing VTable uses information.
2999 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003000 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003001
3002 // Write the record containing dynamic classes declarations.
3003 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003004 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00003005
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003006 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003007 if (!PendingInstantiations.empty())
3008 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00003009
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003010 // Write the record containing declaration references of Sema.
3011 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003012 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00003013
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003014 // Write the record containing CUDA-specific declaration references.
3015 if (!CUDASpecialDeclRefs.empty())
3016 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003017
3018 // Write the delegating constructors.
3019 if (!DelegatingCtorDecls.empty())
3020 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00003021
Douglas Gregorc2fa1692011-06-28 16:20:02 +00003022 // Write the known namespaces.
3023 if (!KnownNamespaces.empty())
3024 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3025
Douglas Gregor08f01292009-04-17 22:13:46 +00003026 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00003027 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00003028 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00003029 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00003030 Record.push_back(NumLexicalDeclContexts);
3031 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00003032 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00003033 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003034}
3035
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003036void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregorc567ba22011-07-22 16:35:34 +00003037 StringRef isysroot) {
Sebastian Redl143413f2010-07-12 22:02:52 +00003038 using namespace llvm;
3039
3040 ASTContext &Context = SemaRef.Context;
3041 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003042
Sebastian Redl143413f2010-07-12 22:02:52 +00003043 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003044 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00003045 WriteMetadata(Context, isysroot, "");
Douglas Gregorc567ba22011-07-22 16:35:34 +00003046 if (StatCalls && isysroot.empty())
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003047 WriteStatCache(*StatCalls);
3048 // FIXME: Source manager block should only write new stuff, which could be
3049 // done by tracking the largest ID in the chain
3050 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00003051
Douglas Gregor2df17cb2011-08-01 16:54:33 +00003052 // Write the mapping information describing our module dependencies and how
3053 // each of those modules were mapped into our own offset/ID space, so that
3054 // the reader can build the appropriate mapping to its own offset/ID space.
Douglas Gregor2df17cb2011-08-01 16:54:33 +00003055 // The map consists solely of a blob with the following format:
Douglas Gregor00659902011-08-02 10:56:51 +00003056 // *(module-name-len:i16 module-name:len*i8
3057 // source-location-offset:i32
3058 // identifier-id:i32
3059 // preprocessed-entity-id:i32
3060 // macro-definition-id:i32
3061 // selector-id:i32
3062 // declaration-id:i32
3063 // c++-base-specifiers-id:i32
3064 // type-id:i32)
3065 //
Douglas Gregor2df17cb2011-08-01 16:54:33 +00003066 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3067 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3068 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor00659902011-08-02 10:56:51 +00003069 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor2df17cb2011-08-01 16:54:33 +00003070 llvm::SmallString<2048> Buffer;
3071 {
3072 llvm::raw_svector_ostream Out(Buffer);
Douglas Gregor00659902011-08-02 10:56:51 +00003073 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
3074 MEnd = Chain->ModuleMgr.end();
3075 M != MEnd; ++M) {
3076 StringRef FileName = (*M)->FileName;
3077 io::Emit16(Out, FileName.size());
3078 Out.write(FileName.data(), FileName.size());
3079 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3080 io::Emit32(Out, (*M)->BaseIdentifierID);
3081 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
3082 io::Emit32(Out, (*M)->BaseMacroDefinitionID);
3083 io::Emit32(Out, (*M)->BaseSelectorID);
3084 io::Emit32(Out, (*M)->BaseDeclID);
Douglas Gregor3b65ed02011-08-02 18:32:54 +00003085 io::Emit32(Out, (*M)->BaseTypeIndex);
Douglas Gregor2df17cb2011-08-01 16:54:33 +00003086 }
3087 }
3088 Record.clear();
3089 Record.push_back(MODULE_OFFSET_MAP);
Douglas Gregor00659902011-08-02 10:56:51 +00003090 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
Douglas Gregor2df17cb2011-08-01 16:54:33 +00003091 Buffer.data(), Buffer.size());
3092
Sebastian Redl143413f2010-07-12 22:02:52 +00003093 // The special types are in the chained PCH.
3094
3095 // We don't start with the translation unit, but with its decls that
Sebastian Redld44cd6a2010-08-18 23:57:06 +00003096 // don't come from the chained PCH.
Sebastian Redl143413f2010-07-12 22:02:52 +00003097 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003098 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003099 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3100 E = TU->noload_decls_end();
Sebastian Redl143413f2010-07-12 22:02:52 +00003101 I != E; ++I) {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00003102 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00003103 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003104 else if ((*I)->isChangedSinceDeserialization())
3105 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl143413f2010-07-12 22:02:52 +00003106 }
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003107 // We also need to write a lexical updates block for the TU.
Sebastian Redl4b1f4902010-07-27 18:24:41 +00003108 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00003109 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redl4b1f4902010-07-27 18:24:41 +00003110 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3111 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3112 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00003113 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00003114 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00003115 data(NewGlobalDecls));
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003116 // And a visible updates block for the DeclContexts.
3117 Abv = new llvm::BitCodeAbbrev();
3118 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3119 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3120 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3121 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3122 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3123 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl143413f2010-07-12 22:02:52 +00003124
Sebastian Redl98912122010-07-27 23:01:28 +00003125 // Build a record containing all of the new tentative definitions in this
3126 // file, in TentativeDefinitions order.
3127 RecordData TentativeDefinitions;
Douglas Gregora94a1542011-07-27 21:45:57 +00003128 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregoreb08bd42011-07-27 20:58:46 +00003129
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003130 // Build a record containing all of the file scoped decls in this file.
3131 RecordData UnusedFileScopedDecls;
Douglas Gregora94a1542011-07-27 21:45:57 +00003132 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3133 UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003134
Alexis Hunt27a761d2011-05-04 23:29:54 +00003135 // Build a record containing all of the delegating constructor decls in this
3136 // file.
3137 RecordData DelegatingCtorDecls;
Douglas Gregorbae31202011-07-27 21:57:17 +00003138 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003139
Sebastian Redl08aca90252010-08-05 18:21:25 +00003140 // We write the entire table, overwriting the tables from the chain.
3141 RecordData WeakUndeclaredIdentifiers;
3142 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor1c4bfe52011-07-28 18:09:57 +00003143 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Sebastian Redl08aca90252010-08-05 18:21:25 +00003144 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3145 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3146 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3147 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3148 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3149 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3150 }
3151 }
3152
Sebastian Redl98912122010-07-27 23:01:28 +00003153 // Build a record containing all of the locally-scoped external
3154 // declarations in this header file. Generally, this record will be
3155 // empty.
3156 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003157 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl98912122010-07-27 23:01:28 +00003158 // nondeterminstic!
3159 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
3160 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3161 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
3162 TD != TDEnd; ++TD) {
3163 if (TD->second->getPCHLevel() == 0)
3164 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3165 }
3166
3167 // Build a record containing all of the ext_vector declarations.
3168 RecordData ExtVectorDecls;
Douglas Gregorb7098a32011-07-28 00:39:29 +00003169 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003170
Sebastian Redl08aca90252010-08-05 18:21:25 +00003171 // Build a record containing all of the VTable uses information.
3172 // We write everything here, because it's too hard to determine whether
3173 // a use is new to this part.
3174 RecordData VTableUses;
3175 if (!SemaRef.VTableUses.empty()) {
Sebastian Redl08aca90252010-08-05 18:21:25 +00003176 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3177 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3178 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3179 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3180 }
3181 }
3182
3183 // Build a record containing all of dynamic classes declarations.
3184 RecordData DynamicClasses;
Douglas Gregor32002192011-07-28 00:53:40 +00003185 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003186
3187 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003188 RecordData PendingInstantiations;
Sebastian Redl08aca90252010-08-05 18:21:25 +00003189 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00003190 I = SemaRef.PendingInstantiations.begin(),
3191 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl14afaf02011-04-24 16:27:30 +00003192 AddDeclRef(I->first, PendingInstantiations);
3193 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003194 }
3195 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3196 "There are local ones at end of translation unit!");
3197
3198 // Build a record containing some declaration references.
3199 // It's not worth the effort to avoid duplication here.
3200 RecordData SemaDeclRefs;
3201 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3202 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3203 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3204 }
3205
Douglas Gregor03412ba2011-06-03 02:27:19 +00003206 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Sebastian Redl143413f2010-07-12 22:02:52 +00003207 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003208 for (DeclsToRewriteTy::iterator
3209 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
3210 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl143413f2010-07-12 22:02:52 +00003211 while (!DeclTypesToEmit.empty()) {
3212 DeclOrType DOT = DeclTypesToEmit.front();
3213 DeclTypesToEmit.pop();
3214 if (DOT.isType())
3215 WriteType(DOT.getType());
3216 else
3217 WriteDecl(Context, DOT.getDecl());
3218 }
3219 Stream.ExitBlock();
3220
Sebastian Redl98912122010-07-27 23:01:28 +00003221 WritePreprocessor(PP);
Sebastian Redl51c79d82010-08-04 22:21:29 +00003222 WriteSelectors(SemaRef);
3223 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlff4a2952010-07-23 23:49:55 +00003224 WriteIdentifierTable(PP);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003225 WriteFPPragmaOptions(SemaRef.getFPOptions());
3226 WriteOpenCLExtensions(SemaRef);
3227
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003228 WriteTypeDeclOffsets();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003229 // FIXME: For chained PCH only write the new mappings (we currently
3230 // write all of them again).
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003231 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl98912122010-07-27 23:01:28 +00003232
Anders Carlsson9bb83e82011-03-06 18:41:18 +00003233 WriteCXXBaseSpecifiersOffsets();
3234
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003235 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003236 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003237 RecordData FirstLatestDeclIDs;
3238 for (FirstLatestDeclMap::iterator
3239 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
3240 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
3241 "Expected first & second to be in different PCHs");
3242 AddDeclRef(I->first, FirstLatestDeclIDs);
3243 AddDeclRef(I->second, FirstLatestDeclIDs);
3244 }
3245 if (!FirstLatestDeclIDs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003246 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003247
Sebastian Redl98912122010-07-27 23:01:28 +00003248 // Write the record containing external, unnamed definitions.
3249 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003250 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00003251
3252 // Write the record containing tentative definitions.
3253 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003254 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00003255
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003256 // Write the record containing unused file scoped decls.
3257 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003258 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003259
Sebastian Redl08aca90252010-08-05 18:21:25 +00003260 // Write the record containing weak undeclared identifiers.
3261 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003262 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl08aca90252010-08-05 18:21:25 +00003263 WeakUndeclaredIdentifiers);
3264
Sebastian Redl98912122010-07-27 23:01:28 +00003265 // Write the record containing locally-scoped external definitions.
3266 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003267 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl98912122010-07-27 23:01:28 +00003268 LocallyScopedExternalDecls);
3269
3270 // Write the record containing ext_vector type names.
3271 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003272 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003273
Sebastian Redl08aca90252010-08-05 18:21:25 +00003274 // Write the record containing VTable uses information.
3275 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003276 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003277
3278 // Write the record containing dynamic classes declarations.
3279 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003280 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003281
3282 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003283 if (!PendingInstantiations.empty())
3284 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003285
3286 // Write the record containing declaration references of Sema.
3287 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003288 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003289
3290 // Write the delegating constructors.
3291 if (!DelegatingCtorDecls.empty())
3292 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003293
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003294 // Write the updates to DeclContexts.
3295 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3296 I = UpdatedDeclContexts.begin(),
3297 E = UpdatedDeclContexts.end();
Sebastian Redla4071b42010-08-24 00:50:09 +00003298 I != E; ++I)
3299 WriteDeclContextVisibleUpdate(*I);
3300
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003301 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003302
Sebastian Redl98912122010-07-27 23:01:28 +00003303 Record.clear();
3304 Record.push_back(NumStatements);
3305 Record.push_back(NumMacros);
3306 Record.push_back(NumLexicalDeclContexts);
3307 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003308 WriteDeclReplacementsBlock();
Sebastian Redl539c5062010-08-18 23:57:32 +00003309 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl143413f2010-07-12 22:02:52 +00003310 Stream.ExitBlock();
3311}
3312
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003313void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003314 if (DeclUpdates.empty())
3315 return;
3316
3317 RecordData OffsetsRecord;
Douglas Gregor03412ba2011-06-03 02:27:19 +00003318 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003319 for (DeclUpdateMap::iterator
3320 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3321 const Decl *D = I->first;
3322 UpdateRecord &URec = I->second;
3323
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00003324 if (DeclsToRewrite.count(D))
3325 continue; // The decl will be written completely,no need to store updates.
3326
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003327 uint64_t Offset = Stream.GetCurrentBitNo();
3328 Stream.EmitRecord(DECL_UPDATES, URec);
3329
3330 OffsetsRecord.push_back(GetDeclRef(D));
3331 OffsetsRecord.push_back(Offset);
3332 }
3333 Stream.ExitBlock();
3334 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3335}
3336
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003337void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003338 if (ReplacedDecls.empty())
3339 return;
3340
3341 RecordData Record;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003342 for (SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003343 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
3344 Record.push_back(I->first);
3345 Record.push_back(I->second);
3346 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003347 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003348}
3349
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003350void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003351 Record.push_back(Loc.getRawEncoding());
3352}
3353
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003354void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003355 AddSourceLocation(Range.getBegin(), Record);
3356 AddSourceLocation(Range.getEnd(), Record);
3357}
3358
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003359void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003360 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00003361 const uint64_t *Words = Value.getRawData();
3362 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003363}
3364
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003365void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00003366 Record.push_back(Value.isUnsigned());
3367 AddAPInt(Value, Record);
3368}
3369
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003370void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00003371 AddAPInt(Value.bitcastToAPInt(), Record);
3372}
3373
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003374void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003375 Record.push_back(getIdentifierRef(II));
3376}
3377
Sebastian Redl539c5062010-08-18 23:57:32 +00003378IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003379 if (II == 0)
3380 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003381
Sebastian Redl539c5062010-08-18 23:57:32 +00003382 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003383 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00003384 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003385 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003386}
3387
Sebastian Redl50e26582010-09-15 19:54:06 +00003388MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregoraae92242010-03-19 21:51:54 +00003389 if (MD == 0)
3390 return 0;
Sebastian Redl50e26582010-09-15 19:54:06 +00003391
3392 MacroID &ID = MacroDefinitions[MD];
Douglas Gregoraae92242010-03-19 21:51:54 +00003393 if (ID == 0)
Douglas Gregor91096292010-10-02 19:29:26 +00003394 ID = NextMacroID++;
Douglas Gregoraae92242010-03-19 21:51:54 +00003395 return ID;
3396}
3397
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003398void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003399 Record.push_back(getSelectorRef(SelRef));
3400}
3401
Sebastian Redl539c5062010-08-18 23:57:32 +00003402SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003403 if (Sel.getAsOpaquePtr() == 0) {
3404 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00003405 }
3406
Sebastian Redl539c5062010-08-18 23:57:32 +00003407 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00003408 if (SID == 0 && Chain) {
3409 // This might trigger a ReadSelector callback, which will set the ID for
3410 // this selector.
3411 Chain->LoadSelector(Sel);
3412 }
Steve Naroff2ddea052009-04-23 10:39:46 +00003413 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003414 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00003415 }
Sebastian Redl834bb972010-08-04 17:20:04 +00003416 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00003417}
3418
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003419void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00003420 AddDeclRef(Temp->getDestructor(), Record);
3421}
3422
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003423void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3424 CXXBaseSpecifier const *BasesEnd,
3425 RecordDataImpl &Record) {
3426 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3427 CXXBaseSpecifiersToWrite.push_back(
3428 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3429 Bases, BasesEnd));
3430 Record.push_back(NextCXXBaseSpecifiersID++);
3431}
3432
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003433void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003434 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003435 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003436 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00003437 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003438 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00003439 break;
3440 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003441 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00003442 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003443 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00003444 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003445 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003446 break;
3447 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00003448 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003449 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003450 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003451 break;
John McCall0ad16662009-10-29 08:12:44 +00003452 case TemplateArgument::Null:
3453 case TemplateArgument::Integral:
3454 case TemplateArgument::Declaration:
3455 case TemplateArgument::Pack:
3456 break;
3457 }
3458}
3459
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003460void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003461 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003462 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003463
3464 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3465 bool InfoHasSameExpr
3466 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3467 Record.push_back(InfoHasSameExpr);
3468 if (InfoHasSameExpr)
3469 return; // Avoid storing the same expr twice.
3470 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003471 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3472 Record);
3473}
3474
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003475void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3476 RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00003477 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00003478 AddTypeRef(QualType(), Record);
3479 return;
3480 }
3481
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003482 AddTypeLoc(TInfo->getTypeLoc(), Record);
3483}
3484
3485void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3486 AddTypeRef(TL.getType(), Record);
3487
John McCall8f115c62009-10-16 21:56:05 +00003488 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003489 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003490 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00003491}
3492
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003493void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00003494 Record.push_back(GetOrCreateTypeID(T));
3495}
3496
3497TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003498 return MakeTypeID(T,
3499 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3500}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003501
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003502TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003503 return MakeTypeID(T,
3504 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003505}
3506
3507TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3508 if (T.isNull())
3509 return TypeIdx();
3510 assert(!T.getLocalFastQualifiers());
3511
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00003512 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003513 if (Idx.getIndex() == 0) {
Douglas Gregor1970d882009-04-26 03:49:13 +00003514 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00003515 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003516 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003517 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00003518 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003519 return Idx;
3520}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003521
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003522TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003523 if (T.isNull())
3524 return TypeIdx();
3525 assert(!T.getLocalFastQualifiers());
3526
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003527 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3528 assert(I != TypeIdxs.end() && "Type not emitted!");
3529 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003530}
3531
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003532void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003533 Record.push_back(GetDeclRef(D));
3534}
3535
Sebastian Redl539c5062010-08-18 23:57:32 +00003536DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003537 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003538 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003539 }
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003540 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00003541 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00003542 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003543 // We haven't seen this declaration before. Give it a new ID and
3544 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00003545 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003546 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003547 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3548 // We don't add it to the replacement collection here, because we don't
3549 // have the offset yet.
3550 DeclTypesToEmit.push(const_cast<Decl *>(D));
3551 // Reset the flag, so that we don't add this decl multiple times.
3552 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003553 }
3554
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003555 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003556}
3557
Sebastian Redl539c5062010-08-18 23:57:32 +00003558DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00003559 if (D == 0)
3560 return 0;
3561
3562 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3563 return DeclIDs[D];
3564}
3565
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003566void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00003567 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003568 Record.push_back(Name.getNameKind());
3569 switch (Name.getNameKind()) {
3570 case DeclarationName::Identifier:
3571 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3572 break;
3573
3574 case DeclarationName::ObjCZeroArgSelector:
3575 case DeclarationName::ObjCOneArgSelector:
3576 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00003577 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003578 break;
3579
3580 case DeclarationName::CXXConstructorName:
3581 case DeclarationName::CXXDestructorName:
3582 case DeclarationName::CXXConversionFunctionName:
3583 AddTypeRef(Name.getCXXNameType(), Record);
3584 break;
3585
3586 case DeclarationName::CXXOperatorName:
3587 Record.push_back(Name.getCXXOverloadedOperator());
3588 break;
3589
Alexis Hunt3d221f22009-11-29 07:34:05 +00003590 case DeclarationName::CXXLiteralOperatorName:
3591 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3592 break;
3593
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003594 case DeclarationName::CXXUsingDirective:
3595 // No extra data to emit
3596 break;
3597 }
3598}
Chris Lattnerca025db2010-05-07 21:43:38 +00003599
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003600void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003601 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003602 switch (Name.getNameKind()) {
3603 case DeclarationName::CXXConstructorName:
3604 case DeclarationName::CXXDestructorName:
3605 case DeclarationName::CXXConversionFunctionName:
3606 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3607 break;
3608
3609 case DeclarationName::CXXOperatorName:
3610 AddSourceLocation(
3611 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3612 Record);
3613 AddSourceLocation(
3614 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3615 Record);
3616 break;
3617
3618 case DeclarationName::CXXLiteralOperatorName:
3619 AddSourceLocation(
3620 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3621 Record);
3622 break;
3623
3624 case DeclarationName::Identifier:
3625 case DeclarationName::ObjCZeroArgSelector:
3626 case DeclarationName::ObjCOneArgSelector:
3627 case DeclarationName::ObjCMultiArgSelector:
3628 case DeclarationName::CXXUsingDirective:
3629 break;
3630 }
3631}
3632
3633void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003634 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003635 AddDeclarationName(NameInfo.getName(), Record);
3636 AddSourceLocation(NameInfo.getLoc(), Record);
3637 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3638}
3639
3640void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003641 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00003642 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003643 Record.push_back(Info.NumTemplParamLists);
3644 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3645 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3646}
3647
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003648void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003649 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003650 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00003651 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003652 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattnerca025db2010-05-07 21:43:38 +00003653
3654 // Push each of the NNS's onto a stack for serialization in reverse order.
3655 while (NNS) {
3656 NestedNames.push_back(NNS);
3657 NNS = NNS->getPrefix();
3658 }
3659
3660 Record.push_back(NestedNames.size());
3661 while(!NestedNames.empty()) {
3662 NNS = NestedNames.pop_back_val();
3663 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3664 Record.push_back(Kind);
3665 switch (Kind) {
3666 case NestedNameSpecifier::Identifier:
3667 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3668 break;
3669
3670 case NestedNameSpecifier::Namespace:
3671 AddDeclRef(NNS->getAsNamespace(), Record);
3672 break;
3673
Douglas Gregor7b26ff92011-02-24 02:36:08 +00003674 case NestedNameSpecifier::NamespaceAlias:
3675 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
3676 break;
3677
Chris Lattnerca025db2010-05-07 21:43:38 +00003678 case NestedNameSpecifier::TypeSpec:
3679 case NestedNameSpecifier::TypeSpecWithTemplate:
3680 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3681 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3682 break;
3683
3684 case NestedNameSpecifier::Global:
3685 // Don't need to write an associated value.
3686 break;
3687 }
3688 }
3689}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003690
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003691void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
3692 RecordDataImpl &Record) {
3693 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00003694 // typically accommodate the vast majority.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003695 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003696
3697 // Push each of the nested-name-specifiers's onto a stack for
3698 // serialization in reverse order.
3699 while (NNS) {
3700 NestedNames.push_back(NNS);
3701 NNS = NNS.getPrefix();
3702 }
3703
3704 Record.push_back(NestedNames.size());
3705 while(!NestedNames.empty()) {
3706 NNS = NestedNames.pop_back_val();
3707 NestedNameSpecifier::SpecifierKind Kind
3708 = NNS.getNestedNameSpecifier()->getKind();
3709 Record.push_back(Kind);
3710 switch (Kind) {
3711 case NestedNameSpecifier::Identifier:
3712 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
3713 AddSourceRange(NNS.getLocalSourceRange(), Record);
3714 break;
3715
3716 case NestedNameSpecifier::Namespace:
3717 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
3718 AddSourceRange(NNS.getLocalSourceRange(), Record);
3719 break;
3720
3721 case NestedNameSpecifier::NamespaceAlias:
3722 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
3723 AddSourceRange(NNS.getLocalSourceRange(), Record);
3724 break;
3725
3726 case NestedNameSpecifier::TypeSpec:
3727 case NestedNameSpecifier::TypeSpecWithTemplate:
3728 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3729 AddTypeLoc(NNS.getTypeLoc(), Record);
3730 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3731 break;
3732
3733 case NestedNameSpecifier::Global:
3734 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3735 break;
3736 }
3737 }
3738}
3739
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003740void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003741 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003742 Record.push_back(Kind);
3743 switch (Kind) {
3744 case TemplateName::Template:
3745 AddDeclRef(Name.getAsTemplateDecl(), Record);
3746 break;
3747
3748 case TemplateName::OverloadedTemplate: {
3749 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3750 Record.push_back(OvT->size());
3751 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3752 I != E; ++I)
3753 AddDeclRef(*I, Record);
3754 break;
3755 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003756
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003757 case TemplateName::QualifiedTemplate: {
3758 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3759 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3760 Record.push_back(QualT->hasTemplateKeyword());
3761 AddDeclRef(QualT->getTemplateDecl(), Record);
3762 break;
3763 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003764
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003765 case TemplateName::DependentTemplate: {
3766 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3767 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3768 Record.push_back(DepT->isIdentifier());
3769 if (DepT->isIdentifier())
3770 AddIdentifierRef(DepT->getIdentifier(), Record);
3771 else
3772 Record.push_back(DepT->getOperator());
3773 break;
3774 }
John McCalld9dfe3a2011-06-30 08:33:18 +00003775
3776 case TemplateName::SubstTemplateTemplateParm: {
3777 SubstTemplateTemplateParmStorage *subst
3778 = Name.getAsSubstTemplateTemplateParm();
3779 AddDeclRef(subst->getParameter(), Record);
3780 AddTemplateName(subst->getReplacement(), Record);
3781 break;
3782 }
Douglas Gregor5590be02011-01-15 06:45:20 +00003783
3784 case TemplateName::SubstTemplateTemplateParmPack: {
3785 SubstTemplateTemplateParmPackStorage *SubstPack
3786 = Name.getAsSubstTemplateTemplateParmPack();
3787 AddDeclRef(SubstPack->getParameterPack(), Record);
3788 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
3789 break;
3790 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003791 }
3792}
3793
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003794void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003795 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003796 Record.push_back(Arg.getKind());
3797 switch (Arg.getKind()) {
3798 case TemplateArgument::Null:
3799 break;
3800 case TemplateArgument::Type:
3801 AddTypeRef(Arg.getAsType(), Record);
3802 break;
3803 case TemplateArgument::Declaration:
3804 AddDeclRef(Arg.getAsDecl(), Record);
3805 break;
3806 case TemplateArgument::Integral:
3807 AddAPSInt(*Arg.getAsIntegral(), Record);
3808 AddTypeRef(Arg.getIntegralType(), Record);
3809 break;
3810 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00003811 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3812 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003813 case TemplateArgument::TemplateExpansion:
3814 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregore1d60df2011-01-14 23:41:42 +00003815 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3816 Record.push_back(*NumExpansions + 1);
3817 else
3818 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003819 break;
3820 case TemplateArgument::Expression:
3821 AddStmt(Arg.getAsExpr());
3822 break;
3823 case TemplateArgument::Pack:
3824 Record.push_back(Arg.pack_size());
3825 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3826 I != E; ++I)
3827 AddTemplateArgument(*I, Record);
3828 break;
3829 }
3830}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003831
3832void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003833ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003834 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003835 assert(TemplateParams && "No TemplateParams!");
3836 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3837 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3838 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3839 Record.push_back(TemplateParams->size());
3840 for (TemplateParameterList::const_iterator
3841 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3842 P != PEnd; ++P)
3843 AddDeclRef(*P, Record);
3844}
3845
3846/// \brief Emit a template argument list.
3847void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003848ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003849 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003850 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003851 Record.push_back(TemplateArgs->size());
3852 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003853 AddTemplateArgument(TemplateArgs->get(i), Record);
3854}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003855
3856
3857void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003858ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003859 Record.push_back(Set.size());
3860 for (UnresolvedSetImpl::const_iterator
3861 I = Set.begin(), E = Set.end(); I != E; ++I) {
3862 AddDeclRef(I.getDecl(), Record);
3863 Record.push_back(I.getAccess());
3864 }
3865}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003866
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003867void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003868 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003869 Record.push_back(Base.isVirtual());
3870 Record.push_back(Base.isBaseOfClass());
3871 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00003872 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00003873 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003874 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00003875 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3876 : SourceLocation(),
3877 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003878}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003879
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003880void ASTWriter::FlushCXXBaseSpecifiers() {
3881 RecordData Record;
3882 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3883 Record.clear();
3884
3885 // Record the offset of this base-specifier set.
Douglas Gregorc27b2872011-08-04 00:01:48 +00003886 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003887 if (Index == CXXBaseSpecifiersOffsets.size())
3888 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3889 else {
3890 if (Index > CXXBaseSpecifiersOffsets.size())
3891 CXXBaseSpecifiersOffsets.resize(Index + 1);
3892 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3893 }
3894
3895 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3896 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3897 Record.push_back(BEnd - B);
3898 for (; B != BEnd; ++B)
3899 AddCXXBaseSpecifier(*B, Record);
3900 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00003901
3902 // Flush any expressions that were written as part of the base specifiers.
3903 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003904 }
3905
3906 CXXBaseSpecifiersToWrite.clear();
3907}
3908
Alexis Hunt1d792652011-01-08 20:30:50 +00003909void ASTWriter::AddCXXCtorInitializers(
3910 const CXXCtorInitializer * const *CtorInitializers,
3911 unsigned NumCtorInitializers,
3912 RecordDataImpl &Record) {
3913 Record.push_back(NumCtorInitializers);
3914 for (unsigned i=0; i != NumCtorInitializers; ++i) {
3915 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003916
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003917 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00003918 Record.push_back(CTOR_INITIALIZER_BASE);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003919 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3920 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00003921 } else if (Init->isDelegatingInitializer()) {
3922 Record.push_back(CTOR_INITIALIZER_DELEGATING);
3923 AddDeclRef(Init->getTargetConstructor(), Record);
3924 } else if (Init->isMemberInitializer()){
3925 Record.push_back(CTOR_INITIALIZER_MEMBER);
3926 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003927 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00003928 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
3929 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003930 }
Francois Pichetd583da02010-12-04 09:14:42 +00003931
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003932 AddSourceLocation(Init->getMemberLocation(), Record);
3933 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003934 AddSourceLocation(Init->getLParenLoc(), Record);
3935 AddSourceLocation(Init->getRParenLoc(), Record);
3936 Record.push_back(Init->isWritten());
3937 if (Init->isWritten()) {
3938 Record.push_back(Init->getSourceOrder());
3939 } else {
3940 Record.push_back(Init->getNumArrayIndices());
3941 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3942 AddDeclRef(Init->getArrayIndex(i), Record);
3943 }
3944 }
3945}
3946
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003947void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3948 assert(D->DefinitionData);
3949 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3950 Record.push_back(Data.UserDeclaredConstructor);
3951 Record.push_back(Data.UserDeclaredCopyConstructor);
3952 Record.push_back(Data.UserDeclaredCopyAssignment);
3953 Record.push_back(Data.UserDeclaredDestructor);
3954 Record.push_back(Data.Aggregate);
3955 Record.push_back(Data.PlainOldData);
3956 Record.push_back(Data.Empty);
3957 Record.push_back(Data.Polymorphic);
3958 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00003959 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00003960 Record.push_back(Data.HasNoNonEmptyBases);
3961 Record.push_back(Data.HasPrivateFields);
3962 Record.push_back(Data.HasProtectedFields);
3963 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00003964 Record.push_back(Data.HasMutableFields);
Alexis Huntf479f1b2011-05-09 18:22:59 +00003965 Record.push_back(Data.HasTrivialDefaultConstructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00003966 Record.push_back(Data.HasConstExprNonCopyMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003967 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruthad7d4042011-04-23 23:10:33 +00003968 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003969 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruthad7d4042011-04-23 23:10:33 +00003970 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003971 Record.push_back(Data.HasTrivialDestructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00003972 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003973 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00003974 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003975 Record.push_back(Data.DeclaredDefaultConstructor);
3976 Record.push_back(Data.DeclaredCopyConstructor);
3977 Record.push_back(Data.DeclaredCopyAssignment);
3978 Record.push_back(Data.DeclaredDestructor);
3979
3980 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003981 if (Data.NumBases > 0)
3982 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3983 Record);
3984
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003985 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3986 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003987 if (Data.NumVBases > 0)
3988 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3989 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003990
3991 AddUnresolvedSet(Data.Conversions, Record);
3992 AddUnresolvedSet(Data.VisibleConversions, Record);
3993 // Data.Definition is the owning decl, no need to write it.
3994 AddDeclRef(Data.FirstFriend, Record);
3995}
3996
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003997void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00003998 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003999 assert(!Chain && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00004000 assert(FirstDeclID == NextDeclID &&
4001 FirstTypeID == NextTypeID &&
4002 FirstIdentID == NextIdentID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00004003 FirstSelectorID == NextSelectorID &&
Douglas Gregor91096292010-10-02 19:29:26 +00004004 FirstMacroID == NextMacroID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00004005 "Setting chain after writing has started.");
Douglas Gregor925296b2011-07-19 16:10:42 +00004006
Sebastian Redl07a89a82010-07-30 00:29:29 +00004007 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00004008
4009 FirstDeclID += Chain->getTotalNumDecls();
4010 FirstTypeID += Chain->getTotalNumTypes();
4011 FirstIdentID += Chain->getTotalNumIdentifiers();
4012 FirstSelectorID += Chain->getTotalNumSelectors();
4013 FirstMacroID += Chain->getTotalNumMacroDefinitions();
4014 NextDeclID = FirstDeclID;
4015 NextTypeID = FirstTypeID;
4016 NextIdentID = FirstIdentID;
4017 NextSelectorID = FirstSelectorID;
4018 NextMacroID = FirstMacroID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00004019}
4020
Sebastian Redl539c5062010-08-18 23:57:32 +00004021void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00004022 IdentifierIDs[II] = ID;
Douglas Gregor68051a72011-02-11 00:26:14 +00004023 if (II->hasMacroDefinition())
4024 DeserializedMacroNames.push_back(II);
Sebastian Redlff4a2952010-07-23 23:49:55 +00004025}
4026
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00004027void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004028 // Always take the highest-numbered type index. This copes with an interesting
4029 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004030 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00004031 // keep the higher-numbered entry so that we can properly write it out to
4032 // the AST file.
4033 TypeIdx &StoredIdx = TypeIdxs[T];
4034 if (Idx.getIndex() >= StoredIdx.getIndex())
4035 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004036}
4037
Sebastian Redl539c5062010-08-18 23:57:32 +00004038void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00004039 DeclIDs[D] = ID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00004040}
Sebastian Redl834bb972010-08-04 17:20:04 +00004041
Sebastian Redl539c5062010-08-18 23:57:32 +00004042void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00004043 SelectorIDs[S] = ID;
4044}
Douglas Gregor91096292010-10-02 19:29:26 +00004045
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00004046void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00004047 MacroDefinition *MD) {
4048 MacroDefinitions[MD] = ID;
4049}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004050
4051void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
4052 assert(D->isDefinition());
4053 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4054 // We are interested when a PCH decl is modified.
4055 if (RD->getPCHLevel() > 0) {
4056 // A forward reference was mutated into a definition. Rewrite it.
4057 // FIXME: This happens during template instantiation, should we
4058 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00004059 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00004060 }
4061
4062 for (CXXRecordDecl::redecl_iterator
4063 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
4064 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
4065 if (Redecl == RD)
4066 continue;
4067
4068 // We are interested when a PCH decl is modified.
4069 if (Redecl->getPCHLevel() > 0) {
4070 UpdateRecord &Record = DeclUpdates[Redecl];
4071 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
4072 assert(Redecl->DefinitionData);
4073 assert(Redecl->DefinitionData->Definition == D);
4074 AddDeclRef(D, Record); // the DefinitionDecl
4075 }
4076 }
4077 }
4078}
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00004079void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
4080 // TU and namespaces are handled elsewhere.
4081 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4082 return;
4083
4084 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
4085 return; // Not a source decl added to a DeclContext from PCH.
4086
4087 AddUpdatedDeclContext(DC);
4088}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00004089
4090void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
4091 assert(D->isImplicit());
4092 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
4093 return; // Not a source member added to a class from PCH.
4094 if (!isa<CXXMethodDecl>(D))
4095 return; // We are interested in lazily declared implicit methods.
4096
4097 // A decl coming from PCH was modified.
4098 assert(RD->isDefinition());
4099 UpdateRecord &Record = DeclUpdates[RD];
4100 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
4101 AddDeclRef(D, Record);
4102}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004103
4104void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4105 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00004106 // The specializations set is kept in the canonical template.
4107 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00004108 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
4109 return; // Not a source specialization added to a template from PCH.
4110
4111 UpdateRecord &Record = DeclUpdates[TD];
4112 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
4113 AddDeclRef(D, Record);
4114}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00004115
Sebastian Redl9ab988f2011-04-14 14:07:59 +00004116void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4117 const FunctionDecl *D) {
4118 // The specializations set is kept in the canonical template.
4119 TD = TD->getCanonicalDecl();
4120 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
4121 return; // Not a source specialization added to a template from PCH.
4122
4123 UpdateRecord &Record = DeclUpdates[TD];
4124 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
4125 AddDeclRef(D, Record);
4126}
4127
Sebastian Redlab238a72011-04-24 16:28:06 +00004128void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
4129 if (D->getPCHLevel() == 0)
4130 return; // Declaration not imported from PCH.
4131
4132 // Implicit decl from a PCH was defined.
4133 // FIXME: Should implicit definition be a separate FunctionDecl?
4134 RewriteDecl(D);
4135}
4136
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004137void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
4138 if (D->getPCHLevel() == 0)
4139 return;
4140
4141 // Since the actual instantiation is delayed, this really means that we need
4142 // to update the instantiation location.
4143 UpdateRecord &Record = DeclUpdates[D];
4144 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4145 AddSourceLocation(
4146 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4147}
4148
Douglas Gregorf88e35b2010-11-30 06:16:57 +00004149ASTSerializationListener::~ASTSerializationListener() { }