blob: 297115c491c9fbdc339f3b2fceb4f1e3828d5db9 [file] [log] [blame]
Sebastian Redld6522cf2010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redl55c0ad52010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregoref84c4b2009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl1914c6f2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Douglas Gregorf88e35b2010-11-30 06:16:57 +000015#include "clang/Serialization/ASTSerializationListener.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000016#include "ASTCommon.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000017#include "clang/Sema/Sema.h"
18#include "clang/Sema/IdentifierResolver.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclContextInternals.h"
John McCall19c1bfd2010-08-25 05:32:35 +000022#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000023#include "clang/AST/DeclFriend.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000024#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000025#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000026#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000027#include "clang/AST/TypeLocVisitor.h"
Sebastian Redlf5b13462010-08-18 23:57:17 +000028#include "clang/Serialization/ASTReader.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000029#include "clang/Lex/MacroInfo.h"
Douglas Gregoraae92242010-03-19 21:51:54 +000030#include "clang/Lex/PreprocessingRecord.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000031#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000032#include "clang/Lex/HeaderSearch.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000033#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000034#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000035#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000036#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000037#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000038#include "clang/Basic/TargetInfo.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000039#include "clang/Basic/Version.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000040#include "clang/Basic/VersionTuple.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000041#include "llvm/ADT/APFloat.h"
42#include "llvm/ADT/APInt.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000043#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000044#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencer740857f2010-12-21 16:45:57 +000045#include "llvm/Support/FileSystem.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000046#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000047#include "llvm/Support/Path.h"
Chris Lattner225dd6c2009-04-11 18:40:46 +000048#include <cstdio>
Douglas Gregor09b69892011-02-10 17:09:37 +000049#include <string.h>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000050using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000051using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000052
Sebastian Redl3df5a082010-07-30 17:03:48 +000053template <typename T, typename Allocator>
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000054static llvm::StringRef data(const std::vector<T, Allocator> &v) {
55 if (v.empty()) return llvm::StringRef();
56 return llvm::StringRef(reinterpret_cast<const char*>(&v[0]),
57 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000058}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000059
60template <typename T>
61static llvm::StringRef data(const llvm::SmallVectorImpl<T> &v) {
62 return llvm::StringRef(reinterpret_cast<const char*>(v.data()),
63 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000064}
65
Douglas Gregoref84c4b2009-04-09 22:27:44 +000066//===----------------------------------------------------------------------===//
67// Type serialization
68//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000069
Douglas Gregoref84c4b2009-04-09 22:27:44 +000070namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000071 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000072 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000073 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000074
75 public:
76 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000077 TypeCode Code;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000078
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000079 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000080 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000081
82 void VisitArrayType(const ArrayType *T);
83 void VisitFunctionType(const FunctionType *T);
84 void VisitTagType(const TagType *T);
85
86#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
87#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000088#include "clang/AST/TypeNodes.def"
89 };
90}
91
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000092void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000093 assert(false && "Built-in types are never serialized");
94}
95
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000096void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000097 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000098 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000099}
100
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000101void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000102 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000103 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000104}
105
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000106void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000107 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000108 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000109}
110
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000111void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000112 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
113 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000114 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000115}
116
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000117void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000118 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000119 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000120}
121
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000122void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000123 Writer.AddTypeRef(T->getPointeeType(), Record);
124 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000125 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000126}
127
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000128void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000129 Writer.AddTypeRef(T->getElementType(), Record);
130 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000131 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000132}
133
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000134void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000135 VisitArrayType(T);
136 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000137 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000138}
139
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000140void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000141 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000142 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000143}
144
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000145void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000146 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000147 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
148 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000149 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000150 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000151}
152
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000153void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000154 Writer.AddTypeRef(T->getElementType(), Record);
155 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000156 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000157 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000158}
159
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000160void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000161 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000162 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000163}
164
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000165void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000166 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000167 FunctionType::ExtInfo C = T->getExtInfo();
168 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000169 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000170 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000171 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000172 Record.push_back(C.getCC());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000173}
174
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000175void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000176 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000177 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000178}
179
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000180void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000181 VisitFunctionType(T);
182 Record.push_back(T->getNumArgs());
183 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
184 Writer.AddTypeRef(T->getArgType(I), Record);
185 Record.push_back(T->isVariadic());
186 Record.push_back(T->getTypeQuals());
Douglas Gregordb9d6642011-01-26 05:01:58 +0000187 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000188 Record.push_back(T->getExceptionSpecType());
189 if (T->getExceptionSpecType() == EST_Dynamic) {
190 Record.push_back(T->getNumExceptions());
191 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
192 Writer.AddTypeRef(T->getExceptionType(I), Record);
193 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
194 Writer.AddStmt(T->getNoexceptExpr());
195 }
Sebastian Redl539c5062010-08-18 23:57:32 +0000196 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000197}
198
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000199void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000200 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000201 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000202}
John McCallb96ec562009-12-04 22:46:56 +0000203
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000204void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000205 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000206 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
207 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000208 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000209}
210
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000211void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000212 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000213 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000214}
215
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000216void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000217 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000218 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000219}
220
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000221void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson81df7b82009-06-24 19:06:50 +0000222 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000223 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000224}
225
Alexis Hunte852b102011-05-24 22:41:36 +0000226void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
227 Writer.AddTypeRef(T->getBaseType(), Record);
228 Writer.AddTypeRef(T->getUnderlyingType(), Record);
229 Record.push_back(T->getUTTKind());
230 Code = TYPE_UNARY_TRANSFORM;
231}
232
Richard Smith30482bc2011-02-20 03:19:35 +0000233void ASTTypeWriter::VisitAutoType(const AutoType *T) {
234 Writer.AddTypeRef(T->getDeducedType(), Record);
235 Code = TYPE_AUTO;
236}
237
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000238void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000239 Record.push_back(T->isDependentType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000240 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000241 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000242 "Cannot serialize in the middle of a type definition");
243}
244
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000245void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000246 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000247 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000248}
249
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000250void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000251 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000252 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000253}
254
John McCall81904512011-01-06 01:58:22 +0000255void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
256 Writer.AddTypeRef(T->getModifiedType(), Record);
257 Writer.AddTypeRef(T->getEquivalentType(), Record);
258 Record.push_back(T->getAttrKind());
259 Code = TYPE_ATTRIBUTED;
260}
261
Mike Stump11289f42009-09-09 15:08:12 +0000262void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000263ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000264 const SubstTemplateTypeParmType *T) {
265 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
266 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000267 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000268}
269
270void
Douglas Gregorada4b792011-01-14 02:55:32 +0000271ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
272 const SubstTemplateTypeParmPackType *T) {
273 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
274 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
275 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
276}
277
278void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000279ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000280 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000281 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000282 Writer.AddTemplateName(T->getTemplateName(), Record);
283 Record.push_back(T->getNumArgs());
284 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
285 ArgI != ArgE; ++ArgI)
286 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000287 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
288 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000289 : T->getCanonicalTypeInternal(),
290 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000291 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000292}
293
294void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000295ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000296 VisitArrayType(T);
297 Writer.AddStmt(T->getSizeExpr());
298 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000299 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000300}
301
302void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000303ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000304 const DependentSizedExtVectorType *T) {
305 // FIXME: Serialize this type (C++ only)
306 assert(false && "Cannot serialize dependent sized extended vector types");
307}
308
309void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000310ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000311 Record.push_back(T->getDepth());
312 Record.push_back(T->getIndex());
313 Record.push_back(T->isParameterPack());
Chandler Carruth08836322011-05-01 00:51:33 +0000314 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000315 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000316}
317
318void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000319ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000320 Record.push_back(T->getKeyword());
321 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
322 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000323 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
324 : T->getCanonicalTypeInternal(),
325 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000326 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000327}
328
329void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000330ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000331 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000332 Record.push_back(T->getKeyword());
333 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
334 Writer.AddIdentifierRef(T->getIdentifier(), Record);
335 Record.push_back(T->getNumArgs());
336 for (DependentTemplateSpecializationType::iterator
337 I = T->begin(), E = T->end(); I != E; ++I)
338 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000339 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000340}
341
Douglas Gregord2fa7662010-12-20 02:24:11 +0000342void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
343 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000344 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
345 Record.push_back(*NumExpansions + 1);
346 else
347 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000348 Code = TYPE_PACK_EXPANSION;
349}
350
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000351void ASTTypeWriter::VisitParenType(const ParenType *T) {
352 Writer.AddTypeRef(T->getInnerType(), Record);
353 Code = TYPE_PAREN;
354}
355
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000356void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000357 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000358 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
359 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000360 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000361}
362
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000363void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCalle78aac42010-03-10 03:28:59 +0000364 Writer.AddDeclRef(T->getDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000365 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000366 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000367}
368
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000369void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1c283312010-08-11 12:19:30 +0000370 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000371 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000372}
373
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000374void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000375 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000376 Record.push_back(T->getNumProtocols());
John McCall8b07ec22010-05-15 11:32:37 +0000377 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000378 E = T->qual_end(); I != E; ++I)
379 Writer.AddDeclRef(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000380 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000381}
382
Steve Narofffb4330f2009-06-17 22:40:22 +0000383void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000384ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000385 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000386 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000387}
388
John McCall8f115c62009-10-16 21:56:05 +0000389namespace {
390
391class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000392 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000393 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000394
395public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000396 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000397 : Writer(Writer), Record(Record) { }
398
John McCall17001972009-10-18 01:05:36 +0000399#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000400#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000401 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000402#include "clang/AST/TypeLocNodes.def"
403
John McCall17001972009-10-18 01:05:36 +0000404 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
405 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000406};
407
408}
409
John McCall17001972009-10-18 01:05:36 +0000410void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
411 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000412}
John McCall17001972009-10-18 01:05:36 +0000413void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000414 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
415 if (TL.needsExtraLocalData()) {
416 Record.push_back(TL.getWrittenTypeSpec());
417 Record.push_back(TL.getWrittenSignSpec());
418 Record.push_back(TL.getWrittenWidthSpec());
419 Record.push_back(TL.hasModeAttr());
420 }
John McCall8f115c62009-10-16 21:56:05 +0000421}
John McCall17001972009-10-18 01:05:36 +0000422void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
423 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000424}
John McCall17001972009-10-18 01:05:36 +0000425void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
426 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000427}
John McCall17001972009-10-18 01:05:36 +0000428void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
429 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000430}
John McCall17001972009-10-18 01:05:36 +0000431void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
432 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000433}
John McCall17001972009-10-18 01:05:36 +0000434void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
435 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000436}
John McCall17001972009-10-18 01:05:36 +0000437void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
438 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnara509357842011-03-05 14:42:21 +0000439 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000440}
John McCall17001972009-10-18 01:05:36 +0000441void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
442 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
443 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
444 Record.push_back(TL.getSizeExpr() ? 1 : 0);
445 if (TL.getSizeExpr())
446 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000447}
John McCall17001972009-10-18 01:05:36 +0000448void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
449 VisitArrayTypeLoc(TL);
450}
451void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
452 VisitArrayTypeLoc(TL);
453}
454void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
455 VisitArrayTypeLoc(TL);
456}
457void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
458 DependentSizedArrayTypeLoc TL) {
459 VisitArrayTypeLoc(TL);
460}
461void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
462 DependentSizedExtVectorTypeLoc TL) {
463 Writer.AddSourceLocation(TL.getNameLoc(), Record);
464}
465void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
466 Writer.AddSourceLocation(TL.getNameLoc(), Record);
467}
468void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
469 Writer.AddSourceLocation(TL.getNameLoc(), Record);
470}
471void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000472 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
473 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Douglas Gregor7fb25412010-10-01 18:44:50 +0000474 Record.push_back(TL.getTrailingReturn());
John McCall17001972009-10-18 01:05:36 +0000475 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
476 Writer.AddDeclRef(TL.getArg(i), Record);
477}
478void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
479 VisitFunctionTypeLoc(TL);
480}
481void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
482 VisitFunctionTypeLoc(TL);
483}
John McCallb96ec562009-12-04 22:46:56 +0000484void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
485 Writer.AddSourceLocation(TL.getNameLoc(), Record);
486}
John McCall17001972009-10-18 01:05:36 +0000487void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
488 Writer.AddSourceLocation(TL.getNameLoc(), Record);
489}
490void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000491 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
492 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
493 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000494}
495void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000496 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
497 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
498 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
499 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000500}
501void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
502 Writer.AddSourceLocation(TL.getNameLoc(), Record);
503}
Alexis Hunte852b102011-05-24 22:41:36 +0000504void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
505 Writer.AddSourceLocation(TL.getKWLoc(), Record);
506 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
507 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
508 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
509}
Richard Smith30482bc2011-02-20 03:19:35 +0000510void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
511 Writer.AddSourceLocation(TL.getNameLoc(), Record);
512}
John McCall17001972009-10-18 01:05:36 +0000513void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
514 Writer.AddSourceLocation(TL.getNameLoc(), Record);
515}
516void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
517 Writer.AddSourceLocation(TL.getNameLoc(), Record);
518}
John McCall81904512011-01-06 01:58:22 +0000519void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
520 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
521 if (TL.hasAttrOperand()) {
522 SourceRange range = TL.getAttrOperandParensRange();
523 Writer.AddSourceLocation(range.getBegin(), Record);
524 Writer.AddSourceLocation(range.getEnd(), Record);
525 }
526 if (TL.hasAttrExprOperand()) {
527 Expr *operand = TL.getAttrExprOperand();
528 Record.push_back(operand ? 1 : 0);
529 if (operand) Writer.AddStmt(operand);
530 } else if (TL.hasAttrEnumOperand()) {
531 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
532 }
533}
John McCall17001972009-10-18 01:05:36 +0000534void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
535 Writer.AddSourceLocation(TL.getNameLoc(), Record);
536}
John McCallcebee162009-10-18 09:09:24 +0000537void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
538 SubstTemplateTypeParmTypeLoc TL) {
539 Writer.AddSourceLocation(TL.getNameLoc(), Record);
540}
Douglas Gregorada4b792011-01-14 02:55:32 +0000541void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
542 SubstTemplateTypeParmPackTypeLoc TL) {
543 Writer.AddSourceLocation(TL.getNameLoc(), Record);
544}
John McCall17001972009-10-18 01:05:36 +0000545void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
546 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +0000547 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
548 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
549 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
550 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000551 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
552 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000553}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000554void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
555 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
556 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
557}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000558void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000559 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000560 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000561}
John McCalle78aac42010-03-10 03:28:59 +0000562void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
563 Writer.AddSourceLocation(TL.getNameLoc(), Record);
564}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000565void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000566 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000567 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000568 Writer.AddSourceLocation(TL.getNameLoc(), Record);
569}
John McCallc392f372010-06-11 00:33:02 +0000570void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
571 DependentTemplateSpecializationTypeLoc TL) {
572 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000573 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000574 Writer.AddSourceLocation(TL.getNameLoc(), Record);
575 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
576 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
577 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000578 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
579 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000580}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000581void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
582 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
583}
John McCall17001972009-10-18 01:05:36 +0000584void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
585 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000586}
587void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
588 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000589 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
590 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
591 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
592 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000593}
John McCallfc93cf92009-10-22 22:37:11 +0000594void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
595 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000596}
John McCall8f115c62009-10-16 21:56:05 +0000597
Chris Lattner19cea4e2009-04-22 05:57:30 +0000598//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000599// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000600//===----------------------------------------------------------------------===//
601
Chris Lattner28fa4e62009-04-26 22:26:21 +0000602static void EmitBlockID(unsigned ID, const char *Name,
603 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000604 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000605 Record.clear();
606 Record.push_back(ID);
607 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
608
609 // Emit the block name if present.
610 if (Name == 0 || Name[0] == 0) return;
611 Record.clear();
612 while (*Name)
613 Record.push_back(*Name++);
614 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
615}
616
617static void EmitRecordID(unsigned ID, const char *Name,
618 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000619 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000620 Record.clear();
621 Record.push_back(ID);
622 while (*Name)
623 Record.push_back(*Name++);
624 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000625}
626
627static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000628 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000629#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000630 RECORD(STMT_STOP);
631 RECORD(STMT_NULL_PTR);
632 RECORD(STMT_NULL);
633 RECORD(STMT_COMPOUND);
634 RECORD(STMT_CASE);
635 RECORD(STMT_DEFAULT);
636 RECORD(STMT_LABEL);
637 RECORD(STMT_IF);
638 RECORD(STMT_SWITCH);
639 RECORD(STMT_WHILE);
640 RECORD(STMT_DO);
641 RECORD(STMT_FOR);
642 RECORD(STMT_GOTO);
643 RECORD(STMT_INDIRECT_GOTO);
644 RECORD(STMT_CONTINUE);
645 RECORD(STMT_BREAK);
646 RECORD(STMT_RETURN);
647 RECORD(STMT_DECL);
648 RECORD(STMT_ASM);
649 RECORD(EXPR_PREDEFINED);
650 RECORD(EXPR_DECL_REF);
651 RECORD(EXPR_INTEGER_LITERAL);
652 RECORD(EXPR_FLOATING_LITERAL);
653 RECORD(EXPR_IMAGINARY_LITERAL);
654 RECORD(EXPR_STRING_LITERAL);
655 RECORD(EXPR_CHARACTER_LITERAL);
656 RECORD(EXPR_PAREN);
657 RECORD(EXPR_UNARY_OPERATOR);
658 RECORD(EXPR_SIZEOF_ALIGN_OF);
659 RECORD(EXPR_ARRAY_SUBSCRIPT);
660 RECORD(EXPR_CALL);
661 RECORD(EXPR_MEMBER);
662 RECORD(EXPR_BINARY_OPERATOR);
663 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
664 RECORD(EXPR_CONDITIONAL_OPERATOR);
665 RECORD(EXPR_IMPLICIT_CAST);
666 RECORD(EXPR_CSTYLE_CAST);
667 RECORD(EXPR_COMPOUND_LITERAL);
668 RECORD(EXPR_EXT_VECTOR_ELEMENT);
669 RECORD(EXPR_INIT_LIST);
670 RECORD(EXPR_DESIGNATED_INIT);
671 RECORD(EXPR_IMPLICIT_VALUE_INIT);
672 RECORD(EXPR_VA_ARG);
673 RECORD(EXPR_ADDR_LABEL);
674 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000675 RECORD(EXPR_CHOOSE);
676 RECORD(EXPR_GNU_NULL);
677 RECORD(EXPR_SHUFFLE_VECTOR);
678 RECORD(EXPR_BLOCK);
679 RECORD(EXPR_BLOCK_DECL_REF);
Peter Collingbourne91147592011-04-15 00:35:48 +0000680 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000681 RECORD(EXPR_OBJC_STRING_LITERAL);
682 RECORD(EXPR_OBJC_ENCODE);
683 RECORD(EXPR_OBJC_SELECTOR_EXPR);
684 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
685 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
686 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
687 RECORD(EXPR_OBJC_KVC_REF_EXPR);
688 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000689 RECORD(STMT_OBJC_FOR_COLLECTION);
690 RECORD(STMT_OBJC_CATCH);
691 RECORD(STMT_OBJC_FINALLY);
692 RECORD(STMT_OBJC_AT_TRY);
693 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
694 RECORD(STMT_OBJC_AT_THROW);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000695 RECORD(EXPR_CXX_OPERATOR_CALL);
696 RECORD(EXPR_CXX_CONSTRUCT);
697 RECORD(EXPR_CXX_STATIC_CAST);
698 RECORD(EXPR_CXX_DYNAMIC_CAST);
699 RECORD(EXPR_CXX_REINTERPRET_CAST);
700 RECORD(EXPR_CXX_CONST_CAST);
701 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
702 RECORD(EXPR_CXX_BOOL_LITERAL);
703 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000704 RECORD(EXPR_CXX_TYPEID_EXPR);
705 RECORD(EXPR_CXX_TYPEID_TYPE);
706 RECORD(EXPR_CXX_UUIDOF_EXPR);
707 RECORD(EXPR_CXX_UUIDOF_TYPE);
708 RECORD(EXPR_CXX_THIS);
709 RECORD(EXPR_CXX_THROW);
710 RECORD(EXPR_CXX_DEFAULT_ARG);
711 RECORD(EXPR_CXX_BIND_TEMPORARY);
712 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
713 RECORD(EXPR_CXX_NEW);
714 RECORD(EXPR_CXX_DELETE);
715 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
716 RECORD(EXPR_EXPR_WITH_CLEANUPS);
717 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
718 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
719 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
720 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
721 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
722 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
723 RECORD(EXPR_CXX_NOEXCEPT);
724 RECORD(EXPR_OPAQUE_VALUE);
725 RECORD(EXPR_BINARY_TYPE_TRAIT);
726 RECORD(EXPR_PACK_EXPANSION);
727 RECORD(EXPR_SIZEOF_PACK);
728 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000729 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000730#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000731}
Mike Stump11289f42009-09-09 15:08:12 +0000732
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000733void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000734 RecordData Record;
735 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000736
Sebastian Redl539c5062010-08-18 23:57:32 +0000737#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
738#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000739
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000740 // AST Top-Level Block.
Sebastian Redlf1642042010-08-18 23:57:22 +0000741 BLOCK(AST_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000742 RECORD(ORIGINAL_FILE_NAME);
Douglas Gregora3b20262011-05-06 21:43:30 +0000743 RECORD(ORIGINAL_FILE_ID);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000744 RECORD(TYPE_OFFSET);
745 RECORD(DECL_OFFSET);
746 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000747 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000748 RECORD(IDENTIFIER_OFFSET);
749 RECORD(IDENTIFIER_TABLE);
750 RECORD(EXTERNAL_DEFINITIONS);
751 RECORD(SPECIAL_TYPES);
752 RECORD(STATISTICS);
753 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000754 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000755 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
756 RECORD(SELECTOR_OFFSETS);
757 RECORD(METHOD_POOL);
758 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000759 RECORD(SOURCE_LOCATION_OFFSETS);
760 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000761 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000762 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek17437132010-01-22 20:59:36 +0000763 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregoraae92242010-03-19 21:51:54 +0000764 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redl595c5132010-07-08 22:01:51 +0000765 RECORD(CHAINED_METADATA);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000766 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000767 RECORD(TU_UPDATE_LEXICAL);
768 RECORD(REDECLS_UPDATE_LATEST);
769 RECORD(SEMA_DECL_REFS);
770 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
771 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
772 RECORD(DECL_REPLACEMENTS);
773 RECORD(UPDATE_VISIBLE);
774 RECORD(DECL_UPDATE_OFFSETS);
775 RECORD(DECL_UPDATES);
776 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
777 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000778 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000779 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000780 RECORD(FP_PRAGMA_OPTIONS);
781 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +0000782 RECORD(DELEGATING_CTORS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000783
Chris Lattner28fa4e62009-04-26 22:26:21 +0000784 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000785 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000786 RECORD(SM_SLOC_FILE_ENTRY);
787 RECORD(SM_SLOC_BUFFER_ENTRY);
788 RECORD(SM_SLOC_BUFFER_BLOB);
789 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
790 RECORD(SM_LINE_TABLE);
Mike Stump11289f42009-09-09 15:08:12 +0000791
Chris Lattner28fa4e62009-04-26 22:26:21 +0000792 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000793 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000794 RECORD(PP_MACRO_OBJECT_LIKE);
795 RECORD(PP_MACRO_FUNCTION_LIKE);
796 RECORD(PP_TOKEN);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000797
Douglas Gregor12bfa382009-10-17 00:13:19 +0000798 // Decls and Types block.
799 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000800 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000801 RECORD(TYPE_COMPLEX);
802 RECORD(TYPE_POINTER);
803 RECORD(TYPE_BLOCK_POINTER);
804 RECORD(TYPE_LVALUE_REFERENCE);
805 RECORD(TYPE_RVALUE_REFERENCE);
806 RECORD(TYPE_MEMBER_POINTER);
807 RECORD(TYPE_CONSTANT_ARRAY);
808 RECORD(TYPE_INCOMPLETE_ARRAY);
809 RECORD(TYPE_VARIABLE_ARRAY);
810 RECORD(TYPE_VECTOR);
811 RECORD(TYPE_EXT_VECTOR);
812 RECORD(TYPE_FUNCTION_PROTO);
813 RECORD(TYPE_FUNCTION_NO_PROTO);
814 RECORD(TYPE_TYPEDEF);
815 RECORD(TYPE_TYPEOF_EXPR);
816 RECORD(TYPE_TYPEOF);
817 RECORD(TYPE_RECORD);
818 RECORD(TYPE_ENUM);
819 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000820 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000821 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000822 RECORD(TYPE_DECLTYPE);
823 RECORD(TYPE_ELABORATED);
824 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
825 RECORD(TYPE_UNRESOLVED_USING);
826 RECORD(TYPE_INJECTED_CLASS_NAME);
827 RECORD(TYPE_OBJC_OBJECT);
828 RECORD(TYPE_TEMPLATE_TYPE_PARM);
829 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
830 RECORD(TYPE_DEPENDENT_NAME);
831 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
832 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
833 RECORD(TYPE_PAREN);
834 RECORD(TYPE_PACK_EXPANSION);
835 RECORD(TYPE_ATTRIBUTED);
836 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000837 RECORD(DECL_TRANSLATION_UNIT);
838 RECORD(DECL_TYPEDEF);
839 RECORD(DECL_ENUM);
840 RECORD(DECL_RECORD);
841 RECORD(DECL_ENUM_CONSTANT);
842 RECORD(DECL_FUNCTION);
843 RECORD(DECL_OBJC_METHOD);
844 RECORD(DECL_OBJC_INTERFACE);
845 RECORD(DECL_OBJC_PROTOCOL);
846 RECORD(DECL_OBJC_IVAR);
847 RECORD(DECL_OBJC_AT_DEFS_FIELD);
848 RECORD(DECL_OBJC_CLASS);
849 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
850 RECORD(DECL_OBJC_CATEGORY);
851 RECORD(DECL_OBJC_CATEGORY_IMPL);
852 RECORD(DECL_OBJC_IMPLEMENTATION);
853 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
854 RECORD(DECL_OBJC_PROPERTY);
855 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000856 RECORD(DECL_FIELD);
857 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000858 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000859 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000860 RECORD(DECL_FILE_SCOPE_ASM);
861 RECORD(DECL_BLOCK);
862 RECORD(DECL_CONTEXT_LEXICAL);
863 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000864 RECORD(DECL_NAMESPACE);
865 RECORD(DECL_NAMESPACE_ALIAS);
866 RECORD(DECL_USING);
867 RECORD(DECL_USING_SHADOW);
868 RECORD(DECL_USING_DIRECTIVE);
869 RECORD(DECL_UNRESOLVED_USING_VALUE);
870 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
871 RECORD(DECL_LINKAGE_SPEC);
872 RECORD(DECL_CXX_RECORD);
873 RECORD(DECL_CXX_METHOD);
874 RECORD(DECL_CXX_CONSTRUCTOR);
875 RECORD(DECL_CXX_DESTRUCTOR);
876 RECORD(DECL_CXX_CONVERSION);
877 RECORD(DECL_ACCESS_SPEC);
878 RECORD(DECL_FRIEND);
879 RECORD(DECL_FRIEND_TEMPLATE);
880 RECORD(DECL_CLASS_TEMPLATE);
881 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
882 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
883 RECORD(DECL_FUNCTION_TEMPLATE);
884 RECORD(DECL_TEMPLATE_TYPE_PARM);
885 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
886 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
887 RECORD(DECL_STATIC_ASSERT);
888 RECORD(DECL_CXX_BASE_SPECIFIERS);
889 RECORD(DECL_INDIRECTFIELD);
890 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
891
Douglas Gregor92a96f52011-02-08 21:58:10 +0000892 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
893 RECORD(PPD_MACRO_INSTANTIATION);
894 RECORD(PPD_MACRO_DEFINITION);
895 RECORD(PPD_INCLUSION_DIRECTIVE);
896
Douglas Gregor12bfa382009-10-17 00:13:19 +0000897 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000898 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000899#undef RECORD
900#undef BLOCK
901 Stream.ExitBlock();
902}
903
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000904/// \brief Adjusts the given filename to only write out the portion of the
905/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000906///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000907/// \param Filename the file name to adjust.
908///
909/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
910/// the returned filename will be adjusted by this system root.
911///
912/// \returns either the original filename (if it needs no adjustment) or the
913/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000914static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000915adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
916 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000917
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000918 if (!isysroot)
919 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000920
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000921 // Verify that the filename and the system root have the same prefix.
922 unsigned Pos = 0;
923 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
924 if (Filename[Pos] != isysroot[Pos])
925 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000926
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000927 // We hit the end of the filename before we hit the end of the system root.
928 if (!Filename[Pos])
929 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000930
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000931 // If the file name has a '/' at the current position, skip over the '/'.
932 // We distinguish sysroot-based includes from absolute includes by the
933 // absence of '/' at the beginning of sysroot-based includes.
934 if (Filename[Pos] == '/')
935 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000936
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000937 return Filename + Pos;
938}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000939
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000940/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000941void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot,
942 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000943 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000944
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000945 // Metadata
946 const TargetInfo &Target = Context.Target;
947 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000948 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl539c5062010-08-18 23:57:32 +0000949 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000950 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
951 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000952 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
953 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
954 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000955 // Target triple or chained PCH name
956 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000957 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000958
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000959 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000960 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
961 Record.push_back(VERSION_MAJOR);
962 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000963 Record.push_back(CLANG_VERSION_MAJOR);
964 Record.push_back(CLANG_VERSION_MINOR);
965 Record.push_back(isysroot != 0);
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000966 // FIXME: This writes the absolute path for chained headers.
967 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
968 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump11289f42009-09-09 15:08:12 +0000969
Douglas Gregora3b20262011-05-06 21:43:30 +0000970 // Original file name and file ID
Douglas Gregor45fe0362009-05-12 01:31:05 +0000971 SourceManager &SM = Context.getSourceManager();
972 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
973 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000974 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregor45fe0362009-05-12 01:31:05 +0000975 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
976 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
977
Michael J. Spencer740857f2010-12-21 16:45:57 +0000978 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +0000979
Michael J. Spencer740857f2010-12-21 16:45:57 +0000980 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000981
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +0000982 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000983 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000984 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000985 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000986 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000987 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregora3b20262011-05-06 21:43:30 +0000988
989 Record.clear();
990 Record.push_back(SM.getMainFileID().getOpaqueValue());
991 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000992 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000993
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000994 // Original PCH directory
995 if (!OutputFile.empty() && OutputFile != "-") {
996 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
997 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
998 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
999 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1000
1001 llvm::SmallString<128> OutputPath(OutputFile);
1002
1003 llvm::sys::fs::make_absolute(OutputPath);
1004 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1005
1006 RecordData Record;
1007 Record.push_back(ORIGINAL_PCH_DIR);
1008 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1009 }
1010
Ted Kremenek18e066f2010-01-22 22:12:47 +00001011 // Repository branch/version information.
1012 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001013 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenek18e066f2010-01-22 22:12:47 +00001014 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1015 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +00001016 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001017 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +00001018 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
1019 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +00001020}
1021
1022/// \brief Write the LangOptions structure.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001023void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001024 RecordData Record;
1025 Record.push_back(LangOpts.Trigraphs);
1026 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1027 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1028 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1029 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carruthe03aa552010-04-17 20:17:31 +00001030 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor55abb232009-04-10 20:39:37 +00001031 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1032 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1033 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1034 Record.push_back(LangOpts.C99); // C99 Support
Peter Collingbournea686b5f2011-04-15 00:35:23 +00001035 Record.push_back(LangOpts.C1X); // C1X Support
Douglas Gregor55abb232009-04-10 20:39:37 +00001036 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +00001037 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
1038 // already saved elsewhere.
Douglas Gregor55abb232009-04-10 20:39:37 +00001039 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1040 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +00001041 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +00001042
Douglas Gregor55abb232009-04-10 20:39:37 +00001043 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1044 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001045 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian45878032010-02-09 19:31:38 +00001046 // modern abi enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001047 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian45878032010-02-09 19:31:38 +00001048 // modern abi enabled.
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +00001049 Record.push_back(LangOpts.AppleKext); // Apple's kernel extensions ABI
Ted Kremenek1d56c9e2010-12-23 21:35:43 +00001050 Record.push_back(LangOpts.ObjCDefaultSynthProperties); // Objective-C auto-synthesized
1051 // properties enabled.
Fariborz Jahanian62c56022010-04-22 21:01:59 +00001052 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump11289f42009-09-09 15:08:12 +00001053
Douglas Gregor55abb232009-04-10 20:39:37 +00001054 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +00001055 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1056 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00001057 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +00001058 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Anders Carlssonce8dd3a2011-02-19 23:53:54 +00001059 Record.push_back(LangOpts.ObjCExceptions);
Anders Carlsson6bbd2682011-02-23 03:04:54 +00001060 Record.push_back(LangOpts.CXXExceptions);
1061 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor55abb232009-04-10 20:39:37 +00001062
Douglas Gregordbe39272011-02-01 15:15:22 +00001063 Record.push_back(LangOpts.MSBitfields); // MS-compatible structure layout
Douglas Gregor55abb232009-04-10 20:39:37 +00001064 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1065 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1066 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1067
Chris Lattner258172e2009-04-27 07:35:58 +00001068 // Whether static initializers are protected by locks.
1069 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00001070 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +00001071 Record.push_back(LangOpts.Blocks); // block extension to C
1072 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1073 // they are unused.
1074 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1075 // (modulo the platform support).
1076
Chris Lattner51924e512010-06-26 21:25:03 +00001077 Record.push_back(LangOpts.getSignedOverflowBehavior());
1078 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor55abb232009-04-10 20:39:37 +00001079
1080 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +00001081 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +00001082 // defined.
1083 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1084 // opposed to __DYNAMIC__).
1085 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1086
1087 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1088 // used (instead of C99 semantics).
1089 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Chandler Carruth7ffce732011-04-23 20:05:38 +00001090 Record.push_back(LangOpts.Deprecated); // Should __DEPRECATED be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +00001091 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
1092 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +00001093 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
1094 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +00001095 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Argyrios Kyrtzidisa88942a2011-01-15 02:56:16 +00001096 Record.push_back(LangOpts.ShortEnums); // Should the enum type be equivalent
1097 // to the smallest integer type with
1098 // enough room.
Douglas Gregor55abb232009-04-10 20:39:37 +00001099 Record.push_back(LangOpts.getGCMode());
1100 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +00001101 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +00001102 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00001103 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +00001104 Record.push_back(LangOpts.CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +00001105 Record.push_back(LangOpts.CatchUndefined);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001106 Record.push_back(LangOpts.DefaultFPContract);
Anders Carlsson9cedbef2009-08-22 22:30:33 +00001107 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +00001108 Record.push_back(LangOpts.SpellChecking);
Roman Divacky65b88cd2011-03-01 17:40:53 +00001109 Record.push_back(LangOpts.MRTD);
Sebastian Redl539c5062010-08-18 23:57:32 +00001110 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +00001111}
1112
Douglas Gregora7f71a92009-04-10 03:52:48 +00001113//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00001114// stat cache Serialization
1115//===----------------------------------------------------------------------===//
1116
1117namespace {
1118// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001119class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001120public:
1121 typedef const char * key_type;
1122 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001123
Chris Lattner2a6fa472010-11-23 19:28:12 +00001124 typedef struct stat data_type;
1125 typedef const data_type &data_type_ref;
Douglas Gregorc5046832009-04-27 18:38:38 +00001126
1127 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001128 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001129 }
Mike Stump11289f42009-09-09 15:08:12 +00001130
1131 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +00001132 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1133 data_type_ref Data) {
1134 unsigned StrLen = strlen(path);
1135 clang::io::Emit16(Out, StrLen);
Chris Lattner2a6fa472010-11-23 19:28:12 +00001136 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregorc5046832009-04-27 18:38:38 +00001137 clang::io::Emit8(Out, DataLen);
1138 return std::make_pair(StrLen + 1, DataLen);
1139 }
Mike Stump11289f42009-09-09 15:08:12 +00001140
Douglas Gregorc5046832009-04-27 18:38:38 +00001141 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1142 Out.write(path, KeyLen);
1143 }
Mike Stump11289f42009-09-09 15:08:12 +00001144
Chris Lattner2a6fa472010-11-23 19:28:12 +00001145 void EmitData(llvm::raw_ostream &Out, key_type_ref,
Douglas Gregorc5046832009-04-27 18:38:38 +00001146 data_type_ref Data, unsigned DataLen) {
1147 using namespace clang::io;
1148 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +00001149
Chris Lattner2a6fa472010-11-23 19:28:12 +00001150 Emit32(Out, (uint32_t) Data.st_ino);
1151 Emit32(Out, (uint32_t) Data.st_dev);
1152 Emit16(Out, (uint16_t) Data.st_mode);
1153 Emit64(Out, (uint64_t) Data.st_mtime);
1154 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregorc5046832009-04-27 18:38:38 +00001155
1156 assert(Out.tell() - Start == DataLen && "Wrong data length");
1157 }
1158};
1159} // end anonymous namespace
1160
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001161/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001162void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001163 // Build the on-disk hash table containing information about every
1164 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001165 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +00001166 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001167 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +00001168 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001169 Stat != StatEnd; ++Stat, ++NumStatEntries) {
1170 const char *Filename = Stat->first();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001171 Generator.insert(Filename, Stat->second);
1172 }
Mike Stump11289f42009-09-09 15:08:12 +00001173
Douglas Gregorc5046832009-04-27 18:38:38 +00001174 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001175 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +00001176 uint32_t BucketOffset;
1177 {
1178 llvm::raw_svector_ostream Out(StatCacheData);
1179 // Make sure that no bucket is at offset 0
1180 clang::io::Emit32(Out, 0);
1181 BucketOffset = Generator.Emit(Out);
1182 }
1183
1184 // Create a blob abbreviation
1185 using namespace llvm;
1186 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001187 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +00001188 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1189 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1190 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1191 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1192
1193 // Write the stat cache
1194 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001195 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +00001196 Record.push_back(BucketOffset);
1197 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001198 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +00001199}
1200
1201//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +00001202// Source Manager Serialization
1203//===----------------------------------------------------------------------===//
1204
1205/// \brief Create an abbreviation for the SLocEntry that refers to a
1206/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001207static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001208 using namespace llvm;
1209 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001210 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001211 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1212 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1213 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1214 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001215 // FileEntry fields.
1216 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1217 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora7f71a92009-04-10 03:52:48 +00001218 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001219 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001220}
1221
1222/// \brief Create an abbreviation for the SLocEntry that refers to a
1223/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001224static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001225 using namespace llvm;
1226 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001227 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001228 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1229 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1230 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1231 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1232 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001233 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001234}
1235
1236/// \brief Create an abbreviation for the SLocEntry that refers to a
1237/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001238static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001239 using namespace llvm;
1240 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001241 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001242 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001243 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001244}
1245
1246/// \brief Create an abbreviation for the SLocEntry that refers to an
1247/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001248static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001249 using namespace llvm;
1250 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001251 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001252 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1253 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1254 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1255 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001256 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001257 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001258}
1259
Douglas Gregor09b69892011-02-10 17:09:37 +00001260namespace {
1261 // Trait used for the on-disk hash table of header search information.
1262 class HeaderFileInfoTrait {
1263 ASTWriter &Writer;
1264 HeaderSearch &HS;
1265
1266 public:
1267 HeaderFileInfoTrait(ASTWriter &Writer, HeaderSearch &HS)
1268 : Writer(Writer), HS(HS) { }
1269
1270 typedef const char *key_type;
1271 typedef key_type key_type_ref;
1272
1273 typedef HeaderFileInfo data_type;
1274 typedef const data_type &data_type_ref;
1275
1276 static unsigned ComputeHash(const char *path) {
1277 // The hash is based only on the filename portion of the key, so that the
1278 // reader can match based on filenames when symlinking or excess path
1279 // elements ("foo/../", "../") change the form of the name. However,
1280 // complete path is still the key.
1281 return llvm::HashString(llvm::sys::path::filename(path));
1282 }
1283
1284 std::pair<unsigned,unsigned>
1285 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1286 data_type_ref Data) {
1287 unsigned StrLen = strlen(path);
1288 clang::io::Emit16(Out, StrLen);
1289 unsigned DataLen = 1 + 2 + 4;
1290 clang::io::Emit8(Out, DataLen);
1291 return std::make_pair(StrLen + 1, DataLen);
1292 }
1293
1294 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1295 Out.write(path, KeyLen);
1296 }
1297
1298 void EmitData(llvm::raw_ostream &Out, key_type_ref,
1299 data_type_ref Data, unsigned DataLen) {
1300 using namespace clang::io;
1301 uint64_t Start = Out.tell(); (void)Start;
1302
Douglas Gregor37aa4932011-05-04 00:14:37 +00001303 unsigned char Flags = (Data.isImport << 4)
1304 | (Data.isPragmaOnce << 3)
Douglas Gregor09b69892011-02-10 17:09:37 +00001305 | (Data.DirInfo << 1)
1306 | Data.Resolved;
1307 Emit8(Out, (uint8_t)Flags);
1308 Emit16(Out, (uint16_t) Data.NumIncludes);
1309
1310 if (!Data.ControllingMacro)
1311 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1312 else
1313 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
1314 assert(Out.tell() - Start == DataLen && "Wrong data length");
1315 }
1316 };
1317} // end anonymous namespace
1318
1319/// \brief Write the header search block for the list of files that
1320///
1321/// \param HS The header search structure to save.
1322///
1323/// \param Chain Whether we're creating a chained AST file.
1324void ASTWriter::WriteHeaderSearch(HeaderSearch &HS, const char* isysroot) {
1325 llvm::SmallVector<const FileEntry *, 16> FilesByUID;
1326 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1327
1328 if (FilesByUID.size() > HS.header_file_size())
1329 FilesByUID.resize(HS.header_file_size());
1330
1331 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1332 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
1333 llvm::SmallVector<const char *, 4> SavedStrings;
1334 unsigned NumHeaderSearchEntries = 0;
1335 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1336 const FileEntry *File = FilesByUID[UID];
1337 if (!File)
1338 continue;
1339
1340 const HeaderFileInfo &HFI = HS.header_file_begin()[UID];
1341 if (HFI.External && Chain)
1342 continue;
1343
1344 // Turn the file name into an absolute path, if it isn't already.
1345 const char *Filename = File->getName();
1346 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1347
1348 // If we performed any translation on the file name at all, we need to
1349 // save this string, since the generator will refer to it later.
1350 if (Filename != File->getName()) {
1351 Filename = strdup(Filename);
1352 SavedStrings.push_back(Filename);
1353 }
1354
1355 Generator.insert(Filename, HFI, GeneratorTrait);
1356 ++NumHeaderSearchEntries;
1357 }
1358
1359 // Create the on-disk hash table in a buffer.
1360 llvm::SmallString<4096> TableData;
1361 uint32_t BucketOffset;
1362 {
1363 llvm::raw_svector_ostream Out(TableData);
1364 // Make sure that no bucket is at offset 0
1365 clang::io::Emit32(Out, 0);
1366 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1367 }
1368
1369 // Create a blob abbreviation
1370 using namespace llvm;
1371 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1372 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1373 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1374 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1375 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1376 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1377
1378 // Write the stat cache
1379 RecordData Record;
1380 Record.push_back(HEADER_SEARCH_TABLE);
1381 Record.push_back(BucketOffset);
1382 Record.push_back(NumHeaderSearchEntries);
1383 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1384
1385 // Free all of the strings we had to duplicate.
1386 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1387 free((void*)SavedStrings[I]);
1388}
1389
Douglas Gregora7f71a92009-04-10 03:52:48 +00001390/// \brief Writes the block containing the serialized form of the
1391/// source manager.
1392///
1393/// TODO: We should probably use an on-disk hash table (stored in a
1394/// blob), indexed based on the file name, so that we only create
1395/// entries for files that we actually need. In the common case (no
1396/// errors), we probably won't have to create file entries for any of
1397/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001398void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001399 const Preprocessor &PP,
1400 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001401 RecordData Record;
1402
Chris Lattner0910e3b2009-04-10 17:16:57 +00001403 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001404 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001405
1406 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001407 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1408 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1409 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1410 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001411
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001412 // Write the line table.
1413 if (SourceMgr.hasLineTable()) {
1414 LineTableInfo &LineTable = SourceMgr.getLineTable();
1415
1416 // Emit the file names
1417 Record.push_back(LineTable.getNumFilenames());
1418 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1419 // Emit the file name
1420 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001421 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001422 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1423 Record.push_back(FilenameLen);
1424 if (FilenameLen)
1425 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1426 }
Mike Stump11289f42009-09-09 15:08:12 +00001427
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001428 // Emit the line entries
1429 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1430 L != LEnd; ++L) {
1431 // Emit the file ID
1432 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +00001433
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001434 // Emit the line entries
1435 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +00001436 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001437 LEEnd = L->second.end();
1438 LE != LEEnd; ++LE) {
1439 Record.push_back(LE->FileOffset);
1440 Record.push_back(LE->LineNo);
1441 Record.push_back(LE->FilenameID);
1442 Record.push_back((unsigned)LE->FileKind);
1443 Record.push_back(LE->IncludeOffset);
1444 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001445 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001446 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001447 }
1448
Douglas Gregor258ae542009-04-27 06:38:32 +00001449 // Write out the source location entry table. We skip the first
1450 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001451 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001452 RecordData PreloadSLocs;
Sebastian Redl5c415f32010-07-22 17:01:13 +00001453 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1454 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1455 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1456 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001457 // Get this source location entry.
1458 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001459
Douglas Gregor258ae542009-04-27 06:38:32 +00001460 // Record the offset of this source-location entry.
1461 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1462
1463 // Figure out which record code to use.
1464 unsigned Code;
1465 if (SLoc->isFile()) {
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001466 if (SLoc->getFile().getContentCache()->OrigEntry)
Sebastian Redl539c5062010-08-18 23:57:32 +00001467 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001468 else
Sebastian Redl539c5062010-08-18 23:57:32 +00001469 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001470 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001471 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001472 Record.clear();
1473 Record.push_back(Code);
1474
1475 Record.push_back(SLoc->getOffset());
1476 if (SLoc->isFile()) {
1477 const SrcMgr::FileInfo &File = SLoc->getFile();
1478 Record.push_back(File.getIncludeLoc().getRawEncoding());
1479 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1480 Record.push_back(File.hasLineDirectives());
1481
1482 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001483 if (Content->OrigEntry) {
1484 assert(Content->OrigEntry == Content->ContentsEntry &&
1485 "Writing to AST an overriden file is not supported");
1486
Douglas Gregor258ae542009-04-27 06:38:32 +00001487 // The source location entry is a file. The blob associated
1488 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001489
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001490 // Emit size/modification time for this file.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001491 Record.push_back(Content->OrigEntry->getSize());
1492 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001493
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001494 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001495 const char *Filename = Content->OrigEntry->getName();
Michael J. Spencer740857f2010-12-21 16:45:57 +00001496 llvm::SmallString<128> FilePath(Filename);
Anders Carlssona4267052011-03-08 16:04:35 +00001497
1498 // Ask the file manager to fixup the relative path for us. This will
1499 // honor the working directory.
1500 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1501
1502 // FIXME: This call to make_absolute shouldn't be necessary, the
1503 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencer740857f2010-12-21 16:45:57 +00001504 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001505 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001506
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001507 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001508 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001509 } else {
1510 // The source location entry is a buffer. The blob associated
1511 // with this entry contains the contents of the buffer.
1512
1513 // We add one to the size so that we capture the trailing NULL
1514 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1515 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001516 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001517 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001518 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001519 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1520 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001521 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001522 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001523 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001524 llvm::StringRef(Buffer->getBufferStart(),
1525 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001526
1527 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl5c415f32010-07-22 17:01:13 +00001528 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001529 }
1530 } else {
1531 // The source location entry is an instantiation.
1532 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1533 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1534 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1535 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1536
1537 // Compute the token length for this macro expansion.
1538 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001539 if (I + 1 != N)
1540 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001541 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1542 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1543 }
1544 }
1545
Douglas Gregor8f45df52009-04-16 22:23:12 +00001546 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001547
1548 if (SLocEntryOffsets.empty())
1549 return;
1550
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001551 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001552 // table is used for lazily loading source-location information.
1553 using namespace llvm;
1554 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001555 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001556 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1557 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1558 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1559 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001560
Douglas Gregor258ae542009-04-27 06:38:32 +00001561 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001562 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001563 Record.push_back(SLocEntryOffsets.size());
Sebastian Redlc1d035f2010-09-22 20:19:08 +00001564 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1565 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001566 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001567
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001568 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001569 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001570 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001571}
1572
Douglas Gregorc5046832009-04-27 18:38:38 +00001573//===----------------------------------------------------------------------===//
1574// Preprocessor Serialization
1575//===----------------------------------------------------------------------===//
1576
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001577static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1578 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1579 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1580 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1581 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1582 return X.first->getName().compare(Y.first->getName());
1583}
1584
Chris Lattnereeffaef2009-04-10 17:15:23 +00001585/// \brief Writes the block containing the serialized form of the
1586/// preprocessor.
1587///
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001588void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001589 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001590
Chris Lattner0af3ba12009-04-13 01:29:17 +00001591 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1592 if (PP.getCounterValue() != 0) {
1593 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001594 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001595 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001596 }
1597
1598 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001599 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001600
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001601 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001602 // FIXME: use diagnostics subsystem for localization etc.
1603 if (PP.SawDateOrTime())
1604 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001605
Douglas Gregor796d76a2010-10-20 22:00:55 +00001606
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001607 // Loop over all the macro definitions that are live at the end of the file,
1608 // emitting each to the PP section.
Douglas Gregoraae92242010-03-19 21:51:54 +00001609 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001610
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001611 // Construct the list of macro definitions that need to be serialized.
1612 llvm::SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
1613 MacrosToEmit;
1614 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor68051a72011-02-11 00:26:14 +00001615 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1616 E = PP.macro_end(Chain == 0);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001617 I != E; ++I) {
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001618 MacroDefinitionsSeen.insert(I->first);
1619 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1620 }
1621
1622 // Sort the set of macro definitions that need to be serialized by the
1623 // name of the macro, to provide a stable ordering.
1624 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1625 &compareMacroDefinitions);
1626
Douglas Gregor68051a72011-02-11 00:26:14 +00001627 // Resolve any identifiers that defined macros at the time they were
1628 // deserialized, adding them to the list of macros to emit (if appropriate).
1629 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1630 IdentifierInfo *Name
1631 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1632 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1633 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1634 }
1635
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001636 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1637 const IdentifierInfo *Name = MacrosToEmit[I].first;
1638 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor68051a72011-02-11 00:26:14 +00001639 if (!MI)
1640 continue;
1641
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001642 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001643 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001644 // Also skip macros from a AST file if we're chaining.
Douglas Gregoreb114da2010-10-01 01:03:07 +00001645
1646 // FIXME: There is a (probably minor) optimization we could do here, if
1647 // the macro comes from the original PCH but the identifier comes from a
1648 // chained PCH, by storing the offset into the original PCH rather than
1649 // writing the macro definition a second time.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001650 if (MI->isBuiltinMacro() ||
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001651 (Chain && Name->isFromAST() && MI->isFromAST()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001652 continue;
1653
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001654 AddIdentifierRef(Name, Record);
1655 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001656 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1657 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001658
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001659 unsigned Code;
1660 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001661 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001662 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001663 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001664
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001665 Record.push_back(MI->isC99Varargs());
1666 Record.push_back(MI->isGNUVarargs());
1667 Record.push_back(MI->getNumArgs());
1668 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1669 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001670 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001671 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001672
Douglas Gregoraae92242010-03-19 21:51:54 +00001673 // If we have a detailed preprocessing record, record the macro definition
1674 // ID that corresponds to this macro.
1675 if (PPRec)
1676 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001677
Douglas Gregor8f45df52009-04-16 22:23:12 +00001678 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001679 Record.clear();
1680
Chris Lattner2199f5b2009-04-10 18:08:30 +00001681 // Emit the tokens array.
1682 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1683 // Note that we know that the preprocessor does not have any annotation
1684 // tokens in it because they are created by the parser, and thus can't be
1685 // in a macro definition.
1686 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001687
Chris Lattner2199f5b2009-04-10 18:08:30 +00001688 Record.push_back(Tok.getLocation().getRawEncoding());
1689 Record.push_back(Tok.getLength());
1690
Chris Lattner2199f5b2009-04-10 18:08:30 +00001691 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1692 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001693 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001694 // FIXME: Should translate token kind to a stable encoding.
1695 Record.push_back(Tok.getKind());
1696 // FIXME: Should translate token flags to a stable encoding.
1697 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001698
Sebastian Redl539c5062010-08-18 23:57:32 +00001699 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001700 Record.clear();
1701 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001702 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001703 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001704 Stream.ExitBlock();
1705
1706 if (PPRec)
1707 WritePreprocessorDetail(*PPRec);
1708}
1709
1710void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
1711 if (PPRec.begin(Chain) == PPRec.end(Chain))
1712 return;
1713
1714 // Enter the preprocessor block.
1715 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001716
Douglas Gregoraae92242010-03-19 21:51:54 +00001717 // If the preprocessor has a preprocessing record, emit it.
1718 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001719 using namespace llvm;
1720
1721 // Set up the abbreviation for
1722 unsigned InclusionAbbrev = 0;
1723 {
1724 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1725 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
1726 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1727 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1728 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1729 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1730 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1731 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1732 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1733 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1734 }
1735
1736 unsigned IndexBase = Chain ? PPRec.getNumPreallocatedEntities() : 0;
1737 RecordData Record;
1738 for (PreprocessingRecord::iterator E = PPRec.begin(Chain),
1739 EEnd = PPRec.end(Chain);
1740 E != EEnd; ++E) {
1741 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001742
Douglas Gregor92a96f52011-02-08 21:58:10 +00001743 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1744 // Record this macro definition's location.
1745 MacroID ID = getMacroDefinitionID(MD);
1746
1747 // Don't write the macro definition if it is from another AST file.
1748 if (ID < FirstMacroID)
Douglas Gregoraae92242010-03-19 21:51:54 +00001749 continue;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001750
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001751 // Notify the serialization listener that we're serializing this entity.
1752 if (SerializationListener)
1753 SerializationListener->SerializedPreprocessedEntity(*E,
Douglas Gregor92a96f52011-02-08 21:58:10 +00001754 Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001755
Douglas Gregor92a96f52011-02-08 21:58:10 +00001756 unsigned Position = ID - FirstMacroID;
1757 if (Position != MacroDefinitionOffsets.size()) {
1758 if (Position > MacroDefinitionOffsets.size())
1759 MacroDefinitionOffsets.resize(Position + 1);
1760
1761 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
1762 } else
1763 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001764
Douglas Gregor92a96f52011-02-08 21:58:10 +00001765 Record.push_back(IndexBase + NumPreprocessingRecords++);
1766 Record.push_back(ID);
1767 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1768 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1769 AddIdentifierRef(MD->getName(), Record);
1770 AddSourceLocation(MD->getLocation(), Record);
1771 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1772 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00001773 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001774
Douglas Gregor92a96f52011-02-08 21:58:10 +00001775 // Notify the serialization listener that we're serializing this entity.
1776 if (SerializationListener)
1777 SerializationListener->SerializedPreprocessedEntity(*E,
1778 Stream.GetCurrentBitNo());
1779
1780 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1781 Record.push_back(IndexBase + NumPreprocessingRecords++);
1782 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1783 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1784 AddIdentifierRef(MI->getName(), Record);
1785 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1786 Stream.EmitRecord(PPD_MACRO_INSTANTIATION, Record);
1787 continue;
1788 }
1789
1790 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1791 Record.push_back(PPD_INCLUSION_DIRECTIVE);
1792 Record.push_back(IndexBase + NumPreprocessingRecords++);
1793 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1794 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1795 Record.push_back(ID->getFileName().size());
1796 Record.push_back(ID->wasInQuotes());
1797 Record.push_back(static_cast<unsigned>(ID->getKind()));
1798 llvm::SmallString<64> Buffer;
1799 Buffer += ID->getFileName();
1800 Buffer += ID->getFile()->getName();
1801 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1802 continue;
1803 }
1804
1805 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1806 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001807 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001808
Douglas Gregoraae92242010-03-19 21:51:54 +00001809 // Write the offsets table for the preprocessing record.
1810 if (NumPreprocessingRecords > 0) {
1811 // Write the offsets table for identifier IDs.
1812 using namespace llvm;
1813 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001814 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregoraae92242010-03-19 21:51:54 +00001815 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1816 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1817 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1818 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001819
Douglas Gregoraae92242010-03-19 21:51:54 +00001820 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001821 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001822 Record.push_back(NumPreprocessingRecords);
1823 Record.push_back(MacroDefinitionOffsets.size());
1824 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001825 data(MacroDefinitionOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00001826 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001827}
1828
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001829void ASTWriter::WritePragmaDiagnosticMappings(const Diagnostic &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001830 RecordData Record;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001831 for (Diagnostic::DiagStatePointsTy::const_iterator
1832 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1833 I != E; ++I) {
1834 const Diagnostic::DiagStatePoint &point = *I;
1835 if (point.Loc.isInvalid())
1836 continue;
1837
1838 Record.push_back(point.Loc.getRawEncoding());
1839 for (Diagnostic::DiagState::iterator
1840 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
1841 unsigned diag = I->first, map = I->second;
1842 if (map & 0x10) { // mapping from a diagnostic pragma.
1843 Record.push_back(diag);
1844 Record.push_back(map & 0x7);
1845 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001846 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001847 Record.push_back(-1); // mark the end of the diag/map pairs for this
1848 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001849 }
1850
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00001851 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001852 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001853}
1854
Anders Carlsson9bb83e82011-03-06 18:41:18 +00001855void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
1856 if (CXXBaseSpecifiersOffsets.empty())
1857 return;
1858
1859 RecordData Record;
1860
1861 // Create a blob abbreviation for the C++ base specifiers offsets.
1862 using namespace llvm;
1863
1864 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1865 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
1866 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
1867 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1868 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1869
1870 // Write the selector offsets table.
1871 Record.clear();
1872 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
1873 Record.push_back(CXXBaseSpecifiersOffsets.size());
1874 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001875 data(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00001876}
1877
Douglas Gregorc5046832009-04-27 18:38:38 +00001878//===----------------------------------------------------------------------===//
1879// Type Serialization
1880//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001881
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001882/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001883void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00001884 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001885 if (Idx.getIndex() == 0) // we haven't seen this type before.
1886 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00001887
Douglas Gregor9b3932c2010-10-05 18:37:06 +00001888 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00001889
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001890 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001891 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001892 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001893 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001894 else if (TypeOffsets.size() < Index) {
1895 TypeOffsets.resize(Index + 1);
1896 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001897 }
1898
1899 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001900
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001901 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001902 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001903
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001904 if (T.hasLocalNonFastQualifiers()) {
1905 Qualifiers Qs = T.getLocalQualifiers();
1906 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001907 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001908 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00001909 } else {
1910 switch (T->getTypeClass()) {
1911 // For all of the concrete, non-dependent types, call the
1912 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001913#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001914 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001915#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001916#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001917 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001918 }
1919
1920 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001921 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001922
1923 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001924 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001925}
1926
Douglas Gregorc5046832009-04-27 18:38:38 +00001927//===----------------------------------------------------------------------===//
1928// Declaration Serialization
1929//===----------------------------------------------------------------------===//
1930
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001931/// \brief Write the block containing all of the declaration IDs
1932/// lexically declared within the given DeclContext.
1933///
1934/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1935/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001936uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001937 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001938 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001939 return 0;
1940
Douglas Gregor8f45df52009-04-16 22:23:12 +00001941 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001942 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001943 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001944 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001945 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1946 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001947 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001948
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001949 ++NumLexicalDeclContexts;
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001950 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001951 return Offset;
1952}
1953
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001954void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001955 using namespace llvm;
1956 RecordData Record;
1957
1958 // Write the type offsets array
1959 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001960 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001961 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1962 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1963 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1964 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001965 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001966 Record.push_back(TypeOffsets.size());
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001967 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001968
1969 // Write the declaration offsets array
1970 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001971 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001972 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1973 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1974 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1975 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001976 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001977 Record.push_back(DeclOffsets.size());
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001978 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001979}
1980
Douglas Gregorc5046832009-04-27 18:38:38 +00001981//===----------------------------------------------------------------------===//
1982// Global Method Pool and Selector Serialization
1983//===----------------------------------------------------------------------===//
1984
Douglas Gregore84a9da2009-04-20 20:36:09 +00001985namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001986// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001987class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001988 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001989
1990public:
1991 typedef Selector key_type;
1992 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001993
Sebastian Redl834bb972010-08-04 17:20:04 +00001994 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00001995 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00001996 ObjCMethodList Instance, Factory;
1997 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00001998 typedef const data_type& data_type_ref;
1999
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002000 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00002001
Douglas Gregorc78d3462009-04-24 21:10:55 +00002002 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00002003 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002004 }
Mike Stump11289f42009-09-09 15:08:12 +00002005
2006 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00002007 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
2008 data_type_ref Methods) {
2009 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2010 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00002011 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2012 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002013 Method = Method->Next)
2014 if (Method->Method)
2015 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00002016 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002017 Method = Method->Next)
2018 if (Method->Method)
2019 DataLen += 4;
2020 clang::io::Emit16(Out, DataLen);
2021 return std::make_pair(KeyLen, DataLen);
2022 }
Mike Stump11289f42009-09-09 15:08:12 +00002023
Douglas Gregor95c13f52009-04-25 17:48:32 +00002024 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00002025 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002026 assert((Start >> 32) == 0 && "Selector key offset too large");
2027 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002028 unsigned N = Sel.getNumArgs();
2029 clang::io::Emit16(Out, N);
2030 if (N == 0)
2031 N = 1;
2032 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00002033 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002034 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2035 }
Mike Stump11289f42009-09-09 15:08:12 +00002036
Douglas Gregorc78d3462009-04-24 21:10:55 +00002037 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002038 data_type_ref Methods, unsigned DataLen) {
2039 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00002040 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002041 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002042 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002043 Method = Method->Next)
2044 if (Method->Method)
2045 ++NumInstanceMethods;
2046
2047 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002048 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002049 Method = Method->Next)
2050 if (Method->Method)
2051 ++NumFactoryMethods;
2052
2053 clang::io::Emit16(Out, NumInstanceMethods);
2054 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00002055 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002056 Method = Method->Next)
2057 if (Method->Method)
2058 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002059 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002060 Method = Method->Next)
2061 if (Method->Method)
2062 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002063
2064 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002065 }
2066};
2067} // end anonymous namespace
2068
Sebastian Redla19a67f2010-08-03 21:58:15 +00002069/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002070///
2071/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002072/// in an on-disk hash table indexed by the selector. The hash table also
2073/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002074void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002075 using namespace llvm;
2076
Sebastian Redla19a67f2010-08-03 21:58:15 +00002077 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002078 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002079 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002080 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002081 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002082 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002083 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002084 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002085
Sebastian Redla19a67f2010-08-03 21:58:15 +00002086 // Create the on-disk hash table representation. We walk through every
2087 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002088 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002089 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002090 I = SelectorIDs.begin(), E = SelectorIDs.end();
2091 I != E; ++I) {
2092 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002093 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002094 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002095 I->second,
2096 ObjCMethodList(),
2097 ObjCMethodList()
2098 };
2099 if (F != SemaRef.MethodPool.end()) {
2100 Data.Instance = F->second.first;
2101 Data.Factory = F->second.second;
2102 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002103 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002104 // changed.
2105 if (Chain && I->second < FirstSelectorID) {
2106 // Selector already exists. Did it change?
2107 bool changed = false;
2108 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2109 M = M->Next) {
2110 if (M->Method->getPCHLevel() == 0)
2111 changed = true;
2112 }
2113 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2114 M = M->Next) {
2115 if (M->Method->getPCHLevel() == 0)
2116 changed = true;
2117 }
2118 if (!changed)
2119 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002120 } else if (Data.Instance.Method || Data.Factory.Method) {
2121 // A new method pool entry.
2122 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002123 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002124 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002125 }
2126
Douglas Gregorc78d3462009-04-24 21:10:55 +00002127 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002128 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002129 uint32_t BucketOffset;
2130 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002131 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002132 llvm::raw_svector_ostream Out(MethodPool);
2133 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002134 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002135 BucketOffset = Generator.Emit(Out, Trait);
2136 }
2137
2138 // Create a blob abbreviation
2139 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002140 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002141 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002142 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002143 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2144 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2145
Douglas Gregor95c13f52009-04-25 17:48:32 +00002146 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002147 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002148 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002149 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002150 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002151 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002152
2153 // Create a blob abbreviation for the selector table offsets.
2154 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002155 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002156 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor95c13f52009-04-25 17:48:32 +00002157 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2158 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2159
2160 // Write the selector offsets table.
2161 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002162 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002163 Record.push_back(SelectorOffsets.size());
2164 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002165 data(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002166 }
2167}
2168
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002169/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002170void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002171 using namespace llvm;
2172 if (SemaRef.ReferencedSelectors.empty())
2173 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002174
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002175 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002176
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002177 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002178 // very tricky to fix, and given that @selector shouldn't really appear in
2179 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002180 for (DenseMap<Selector, SourceLocation>::iterator S =
2181 SemaRef.ReferencedSelectors.begin(),
2182 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2183 Selector Sel = (*S).first;
2184 SourceLocation Loc = (*S).second;
2185 AddSelectorRef(Sel, Record);
2186 AddSourceLocation(Loc, Record);
2187 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002188 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002189}
2190
Douglas Gregorc5046832009-04-27 18:38:38 +00002191//===----------------------------------------------------------------------===//
2192// Identifier Table Serialization
2193//===----------------------------------------------------------------------===//
2194
Douglas Gregorc78d3462009-04-24 21:10:55 +00002195namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002196class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002197 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002198 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002199
Douglas Gregor1d583f22009-04-28 21:18:29 +00002200 /// \brief Determines whether this is an "interesting" identifier
2201 /// that needs a full IdentifierInfo structure written into the hash
2202 /// table.
2203 static bool isInterestingIdentifier(const IdentifierInfo *II) {
2204 return II->isPoisoned() ||
2205 II->isExtensionToken() ||
2206 II->hasMacroDefinition() ||
2207 II->getObjCOrBuiltinID() ||
2208 II->getFETokenInfo<void>();
2209 }
2210
Douglas Gregore84a9da2009-04-20 20:36:09 +00002211public:
2212 typedef const IdentifierInfo* key_type;
2213 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002214
Sebastian Redl539c5062010-08-18 23:57:32 +00002215 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002216 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002217
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002218 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00002219 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00002220
2221 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00002222 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00002223 }
Mike Stump11289f42009-09-09 15:08:12 +00002224
2225 std::pair<unsigned,unsigned>
2226 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002227 IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002228 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002229 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
2230 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00002231 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00002232 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00002233 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00002234 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002235 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2236 DEnd = IdentifierResolver::end();
2237 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00002238 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00002239 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00002240 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00002241 // We emit the key length after the data length so that every
2242 // string is preceded by a 16-bit length. This matches the PTH
2243 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00002244 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002245 return std::make_pair(KeyLen, DataLen);
2246 }
Mike Stump11289f42009-09-09 15:08:12 +00002247
2248 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00002249 unsigned KeyLen) {
2250 // Record the location of the key data. This is used when generating
2251 // the mapping from persistent IDs to strings.
2252 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002253 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002254 }
Mike Stump11289f42009-09-09 15:08:12 +00002255
2256 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002257 IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00002258 if (!isInterestingIdentifier(II)) {
2259 clang::io::Emit32(Out, ID << 1);
2260 return;
2261 }
Douglas Gregorb9256522009-04-28 21:32:13 +00002262
Douglas Gregor1d583f22009-04-28 21:18:29 +00002263 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002264 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002265 bool hasMacroDefinition =
2266 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00002267 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00002268 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002269 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
2270 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2271 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00002272 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002273 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00002274 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002275
Douglas Gregorc3366a52009-04-21 23:56:24 +00002276 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00002277 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00002278
Douglas Gregora868bbd2009-04-21 22:25:48 +00002279 // Emit the declaration IDs in reverse order, because the
2280 // IdentifierResolver provides the declarations as they would be
2281 // visible (e.g., the function "stat" would come before the struct
2282 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2283 // adds declarations to the end of the list (so we need to see the
2284 // struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00002285 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump11289f42009-09-09 15:08:12 +00002286 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00002287 IdentifierResolver::end());
2288 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
2289 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00002290 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00002291 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002292 }
2293};
2294} // end anonymous namespace
2295
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002296/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002297///
2298/// The identifier table consists of a blob containing string data
2299/// (the actual identifiers themselves) and a separate "offsets" index
2300/// that maps identifier IDs to locations within the blob.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002301void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002302 using namespace llvm;
2303
2304 // Create and write out the blob that contains the identifier
2305 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002306 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002307 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002308 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump11289f42009-09-09 15:08:12 +00002309
Douglas Gregore6648fb2009-04-28 20:33:11 +00002310 // Look for any identifiers that were named while processing the
2311 // headers, but are otherwise not needed. We add these to the hash
2312 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002313 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00002314 // file.
2315 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2316 IDEnd = PP.getIdentifierTable().end();
2317 ID != IDEnd; ++ID)
2318 getIdentifierRef(ID->second);
2319
Sebastian Redlff4a2952010-07-23 23:49:55 +00002320 // Create the on-disk hash table representation. We only store offsets
2321 // for identifiers that appear here for the first time.
2322 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002323 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002324 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2325 ID != IDEnd; ++ID) {
2326 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002327 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002328 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002329 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002330
Douglas Gregore84a9da2009-04-20 20:36:09 +00002331 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002332 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00002333 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002334 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002335 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002336 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002337 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002338 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002339 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002340 }
2341
2342 // Create a blob abbreviation
2343 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002344 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00002345 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002346 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00002347 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002348
2349 // Write the identifier table
2350 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002351 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002352 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002353 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002354 }
2355
2356 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00002357 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002358 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00002359 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2360 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2361 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2362
2363 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002364 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00002365 Record.push_back(IdentifierOffsets.size());
2366 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002367 data(IdentifierOffsets));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002368}
2369
Douglas Gregorc5046832009-04-27 18:38:38 +00002370//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002371// DeclContext's Name Lookup Table Serialization
2372//===----------------------------------------------------------------------===//
2373
2374namespace {
2375// Trait used for the on-disk hash table used in the method pool.
2376class ASTDeclContextNameLookupTrait {
2377 ASTWriter &Writer;
2378
2379public:
2380 typedef DeclarationName key_type;
2381 typedef key_type key_type_ref;
2382
2383 typedef DeclContext::lookup_result data_type;
2384 typedef const data_type& data_type_ref;
2385
2386 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2387
2388 unsigned ComputeHash(DeclarationName Name) {
2389 llvm::FoldingSetNodeID ID;
2390 ID.AddInteger(Name.getNameKind());
2391
2392 switch (Name.getNameKind()) {
2393 case DeclarationName::Identifier:
2394 ID.AddString(Name.getAsIdentifierInfo()->getName());
2395 break;
2396 case DeclarationName::ObjCZeroArgSelector:
2397 case DeclarationName::ObjCOneArgSelector:
2398 case DeclarationName::ObjCMultiArgSelector:
2399 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2400 break;
2401 case DeclarationName::CXXConstructorName:
2402 case DeclarationName::CXXDestructorName:
2403 case DeclarationName::CXXConversionFunctionName:
2404 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
2405 break;
2406 case DeclarationName::CXXOperatorName:
2407 ID.AddInteger(Name.getCXXOverloadedOperator());
2408 break;
2409 case DeclarationName::CXXLiteralOperatorName:
2410 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2411 case DeclarationName::CXXUsingDirective:
2412 break;
2413 }
2414
2415 return ID.ComputeHash();
2416 }
2417
2418 std::pair<unsigned,unsigned>
2419 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2420 data_type_ref Lookup) {
2421 unsigned KeyLen = 1;
2422 switch (Name.getNameKind()) {
2423 case DeclarationName::Identifier:
2424 case DeclarationName::ObjCZeroArgSelector:
2425 case DeclarationName::ObjCOneArgSelector:
2426 case DeclarationName::ObjCMultiArgSelector:
2427 case DeclarationName::CXXConstructorName:
2428 case DeclarationName::CXXDestructorName:
2429 case DeclarationName::CXXConversionFunctionName:
2430 case DeclarationName::CXXLiteralOperatorName:
2431 KeyLen += 4;
2432 break;
2433 case DeclarationName::CXXOperatorName:
2434 KeyLen += 1;
2435 break;
2436 case DeclarationName::CXXUsingDirective:
2437 break;
2438 }
2439 clang::io::Emit16(Out, KeyLen);
2440
2441 // 2 bytes for num of decls and 4 for each DeclID.
2442 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2443 clang::io::Emit16(Out, DataLen);
2444
2445 return std::make_pair(KeyLen, DataLen);
2446 }
2447
2448 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2449 using namespace clang::io;
2450
2451 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2452 Emit8(Out, Name.getNameKind());
2453 switch (Name.getNameKind()) {
2454 case DeclarationName::Identifier:
2455 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2456 break;
2457 case DeclarationName::ObjCZeroArgSelector:
2458 case DeclarationName::ObjCOneArgSelector:
2459 case DeclarationName::ObjCMultiArgSelector:
2460 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2461 break;
2462 case DeclarationName::CXXConstructorName:
2463 case DeclarationName::CXXDestructorName:
2464 case DeclarationName::CXXConversionFunctionName:
2465 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2466 break;
2467 case DeclarationName::CXXOperatorName:
2468 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2469 Emit8(Out, Name.getCXXOverloadedOperator());
2470 break;
2471 case DeclarationName::CXXLiteralOperatorName:
2472 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2473 break;
2474 case DeclarationName::CXXUsingDirective:
2475 break;
2476 }
2477 }
2478
2479 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2480 data_type Lookup, unsigned DataLen) {
2481 uint64_t Start = Out.tell(); (void)Start;
2482 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2483 for (; Lookup.first != Lookup.second; ++Lookup.first)
2484 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2485
2486 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2487 }
2488};
2489} // end anonymous namespace
2490
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002491/// \brief Write the block containing all of the declaration IDs
2492/// visible from the given DeclContext.
2493///
2494/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002495/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002496uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2497 DeclContext *DC) {
2498 if (DC->getPrimaryContext() != DC)
2499 return 0;
2500
2501 // Since there is no name lookup into functions or methods, don't bother to
2502 // build a visible-declarations table for these entities.
2503 if (DC->isFunctionOrMethod())
2504 return 0;
2505
2506 // If not in C++, we perform name lookup for the translation unit via the
2507 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2508 // FIXME: In C++ we need the visible declarations in order to "see" the
2509 // friend declarations, is there a way to do this without writing the table ?
2510 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2511 return 0;
2512
2513 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00002514 if (DC->hasExternalVisibleStorage())
2515 DC->MaterializeVisibleDeclsFromExternalStorage();
2516 else
2517 DC->lookup(DeclarationName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002518
2519 // Serialize the contents of the mapping used for lookup. Note that,
2520 // although we have two very different code paths, the serialized
2521 // representation is the same for both cases: a declaration name,
2522 // followed by a size, followed by references to the visible
2523 // declarations that have that name.
2524 uint64_t Offset = Stream.GetCurrentBitNo();
2525 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2526 if (!Map || Map->empty())
2527 return 0;
2528
2529 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2530 ASTDeclContextNameLookupTrait Trait(*this);
2531
2532 // Create the on-disk hash table representation.
2533 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2534 D != DEnd; ++D) {
2535 DeclarationName Name = D->first;
2536 DeclContext::lookup_result Result = D->second.getLookupResult();
2537 Generator.insert(Name, Result, Trait);
2538 }
2539
2540 // Create the on-disk hash table in a buffer.
2541 llvm::SmallString<4096> LookupTable;
2542 uint32_t BucketOffset;
2543 {
2544 llvm::raw_svector_ostream Out(LookupTable);
2545 // Make sure that no bucket is at offset 0
2546 clang::io::Emit32(Out, 0);
2547 BucketOffset = Generator.Emit(Out, Trait);
2548 }
2549
2550 // Write the lookup table
2551 RecordData Record;
2552 Record.push_back(DECL_CONTEXT_VISIBLE);
2553 Record.push_back(BucketOffset);
2554 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2555 LookupTable.str());
2556
2557 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2558 ++NumVisibleDeclContexts;
2559 return Offset;
2560}
2561
Sebastian Redla4071b42010-08-24 00:50:09 +00002562/// \brief Write an UPDATE_VISIBLE block for the given context.
2563///
2564/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2565/// DeclContext in a dependent AST file. As such, they only exist for the TU
2566/// (in C++) and for namespaces.
2567void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00002568 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2569 if (!Map || Map->empty())
2570 return;
2571
2572 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2573 ASTDeclContextNameLookupTrait Trait(*this);
2574
2575 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002576 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2577 D != DEnd; ++D) {
2578 DeclarationName Name = D->first;
2579 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002580 // For any name that appears in this table, the results are complete, i.e.
2581 // they overwrite results from previous PCHs. Merging is always a mess.
2582 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002583 }
2584
2585 // Create the on-disk hash table in a buffer.
2586 llvm::SmallString<4096> LookupTable;
2587 uint32_t BucketOffset;
2588 {
2589 llvm::raw_svector_ostream Out(LookupTable);
2590 // Make sure that no bucket is at offset 0
2591 clang::io::Emit32(Out, 0);
2592 BucketOffset = Generator.Emit(Out, Trait);
2593 }
2594
2595 // Write the lookup table
2596 RecordData Record;
2597 Record.push_back(UPDATE_VISIBLE);
2598 Record.push_back(getDeclID(cast<Decl>(DC)));
2599 Record.push_back(BucketOffset);
2600 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2601}
2602
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002603/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2604void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2605 RecordData Record;
2606 Record.push_back(Opts.fp_contract);
2607 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2608}
2609
2610/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2611void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
2612 if (!SemaRef.Context.getLangOptions().OpenCL)
2613 return;
2614
2615 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2616 RecordData Record;
2617#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2618#include "clang/Basic/OpenCLExtensions.def"
2619 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2620}
2621
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002622//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00002623// General Serialization Routines
2624//===----------------------------------------------------------------------===//
2625
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002626/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002627void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00002628 Record.push_back(Attrs.size());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002629 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2630 const Attr * A = *i;
2631 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2632 AddSourceLocation(A->getLocation(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002633
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002634#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00002635
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002636 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002637}
2638
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002639void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002640 Record.push_back(Str.size());
2641 Record.insert(Record.end(), Str.begin(), Str.end());
2642}
2643
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002644void ASTWriter::AddVersionTuple(const VersionTuple &Version,
2645 RecordDataImpl &Record) {
2646 Record.push_back(Version.getMajor());
2647 if (llvm::Optional<unsigned> Minor = Version.getMinor())
2648 Record.push_back(*Minor + 1);
2649 else
2650 Record.push_back(0);
2651 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
2652 Record.push_back(*Subminor + 1);
2653 else
2654 Record.push_back(0);
2655}
2656
Douglas Gregore84a9da2009-04-20 20:36:09 +00002657/// \brief Note that the identifier II occurs at the given offset
2658/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002659void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002660 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002661 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00002662 // up earlier in the chain and thus don't need an offset.
2663 if (ID >= FirstIdentID)
2664 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002665}
2666
Douglas Gregor95c13f52009-04-25 17:48:32 +00002667/// \brief Note that the selector Sel occurs at the given offset
2668/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002669void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00002670 unsigned ID = SelectorIDs[Sel];
2671 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00002672 // Don't record offsets for selectors that are also available in a different
2673 // file.
2674 if (ID < FirstSelectorID)
2675 return;
2676 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002677}
2678
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002679ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00002680 : Stream(Stream), Chain(0), SerializationListener(0),
2681 FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00002682 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002683 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor91096292010-10-02 19:29:26 +00002684 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2685 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002686 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002687 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
2688 NextCXXBaseSpecifiersID(1)
2689{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002690}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002691
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002692void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002693 const std::string &OutputFile,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002694 const char *isysroot) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002695 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002696 Stream.Emit((unsigned)'C', 8);
2697 Stream.Emit((unsigned)'P', 8);
2698 Stream.Emit((unsigned)'C', 8);
2699 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002700
Chris Lattner28fa4e62009-04-26 22:26:21 +00002701 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002702
Sebastian Redl143413f2010-07-12 22:02:52 +00002703 if (Chain)
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002704 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002705 else
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002706 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile);
Sebastian Redl143413f2010-07-12 22:02:52 +00002707}
2708
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002709void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002710 const char *isysroot,
2711 const std::string &OutputFile) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002712 using namespace llvm;
2713
2714 ASTContext &Context = SemaRef.Context;
2715 Preprocessor &PP = SemaRef.PP;
2716
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002717 // The translation unit is the first declaration we'll emit.
2718 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlff4a2952010-07-23 23:49:55 +00002719 ++NextDeclID;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002720 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002721
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002722 // Make sure that we emit IdentifierInfos (and any attached
2723 // declarations) for builtins.
2724 {
2725 IdentifierTable &Table = PP.getIdentifierTable();
2726 llvm::SmallVector<const char *, 32> BuiltinNames;
2727 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2728 Context.getLangOptions().NoBuiltin);
2729 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2730 getIdentifierRef(&Table.get(BuiltinNames[I]));
2731 }
2732
Chris Lattner0c797362009-09-08 18:19:27 +00002733 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002734 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002735 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002736 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00002737 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2738 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00002739 }
Douglas Gregord4df8652009-04-22 22:02:47 +00002740
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002741 // Build a record containing all of the file scoped decls in this file.
2742 RecordData UnusedFileScopedDecls;
2743 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2744 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002745
Alexis Hunt27a761d2011-05-04 23:29:54 +00002746 RecordData DelegatingCtorDecls;
2747 for (unsigned i=0, e = SemaRef.DelegatingCtorDecls.size(); i != e; ++i)
2748 AddDeclRef(SemaRef.DelegatingCtorDecls[i], DelegatingCtorDecls);
2749
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002750 RecordData WeakUndeclaredIdentifiers;
2751 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2752 WeakUndeclaredIdentifiers.push_back(
2753 SemaRef.WeakUndeclaredIdentifiers.size());
2754 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2755 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2756 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2757 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2758 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2759 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2760 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2761 }
2762 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002763
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002764 // Build a record containing all of the locally-scoped external
2765 // declarations in this header file. Generally, this record will be
2766 // empty.
2767 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002768 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00002769 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002770 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002771 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2772 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2773 TD != TDEnd; ++TD)
2774 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2775
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002776 // Build a record containing all of the ext_vector declarations.
2777 RecordData ExtVectorDecls;
2778 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2779 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2780
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002781 // Build a record containing all of the VTable uses information.
2782 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00002783 if (!SemaRef.VTableUses.empty()) {
2784 VTableUses.push_back(SemaRef.VTableUses.size());
2785 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2786 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2787 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2788 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2789 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002790 }
2791
2792 // Build a record containing all of dynamic classes declarations.
2793 RecordData DynamicClasses;
2794 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2795 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2796
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002797 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002798 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002799 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002800 I = SemaRef.PendingInstantiations.begin(),
2801 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2802 AddDeclRef(I->first, PendingInstantiations);
2803 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002804 }
2805 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2806 "There are local ones at end of translation unit!");
2807
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002808 // Build a record containing some declaration references.
2809 RecordData SemaDeclRefs;
2810 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2811 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2812 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2813 }
2814
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002815 RecordData CUDASpecialDeclRefs;
2816 if (Context.getcudaConfigureCallDecl()) {
2817 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
2818 }
2819
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002820 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002821 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002822 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002823 WriteMetadata(Context, isysroot, OutputFile);
Sebastian Redl143413f2010-07-12 22:02:52 +00002824 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002825 if (StatCalls && !isysroot)
Douglas Gregor11cfd942010-07-12 23:48:14 +00002826 WriteStatCache(*StatCalls);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002827 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc277ad12009-07-18 15:33:26 +00002828 // Write the record of special types.
2829 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002830
Steve Naroffc277ad12009-07-18 15:33:26 +00002831 AddTypeRef(Context.getBuiltinVaListType(), Record);
2832 AddTypeRef(Context.getObjCIdType(), Record);
2833 AddTypeRef(Context.getObjCSelType(), Record);
2834 AddTypeRef(Context.getObjCProtoType(), Record);
2835 AddTypeRef(Context.getObjCClassType(), Record);
2836 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2837 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2838 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002839 AddTypeRef(Context.getjmp_bufType(), Record);
2840 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002841 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2842 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpd0153282009-10-20 02:12:22 +00002843 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002844 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002845 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2846 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002847 Record.push_back(Context.isInt128Installed());
Richard Smith02e85f32011-04-14 22:09:26 +00002848 AddTypeRef(Context.AutoDeductTy, Record);
2849 AddTypeRef(Context.AutoRRefDeductTy, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +00002850 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002851
Douglas Gregor1970d882009-04-26 03:49:13 +00002852 // Keep writing types and declarations until all types and
2853 // declarations have been written.
Sebastian Redl539c5062010-08-18 23:57:32 +00002854 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002855 WriteDeclsBlockAbbrevs();
2856 while (!DeclTypesToEmit.empty()) {
2857 DeclOrType DOT = DeclTypesToEmit.front();
2858 DeclTypesToEmit.pop();
2859 if (DOT.isType())
2860 WriteType(DOT.getType());
2861 else
2862 WriteDecl(Context, DOT.getDecl());
2863 }
2864 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002865
Douglas Gregor45053152009-10-17 17:25:45 +00002866 WritePreprocessor(PP);
Douglas Gregor09b69892011-02-10 17:09:37 +00002867 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00002868 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002869 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002870 WriteIdentifierTable(PP);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002871 WriteFPPragmaOptions(SemaRef.getFPOptions());
2872 WriteOpenCLExtensions(SemaRef);
Douglas Gregor745ed142009-04-25 18:35:21 +00002873
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002874 WriteTypeDeclOffsets();
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002875 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00002876
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002877 WriteCXXBaseSpecifiersOffsets();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002878
Douglas Gregord4df8652009-04-22 22:02:47 +00002879 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002880 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002881 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002882
2883 // Write the record containing tentative definitions.
2884 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002885 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002886
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002887 // Write the record containing unused file scoped decls.
2888 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002889 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002890
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002891 // Write the record containing weak undeclared identifiers.
2892 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002893 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002894 WeakUndeclaredIdentifiers);
2895
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002896 // Write the record containing locally-scoped external definitions.
2897 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002898 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002899 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002900
2901 // Write the record containing ext_vector type names.
2902 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002903 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002904
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002905 // Write the record containing VTable uses information.
2906 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002907 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002908
2909 // Write the record containing dynamic classes declarations.
2910 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002911 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002912
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002913 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002914 if (!PendingInstantiations.empty())
2915 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002916
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002917 // Write the record containing declaration references of Sema.
2918 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002919 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002920
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002921 // Write the record containing CUDA-specific declaration references.
2922 if (!CUDASpecialDeclRefs.empty())
2923 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00002924
2925 // Write the delegating constructors.
2926 if (!DelegatingCtorDecls.empty())
2927 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002928
Douglas Gregor08f01292009-04-17 22:13:46 +00002929 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002930 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002931 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002932 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002933 Record.push_back(NumLexicalDeclContexts);
2934 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00002935 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002936 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002937}
2938
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002939void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002940 const char *isysroot) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002941 using namespace llvm;
2942
2943 ASTContext &Context = SemaRef.Context;
2944 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002945
Sebastian Redl143413f2010-07-12 22:02:52 +00002946 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002947 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002948 WriteMetadata(Context, isysroot, "");
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002949 if (StatCalls && !isysroot)
2950 WriteStatCache(*StatCalls);
2951 // FIXME: Source manager block should only write new stuff, which could be
2952 // done by tracking the largest ID in the chain
2953 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002954
2955 // The special types are in the chained PCH.
2956
2957 // We don't start with the translation unit, but with its decls that
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002958 // don't come from the chained PCH.
Sebastian Redl143413f2010-07-12 22:02:52 +00002959 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002960 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002961 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2962 E = TU->noload_decls_end();
Sebastian Redl143413f2010-07-12 22:02:52 +00002963 I != E; ++I) {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002964 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002965 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002966 else if ((*I)->isChangedSinceDeserialization())
2967 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl143413f2010-07-12 22:02:52 +00002968 }
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002969 // We also need to write a lexical updates block for the TU.
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002970 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002971 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002972 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2973 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2974 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002975 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002976 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002977 data(NewGlobalDecls));
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00002978 // And a visible updates block for the DeclContexts.
2979 Abv = new llvm::BitCodeAbbrev();
2980 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2981 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2982 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2983 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2984 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2985 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl143413f2010-07-12 22:02:52 +00002986
Sebastian Redl98912122010-07-27 23:01:28 +00002987 // Build a record containing all of the new tentative definitions in this
2988 // file, in TentativeDefinitions order.
2989 RecordData TentativeDefinitions;
2990 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2991 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2992 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2993 }
2994
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002995 // Build a record containing all of the file scoped decls in this file.
2996 RecordData UnusedFileScopedDecls;
2997 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2998 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2999 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003000 }
3001
Alexis Hunt27a761d2011-05-04 23:29:54 +00003002 // Build a record containing all of the delegating constructor decls in this
3003 // file.
3004 RecordData DelegatingCtorDecls;
3005 for (unsigned i=0, e = SemaRef.DelegatingCtorDecls.size(); i != e; ++i) {
3006 if (SemaRef.DelegatingCtorDecls[i]->getPCHLevel() == 0)
3007 AddDeclRef(SemaRef.DelegatingCtorDecls[i], DelegatingCtorDecls);
3008 }
3009
Sebastian Redl08aca90252010-08-05 18:21:25 +00003010 // We write the entire table, overwriting the tables from the chain.
3011 RecordData WeakUndeclaredIdentifiers;
3012 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
3013 WeakUndeclaredIdentifiers.push_back(
3014 SemaRef.WeakUndeclaredIdentifiers.size());
3015 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
3016 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3017 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3018 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3019 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3020 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3021 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3022 }
3023 }
3024
Sebastian Redl98912122010-07-27 23:01:28 +00003025 // Build a record containing all of the locally-scoped external
3026 // declarations in this header file. Generally, this record will be
3027 // empty.
3028 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003029 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl98912122010-07-27 23:01:28 +00003030 // nondeterminstic!
3031 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
3032 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3033 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
3034 TD != TDEnd; ++TD) {
3035 if (TD->second->getPCHLevel() == 0)
3036 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3037 }
3038
3039 // Build a record containing all of the ext_vector declarations.
3040 RecordData ExtVectorDecls;
3041 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
3042 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
3043 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
3044 }
3045
Sebastian Redl08aca90252010-08-05 18:21:25 +00003046 // Build a record containing all of the VTable uses information.
3047 // We write everything here, because it's too hard to determine whether
3048 // a use is new to this part.
3049 RecordData VTableUses;
3050 if (!SemaRef.VTableUses.empty()) {
3051 VTableUses.push_back(SemaRef.VTableUses.size());
3052 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3053 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3054 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3055 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3056 }
3057 }
3058
3059 // Build a record containing all of dynamic classes declarations.
3060 RecordData DynamicClasses;
3061 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
3062 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
3063 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
3064
3065 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003066 RecordData PendingInstantiations;
Sebastian Redl08aca90252010-08-05 18:21:25 +00003067 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00003068 I = SemaRef.PendingInstantiations.begin(),
3069 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl14afaf02011-04-24 16:27:30 +00003070 AddDeclRef(I->first, PendingInstantiations);
3071 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003072 }
3073 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3074 "There are local ones at end of translation unit!");
3075
3076 // Build a record containing some declaration references.
3077 // It's not worth the effort to avoid duplication here.
3078 RecordData SemaDeclRefs;
3079 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3080 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3081 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3082 }
3083
Sebastian Redl539c5062010-08-18 23:57:32 +00003084 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl143413f2010-07-12 22:02:52 +00003085 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003086 for (DeclsToRewriteTy::iterator
3087 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
3088 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl143413f2010-07-12 22:02:52 +00003089 while (!DeclTypesToEmit.empty()) {
3090 DeclOrType DOT = DeclTypesToEmit.front();
3091 DeclTypesToEmit.pop();
3092 if (DOT.isType())
3093 WriteType(DOT.getType());
3094 else
3095 WriteDecl(Context, DOT.getDecl());
3096 }
3097 Stream.ExitBlock();
3098
Sebastian Redl98912122010-07-27 23:01:28 +00003099 WritePreprocessor(PP);
Sebastian Redl51c79d82010-08-04 22:21:29 +00003100 WriteSelectors(SemaRef);
3101 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlff4a2952010-07-23 23:49:55 +00003102 WriteIdentifierTable(PP);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003103 WriteFPPragmaOptions(SemaRef.getFPOptions());
3104 WriteOpenCLExtensions(SemaRef);
3105
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003106 WriteTypeDeclOffsets();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003107 // FIXME: For chained PCH only write the new mappings (we currently
3108 // write all of them again).
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003109 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl98912122010-07-27 23:01:28 +00003110
Anders Carlsson9bb83e82011-03-06 18:41:18 +00003111 WriteCXXBaseSpecifiersOffsets();
3112
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003113 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003114 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003115 RecordData FirstLatestDeclIDs;
3116 for (FirstLatestDeclMap::iterator
3117 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
3118 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
3119 "Expected first & second to be in different PCHs");
3120 AddDeclRef(I->first, FirstLatestDeclIDs);
3121 AddDeclRef(I->second, FirstLatestDeclIDs);
3122 }
3123 if (!FirstLatestDeclIDs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003124 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003125
Sebastian Redl98912122010-07-27 23:01:28 +00003126 // Write the record containing external, unnamed definitions.
3127 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003128 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00003129
3130 // Write the record containing tentative definitions.
3131 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003132 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00003133
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003134 // Write the record containing unused file scoped decls.
3135 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003136 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003137
Sebastian Redl08aca90252010-08-05 18:21:25 +00003138 // Write the record containing weak undeclared identifiers.
3139 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003140 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl08aca90252010-08-05 18:21:25 +00003141 WeakUndeclaredIdentifiers);
3142
Sebastian Redl98912122010-07-27 23:01:28 +00003143 // Write the record containing locally-scoped external definitions.
3144 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003145 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl98912122010-07-27 23:01:28 +00003146 LocallyScopedExternalDecls);
3147
3148 // Write the record containing ext_vector type names.
3149 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003150 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003151
Sebastian Redl08aca90252010-08-05 18:21:25 +00003152 // Write the record containing VTable uses information.
3153 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003154 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003155
3156 // Write the record containing dynamic classes declarations.
3157 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003158 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003159
3160 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003161 if (!PendingInstantiations.empty())
3162 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003163
3164 // Write the record containing declaration references of Sema.
3165 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003166 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003167
3168 // Write the delegating constructors.
3169 if (!DelegatingCtorDecls.empty())
3170 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003171
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003172 // Write the updates to DeclContexts.
3173 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3174 I = UpdatedDeclContexts.begin(),
3175 E = UpdatedDeclContexts.end();
Sebastian Redla4071b42010-08-24 00:50:09 +00003176 I != E; ++I)
3177 WriteDeclContextVisibleUpdate(*I);
3178
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003179 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003180
Sebastian Redl98912122010-07-27 23:01:28 +00003181 Record.clear();
3182 Record.push_back(NumStatements);
3183 Record.push_back(NumMacros);
3184 Record.push_back(NumLexicalDeclContexts);
3185 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003186 WriteDeclReplacementsBlock();
Sebastian Redl539c5062010-08-18 23:57:32 +00003187 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl143413f2010-07-12 22:02:52 +00003188 Stream.ExitBlock();
3189}
3190
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003191void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003192 if (DeclUpdates.empty())
3193 return;
3194
3195 RecordData OffsetsRecord;
3196 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
3197 for (DeclUpdateMap::iterator
3198 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3199 const Decl *D = I->first;
3200 UpdateRecord &URec = I->second;
3201
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00003202 if (DeclsToRewrite.count(D))
3203 continue; // The decl will be written completely,no need to store updates.
3204
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003205 uint64_t Offset = Stream.GetCurrentBitNo();
3206 Stream.EmitRecord(DECL_UPDATES, URec);
3207
3208 OffsetsRecord.push_back(GetDeclRef(D));
3209 OffsetsRecord.push_back(Offset);
3210 }
3211 Stream.ExitBlock();
3212 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3213}
3214
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003215void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003216 if (ReplacedDecls.empty())
3217 return;
3218
3219 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003220 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003221 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
3222 Record.push_back(I->first);
3223 Record.push_back(I->second);
3224 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003225 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003226}
3227
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003228void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003229 Record.push_back(Loc.getRawEncoding());
3230}
3231
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003232void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003233 AddSourceLocation(Range.getBegin(), Record);
3234 AddSourceLocation(Range.getEnd(), Record);
3235}
3236
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003237void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003238 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00003239 const uint64_t *Words = Value.getRawData();
3240 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003241}
3242
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003243void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00003244 Record.push_back(Value.isUnsigned());
3245 AddAPInt(Value, Record);
3246}
3247
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003248void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00003249 AddAPInt(Value.bitcastToAPInt(), Record);
3250}
3251
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003252void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003253 Record.push_back(getIdentifierRef(II));
3254}
3255
Sebastian Redl539c5062010-08-18 23:57:32 +00003256IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003257 if (II == 0)
3258 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003259
Sebastian Redl539c5062010-08-18 23:57:32 +00003260 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003261 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00003262 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003263 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003264}
3265
Sebastian Redl50e26582010-09-15 19:54:06 +00003266MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregoraae92242010-03-19 21:51:54 +00003267 if (MD == 0)
3268 return 0;
Sebastian Redl50e26582010-09-15 19:54:06 +00003269
3270 MacroID &ID = MacroDefinitions[MD];
Douglas Gregoraae92242010-03-19 21:51:54 +00003271 if (ID == 0)
Douglas Gregor91096292010-10-02 19:29:26 +00003272 ID = NextMacroID++;
Douglas Gregoraae92242010-03-19 21:51:54 +00003273 return ID;
3274}
3275
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003276void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003277 Record.push_back(getSelectorRef(SelRef));
3278}
3279
Sebastian Redl539c5062010-08-18 23:57:32 +00003280SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003281 if (Sel.getAsOpaquePtr() == 0) {
3282 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00003283 }
3284
Sebastian Redl539c5062010-08-18 23:57:32 +00003285 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00003286 if (SID == 0 && Chain) {
3287 // This might trigger a ReadSelector callback, which will set the ID for
3288 // this selector.
3289 Chain->LoadSelector(Sel);
3290 }
Steve Naroff2ddea052009-04-23 10:39:46 +00003291 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003292 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00003293 }
Sebastian Redl834bb972010-08-04 17:20:04 +00003294 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00003295}
3296
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003297void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00003298 AddDeclRef(Temp->getDestructor(), Record);
3299}
3300
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003301void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3302 CXXBaseSpecifier const *BasesEnd,
3303 RecordDataImpl &Record) {
3304 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3305 CXXBaseSpecifiersToWrite.push_back(
3306 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3307 Bases, BasesEnd));
3308 Record.push_back(NextCXXBaseSpecifiersID++);
3309}
3310
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003311void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003312 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003313 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003314 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00003315 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003316 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00003317 break;
3318 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003319 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00003320 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003321 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00003322 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003323 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003324 break;
3325 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00003326 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003327 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003328 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003329 break;
John McCall0ad16662009-10-29 08:12:44 +00003330 case TemplateArgument::Null:
3331 case TemplateArgument::Integral:
3332 case TemplateArgument::Declaration:
3333 case TemplateArgument::Pack:
3334 break;
3335 }
3336}
3337
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003338void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003339 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003340 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003341
3342 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3343 bool InfoHasSameExpr
3344 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3345 Record.push_back(InfoHasSameExpr);
3346 if (InfoHasSameExpr)
3347 return; // Avoid storing the same expr twice.
3348 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003349 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3350 Record);
3351}
3352
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003353void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3354 RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00003355 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00003356 AddTypeRef(QualType(), Record);
3357 return;
3358 }
3359
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003360 AddTypeLoc(TInfo->getTypeLoc(), Record);
3361}
3362
3363void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3364 AddTypeRef(TL.getType(), Record);
3365
John McCall8f115c62009-10-16 21:56:05 +00003366 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003367 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003368 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00003369}
3370
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003371void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00003372 Record.push_back(GetOrCreateTypeID(T));
3373}
3374
3375TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003376 return MakeTypeID(T,
3377 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3378}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003379
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003380TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003381 return MakeTypeID(T,
3382 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003383}
3384
3385TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3386 if (T.isNull())
3387 return TypeIdx();
3388 assert(!T.getLocalFastQualifiers());
3389
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00003390 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003391 if (Idx.getIndex() == 0) {
Douglas Gregor1970d882009-04-26 03:49:13 +00003392 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00003393 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003394 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003395 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00003396 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003397 return Idx;
3398}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003399
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003400TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003401 if (T.isNull())
3402 return TypeIdx();
3403 assert(!T.getLocalFastQualifiers());
3404
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003405 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3406 assert(I != TypeIdxs.end() && "Type not emitted!");
3407 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003408}
3409
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003410void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003411 Record.push_back(GetDeclRef(D));
3412}
3413
Sebastian Redl539c5062010-08-18 23:57:32 +00003414DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003415 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003416 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003417 }
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003418 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00003419 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00003420 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003421 // We haven't seen this declaration before. Give it a new ID and
3422 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00003423 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003424 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003425 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3426 // We don't add it to the replacement collection here, because we don't
3427 // have the offset yet.
3428 DeclTypesToEmit.push(const_cast<Decl *>(D));
3429 // Reset the flag, so that we don't add this decl multiple times.
3430 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003431 }
3432
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003433 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003434}
3435
Sebastian Redl539c5062010-08-18 23:57:32 +00003436DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00003437 if (D == 0)
3438 return 0;
3439
3440 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3441 return DeclIDs[D];
3442}
3443
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003444void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00003445 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003446 Record.push_back(Name.getNameKind());
3447 switch (Name.getNameKind()) {
3448 case DeclarationName::Identifier:
3449 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3450 break;
3451
3452 case DeclarationName::ObjCZeroArgSelector:
3453 case DeclarationName::ObjCOneArgSelector:
3454 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00003455 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003456 break;
3457
3458 case DeclarationName::CXXConstructorName:
3459 case DeclarationName::CXXDestructorName:
3460 case DeclarationName::CXXConversionFunctionName:
3461 AddTypeRef(Name.getCXXNameType(), Record);
3462 break;
3463
3464 case DeclarationName::CXXOperatorName:
3465 Record.push_back(Name.getCXXOverloadedOperator());
3466 break;
3467
Alexis Hunt3d221f22009-11-29 07:34:05 +00003468 case DeclarationName::CXXLiteralOperatorName:
3469 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3470 break;
3471
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003472 case DeclarationName::CXXUsingDirective:
3473 // No extra data to emit
3474 break;
3475 }
3476}
Chris Lattnerca025db2010-05-07 21:43:38 +00003477
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003478void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003479 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003480 switch (Name.getNameKind()) {
3481 case DeclarationName::CXXConstructorName:
3482 case DeclarationName::CXXDestructorName:
3483 case DeclarationName::CXXConversionFunctionName:
3484 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3485 break;
3486
3487 case DeclarationName::CXXOperatorName:
3488 AddSourceLocation(
3489 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3490 Record);
3491 AddSourceLocation(
3492 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3493 Record);
3494 break;
3495
3496 case DeclarationName::CXXLiteralOperatorName:
3497 AddSourceLocation(
3498 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3499 Record);
3500 break;
3501
3502 case DeclarationName::Identifier:
3503 case DeclarationName::ObjCZeroArgSelector:
3504 case DeclarationName::ObjCOneArgSelector:
3505 case DeclarationName::ObjCMultiArgSelector:
3506 case DeclarationName::CXXUsingDirective:
3507 break;
3508 }
3509}
3510
3511void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003512 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003513 AddDeclarationName(NameInfo.getName(), Record);
3514 AddSourceLocation(NameInfo.getLoc(), Record);
3515 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3516}
3517
3518void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003519 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00003520 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003521 Record.push_back(Info.NumTemplParamLists);
3522 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3523 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3524}
3525
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003526void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003527 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003528 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00003529 // typically accommodate the vast majority.
Chris Lattnerca025db2010-05-07 21:43:38 +00003530 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3531
3532 // Push each of the NNS's onto a stack for serialization in reverse order.
3533 while (NNS) {
3534 NestedNames.push_back(NNS);
3535 NNS = NNS->getPrefix();
3536 }
3537
3538 Record.push_back(NestedNames.size());
3539 while(!NestedNames.empty()) {
3540 NNS = NestedNames.pop_back_val();
3541 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3542 Record.push_back(Kind);
3543 switch (Kind) {
3544 case NestedNameSpecifier::Identifier:
3545 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3546 break;
3547
3548 case NestedNameSpecifier::Namespace:
3549 AddDeclRef(NNS->getAsNamespace(), Record);
3550 break;
3551
Douglas Gregor7b26ff92011-02-24 02:36:08 +00003552 case NestedNameSpecifier::NamespaceAlias:
3553 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
3554 break;
3555
Chris Lattnerca025db2010-05-07 21:43:38 +00003556 case NestedNameSpecifier::TypeSpec:
3557 case NestedNameSpecifier::TypeSpecWithTemplate:
3558 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3559 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3560 break;
3561
3562 case NestedNameSpecifier::Global:
3563 // Don't need to write an associated value.
3564 break;
3565 }
3566 }
3567}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003568
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003569void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
3570 RecordDataImpl &Record) {
3571 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00003572 // typically accommodate the vast majority.
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003573 llvm::SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
3574
3575 // Push each of the nested-name-specifiers's onto a stack for
3576 // serialization in reverse order.
3577 while (NNS) {
3578 NestedNames.push_back(NNS);
3579 NNS = NNS.getPrefix();
3580 }
3581
3582 Record.push_back(NestedNames.size());
3583 while(!NestedNames.empty()) {
3584 NNS = NestedNames.pop_back_val();
3585 NestedNameSpecifier::SpecifierKind Kind
3586 = NNS.getNestedNameSpecifier()->getKind();
3587 Record.push_back(Kind);
3588 switch (Kind) {
3589 case NestedNameSpecifier::Identifier:
3590 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
3591 AddSourceRange(NNS.getLocalSourceRange(), Record);
3592 break;
3593
3594 case NestedNameSpecifier::Namespace:
3595 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
3596 AddSourceRange(NNS.getLocalSourceRange(), Record);
3597 break;
3598
3599 case NestedNameSpecifier::NamespaceAlias:
3600 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
3601 AddSourceRange(NNS.getLocalSourceRange(), Record);
3602 break;
3603
3604 case NestedNameSpecifier::TypeSpec:
3605 case NestedNameSpecifier::TypeSpecWithTemplate:
3606 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3607 AddTypeLoc(NNS.getTypeLoc(), Record);
3608 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3609 break;
3610
3611 case NestedNameSpecifier::Global:
3612 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3613 break;
3614 }
3615 }
3616}
3617
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003618void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003619 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003620 Record.push_back(Kind);
3621 switch (Kind) {
3622 case TemplateName::Template:
3623 AddDeclRef(Name.getAsTemplateDecl(), Record);
3624 break;
3625
3626 case TemplateName::OverloadedTemplate: {
3627 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3628 Record.push_back(OvT->size());
3629 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3630 I != E; ++I)
3631 AddDeclRef(*I, Record);
3632 break;
3633 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003634
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003635 case TemplateName::QualifiedTemplate: {
3636 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3637 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3638 Record.push_back(QualT->hasTemplateKeyword());
3639 AddDeclRef(QualT->getTemplateDecl(), Record);
3640 break;
3641 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003642
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003643 case TemplateName::DependentTemplate: {
3644 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3645 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3646 Record.push_back(DepT->isIdentifier());
3647 if (DepT->isIdentifier())
3648 AddIdentifierRef(DepT->getIdentifier(), Record);
3649 else
3650 Record.push_back(DepT->getOperator());
3651 break;
3652 }
Douglas Gregor5590be02011-01-15 06:45:20 +00003653
3654 case TemplateName::SubstTemplateTemplateParmPack: {
3655 SubstTemplateTemplateParmPackStorage *SubstPack
3656 = Name.getAsSubstTemplateTemplateParmPack();
3657 AddDeclRef(SubstPack->getParameterPack(), Record);
3658 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
3659 break;
3660 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003661 }
3662}
3663
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003664void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003665 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003666 Record.push_back(Arg.getKind());
3667 switch (Arg.getKind()) {
3668 case TemplateArgument::Null:
3669 break;
3670 case TemplateArgument::Type:
3671 AddTypeRef(Arg.getAsType(), Record);
3672 break;
3673 case TemplateArgument::Declaration:
3674 AddDeclRef(Arg.getAsDecl(), Record);
3675 break;
3676 case TemplateArgument::Integral:
3677 AddAPSInt(*Arg.getAsIntegral(), Record);
3678 AddTypeRef(Arg.getIntegralType(), Record);
3679 break;
3680 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00003681 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3682 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003683 case TemplateArgument::TemplateExpansion:
3684 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregore1d60df2011-01-14 23:41:42 +00003685 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3686 Record.push_back(*NumExpansions + 1);
3687 else
3688 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003689 break;
3690 case TemplateArgument::Expression:
3691 AddStmt(Arg.getAsExpr());
3692 break;
3693 case TemplateArgument::Pack:
3694 Record.push_back(Arg.pack_size());
3695 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3696 I != E; ++I)
3697 AddTemplateArgument(*I, Record);
3698 break;
3699 }
3700}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003701
3702void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003703ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003704 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003705 assert(TemplateParams && "No TemplateParams!");
3706 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3707 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3708 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3709 Record.push_back(TemplateParams->size());
3710 for (TemplateParameterList::const_iterator
3711 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3712 P != PEnd; ++P)
3713 AddDeclRef(*P, Record);
3714}
3715
3716/// \brief Emit a template argument list.
3717void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003718ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003719 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003720 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003721 Record.push_back(TemplateArgs->size());
3722 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003723 AddTemplateArgument(TemplateArgs->get(i), Record);
3724}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003725
3726
3727void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003728ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003729 Record.push_back(Set.size());
3730 for (UnresolvedSetImpl::const_iterator
3731 I = Set.begin(), E = Set.end(); I != E; ++I) {
3732 AddDeclRef(I.getDecl(), Record);
3733 Record.push_back(I.getAccess());
3734 }
3735}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003736
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003737void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003738 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003739 Record.push_back(Base.isVirtual());
3740 Record.push_back(Base.isBaseOfClass());
3741 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00003742 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00003743 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003744 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00003745 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3746 : SourceLocation(),
3747 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003748}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003749
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003750void ASTWriter::FlushCXXBaseSpecifiers() {
3751 RecordData Record;
3752 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3753 Record.clear();
3754
3755 // Record the offset of this base-specifier set.
3756 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3757 if (Index == CXXBaseSpecifiersOffsets.size())
3758 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3759 else {
3760 if (Index > CXXBaseSpecifiersOffsets.size())
3761 CXXBaseSpecifiersOffsets.resize(Index + 1);
3762 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3763 }
3764
3765 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3766 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3767 Record.push_back(BEnd - B);
3768 for (; B != BEnd; ++B)
3769 AddCXXBaseSpecifier(*B, Record);
3770 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00003771
3772 // Flush any expressions that were written as part of the base specifiers.
3773 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003774 }
3775
3776 CXXBaseSpecifiersToWrite.clear();
3777}
3778
Alexis Hunt1d792652011-01-08 20:30:50 +00003779void ASTWriter::AddCXXCtorInitializers(
3780 const CXXCtorInitializer * const *CtorInitializers,
3781 unsigned NumCtorInitializers,
3782 RecordDataImpl &Record) {
3783 Record.push_back(NumCtorInitializers);
3784 for (unsigned i=0; i != NumCtorInitializers; ++i) {
3785 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003786
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003787 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00003788 Record.push_back(CTOR_INITIALIZER_BASE);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003789 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3790 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00003791 } else if (Init->isDelegatingInitializer()) {
3792 Record.push_back(CTOR_INITIALIZER_DELEGATING);
3793 AddDeclRef(Init->getTargetConstructor(), Record);
3794 } else if (Init->isMemberInitializer()){
3795 Record.push_back(CTOR_INITIALIZER_MEMBER);
3796 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003797 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00003798 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
3799 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003800 }
Francois Pichetd583da02010-12-04 09:14:42 +00003801
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003802 AddSourceLocation(Init->getMemberLocation(), Record);
3803 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003804 AddSourceLocation(Init->getLParenLoc(), Record);
3805 AddSourceLocation(Init->getRParenLoc(), Record);
3806 Record.push_back(Init->isWritten());
3807 if (Init->isWritten()) {
3808 Record.push_back(Init->getSourceOrder());
3809 } else {
3810 Record.push_back(Init->getNumArrayIndices());
3811 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3812 AddDeclRef(Init->getArrayIndex(i), Record);
3813 }
3814 }
3815}
3816
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003817void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3818 assert(D->DefinitionData);
3819 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3820 Record.push_back(Data.UserDeclaredConstructor);
3821 Record.push_back(Data.UserDeclaredCopyConstructor);
3822 Record.push_back(Data.UserDeclaredCopyAssignment);
3823 Record.push_back(Data.UserDeclaredDestructor);
3824 Record.push_back(Data.Aggregate);
3825 Record.push_back(Data.PlainOldData);
3826 Record.push_back(Data.Empty);
3827 Record.push_back(Data.Polymorphic);
3828 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00003829 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00003830 Record.push_back(Data.HasNoNonEmptyBases);
3831 Record.push_back(Data.HasPrivateFields);
3832 Record.push_back(Data.HasProtectedFields);
3833 Record.push_back(Data.HasPublicFields);
Douglas Gregor61226d32011-05-13 01:05:07 +00003834 Record.push_back(Data.HasMutableFields);
Alexis Huntf479f1b2011-05-09 18:22:59 +00003835 Record.push_back(Data.HasTrivialDefaultConstructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00003836 Record.push_back(Data.HasConstExprNonCopyMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003837 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruthad7d4042011-04-23 23:10:33 +00003838 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003839 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruthad7d4042011-04-23 23:10:33 +00003840 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003841 Record.push_back(Data.HasTrivialDestructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00003842 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003843 Record.push_back(Data.ComputedVisibleConversions);
Alexis Huntea6f0322011-05-11 22:34:38 +00003844 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003845 Record.push_back(Data.DeclaredDefaultConstructor);
3846 Record.push_back(Data.DeclaredCopyConstructor);
3847 Record.push_back(Data.DeclaredCopyAssignment);
3848 Record.push_back(Data.DeclaredDestructor);
3849
3850 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003851 if (Data.NumBases > 0)
3852 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3853 Record);
3854
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003855 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3856 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003857 if (Data.NumVBases > 0)
3858 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3859 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003860
3861 AddUnresolvedSet(Data.Conversions, Record);
3862 AddUnresolvedSet(Data.VisibleConversions, Record);
3863 // Data.Definition is the owning decl, no need to write it.
3864 AddDeclRef(Data.FirstFriend, Record);
3865}
3866
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003867void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00003868 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003869 assert(!Chain && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00003870 assert(FirstDeclID == NextDeclID &&
3871 FirstTypeID == NextTypeID &&
3872 FirstIdentID == NextIdentID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00003873 FirstSelectorID == NextSelectorID &&
Douglas Gregor91096292010-10-02 19:29:26 +00003874 FirstMacroID == NextMacroID &&
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003875 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00003876 "Setting chain after writing has started.");
3877 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003878
3879 FirstDeclID += Chain->getTotalNumDecls();
3880 FirstTypeID += Chain->getTotalNumTypes();
3881 FirstIdentID += Chain->getTotalNumIdentifiers();
3882 FirstSelectorID += Chain->getTotalNumSelectors();
3883 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003884 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003885 NextDeclID = FirstDeclID;
3886 NextTypeID = FirstTypeID;
3887 NextIdentID = FirstIdentID;
3888 NextSelectorID = FirstSelectorID;
3889 NextMacroID = FirstMacroID;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003890 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00003891}
3892
Sebastian Redl539c5062010-08-18 23:57:32 +00003893void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00003894 IdentifierIDs[II] = ID;
Douglas Gregor68051a72011-02-11 00:26:14 +00003895 if (II->hasMacroDefinition())
3896 DeserializedMacroNames.push_back(II);
Sebastian Redlff4a2952010-07-23 23:49:55 +00003897}
3898
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003899void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003900 // Always take the highest-numbered type index. This copes with an interesting
3901 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003902 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003903 // keep the higher-numbered entry so that we can properly write it out to
3904 // the AST file.
3905 TypeIdx &StoredIdx = TypeIdxs[T];
3906 if (Idx.getIndex() >= StoredIdx.getIndex())
3907 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003908}
3909
Sebastian Redl539c5062010-08-18 23:57:32 +00003910void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003911 DeclIDs[D] = ID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003912}
Sebastian Redl834bb972010-08-04 17:20:04 +00003913
Sebastian Redl539c5062010-08-18 23:57:32 +00003914void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003915 SelectorIDs[S] = ID;
3916}
Douglas Gregor91096292010-10-02 19:29:26 +00003917
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003918void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00003919 MacroDefinition *MD) {
3920 MacroDefinitions[MD] = ID;
3921}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003922
3923void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3924 assert(D->isDefinition());
3925 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3926 // We are interested when a PCH decl is modified.
3927 if (RD->getPCHLevel() > 0) {
3928 // A forward reference was mutated into a definition. Rewrite it.
3929 // FIXME: This happens during template instantiation, should we
3930 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003931 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003932 }
3933
3934 for (CXXRecordDecl::redecl_iterator
3935 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3936 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3937 if (Redecl == RD)
3938 continue;
3939
3940 // We are interested when a PCH decl is modified.
3941 if (Redecl->getPCHLevel() > 0) {
3942 UpdateRecord &Record = DeclUpdates[Redecl];
3943 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3944 assert(Redecl->DefinitionData);
3945 assert(Redecl->DefinitionData->Definition == D);
3946 AddDeclRef(D, Record); // the DefinitionDecl
3947 }
3948 }
3949 }
3950}
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003951void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3952 // TU and namespaces are handled elsewhere.
3953 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3954 return;
3955
3956 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3957 return; // Not a source decl added to a DeclContext from PCH.
3958
3959 AddUpdatedDeclContext(DC);
3960}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003961
3962void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3963 assert(D->isImplicit());
3964 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3965 return; // Not a source member added to a class from PCH.
3966 if (!isa<CXXMethodDecl>(D))
3967 return; // We are interested in lazily declared implicit methods.
3968
3969 // A decl coming from PCH was modified.
3970 assert(RD->isDefinition());
3971 UpdateRecord &Record = DeclUpdates[RD];
3972 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3973 AddDeclRef(D, Record);
3974}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003975
3976void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3977 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00003978 // The specializations set is kept in the canonical template.
3979 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003980 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3981 return; // Not a source specialization added to a template from PCH.
3982
3983 UpdateRecord &Record = DeclUpdates[TD];
3984 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3985 AddDeclRef(D, Record);
3986}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003987
Sebastian Redl9ab988f2011-04-14 14:07:59 +00003988void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
3989 const FunctionDecl *D) {
3990 // The specializations set is kept in the canonical template.
3991 TD = TD->getCanonicalDecl();
3992 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3993 return; // Not a source specialization added to a template from PCH.
3994
3995 UpdateRecord &Record = DeclUpdates[TD];
3996 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3997 AddDeclRef(D, Record);
3998}
3999
Sebastian Redlab238a72011-04-24 16:28:06 +00004000void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
4001 if (D->getPCHLevel() == 0)
4002 return; // Declaration not imported from PCH.
4003
4004 // Implicit decl from a PCH was defined.
4005 // FIXME: Should implicit definition be a separate FunctionDecl?
4006 RewriteDecl(D);
4007}
4008
Sebastian Redl2ac2c722011-04-29 08:19:30 +00004009void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
4010 if (D->getPCHLevel() == 0)
4011 return;
4012
4013 // Since the actual instantiation is delayed, this really means that we need
4014 // to update the instantiation location.
4015 UpdateRecord &Record = DeclUpdates[D];
4016 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4017 AddSourceLocation(
4018 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4019}
4020
Douglas Gregorf88e35b2010-11-30 06:16:57 +00004021ASTSerializationListener::~ASTSerializationListener() { }