blob: e9ad4a81b4dc8c046ba22f65706a23db6c730c59 [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 Gregore0a3a512009-04-14 21:55:33 +000040#include "llvm/ADT/APFloat.h"
41#include "llvm/ADT/APInt.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000042#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000043#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencer740857f2010-12-21 16:45:57 +000044#include "llvm/Support/FileSystem.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000045#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000046#include "llvm/Support/Path.h"
Chris Lattner225dd6c2009-04-11 18:40:46 +000047#include <cstdio>
Douglas Gregor09b69892011-02-10 17:09:37 +000048#include <string.h>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000049using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000050using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000051
Sebastian Redl3df5a082010-07-30 17:03:48 +000052template <typename T, typename Allocator>
53T *data(std::vector<T, Allocator> &v) {
54 return v.empty() ? 0 : &v.front();
55}
56template <typename T, typename Allocator>
57const T *data(const std::vector<T, Allocator> &v) {
58 return v.empty() ? 0 : &v.front();
59}
60
Douglas Gregoref84c4b2009-04-09 22:27:44 +000061//===----------------------------------------------------------------------===//
62// Type serialization
63//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000064
Douglas Gregoref84c4b2009-04-09 22:27:44 +000065namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000066 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000067 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000068 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000069
70 public:
71 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000072 TypeCode Code;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000073
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000074 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000075 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000076
77 void VisitArrayType(const ArrayType *T);
78 void VisitFunctionType(const FunctionType *T);
79 void VisitTagType(const TagType *T);
80
81#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
82#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000083#include "clang/AST/TypeNodes.def"
84 };
85}
86
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000087void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000088 assert(false && "Built-in types are never serialized");
89}
90
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000091void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000092 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000093 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000094}
95
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000096void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000097 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000098 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000099}
100
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000101void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000102 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000103 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000104}
105
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000106void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000107 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000108 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000109}
110
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000111void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000112 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000113 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000114}
115
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000116void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000117 Writer.AddTypeRef(T->getPointeeType(), Record);
118 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000119 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000120}
121
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000122void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000123 Writer.AddTypeRef(T->getElementType(), Record);
124 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000125 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000126}
127
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000128void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000129 VisitArrayType(T);
130 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000131 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000132}
133
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000134void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000135 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000136 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000137}
138
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000139void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000140 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000141 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
142 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000143 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000144 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000145}
146
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000147void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000148 Writer.AddTypeRef(T->getElementType(), Record);
149 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000150 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000151 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000152}
153
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000154void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000155 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000156 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000157}
158
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000159void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000160 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000161 FunctionType::ExtInfo C = T->getExtInfo();
162 Record.push_back(C.getNoReturn());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000163 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000164 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000165 Record.push_back(C.getCC());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000166}
167
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000168void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000169 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000170 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000171}
172
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000173void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000174 VisitFunctionType(T);
175 Record.push_back(T->getNumArgs());
176 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
177 Writer.AddTypeRef(T->getArgType(I), Record);
178 Record.push_back(T->isVariadic());
179 Record.push_back(T->getTypeQuals());
Douglas Gregordb9d6642011-01-26 05:01:58 +0000180 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redl5068f77ac2009-05-27 22:11:52 +0000181 Record.push_back(T->hasExceptionSpec());
182 Record.push_back(T->hasAnyExceptionSpec());
183 Record.push_back(T->getNumExceptions());
184 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
185 Writer.AddTypeRef(T->getExceptionType(I), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000186 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000187}
188
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000189void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000190 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000191 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000192}
John McCallb96ec562009-12-04 22:46:56 +0000193
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000194void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000195 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000196 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
197 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000198 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000199}
200
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000201void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000202 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000203 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000204}
205
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000206void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000207 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000208 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000209}
210
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000211void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson81df7b82009-06-24 19:06:50 +0000212 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000213 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000214}
215
Richard Smith30482bc2011-02-20 03:19:35 +0000216void ASTTypeWriter::VisitAutoType(const AutoType *T) {
217 Writer.AddTypeRef(T->getDeducedType(), Record);
218 Code = TYPE_AUTO;
219}
220
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000221void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000222 Record.push_back(T->isDependentType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000223 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000224 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000225 "Cannot serialize in the middle of a type definition");
226}
227
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000228void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000229 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000230 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000231}
232
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000233void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000234 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000235 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000236}
237
John McCall81904512011-01-06 01:58:22 +0000238void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
239 Writer.AddTypeRef(T->getModifiedType(), Record);
240 Writer.AddTypeRef(T->getEquivalentType(), Record);
241 Record.push_back(T->getAttrKind());
242 Code = TYPE_ATTRIBUTED;
243}
244
Mike Stump11289f42009-09-09 15:08:12 +0000245void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000246ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000247 const SubstTemplateTypeParmType *T) {
248 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
249 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000250 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000251}
252
253void
Douglas Gregorada4b792011-01-14 02:55:32 +0000254ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
255 const SubstTemplateTypeParmPackType *T) {
256 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
257 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
258 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
259}
260
261void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000262ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000263 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000264 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000265 Writer.AddTemplateName(T->getTemplateName(), Record);
266 Record.push_back(T->getNumArgs());
267 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
268 ArgI != ArgE; ++ArgI)
269 Writer.AddTemplateArgument(*ArgI, Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000270 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
271 : T->getCanonicalTypeInternal(),
272 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000273 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000274}
275
276void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000277ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000278 VisitArrayType(T);
279 Writer.AddStmt(T->getSizeExpr());
280 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000281 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000282}
283
284void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000285ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000286 const DependentSizedExtVectorType *T) {
287 // FIXME: Serialize this type (C++ only)
288 assert(false && "Cannot serialize dependent sized extended vector types");
289}
290
291void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000292ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000293 Record.push_back(T->getDepth());
294 Record.push_back(T->getIndex());
295 Record.push_back(T->isParameterPack());
296 Writer.AddIdentifierRef(T->getName(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000297 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000298}
299
300void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000301ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000302 Record.push_back(T->getKeyword());
303 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
304 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000305 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
306 : T->getCanonicalTypeInternal(),
307 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000308 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000309}
310
311void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000312ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000313 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000314 Record.push_back(T->getKeyword());
315 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
316 Writer.AddIdentifierRef(T->getIdentifier(), Record);
317 Record.push_back(T->getNumArgs());
318 for (DependentTemplateSpecializationType::iterator
319 I = T->begin(), E = T->end(); I != E; ++I)
320 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000321 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000322}
323
Douglas Gregord2fa7662010-12-20 02:24:11 +0000324void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
325 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000326 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
327 Record.push_back(*NumExpansions + 1);
328 else
329 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000330 Code = TYPE_PACK_EXPANSION;
331}
332
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000333void ASTTypeWriter::VisitParenType(const ParenType *T) {
334 Writer.AddTypeRef(T->getInnerType(), Record);
335 Code = TYPE_PAREN;
336}
337
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000338void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000339 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000340 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
341 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000342 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000343}
344
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000345void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCalle78aac42010-03-10 03:28:59 +0000346 Writer.AddDeclRef(T->getDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000347 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000348 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000349}
350
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000351void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1c283312010-08-11 12:19:30 +0000352 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000353 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000354}
355
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000356void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000357 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000358 Record.push_back(T->getNumProtocols());
John McCall8b07ec22010-05-15 11:32:37 +0000359 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000360 E = T->qual_end(); I != E; ++I)
361 Writer.AddDeclRef(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000362 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000363}
364
Steve Narofffb4330f2009-06-17 22:40:22 +0000365void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000366ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000367 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000368 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000369}
370
John McCall8f115c62009-10-16 21:56:05 +0000371namespace {
372
373class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000374 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000375 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000376
377public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000378 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000379 : Writer(Writer), Record(Record) { }
380
John McCall17001972009-10-18 01:05:36 +0000381#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000382#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000383 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000384#include "clang/AST/TypeLocNodes.def"
385
John McCall17001972009-10-18 01:05:36 +0000386 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
387 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000388};
389
390}
391
John McCall17001972009-10-18 01:05:36 +0000392void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
393 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000394}
John McCall17001972009-10-18 01:05:36 +0000395void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000396 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
397 if (TL.needsExtraLocalData()) {
398 Record.push_back(TL.getWrittenTypeSpec());
399 Record.push_back(TL.getWrittenSignSpec());
400 Record.push_back(TL.getWrittenWidthSpec());
401 Record.push_back(TL.hasModeAttr());
402 }
John McCall8f115c62009-10-16 21:56:05 +0000403}
John McCall17001972009-10-18 01:05:36 +0000404void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
405 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000406}
John McCall17001972009-10-18 01:05:36 +0000407void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
408 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000409}
John McCall17001972009-10-18 01:05:36 +0000410void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
411 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000412}
John McCall17001972009-10-18 01:05:36 +0000413void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
414 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000415}
John McCall17001972009-10-18 01:05:36 +0000416void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
417 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000418}
John McCall17001972009-10-18 01:05:36 +0000419void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
420 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000421}
John McCall17001972009-10-18 01:05:36 +0000422void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
423 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
424 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
425 Record.push_back(TL.getSizeExpr() ? 1 : 0);
426 if (TL.getSizeExpr())
427 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000428}
John McCall17001972009-10-18 01:05:36 +0000429void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
430 VisitArrayTypeLoc(TL);
431}
432void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
433 VisitArrayTypeLoc(TL);
434}
435void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
436 VisitArrayTypeLoc(TL);
437}
438void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
439 DependentSizedArrayTypeLoc TL) {
440 VisitArrayTypeLoc(TL);
441}
442void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
443 DependentSizedExtVectorTypeLoc TL) {
444 Writer.AddSourceLocation(TL.getNameLoc(), Record);
445}
446void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
447 Writer.AddSourceLocation(TL.getNameLoc(), Record);
448}
449void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
450 Writer.AddSourceLocation(TL.getNameLoc(), Record);
451}
452void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
453 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
454 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Douglas Gregor7fb25412010-10-01 18:44:50 +0000455 Record.push_back(TL.getTrailingReturn());
John McCall17001972009-10-18 01:05:36 +0000456 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
457 Writer.AddDeclRef(TL.getArg(i), Record);
458}
459void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
460 VisitFunctionTypeLoc(TL);
461}
462void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
463 VisitFunctionTypeLoc(TL);
464}
John McCallb96ec562009-12-04 22:46:56 +0000465void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
466 Writer.AddSourceLocation(TL.getNameLoc(), Record);
467}
John McCall17001972009-10-18 01:05:36 +0000468void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
469 Writer.AddSourceLocation(TL.getNameLoc(), Record);
470}
471void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000472 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
473 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
474 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000475}
476void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000477 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
478 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
479 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
480 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000481}
482void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
483 Writer.AddSourceLocation(TL.getNameLoc(), Record);
484}
Richard Smith30482bc2011-02-20 03:19:35 +0000485void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
486 Writer.AddSourceLocation(TL.getNameLoc(), Record);
487}
John McCall17001972009-10-18 01:05:36 +0000488void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
489 Writer.AddSourceLocation(TL.getNameLoc(), Record);
490}
491void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
492 Writer.AddSourceLocation(TL.getNameLoc(), Record);
493}
John McCall81904512011-01-06 01:58:22 +0000494void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
495 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
496 if (TL.hasAttrOperand()) {
497 SourceRange range = TL.getAttrOperandParensRange();
498 Writer.AddSourceLocation(range.getBegin(), Record);
499 Writer.AddSourceLocation(range.getEnd(), Record);
500 }
501 if (TL.hasAttrExprOperand()) {
502 Expr *operand = TL.getAttrExprOperand();
503 Record.push_back(operand ? 1 : 0);
504 if (operand) Writer.AddStmt(operand);
505 } else if (TL.hasAttrEnumOperand()) {
506 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
507 }
508}
John McCall17001972009-10-18 01:05:36 +0000509void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
510 Writer.AddSourceLocation(TL.getNameLoc(), Record);
511}
John McCallcebee162009-10-18 09:09:24 +0000512void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
513 SubstTemplateTypeParmTypeLoc TL) {
514 Writer.AddSourceLocation(TL.getNameLoc(), Record);
515}
Douglas Gregorada4b792011-01-14 02:55:32 +0000516void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
517 SubstTemplateTypeParmPackTypeLoc TL) {
518 Writer.AddSourceLocation(TL.getNameLoc(), Record);
519}
John McCall17001972009-10-18 01:05:36 +0000520void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
521 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +0000522 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
523 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
524 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
525 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000526 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
527 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000528}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000529void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
530 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
531 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
532}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000533void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000534 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000535 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000536}
John McCalle78aac42010-03-10 03:28:59 +0000537void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
538 Writer.AddSourceLocation(TL.getNameLoc(), Record);
539}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000540void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000541 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000542 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000543 Writer.AddSourceLocation(TL.getNameLoc(), Record);
544}
John McCallc392f372010-06-11 00:33:02 +0000545void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
546 DependentTemplateSpecializationTypeLoc TL) {
547 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
548 Writer.AddSourceRange(TL.getQualifierRange(), Record);
549 Writer.AddSourceLocation(TL.getNameLoc(), Record);
550 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
551 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
552 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000553 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
554 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000555}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000556void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
557 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
558}
John McCall17001972009-10-18 01:05:36 +0000559void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
560 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000561}
562void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
563 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000564 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
565 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
566 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
567 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000568}
John McCallfc93cf92009-10-22 22:37:11 +0000569void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
570 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000571}
John McCall8f115c62009-10-16 21:56:05 +0000572
Chris Lattner19cea4e2009-04-22 05:57:30 +0000573//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000574// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000575//===----------------------------------------------------------------------===//
576
Chris Lattner28fa4e62009-04-26 22:26:21 +0000577static void EmitBlockID(unsigned ID, const char *Name,
578 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000579 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000580 Record.clear();
581 Record.push_back(ID);
582 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
583
584 // Emit the block name if present.
585 if (Name == 0 || Name[0] == 0) return;
586 Record.clear();
587 while (*Name)
588 Record.push_back(*Name++);
589 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
590}
591
592static void EmitRecordID(unsigned ID, const char *Name,
593 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000594 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000595 Record.clear();
596 Record.push_back(ID);
597 while (*Name)
598 Record.push_back(*Name++);
599 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000600}
601
602static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000603 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000604#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000605 RECORD(STMT_STOP);
606 RECORD(STMT_NULL_PTR);
607 RECORD(STMT_NULL);
608 RECORD(STMT_COMPOUND);
609 RECORD(STMT_CASE);
610 RECORD(STMT_DEFAULT);
611 RECORD(STMT_LABEL);
612 RECORD(STMT_IF);
613 RECORD(STMT_SWITCH);
614 RECORD(STMT_WHILE);
615 RECORD(STMT_DO);
616 RECORD(STMT_FOR);
617 RECORD(STMT_GOTO);
618 RECORD(STMT_INDIRECT_GOTO);
619 RECORD(STMT_CONTINUE);
620 RECORD(STMT_BREAK);
621 RECORD(STMT_RETURN);
622 RECORD(STMT_DECL);
623 RECORD(STMT_ASM);
624 RECORD(EXPR_PREDEFINED);
625 RECORD(EXPR_DECL_REF);
626 RECORD(EXPR_INTEGER_LITERAL);
627 RECORD(EXPR_FLOATING_LITERAL);
628 RECORD(EXPR_IMAGINARY_LITERAL);
629 RECORD(EXPR_STRING_LITERAL);
630 RECORD(EXPR_CHARACTER_LITERAL);
631 RECORD(EXPR_PAREN);
632 RECORD(EXPR_UNARY_OPERATOR);
633 RECORD(EXPR_SIZEOF_ALIGN_OF);
634 RECORD(EXPR_ARRAY_SUBSCRIPT);
635 RECORD(EXPR_CALL);
636 RECORD(EXPR_MEMBER);
637 RECORD(EXPR_BINARY_OPERATOR);
638 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
639 RECORD(EXPR_CONDITIONAL_OPERATOR);
640 RECORD(EXPR_IMPLICIT_CAST);
641 RECORD(EXPR_CSTYLE_CAST);
642 RECORD(EXPR_COMPOUND_LITERAL);
643 RECORD(EXPR_EXT_VECTOR_ELEMENT);
644 RECORD(EXPR_INIT_LIST);
645 RECORD(EXPR_DESIGNATED_INIT);
646 RECORD(EXPR_IMPLICIT_VALUE_INIT);
647 RECORD(EXPR_VA_ARG);
648 RECORD(EXPR_ADDR_LABEL);
649 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000650 RECORD(EXPR_CHOOSE);
651 RECORD(EXPR_GNU_NULL);
652 RECORD(EXPR_SHUFFLE_VECTOR);
653 RECORD(EXPR_BLOCK);
654 RECORD(EXPR_BLOCK_DECL_REF);
655 RECORD(EXPR_OBJC_STRING_LITERAL);
656 RECORD(EXPR_OBJC_ENCODE);
657 RECORD(EXPR_OBJC_SELECTOR_EXPR);
658 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
659 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
660 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
661 RECORD(EXPR_OBJC_KVC_REF_EXPR);
662 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000663 RECORD(STMT_OBJC_FOR_COLLECTION);
664 RECORD(STMT_OBJC_CATCH);
665 RECORD(STMT_OBJC_FINALLY);
666 RECORD(STMT_OBJC_AT_TRY);
667 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
668 RECORD(STMT_OBJC_AT_THROW);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000669 RECORD(EXPR_CXX_OPERATOR_CALL);
670 RECORD(EXPR_CXX_CONSTRUCT);
671 RECORD(EXPR_CXX_STATIC_CAST);
672 RECORD(EXPR_CXX_DYNAMIC_CAST);
673 RECORD(EXPR_CXX_REINTERPRET_CAST);
674 RECORD(EXPR_CXX_CONST_CAST);
675 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
676 RECORD(EXPR_CXX_BOOL_LITERAL);
677 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000678 RECORD(EXPR_CXX_TYPEID_EXPR);
679 RECORD(EXPR_CXX_TYPEID_TYPE);
680 RECORD(EXPR_CXX_UUIDOF_EXPR);
681 RECORD(EXPR_CXX_UUIDOF_TYPE);
682 RECORD(EXPR_CXX_THIS);
683 RECORD(EXPR_CXX_THROW);
684 RECORD(EXPR_CXX_DEFAULT_ARG);
685 RECORD(EXPR_CXX_BIND_TEMPORARY);
686 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
687 RECORD(EXPR_CXX_NEW);
688 RECORD(EXPR_CXX_DELETE);
689 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
690 RECORD(EXPR_EXPR_WITH_CLEANUPS);
691 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
692 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
693 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
694 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
695 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
696 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
697 RECORD(EXPR_CXX_NOEXCEPT);
698 RECORD(EXPR_OPAQUE_VALUE);
699 RECORD(EXPR_BINARY_TYPE_TRAIT);
700 RECORD(EXPR_PACK_EXPANSION);
701 RECORD(EXPR_SIZEOF_PACK);
702 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000703 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000704#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000705}
Mike Stump11289f42009-09-09 15:08:12 +0000706
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000707void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000708 RecordData Record;
709 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000710
Sebastian Redl539c5062010-08-18 23:57:32 +0000711#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
712#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000713
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000714 // AST Top-Level Block.
Sebastian Redlf1642042010-08-18 23:57:22 +0000715 BLOCK(AST_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000716 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000717 RECORD(TYPE_OFFSET);
718 RECORD(DECL_OFFSET);
719 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000720 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000721 RECORD(IDENTIFIER_OFFSET);
722 RECORD(IDENTIFIER_TABLE);
723 RECORD(EXTERNAL_DEFINITIONS);
724 RECORD(SPECIAL_TYPES);
725 RECORD(STATISTICS);
726 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000727 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000728 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
729 RECORD(SELECTOR_OFFSETS);
730 RECORD(METHOD_POOL);
731 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000732 RECORD(SOURCE_LOCATION_OFFSETS);
733 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000734 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000735 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek17437132010-01-22 20:59:36 +0000736 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregoraae92242010-03-19 21:51:54 +0000737 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redl595c5132010-07-08 22:01:51 +0000738 RECORD(CHAINED_METADATA);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000739 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000740 RECORD(TU_UPDATE_LEXICAL);
741 RECORD(REDECLS_UPDATE_LATEST);
742 RECORD(SEMA_DECL_REFS);
743 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
744 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
745 RECORD(DECL_REPLACEMENTS);
746 RECORD(UPDATE_VISIBLE);
747 RECORD(DECL_UPDATE_OFFSETS);
748 RECORD(DECL_UPDATES);
749 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
750 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000751 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000752 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000753 RECORD(FP_PRAGMA_OPTIONS);
754 RECORD(OPENCL_EXTENSIONS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000755
Chris Lattner28fa4e62009-04-26 22:26:21 +0000756 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000757 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000758 RECORD(SM_SLOC_FILE_ENTRY);
759 RECORD(SM_SLOC_BUFFER_ENTRY);
760 RECORD(SM_SLOC_BUFFER_BLOB);
761 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
762 RECORD(SM_LINE_TABLE);
Mike Stump11289f42009-09-09 15:08:12 +0000763
Chris Lattner28fa4e62009-04-26 22:26:21 +0000764 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000765 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000766 RECORD(PP_MACRO_OBJECT_LIKE);
767 RECORD(PP_MACRO_FUNCTION_LIKE);
768 RECORD(PP_TOKEN);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000769
Douglas Gregor12bfa382009-10-17 00:13:19 +0000770 // Decls and Types block.
771 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000772 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000773 RECORD(TYPE_COMPLEX);
774 RECORD(TYPE_POINTER);
775 RECORD(TYPE_BLOCK_POINTER);
776 RECORD(TYPE_LVALUE_REFERENCE);
777 RECORD(TYPE_RVALUE_REFERENCE);
778 RECORD(TYPE_MEMBER_POINTER);
779 RECORD(TYPE_CONSTANT_ARRAY);
780 RECORD(TYPE_INCOMPLETE_ARRAY);
781 RECORD(TYPE_VARIABLE_ARRAY);
782 RECORD(TYPE_VECTOR);
783 RECORD(TYPE_EXT_VECTOR);
784 RECORD(TYPE_FUNCTION_PROTO);
785 RECORD(TYPE_FUNCTION_NO_PROTO);
786 RECORD(TYPE_TYPEDEF);
787 RECORD(TYPE_TYPEOF_EXPR);
788 RECORD(TYPE_TYPEOF);
789 RECORD(TYPE_RECORD);
790 RECORD(TYPE_ENUM);
791 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000792 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000793 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000794 RECORD(TYPE_DECLTYPE);
795 RECORD(TYPE_ELABORATED);
796 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
797 RECORD(TYPE_UNRESOLVED_USING);
798 RECORD(TYPE_INJECTED_CLASS_NAME);
799 RECORD(TYPE_OBJC_OBJECT);
800 RECORD(TYPE_TEMPLATE_TYPE_PARM);
801 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
802 RECORD(TYPE_DEPENDENT_NAME);
803 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
804 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
805 RECORD(TYPE_PAREN);
806 RECORD(TYPE_PACK_EXPANSION);
807 RECORD(TYPE_ATTRIBUTED);
808 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000809 RECORD(DECL_TRANSLATION_UNIT);
810 RECORD(DECL_TYPEDEF);
811 RECORD(DECL_ENUM);
812 RECORD(DECL_RECORD);
813 RECORD(DECL_ENUM_CONSTANT);
814 RECORD(DECL_FUNCTION);
815 RECORD(DECL_OBJC_METHOD);
816 RECORD(DECL_OBJC_INTERFACE);
817 RECORD(DECL_OBJC_PROTOCOL);
818 RECORD(DECL_OBJC_IVAR);
819 RECORD(DECL_OBJC_AT_DEFS_FIELD);
820 RECORD(DECL_OBJC_CLASS);
821 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
822 RECORD(DECL_OBJC_CATEGORY);
823 RECORD(DECL_OBJC_CATEGORY_IMPL);
824 RECORD(DECL_OBJC_IMPLEMENTATION);
825 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
826 RECORD(DECL_OBJC_PROPERTY);
827 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000828 RECORD(DECL_FIELD);
829 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000830 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000831 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000832 RECORD(DECL_FILE_SCOPE_ASM);
833 RECORD(DECL_BLOCK);
834 RECORD(DECL_CONTEXT_LEXICAL);
835 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000836 RECORD(DECL_NAMESPACE);
837 RECORD(DECL_NAMESPACE_ALIAS);
838 RECORD(DECL_USING);
839 RECORD(DECL_USING_SHADOW);
840 RECORD(DECL_USING_DIRECTIVE);
841 RECORD(DECL_UNRESOLVED_USING_VALUE);
842 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
843 RECORD(DECL_LINKAGE_SPEC);
844 RECORD(DECL_CXX_RECORD);
845 RECORD(DECL_CXX_METHOD);
846 RECORD(DECL_CXX_CONSTRUCTOR);
847 RECORD(DECL_CXX_DESTRUCTOR);
848 RECORD(DECL_CXX_CONVERSION);
849 RECORD(DECL_ACCESS_SPEC);
850 RECORD(DECL_FRIEND);
851 RECORD(DECL_FRIEND_TEMPLATE);
852 RECORD(DECL_CLASS_TEMPLATE);
853 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
854 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
855 RECORD(DECL_FUNCTION_TEMPLATE);
856 RECORD(DECL_TEMPLATE_TYPE_PARM);
857 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
858 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
859 RECORD(DECL_STATIC_ASSERT);
860 RECORD(DECL_CXX_BASE_SPECIFIERS);
861 RECORD(DECL_INDIRECTFIELD);
862 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
863
Douglas Gregor92a96f52011-02-08 21:58:10 +0000864 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
865 RECORD(PPD_MACRO_INSTANTIATION);
866 RECORD(PPD_MACRO_DEFINITION);
867 RECORD(PPD_INCLUSION_DIRECTIVE);
868
Douglas Gregor12bfa382009-10-17 00:13:19 +0000869 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000870 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000871#undef RECORD
872#undef BLOCK
873 Stream.ExitBlock();
874}
875
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000876/// \brief Adjusts the given filename to only write out the portion of the
877/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000878///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000879/// \param Filename the file name to adjust.
880///
881/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
882/// the returned filename will be adjusted by this system root.
883///
884/// \returns either the original filename (if it needs no adjustment) or the
885/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000886static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000887adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
888 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000889
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000890 if (!isysroot)
891 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000892
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000893 // Verify that the filename and the system root have the same prefix.
894 unsigned Pos = 0;
895 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
896 if (Filename[Pos] != isysroot[Pos])
897 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000898
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000899 // We hit the end of the filename before we hit the end of the system root.
900 if (!Filename[Pos])
901 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000902
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000903 // If the file name has a '/' at the current position, skip over the '/'.
904 // We distinguish sysroot-based includes from absolute includes by the
905 // absence of '/' at the beginning of sysroot-based includes.
906 if (Filename[Pos] == '/')
907 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000908
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000909 return Filename + Pos;
910}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000911
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000912/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000913void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot,
914 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000915 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000916
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000917 // Metadata
918 const TargetInfo &Target = Context.Target;
919 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000920 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl539c5062010-08-18 23:57:32 +0000921 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000922 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
923 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000924 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
925 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
926 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000927 // Target triple or chained PCH name
928 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000929 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000930
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000931 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000932 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
933 Record.push_back(VERSION_MAJOR);
934 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000935 Record.push_back(CLANG_VERSION_MAJOR);
936 Record.push_back(CLANG_VERSION_MINOR);
937 Record.push_back(isysroot != 0);
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000938 // FIXME: This writes the absolute path for chained headers.
939 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
940 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump11289f42009-09-09 15:08:12 +0000941
Douglas Gregor45fe0362009-05-12 01:31:05 +0000942 // Original file name
943 SourceManager &SM = Context.getSourceManager();
944 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
945 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000946 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregor45fe0362009-05-12 01:31:05 +0000947 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
948 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
949
Michael J. Spencer740857f2010-12-21 16:45:57 +0000950 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +0000951
Michael J. Spencer740857f2010-12-21 16:45:57 +0000952 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000953
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +0000954 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000955 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000956 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000957 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000958 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000959 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000960 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000961
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000962 // Original PCH directory
963 if (!OutputFile.empty() && OutputFile != "-") {
964 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
965 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
966 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
967 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
968
969 llvm::SmallString<128> OutputPath(OutputFile);
970
971 llvm::sys::fs::make_absolute(OutputPath);
972 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
973
974 RecordData Record;
975 Record.push_back(ORIGINAL_PCH_DIR);
976 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
977 }
978
Ted Kremenek18e066f2010-01-22 22:12:47 +0000979 // Repository branch/version information.
980 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000981 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenek18e066f2010-01-22 22:12:47 +0000982 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
983 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000984 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +0000985 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +0000986 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
987 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000988}
989
990/// \brief Write the LangOptions structure.
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000991void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor55abb232009-04-10 20:39:37 +0000992 RecordData Record;
993 Record.push_back(LangOpts.Trigraphs);
994 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
995 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
996 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
997 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carruthe03aa552010-04-17 20:17:31 +0000998 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor55abb232009-04-10 20:39:37 +0000999 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1000 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1001 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1002 Record.push_back(LangOpts.C99); // C99 Support
1003 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +00001004 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
1005 // already saved elsewhere.
Douglas Gregor55abb232009-04-10 20:39:37 +00001006 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1007 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +00001008 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +00001009
Douglas Gregor55abb232009-04-10 20:39:37 +00001010 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1011 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001012 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian45878032010-02-09 19:31:38 +00001013 // modern abi enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001014 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian45878032010-02-09 19:31:38 +00001015 // modern abi enabled.
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +00001016 Record.push_back(LangOpts.AppleKext); // Apple's kernel extensions ABI
Ted Kremenek1d56c9e2010-12-23 21:35:43 +00001017 Record.push_back(LangOpts.ObjCDefaultSynthProperties); // Objective-C auto-synthesized
1018 // properties enabled.
Fariborz Jahanian62c56022010-04-22 21:01:59 +00001019 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump11289f42009-09-09 15:08:12 +00001020
Douglas Gregor55abb232009-04-10 20:39:37 +00001021 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +00001022 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1023 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00001024 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +00001025 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Anders Carlssonce8dd3a2011-02-19 23:53:54 +00001026 Record.push_back(LangOpts.ObjCExceptions);
Anders Carlsson6bbd2682011-02-23 03:04:54 +00001027 Record.push_back(LangOpts.CXXExceptions);
1028 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor55abb232009-04-10 20:39:37 +00001029
Douglas Gregordbe39272011-02-01 15:15:22 +00001030 Record.push_back(LangOpts.MSBitfields); // MS-compatible structure layout
Douglas Gregor55abb232009-04-10 20:39:37 +00001031 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1032 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1033 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1034
Chris Lattner258172e2009-04-27 07:35:58 +00001035 // Whether static initializers are protected by locks.
1036 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00001037 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +00001038 Record.push_back(LangOpts.Blocks); // block extension to C
1039 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1040 // they are unused.
1041 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1042 // (modulo the platform support).
1043
Chris Lattner51924e512010-06-26 21:25:03 +00001044 Record.push_back(LangOpts.getSignedOverflowBehavior());
1045 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor55abb232009-04-10 20:39:37 +00001046
1047 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +00001048 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +00001049 // defined.
1050 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1051 // opposed to __DYNAMIC__).
1052 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1053
1054 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1055 // used (instead of C99 semantics).
1056 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +00001057 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
1058 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +00001059 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
1060 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +00001061 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Argyrios Kyrtzidisa88942a2011-01-15 02:56:16 +00001062 Record.push_back(LangOpts.ShortEnums); // Should the enum type be equivalent
1063 // to the smallest integer type with
1064 // enough room.
Douglas Gregor55abb232009-04-10 20:39:37 +00001065 Record.push_back(LangOpts.getGCMode());
1066 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +00001067 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +00001068 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00001069 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +00001070 Record.push_back(LangOpts.CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +00001071 Record.push_back(LangOpts.CatchUndefined);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001072 Record.push_back(LangOpts.DefaultFPContract);
Anders Carlsson9cedbef2009-08-22 22:30:33 +00001073 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +00001074 Record.push_back(LangOpts.SpellChecking);
Roman Divacky65b88cd2011-03-01 17:40:53 +00001075 Record.push_back(LangOpts.MRTD);
Sebastian Redl539c5062010-08-18 23:57:32 +00001076 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +00001077}
1078
Douglas Gregora7f71a92009-04-10 03:52:48 +00001079//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00001080// stat cache Serialization
1081//===----------------------------------------------------------------------===//
1082
1083namespace {
1084// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001085class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001086public:
1087 typedef const char * key_type;
1088 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001089
Chris Lattner2a6fa472010-11-23 19:28:12 +00001090 typedef struct stat data_type;
1091 typedef const data_type &data_type_ref;
Douglas Gregorc5046832009-04-27 18:38:38 +00001092
1093 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001094 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001095 }
Mike Stump11289f42009-09-09 15:08:12 +00001096
1097 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +00001098 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1099 data_type_ref Data) {
1100 unsigned StrLen = strlen(path);
1101 clang::io::Emit16(Out, StrLen);
Chris Lattner2a6fa472010-11-23 19:28:12 +00001102 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregorc5046832009-04-27 18:38:38 +00001103 clang::io::Emit8(Out, DataLen);
1104 return std::make_pair(StrLen + 1, DataLen);
1105 }
Mike Stump11289f42009-09-09 15:08:12 +00001106
Douglas Gregorc5046832009-04-27 18:38:38 +00001107 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1108 Out.write(path, KeyLen);
1109 }
Mike Stump11289f42009-09-09 15:08:12 +00001110
Chris Lattner2a6fa472010-11-23 19:28:12 +00001111 void EmitData(llvm::raw_ostream &Out, key_type_ref,
Douglas Gregorc5046832009-04-27 18:38:38 +00001112 data_type_ref Data, unsigned DataLen) {
1113 using namespace clang::io;
1114 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +00001115
Chris Lattner2a6fa472010-11-23 19:28:12 +00001116 Emit32(Out, (uint32_t) Data.st_ino);
1117 Emit32(Out, (uint32_t) Data.st_dev);
1118 Emit16(Out, (uint16_t) Data.st_mode);
1119 Emit64(Out, (uint64_t) Data.st_mtime);
1120 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregorc5046832009-04-27 18:38:38 +00001121
1122 assert(Out.tell() - Start == DataLen && "Wrong data length");
1123 }
1124};
1125} // end anonymous namespace
1126
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001127/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001128void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001129 // Build the on-disk hash table containing information about every
1130 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001131 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +00001132 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001133 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +00001134 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001135 Stat != StatEnd; ++Stat, ++NumStatEntries) {
1136 const char *Filename = Stat->first();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001137 Generator.insert(Filename, Stat->second);
1138 }
Mike Stump11289f42009-09-09 15:08:12 +00001139
Douglas Gregorc5046832009-04-27 18:38:38 +00001140 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001141 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +00001142 uint32_t BucketOffset;
1143 {
1144 llvm::raw_svector_ostream Out(StatCacheData);
1145 // Make sure that no bucket is at offset 0
1146 clang::io::Emit32(Out, 0);
1147 BucketOffset = Generator.Emit(Out);
1148 }
1149
1150 // Create a blob abbreviation
1151 using namespace llvm;
1152 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001153 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +00001154 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1155 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1156 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1157 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1158
1159 // Write the stat cache
1160 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001161 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +00001162 Record.push_back(BucketOffset);
1163 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001164 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +00001165}
1166
1167//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +00001168// Source Manager Serialization
1169//===----------------------------------------------------------------------===//
1170
1171/// \brief Create an abbreviation for the SLocEntry that refers to a
1172/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001173static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001174 using namespace llvm;
1175 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001176 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001177 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1178 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1179 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1180 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001181 // FileEntry fields.
1182 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1183 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora7f71a92009-04-10 03:52:48 +00001184 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001185 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001186}
1187
1188/// \brief Create an abbreviation for the SLocEntry that refers to a
1189/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001190static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001191 using namespace llvm;
1192 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001193 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001194 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1195 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1196 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1197 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001199 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001200}
1201
1202/// \brief Create an abbreviation for the SLocEntry that refers to a
1203/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001204static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001205 using namespace llvm;
1206 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001207 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001208 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001209 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001210}
1211
1212/// \brief Create an abbreviation for the SLocEntry that refers to an
1213/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001214static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001215 using namespace llvm;
1216 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001217 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001218 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1219 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1220 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1221 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001222 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001223 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001224}
1225
Douglas Gregor09b69892011-02-10 17:09:37 +00001226namespace {
1227 // Trait used for the on-disk hash table of header search information.
1228 class HeaderFileInfoTrait {
1229 ASTWriter &Writer;
1230 HeaderSearch &HS;
1231
1232 public:
1233 HeaderFileInfoTrait(ASTWriter &Writer, HeaderSearch &HS)
1234 : Writer(Writer), HS(HS) { }
1235
1236 typedef const char *key_type;
1237 typedef key_type key_type_ref;
1238
1239 typedef HeaderFileInfo data_type;
1240 typedef const data_type &data_type_ref;
1241
1242 static unsigned ComputeHash(const char *path) {
1243 // The hash is based only on the filename portion of the key, so that the
1244 // reader can match based on filenames when symlinking or excess path
1245 // elements ("foo/../", "../") change the form of the name. However,
1246 // complete path is still the key.
1247 return llvm::HashString(llvm::sys::path::filename(path));
1248 }
1249
1250 std::pair<unsigned,unsigned>
1251 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1252 data_type_ref Data) {
1253 unsigned StrLen = strlen(path);
1254 clang::io::Emit16(Out, StrLen);
1255 unsigned DataLen = 1 + 2 + 4;
1256 clang::io::Emit8(Out, DataLen);
1257 return std::make_pair(StrLen + 1, DataLen);
1258 }
1259
1260 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1261 Out.write(path, KeyLen);
1262 }
1263
1264 void EmitData(llvm::raw_ostream &Out, key_type_ref,
1265 data_type_ref Data, unsigned DataLen) {
1266 using namespace clang::io;
1267 uint64_t Start = Out.tell(); (void)Start;
1268
1269 unsigned char Flags = (Data.isImport << 3)
1270 | (Data.DirInfo << 1)
1271 | Data.Resolved;
1272 Emit8(Out, (uint8_t)Flags);
1273 Emit16(Out, (uint16_t) Data.NumIncludes);
1274
1275 if (!Data.ControllingMacro)
1276 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1277 else
1278 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
1279 assert(Out.tell() - Start == DataLen && "Wrong data length");
1280 }
1281 };
1282} // end anonymous namespace
1283
1284/// \brief Write the header search block for the list of files that
1285///
1286/// \param HS The header search structure to save.
1287///
1288/// \param Chain Whether we're creating a chained AST file.
1289void ASTWriter::WriteHeaderSearch(HeaderSearch &HS, const char* isysroot) {
1290 llvm::SmallVector<const FileEntry *, 16> FilesByUID;
1291 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1292
1293 if (FilesByUID.size() > HS.header_file_size())
1294 FilesByUID.resize(HS.header_file_size());
1295
1296 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1297 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
1298 llvm::SmallVector<const char *, 4> SavedStrings;
1299 unsigned NumHeaderSearchEntries = 0;
1300 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1301 const FileEntry *File = FilesByUID[UID];
1302 if (!File)
1303 continue;
1304
1305 const HeaderFileInfo &HFI = HS.header_file_begin()[UID];
1306 if (HFI.External && Chain)
1307 continue;
1308
1309 // Turn the file name into an absolute path, if it isn't already.
1310 const char *Filename = File->getName();
1311 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1312
1313 // If we performed any translation on the file name at all, we need to
1314 // save this string, since the generator will refer to it later.
1315 if (Filename != File->getName()) {
1316 Filename = strdup(Filename);
1317 SavedStrings.push_back(Filename);
1318 }
1319
1320 Generator.insert(Filename, HFI, GeneratorTrait);
1321 ++NumHeaderSearchEntries;
1322 }
1323
1324 // Create the on-disk hash table in a buffer.
1325 llvm::SmallString<4096> TableData;
1326 uint32_t BucketOffset;
1327 {
1328 llvm::raw_svector_ostream Out(TableData);
1329 // Make sure that no bucket is at offset 0
1330 clang::io::Emit32(Out, 0);
1331 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1332 }
1333
1334 // Create a blob abbreviation
1335 using namespace llvm;
1336 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1337 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1338 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1339 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1340 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1341 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1342
1343 // Write the stat cache
1344 RecordData Record;
1345 Record.push_back(HEADER_SEARCH_TABLE);
1346 Record.push_back(BucketOffset);
1347 Record.push_back(NumHeaderSearchEntries);
1348 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1349
1350 // Free all of the strings we had to duplicate.
1351 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1352 free((void*)SavedStrings[I]);
1353}
1354
Douglas Gregora7f71a92009-04-10 03:52:48 +00001355/// \brief Writes the block containing the serialized form of the
1356/// source manager.
1357///
1358/// TODO: We should probably use an on-disk hash table (stored in a
1359/// blob), indexed based on the file name, so that we only create
1360/// entries for files that we actually need. In the common case (no
1361/// errors), we probably won't have to create file entries for any of
1362/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001363void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001364 const Preprocessor &PP,
1365 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001366 RecordData Record;
1367
Chris Lattner0910e3b2009-04-10 17:16:57 +00001368 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001369 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001370
1371 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001372 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1373 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1374 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1375 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001376
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001377 // Write the line table.
1378 if (SourceMgr.hasLineTable()) {
1379 LineTableInfo &LineTable = SourceMgr.getLineTable();
1380
1381 // Emit the file names
1382 Record.push_back(LineTable.getNumFilenames());
1383 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1384 // Emit the file name
1385 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001386 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001387 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1388 Record.push_back(FilenameLen);
1389 if (FilenameLen)
1390 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1391 }
Mike Stump11289f42009-09-09 15:08:12 +00001392
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001393 // Emit the line entries
1394 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1395 L != LEnd; ++L) {
1396 // Emit the file ID
1397 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +00001398
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001399 // Emit the line entries
1400 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +00001401 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001402 LEEnd = L->second.end();
1403 LE != LEEnd; ++LE) {
1404 Record.push_back(LE->FileOffset);
1405 Record.push_back(LE->LineNo);
1406 Record.push_back(LE->FilenameID);
1407 Record.push_back((unsigned)LE->FileKind);
1408 Record.push_back(LE->IncludeOffset);
1409 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001410 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001411 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001412 }
1413
Douglas Gregor258ae542009-04-27 06:38:32 +00001414 // Write out the source location entry table. We skip the first
1415 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001416 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001417 RecordData PreloadSLocs;
Sebastian Redl5c415f32010-07-22 17:01:13 +00001418 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1419 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1420 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1421 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001422 // Get this source location entry.
1423 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001424
Douglas Gregor258ae542009-04-27 06:38:32 +00001425 // Record the offset of this source-location entry.
1426 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1427
1428 // Figure out which record code to use.
1429 unsigned Code;
1430 if (SLoc->isFile()) {
1431 if (SLoc->getFile().getContentCache()->Entry)
Sebastian Redl539c5062010-08-18 23:57:32 +00001432 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001433 else
Sebastian Redl539c5062010-08-18 23:57:32 +00001434 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001435 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001436 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001437 Record.clear();
1438 Record.push_back(Code);
1439
1440 Record.push_back(SLoc->getOffset());
1441 if (SLoc->isFile()) {
1442 const SrcMgr::FileInfo &File = SLoc->getFile();
1443 Record.push_back(File.getIncludeLoc().getRawEncoding());
1444 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1445 Record.push_back(File.hasLineDirectives());
1446
1447 const SrcMgr::ContentCache *Content = File.getContentCache();
1448 if (Content->Entry) {
1449 // The source location entry is a file. The blob associated
1450 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001451
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001452 // Emit size/modification time for this file.
1453 Record.push_back(Content->Entry->getSize());
1454 Record.push_back(Content->Entry->getModificationTime());
1455
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001456 // Turn the file name into an absolute path, if it isn't already.
1457 const char *Filename = Content->Entry->getName();
Michael J. Spencer740857f2010-12-21 16:45:57 +00001458 llvm::SmallString<128> FilePath(Filename);
1459 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001460 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001461
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001462 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001463 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001464 } else {
1465 // The source location entry is a buffer. The blob associated
1466 // with this entry contains the contents of the buffer.
1467
1468 // We add one to the size so that we capture the trailing NULL
1469 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1470 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001471 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001472 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001473 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001474 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1475 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001476 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001477 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001478 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001479 llvm::StringRef(Buffer->getBufferStart(),
1480 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001481
1482 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl5c415f32010-07-22 17:01:13 +00001483 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001484 }
1485 } else {
1486 // The source location entry is an instantiation.
1487 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1488 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1489 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1490 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1491
1492 // Compute the token length for this macro expansion.
1493 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001494 if (I + 1 != N)
1495 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001496 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1497 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1498 }
1499 }
1500
Douglas Gregor8f45df52009-04-16 22:23:12 +00001501 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001502
1503 if (SLocEntryOffsets.empty())
1504 return;
1505
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001506 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001507 // table is used for lazily loading source-location information.
1508 using namespace llvm;
1509 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001510 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001511 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1512 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1513 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1514 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001515
Douglas Gregor258ae542009-04-27 06:38:32 +00001516 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001517 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001518 Record.push_back(SLocEntryOffsets.size());
Sebastian Redlc1d035f2010-09-22 20:19:08 +00001519 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1520 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Douglas Gregor258ae542009-04-27 06:38:32 +00001521 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001522 (const char *)data(SLocEntryOffsets),
Chris Lattner12d61d32009-04-27 19:01:47 +00001523 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001524
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001525 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001526 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001527 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001528}
1529
Douglas Gregorc5046832009-04-27 18:38:38 +00001530//===----------------------------------------------------------------------===//
1531// Preprocessor Serialization
1532//===----------------------------------------------------------------------===//
1533
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001534static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1535 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1536 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1537 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1538 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1539 return X.first->getName().compare(Y.first->getName());
1540}
1541
Chris Lattnereeffaef2009-04-10 17:15:23 +00001542/// \brief Writes the block containing the serialized form of the
1543/// preprocessor.
1544///
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001545void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001546 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001547
Chris Lattner0af3ba12009-04-13 01:29:17 +00001548 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1549 if (PP.getCounterValue() != 0) {
1550 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001551 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001552 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001553 }
1554
1555 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001556 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001557
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001558 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001559 // FIXME: use diagnostics subsystem for localization etc.
1560 if (PP.SawDateOrTime())
1561 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001562
Douglas Gregor796d76a2010-10-20 22:00:55 +00001563
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001564 // Loop over all the macro definitions that are live at the end of the file,
1565 // emitting each to the PP section.
Douglas Gregoraae92242010-03-19 21:51:54 +00001566 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001567
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001568 // Construct the list of macro definitions that need to be serialized.
1569 llvm::SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
1570 MacrosToEmit;
1571 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor68051a72011-02-11 00:26:14 +00001572 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1573 E = PP.macro_end(Chain == 0);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001574 I != E; ++I) {
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001575 MacroDefinitionsSeen.insert(I->first);
1576 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1577 }
1578
1579 // Sort the set of macro definitions that need to be serialized by the
1580 // name of the macro, to provide a stable ordering.
1581 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1582 &compareMacroDefinitions);
1583
Douglas Gregor68051a72011-02-11 00:26:14 +00001584 // Resolve any identifiers that defined macros at the time they were
1585 // deserialized, adding them to the list of macros to emit (if appropriate).
1586 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1587 IdentifierInfo *Name
1588 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1589 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1590 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1591 }
1592
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001593 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1594 const IdentifierInfo *Name = MacrosToEmit[I].first;
1595 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor68051a72011-02-11 00:26:14 +00001596 if (!MI)
1597 continue;
1598
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001599 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001600 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001601 // Also skip macros from a AST file if we're chaining.
Douglas Gregoreb114da2010-10-01 01:03:07 +00001602
1603 // FIXME: There is a (probably minor) optimization we could do here, if
1604 // the macro comes from the original PCH but the identifier comes from a
1605 // chained PCH, by storing the offset into the original PCH rather than
1606 // writing the macro definition a second time.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001607 if (MI->isBuiltinMacro() ||
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001608 (Chain && Name->isFromAST() && MI->isFromAST()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001609 continue;
1610
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001611 AddIdentifierRef(Name, Record);
1612 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001613 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1614 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001615
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001616 unsigned Code;
1617 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001618 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001619 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001620 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001621
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001622 Record.push_back(MI->isC99Varargs());
1623 Record.push_back(MI->isGNUVarargs());
1624 Record.push_back(MI->getNumArgs());
1625 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1626 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001627 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001628 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001629
Douglas Gregoraae92242010-03-19 21:51:54 +00001630 // If we have a detailed preprocessing record, record the macro definition
1631 // ID that corresponds to this macro.
1632 if (PPRec)
1633 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001634
Douglas Gregor8f45df52009-04-16 22:23:12 +00001635 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001636 Record.clear();
1637
Chris Lattner2199f5b2009-04-10 18:08:30 +00001638 // Emit the tokens array.
1639 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1640 // Note that we know that the preprocessor does not have any annotation
1641 // tokens in it because they are created by the parser, and thus can't be
1642 // in a macro definition.
1643 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001644
Chris Lattner2199f5b2009-04-10 18:08:30 +00001645 Record.push_back(Tok.getLocation().getRawEncoding());
1646 Record.push_back(Tok.getLength());
1647
Chris Lattner2199f5b2009-04-10 18:08:30 +00001648 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1649 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001650 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001651 // FIXME: Should translate token kind to a stable encoding.
1652 Record.push_back(Tok.getKind());
1653 // FIXME: Should translate token flags to a stable encoding.
1654 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001655
Sebastian Redl539c5062010-08-18 23:57:32 +00001656 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001657 Record.clear();
1658 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001659 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001660 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001661 Stream.ExitBlock();
1662
1663 if (PPRec)
1664 WritePreprocessorDetail(*PPRec);
1665}
1666
1667void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
1668 if (PPRec.begin(Chain) == PPRec.end(Chain))
1669 return;
1670
1671 // Enter the preprocessor block.
1672 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001673
Douglas Gregoraae92242010-03-19 21:51:54 +00001674 // If the preprocessor has a preprocessing record, emit it.
1675 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001676 using namespace llvm;
1677
1678 // Set up the abbreviation for
1679 unsigned InclusionAbbrev = 0;
1680 {
1681 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1682 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
1683 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1684 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1685 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1686 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1687 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1688 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1689 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1690 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1691 }
1692
1693 unsigned IndexBase = Chain ? PPRec.getNumPreallocatedEntities() : 0;
1694 RecordData Record;
1695 for (PreprocessingRecord::iterator E = PPRec.begin(Chain),
1696 EEnd = PPRec.end(Chain);
1697 E != EEnd; ++E) {
1698 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001699
Douglas Gregor92a96f52011-02-08 21:58:10 +00001700 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1701 // Record this macro definition's location.
1702 MacroID ID = getMacroDefinitionID(MD);
1703
1704 // Don't write the macro definition if it is from another AST file.
1705 if (ID < FirstMacroID)
Douglas Gregoraae92242010-03-19 21:51:54 +00001706 continue;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001707
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001708 // Notify the serialization listener that we're serializing this entity.
1709 if (SerializationListener)
1710 SerializationListener->SerializedPreprocessedEntity(*E,
Douglas Gregor92a96f52011-02-08 21:58:10 +00001711 Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001712
Douglas Gregor92a96f52011-02-08 21:58:10 +00001713 unsigned Position = ID - FirstMacroID;
1714 if (Position != MacroDefinitionOffsets.size()) {
1715 if (Position > MacroDefinitionOffsets.size())
1716 MacroDefinitionOffsets.resize(Position + 1);
1717
1718 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
1719 } else
1720 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001721
Douglas Gregor92a96f52011-02-08 21:58:10 +00001722 Record.push_back(IndexBase + NumPreprocessingRecords++);
1723 Record.push_back(ID);
1724 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1725 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1726 AddIdentifierRef(MD->getName(), Record);
1727 AddSourceLocation(MD->getLocation(), Record);
1728 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1729 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00001730 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001731
Douglas Gregor92a96f52011-02-08 21:58:10 +00001732 // Notify the serialization listener that we're serializing this entity.
1733 if (SerializationListener)
1734 SerializationListener->SerializedPreprocessedEntity(*E,
1735 Stream.GetCurrentBitNo());
1736
1737 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1738 Record.push_back(IndexBase + NumPreprocessingRecords++);
1739 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1740 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1741 AddIdentifierRef(MI->getName(), Record);
1742 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1743 Stream.EmitRecord(PPD_MACRO_INSTANTIATION, Record);
1744 continue;
1745 }
1746
1747 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1748 Record.push_back(PPD_INCLUSION_DIRECTIVE);
1749 Record.push_back(IndexBase + NumPreprocessingRecords++);
1750 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1751 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1752 Record.push_back(ID->getFileName().size());
1753 Record.push_back(ID->wasInQuotes());
1754 Record.push_back(static_cast<unsigned>(ID->getKind()));
1755 llvm::SmallString<64> Buffer;
1756 Buffer += ID->getFileName();
1757 Buffer += ID->getFile()->getName();
1758 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1759 continue;
1760 }
1761
1762 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1763 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001764 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001765
Douglas Gregoraae92242010-03-19 21:51:54 +00001766 // Write the offsets table for the preprocessing record.
1767 if (NumPreprocessingRecords > 0) {
1768 // Write the offsets table for identifier IDs.
1769 using namespace llvm;
1770 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001771 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregoraae92242010-03-19 21:51:54 +00001772 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1773 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1774 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1775 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001776
Douglas Gregoraae92242010-03-19 21:51:54 +00001777 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001778 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001779 Record.push_back(NumPreprocessingRecords);
1780 Record.push_back(MacroDefinitionOffsets.size());
1781 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001782 (const char *)data(MacroDefinitionOffsets),
Douglas Gregoraae92242010-03-19 21:51:54 +00001783 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1784 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001785}
1786
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001787void ASTWriter::WritePragmaDiagnosticMappings(const Diagnostic &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001788 RecordData Record;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001789 for (Diagnostic::DiagStatePointsTy::const_iterator
1790 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1791 I != E; ++I) {
1792 const Diagnostic::DiagStatePoint &point = *I;
1793 if (point.Loc.isInvalid())
1794 continue;
1795
1796 Record.push_back(point.Loc.getRawEncoding());
1797 for (Diagnostic::DiagState::iterator
1798 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
1799 unsigned diag = I->first, map = I->second;
1800 if (map & 0x10) { // mapping from a diagnostic pragma.
1801 Record.push_back(diag);
1802 Record.push_back(map & 0x7);
1803 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001804 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001805 Record.push_back(-1); // mark the end of the diag/map pairs for this
1806 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001807 }
1808
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00001809 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001810 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001811}
1812
Douglas Gregorc5046832009-04-27 18:38:38 +00001813//===----------------------------------------------------------------------===//
1814// Type Serialization
1815//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001816
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001817/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001818void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00001819 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001820 if (Idx.getIndex() == 0) // we haven't seen this type before.
1821 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00001822
Douglas Gregor9b3932c2010-10-05 18:37:06 +00001823 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00001824
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001825 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001826 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001827 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001828 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001829 else if (TypeOffsets.size() < Index) {
1830 TypeOffsets.resize(Index + 1);
1831 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001832 }
1833
1834 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001835
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001836 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001837 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001838
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001839 if (T.hasLocalNonFastQualifiers()) {
1840 Qualifiers Qs = T.getLocalQualifiers();
1841 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001842 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001843 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00001844 } else {
1845 switch (T->getTypeClass()) {
1846 // For all of the concrete, non-dependent types, call the
1847 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001848#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001849 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001850#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001851#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001852 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001853 }
1854
1855 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001856 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001857
1858 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001859 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001860}
1861
Douglas Gregorc5046832009-04-27 18:38:38 +00001862//===----------------------------------------------------------------------===//
1863// Declaration Serialization
1864//===----------------------------------------------------------------------===//
1865
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001866/// \brief Write the block containing all of the declaration IDs
1867/// lexically declared within the given DeclContext.
1868///
1869/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1870/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001871uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001872 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001873 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001874 return 0;
1875
Douglas Gregor8f45df52009-04-16 22:23:12 +00001876 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001877 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001878 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001879 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001880 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1881 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001882 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001883
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001884 ++NumLexicalDeclContexts;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001885 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001886 reinterpret_cast<char*>(Decls.data()),
1887 Decls.size() * sizeof(KindDeclIDPair));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001888 return Offset;
1889}
1890
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001891void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001892 using namespace llvm;
1893 RecordData Record;
1894
1895 // Write the type offsets array
1896 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001897 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001898 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1899 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1900 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1901 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001902 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001903 Record.push_back(TypeOffsets.size());
1904 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001905 (const char *)data(TypeOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001906 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1907
1908 // Write the declaration offsets array
1909 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001910 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001911 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1912 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1913 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1914 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001915 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001916 Record.push_back(DeclOffsets.size());
1917 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001918 (const char *)data(DeclOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001919 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1920}
1921
Douglas Gregorc5046832009-04-27 18:38:38 +00001922//===----------------------------------------------------------------------===//
1923// Global Method Pool and Selector Serialization
1924//===----------------------------------------------------------------------===//
1925
Douglas Gregore84a9da2009-04-20 20:36:09 +00001926namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001927// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001928class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001929 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001930
1931public:
1932 typedef Selector key_type;
1933 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001934
Sebastian Redl834bb972010-08-04 17:20:04 +00001935 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00001936 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00001937 ObjCMethodList Instance, Factory;
1938 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00001939 typedef const data_type& data_type_ref;
1940
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001941 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001942
Douglas Gregorc78d3462009-04-24 21:10:55 +00001943 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00001944 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001945 }
Mike Stump11289f42009-09-09 15:08:12 +00001946
1947 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001948 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1949 data_type_ref Methods) {
1950 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1951 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00001952 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1953 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001954 Method = Method->Next)
1955 if (Method->Method)
1956 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00001957 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001958 Method = Method->Next)
1959 if (Method->Method)
1960 DataLen += 4;
1961 clang::io::Emit16(Out, DataLen);
1962 return std::make_pair(KeyLen, DataLen);
1963 }
Mike Stump11289f42009-09-09 15:08:12 +00001964
Douglas Gregor95c13f52009-04-25 17:48:32 +00001965 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001966 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001967 assert((Start >> 32) == 0 && "Selector key offset too large");
1968 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001969 unsigned N = Sel.getNumArgs();
1970 clang::io::Emit16(Out, N);
1971 if (N == 0)
1972 N = 1;
1973 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001974 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001975 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1976 }
Mike Stump11289f42009-09-09 15:08:12 +00001977
Douglas Gregorc78d3462009-04-24 21:10:55 +00001978 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001979 data_type_ref Methods, unsigned DataLen) {
1980 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00001981 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001982 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001983 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001984 Method = Method->Next)
1985 if (Method->Method)
1986 ++NumInstanceMethods;
1987
1988 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001989 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001990 Method = Method->Next)
1991 if (Method->Method)
1992 ++NumFactoryMethods;
1993
1994 clang::io::Emit16(Out, NumInstanceMethods);
1995 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00001996 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001997 Method = Method->Next)
1998 if (Method->Method)
1999 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002000 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002001 Method = Method->Next)
2002 if (Method->Method)
2003 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002004
2005 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002006 }
2007};
2008} // end anonymous namespace
2009
Sebastian Redla19a67f2010-08-03 21:58:15 +00002010/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002011///
2012/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002013/// in an on-disk hash table indexed by the selector. The hash table also
2014/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002015void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002016 using namespace llvm;
2017
Sebastian Redla19a67f2010-08-03 21:58:15 +00002018 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002019 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002020 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002021 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002022 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002023 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002024 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002025 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002026
Sebastian Redla19a67f2010-08-03 21:58:15 +00002027 // Create the on-disk hash table representation. We walk through every
2028 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002029 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002030 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002031 I = SelectorIDs.begin(), E = SelectorIDs.end();
2032 I != E; ++I) {
2033 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002034 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002035 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002036 I->second,
2037 ObjCMethodList(),
2038 ObjCMethodList()
2039 };
2040 if (F != SemaRef.MethodPool.end()) {
2041 Data.Instance = F->second.first;
2042 Data.Factory = F->second.second;
2043 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002044 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002045 // changed.
2046 if (Chain && I->second < FirstSelectorID) {
2047 // Selector already exists. Did it change?
2048 bool changed = false;
2049 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2050 M = M->Next) {
2051 if (M->Method->getPCHLevel() == 0)
2052 changed = true;
2053 }
2054 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2055 M = M->Next) {
2056 if (M->Method->getPCHLevel() == 0)
2057 changed = true;
2058 }
2059 if (!changed)
2060 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002061 } else if (Data.Instance.Method || Data.Factory.Method) {
2062 // A new method pool entry.
2063 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002064 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002065 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002066 }
2067
Douglas Gregorc78d3462009-04-24 21:10:55 +00002068 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002069 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002070 uint32_t BucketOffset;
2071 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002072 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002073 llvm::raw_svector_ostream Out(MethodPool);
2074 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002075 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002076 BucketOffset = Generator.Emit(Out, Trait);
2077 }
2078
2079 // Create a blob abbreviation
2080 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002081 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002082 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002083 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002084 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2085 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2086
Douglas Gregor95c13f52009-04-25 17:48:32 +00002087 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002088 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002089 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002090 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002091 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002092 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002093
2094 // Create a blob abbreviation for the selector table offsets.
2095 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002096 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002097 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor95c13f52009-04-25 17:48:32 +00002098 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2099 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2100
2101 // Write the selector offsets table.
2102 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002103 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002104 Record.push_back(SelectorOffsets.size());
2105 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00002106 (const char *)data(SelectorOffsets),
Douglas Gregor95c13f52009-04-25 17:48:32 +00002107 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002108 }
2109}
2110
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002111/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002112void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002113 using namespace llvm;
2114 if (SemaRef.ReferencedSelectors.empty())
2115 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002116
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002117 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002118
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002119 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002120 // very tricky to fix, and given that @selector shouldn't really appear in
2121 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002122 for (DenseMap<Selector, SourceLocation>::iterator S =
2123 SemaRef.ReferencedSelectors.begin(),
2124 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2125 Selector Sel = (*S).first;
2126 SourceLocation Loc = (*S).second;
2127 AddSelectorRef(Sel, Record);
2128 AddSourceLocation(Loc, Record);
2129 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002130 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002131}
2132
Douglas Gregorc5046832009-04-27 18:38:38 +00002133//===----------------------------------------------------------------------===//
2134// Identifier Table Serialization
2135//===----------------------------------------------------------------------===//
2136
Douglas Gregorc78d3462009-04-24 21:10:55 +00002137namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002138class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002139 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002140 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002141
Douglas Gregor1d583f22009-04-28 21:18:29 +00002142 /// \brief Determines whether this is an "interesting" identifier
2143 /// that needs a full IdentifierInfo structure written into the hash
2144 /// table.
2145 static bool isInterestingIdentifier(const IdentifierInfo *II) {
2146 return II->isPoisoned() ||
2147 II->isExtensionToken() ||
2148 II->hasMacroDefinition() ||
2149 II->getObjCOrBuiltinID() ||
2150 II->getFETokenInfo<void>();
2151 }
2152
Douglas Gregore84a9da2009-04-20 20:36:09 +00002153public:
2154 typedef const IdentifierInfo* key_type;
2155 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002156
Sebastian Redl539c5062010-08-18 23:57:32 +00002157 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002158 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002159
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002160 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00002161 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00002162
2163 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00002164 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00002165 }
Mike Stump11289f42009-09-09 15:08:12 +00002166
2167 std::pair<unsigned,unsigned>
2168 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002169 IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002170 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002171 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
2172 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00002173 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00002174 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00002175 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00002176 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002177 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2178 DEnd = IdentifierResolver::end();
2179 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00002180 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00002181 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00002182 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00002183 // We emit the key length after the data length so that every
2184 // string is preceded by a 16-bit length. This matches the PTH
2185 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00002186 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002187 return std::make_pair(KeyLen, DataLen);
2188 }
Mike Stump11289f42009-09-09 15:08:12 +00002189
2190 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00002191 unsigned KeyLen) {
2192 // Record the location of the key data. This is used when generating
2193 // the mapping from persistent IDs to strings.
2194 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002195 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002196 }
Mike Stump11289f42009-09-09 15:08:12 +00002197
2198 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002199 IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00002200 if (!isInterestingIdentifier(II)) {
2201 clang::io::Emit32(Out, ID << 1);
2202 return;
2203 }
Douglas Gregorb9256522009-04-28 21:32:13 +00002204
Douglas Gregor1d583f22009-04-28 21:18:29 +00002205 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002206 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002207 bool hasMacroDefinition =
2208 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00002209 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00002210 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002211 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
2212 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2213 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00002214 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002215 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00002216 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002217
Douglas Gregorc3366a52009-04-21 23:56:24 +00002218 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00002219 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00002220
Douglas Gregora868bbd2009-04-21 22:25:48 +00002221 // Emit the declaration IDs in reverse order, because the
2222 // IdentifierResolver provides the declarations as they would be
2223 // visible (e.g., the function "stat" would come before the struct
2224 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2225 // adds declarations to the end of the list (so we need to see the
2226 // struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00002227 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump11289f42009-09-09 15:08:12 +00002228 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00002229 IdentifierResolver::end());
2230 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
2231 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00002232 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00002233 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002234 }
2235};
2236} // end anonymous namespace
2237
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002238/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002239///
2240/// The identifier table consists of a blob containing string data
2241/// (the actual identifiers themselves) and a separate "offsets" index
2242/// that maps identifier IDs to locations within the blob.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002243void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002244 using namespace llvm;
2245
2246 // Create and write out the blob that contains the identifier
2247 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002248 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002249 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002250 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump11289f42009-09-09 15:08:12 +00002251
Douglas Gregore6648fb2009-04-28 20:33:11 +00002252 // Look for any identifiers that were named while processing the
2253 // headers, but are otherwise not needed. We add these to the hash
2254 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002255 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00002256 // file.
2257 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2258 IDEnd = PP.getIdentifierTable().end();
2259 ID != IDEnd; ++ID)
2260 getIdentifierRef(ID->second);
2261
Sebastian Redlff4a2952010-07-23 23:49:55 +00002262 // Create the on-disk hash table representation. We only store offsets
2263 // for identifiers that appear here for the first time.
2264 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002265 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002266 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2267 ID != IDEnd; ++ID) {
2268 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002269 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002270 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002271 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002272
Douglas Gregore84a9da2009-04-20 20:36:09 +00002273 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002274 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00002275 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002276 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002277 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002278 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002279 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002280 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002281 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002282 }
2283
2284 // Create a blob abbreviation
2285 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002286 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00002287 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002288 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00002289 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002290
2291 // Write the identifier table
2292 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002293 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002294 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002295 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002296 }
2297
2298 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00002299 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002300 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00002301 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2302 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2303 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2304
2305 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002306 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00002307 Record.push_back(IdentifierOffsets.size());
2308 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00002309 (const char *)data(IdentifierOffsets),
Douglas Gregor0e149972009-04-25 19:10:14 +00002310 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002311}
2312
Douglas Gregorc5046832009-04-27 18:38:38 +00002313//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002314// DeclContext's Name Lookup Table Serialization
2315//===----------------------------------------------------------------------===//
2316
2317namespace {
2318// Trait used for the on-disk hash table used in the method pool.
2319class ASTDeclContextNameLookupTrait {
2320 ASTWriter &Writer;
2321
2322public:
2323 typedef DeclarationName key_type;
2324 typedef key_type key_type_ref;
2325
2326 typedef DeclContext::lookup_result data_type;
2327 typedef const data_type& data_type_ref;
2328
2329 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2330
2331 unsigned ComputeHash(DeclarationName Name) {
2332 llvm::FoldingSetNodeID ID;
2333 ID.AddInteger(Name.getNameKind());
2334
2335 switch (Name.getNameKind()) {
2336 case DeclarationName::Identifier:
2337 ID.AddString(Name.getAsIdentifierInfo()->getName());
2338 break;
2339 case DeclarationName::ObjCZeroArgSelector:
2340 case DeclarationName::ObjCOneArgSelector:
2341 case DeclarationName::ObjCMultiArgSelector:
2342 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2343 break;
2344 case DeclarationName::CXXConstructorName:
2345 case DeclarationName::CXXDestructorName:
2346 case DeclarationName::CXXConversionFunctionName:
2347 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
2348 break;
2349 case DeclarationName::CXXOperatorName:
2350 ID.AddInteger(Name.getCXXOverloadedOperator());
2351 break;
2352 case DeclarationName::CXXLiteralOperatorName:
2353 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2354 case DeclarationName::CXXUsingDirective:
2355 break;
2356 }
2357
2358 return ID.ComputeHash();
2359 }
2360
2361 std::pair<unsigned,unsigned>
2362 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2363 data_type_ref Lookup) {
2364 unsigned KeyLen = 1;
2365 switch (Name.getNameKind()) {
2366 case DeclarationName::Identifier:
2367 case DeclarationName::ObjCZeroArgSelector:
2368 case DeclarationName::ObjCOneArgSelector:
2369 case DeclarationName::ObjCMultiArgSelector:
2370 case DeclarationName::CXXConstructorName:
2371 case DeclarationName::CXXDestructorName:
2372 case DeclarationName::CXXConversionFunctionName:
2373 case DeclarationName::CXXLiteralOperatorName:
2374 KeyLen += 4;
2375 break;
2376 case DeclarationName::CXXOperatorName:
2377 KeyLen += 1;
2378 break;
2379 case DeclarationName::CXXUsingDirective:
2380 break;
2381 }
2382 clang::io::Emit16(Out, KeyLen);
2383
2384 // 2 bytes for num of decls and 4 for each DeclID.
2385 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2386 clang::io::Emit16(Out, DataLen);
2387
2388 return std::make_pair(KeyLen, DataLen);
2389 }
2390
2391 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2392 using namespace clang::io;
2393
2394 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2395 Emit8(Out, Name.getNameKind());
2396 switch (Name.getNameKind()) {
2397 case DeclarationName::Identifier:
2398 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2399 break;
2400 case DeclarationName::ObjCZeroArgSelector:
2401 case DeclarationName::ObjCOneArgSelector:
2402 case DeclarationName::ObjCMultiArgSelector:
2403 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2404 break;
2405 case DeclarationName::CXXConstructorName:
2406 case DeclarationName::CXXDestructorName:
2407 case DeclarationName::CXXConversionFunctionName:
2408 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2409 break;
2410 case DeclarationName::CXXOperatorName:
2411 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2412 Emit8(Out, Name.getCXXOverloadedOperator());
2413 break;
2414 case DeclarationName::CXXLiteralOperatorName:
2415 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2416 break;
2417 case DeclarationName::CXXUsingDirective:
2418 break;
2419 }
2420 }
2421
2422 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2423 data_type Lookup, unsigned DataLen) {
2424 uint64_t Start = Out.tell(); (void)Start;
2425 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2426 for (; Lookup.first != Lookup.second; ++Lookup.first)
2427 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2428
2429 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2430 }
2431};
2432} // end anonymous namespace
2433
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002434/// \brief Write the block containing all of the declaration IDs
2435/// visible from the given DeclContext.
2436///
2437/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002438/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002439uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2440 DeclContext *DC) {
2441 if (DC->getPrimaryContext() != DC)
2442 return 0;
2443
2444 // Since there is no name lookup into functions or methods, don't bother to
2445 // build a visible-declarations table for these entities.
2446 if (DC->isFunctionOrMethod())
2447 return 0;
2448
2449 // If not in C++, we perform name lookup for the translation unit via the
2450 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2451 // FIXME: In C++ we need the visible declarations in order to "see" the
2452 // friend declarations, is there a way to do this without writing the table ?
2453 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2454 return 0;
2455
2456 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00002457 if (DC->hasExternalVisibleStorage())
2458 DC->MaterializeVisibleDeclsFromExternalStorage();
2459 else
2460 DC->lookup(DeclarationName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002461
2462 // Serialize the contents of the mapping used for lookup. Note that,
2463 // although we have two very different code paths, the serialized
2464 // representation is the same for both cases: a declaration name,
2465 // followed by a size, followed by references to the visible
2466 // declarations that have that name.
2467 uint64_t Offset = Stream.GetCurrentBitNo();
2468 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2469 if (!Map || Map->empty())
2470 return 0;
2471
2472 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2473 ASTDeclContextNameLookupTrait Trait(*this);
2474
2475 // Create the on-disk hash table representation.
2476 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2477 D != DEnd; ++D) {
2478 DeclarationName Name = D->first;
2479 DeclContext::lookup_result Result = D->second.getLookupResult();
2480 Generator.insert(Name, Result, Trait);
2481 }
2482
2483 // Create the on-disk hash table in a buffer.
2484 llvm::SmallString<4096> LookupTable;
2485 uint32_t BucketOffset;
2486 {
2487 llvm::raw_svector_ostream Out(LookupTable);
2488 // Make sure that no bucket is at offset 0
2489 clang::io::Emit32(Out, 0);
2490 BucketOffset = Generator.Emit(Out, Trait);
2491 }
2492
2493 // Write the lookup table
2494 RecordData Record;
2495 Record.push_back(DECL_CONTEXT_VISIBLE);
2496 Record.push_back(BucketOffset);
2497 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2498 LookupTable.str());
2499
2500 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2501 ++NumVisibleDeclContexts;
2502 return Offset;
2503}
2504
Sebastian Redla4071b42010-08-24 00:50:09 +00002505/// \brief Write an UPDATE_VISIBLE block for the given context.
2506///
2507/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2508/// DeclContext in a dependent AST file. As such, they only exist for the TU
2509/// (in C++) and for namespaces.
2510void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00002511 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2512 if (!Map || Map->empty())
2513 return;
2514
2515 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2516 ASTDeclContextNameLookupTrait Trait(*this);
2517
2518 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002519 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2520 D != DEnd; ++D) {
2521 DeclarationName Name = D->first;
2522 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002523 // For any name that appears in this table, the results are complete, i.e.
2524 // they overwrite results from previous PCHs. Merging is always a mess.
2525 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002526 }
2527
2528 // Create the on-disk hash table in a buffer.
2529 llvm::SmallString<4096> LookupTable;
2530 uint32_t BucketOffset;
2531 {
2532 llvm::raw_svector_ostream Out(LookupTable);
2533 // Make sure that no bucket is at offset 0
2534 clang::io::Emit32(Out, 0);
2535 BucketOffset = Generator.Emit(Out, Trait);
2536 }
2537
2538 // Write the lookup table
2539 RecordData Record;
2540 Record.push_back(UPDATE_VISIBLE);
2541 Record.push_back(getDeclID(cast<Decl>(DC)));
2542 Record.push_back(BucketOffset);
2543 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2544}
2545
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002546/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2547void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2548 RecordData Record;
2549 Record.push_back(Opts.fp_contract);
2550 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2551}
2552
2553/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2554void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
2555 if (!SemaRef.Context.getLangOptions().OpenCL)
2556 return;
2557
2558 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2559 RecordData Record;
2560#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2561#include "clang/Basic/OpenCLExtensions.def"
2562 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2563}
2564
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002565//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00002566// General Serialization Routines
2567//===----------------------------------------------------------------------===//
2568
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002569/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002570void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00002571 Record.push_back(Attrs.size());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002572 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2573 const Attr * A = *i;
2574 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2575 AddSourceLocation(A->getLocation(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002576
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002577#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00002578
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002579 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002580}
2581
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002582void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002583 Record.push_back(Str.size());
2584 Record.insert(Record.end(), Str.begin(), Str.end());
2585}
2586
Douglas Gregore84a9da2009-04-20 20:36:09 +00002587/// \brief Note that the identifier II occurs at the given offset
2588/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002589void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002590 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002591 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00002592 // up earlier in the chain and thus don't need an offset.
2593 if (ID >= FirstIdentID)
2594 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002595}
2596
Douglas Gregor95c13f52009-04-25 17:48:32 +00002597/// \brief Note that the selector Sel occurs at the given offset
2598/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002599void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00002600 unsigned ID = SelectorIDs[Sel];
2601 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00002602 // Don't record offsets for selectors that are also available in a different
2603 // file.
2604 if (ID < FirstSelectorID)
2605 return;
2606 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002607}
2608
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002609ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00002610 : Stream(Stream), Chain(0), SerializationListener(0),
2611 FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00002612 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002613 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor91096292010-10-02 19:29:26 +00002614 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2615 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002616 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002617 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
2618 NextCXXBaseSpecifiersID(1)
2619{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002620}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002621
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002622void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002623 const std::string &OutputFile,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002624 const char *isysroot) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002625 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002626 Stream.Emit((unsigned)'C', 8);
2627 Stream.Emit((unsigned)'P', 8);
2628 Stream.Emit((unsigned)'C', 8);
2629 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002630
Chris Lattner28fa4e62009-04-26 22:26:21 +00002631 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002632
Sebastian Redl143413f2010-07-12 22:02:52 +00002633 if (Chain)
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002634 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002635 else
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002636 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile);
Sebastian Redl143413f2010-07-12 22:02:52 +00002637}
2638
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002639void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002640 const char *isysroot,
2641 const std::string &OutputFile) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002642 using namespace llvm;
2643
2644 ASTContext &Context = SemaRef.Context;
2645 Preprocessor &PP = SemaRef.PP;
2646
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002647 // The translation unit is the first declaration we'll emit.
2648 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlff4a2952010-07-23 23:49:55 +00002649 ++NextDeclID;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002650 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002651
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002652 // Make sure that we emit IdentifierInfos (and any attached
2653 // declarations) for builtins.
2654 {
2655 IdentifierTable &Table = PP.getIdentifierTable();
2656 llvm::SmallVector<const char *, 32> BuiltinNames;
2657 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2658 Context.getLangOptions().NoBuiltin);
2659 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2660 getIdentifierRef(&Table.get(BuiltinNames[I]));
2661 }
2662
Chris Lattner0c797362009-09-08 18:19:27 +00002663 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002664 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002665 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002666 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00002667 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2668 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00002669 }
Douglas Gregord4df8652009-04-22 22:02:47 +00002670
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002671 // Build a record containing all of the file scoped decls in this file.
2672 RecordData UnusedFileScopedDecls;
2673 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2674 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002675
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002676 RecordData WeakUndeclaredIdentifiers;
2677 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2678 WeakUndeclaredIdentifiers.push_back(
2679 SemaRef.WeakUndeclaredIdentifiers.size());
2680 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2681 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2682 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2683 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2684 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2685 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2686 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2687 }
2688 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002689
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002690 // Build a record containing all of the locally-scoped external
2691 // declarations in this header file. Generally, this record will be
2692 // empty.
2693 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002694 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00002695 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002696 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002697 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2698 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2699 TD != TDEnd; ++TD)
2700 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2701
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002702 // Build a record containing all of the ext_vector declarations.
2703 RecordData ExtVectorDecls;
2704 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2705 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2706
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002707 // Build a record containing all of the VTable uses information.
2708 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00002709 if (!SemaRef.VTableUses.empty()) {
2710 VTableUses.push_back(SemaRef.VTableUses.size());
2711 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2712 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2713 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2714 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2715 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002716 }
2717
2718 // Build a record containing all of dynamic classes declarations.
2719 RecordData DynamicClasses;
2720 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2721 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2722
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002723 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002724 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002725 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002726 I = SemaRef.PendingInstantiations.begin(),
2727 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2728 AddDeclRef(I->first, PendingInstantiations);
2729 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002730 }
2731 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2732 "There are local ones at end of translation unit!");
2733
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002734 // Build a record containing some declaration references.
2735 RecordData SemaDeclRefs;
2736 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2737 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2738 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2739 }
2740
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002741 RecordData CUDASpecialDeclRefs;
2742 if (Context.getcudaConfigureCallDecl()) {
2743 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
2744 }
2745
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002746 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002747 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002748 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002749 WriteMetadata(Context, isysroot, OutputFile);
Sebastian Redl143413f2010-07-12 22:02:52 +00002750 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002751 if (StatCalls && !isysroot)
Douglas Gregor11cfd942010-07-12 23:48:14 +00002752 WriteStatCache(*StatCalls);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002753 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc277ad12009-07-18 15:33:26 +00002754 // Write the record of special types.
2755 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002756
Steve Naroffc277ad12009-07-18 15:33:26 +00002757 AddTypeRef(Context.getBuiltinVaListType(), Record);
2758 AddTypeRef(Context.getObjCIdType(), Record);
2759 AddTypeRef(Context.getObjCSelType(), Record);
2760 AddTypeRef(Context.getObjCProtoType(), Record);
2761 AddTypeRef(Context.getObjCClassType(), Record);
2762 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2763 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2764 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002765 AddTypeRef(Context.getjmp_bufType(), Record);
2766 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002767 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2768 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpd0153282009-10-20 02:12:22 +00002769 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002770 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002771 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2772 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002773 Record.push_back(Context.isInt128Installed());
Sebastian Redl539c5062010-08-18 23:57:32 +00002774 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002775
Douglas Gregor1970d882009-04-26 03:49:13 +00002776 // Keep writing types and declarations until all types and
2777 // declarations have been written.
Sebastian Redl539c5062010-08-18 23:57:32 +00002778 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002779 WriteDeclsBlockAbbrevs();
2780 while (!DeclTypesToEmit.empty()) {
2781 DeclOrType DOT = DeclTypesToEmit.front();
2782 DeclTypesToEmit.pop();
2783 if (DOT.isType())
2784 WriteType(DOT.getType());
2785 else
2786 WriteDecl(Context, DOT.getDecl());
2787 }
2788 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002789
Douglas Gregor45053152009-10-17 17:25:45 +00002790 WritePreprocessor(PP);
Douglas Gregor09b69892011-02-10 17:09:37 +00002791 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00002792 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002793 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002794 WriteIdentifierTable(PP);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002795 WriteFPPragmaOptions(SemaRef.getFPOptions());
2796 WriteOpenCLExtensions(SemaRef);
Douglas Gregor745ed142009-04-25 18:35:21 +00002797
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002798 WriteTypeDeclOffsets();
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002799 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00002800
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002801 // Write the C++ base-specifier set offsets.
2802 if (!CXXBaseSpecifiersOffsets.empty()) {
2803 // Create a blob abbreviation for the C++ base specifiers offsets.
2804 using namespace llvm;
2805
2806 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2807 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2808 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2809 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2810 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2811
2812 // Write the selector offsets table.
2813 Record.clear();
2814 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2815 Record.push_back(CXXBaseSpecifiersOffsets.size());
2816 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
2817 (const char *)CXXBaseSpecifiersOffsets.data(),
2818 CXXBaseSpecifiersOffsets.size() * sizeof(uint32_t));
2819 }
2820
Douglas Gregord4df8652009-04-22 22:02:47 +00002821 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002822 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002823 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002824
2825 // Write the record containing tentative definitions.
2826 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002827 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002828
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002829 // Write the record containing unused file scoped decls.
2830 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002831 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002832
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002833 // Write the record containing weak undeclared identifiers.
2834 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002835 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002836 WeakUndeclaredIdentifiers);
2837
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002838 // Write the record containing locally-scoped external definitions.
2839 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002840 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002841 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002842
2843 // Write the record containing ext_vector type names.
2844 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002845 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002846
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002847 // Write the record containing VTable uses information.
2848 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002849 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002850
2851 // Write the record containing dynamic classes declarations.
2852 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002853 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002854
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002855 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002856 if (!PendingInstantiations.empty())
2857 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002858
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002859 // Write the record containing declaration references of Sema.
2860 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002861 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002862
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002863 // Write the record containing CUDA-specific declaration references.
2864 if (!CUDASpecialDeclRefs.empty())
2865 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
2866
Douglas Gregor08f01292009-04-17 22:13:46 +00002867 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002868 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002869 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002870 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002871 Record.push_back(NumLexicalDeclContexts);
2872 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00002873 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002874 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002875}
2876
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002877void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002878 const char *isysroot) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002879 using namespace llvm;
2880
2881 ASTContext &Context = SemaRef.Context;
2882 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002883
Sebastian Redl143413f2010-07-12 22:02:52 +00002884 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002885 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002886 WriteMetadata(Context, isysroot, "");
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002887 if (StatCalls && !isysroot)
2888 WriteStatCache(*StatCalls);
2889 // FIXME: Source manager block should only write new stuff, which could be
2890 // done by tracking the largest ID in the chain
2891 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002892
2893 // The special types are in the chained PCH.
2894
2895 // We don't start with the translation unit, but with its decls that
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002896 // don't come from the chained PCH.
Sebastian Redl143413f2010-07-12 22:02:52 +00002897 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002898 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002899 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2900 E = TU->noload_decls_end();
Sebastian Redl143413f2010-07-12 22:02:52 +00002901 I != E; ++I) {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002902 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002903 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002904 else if ((*I)->isChangedSinceDeserialization())
2905 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl143413f2010-07-12 22:02:52 +00002906 }
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002907 // We also need to write a lexical updates block for the TU.
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002908 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002909 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002910 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2911 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2912 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002913 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002914 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2915 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002916 NewGlobalDecls.size() * sizeof(KindDeclIDPair));
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00002917 // And a visible updates block for the DeclContexts.
2918 Abv = new llvm::BitCodeAbbrev();
2919 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2920 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2921 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2922 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2923 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2924 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl143413f2010-07-12 22:02:52 +00002925
Sebastian Redl98912122010-07-27 23:01:28 +00002926 // Build a record containing all of the new tentative definitions in this
2927 // file, in TentativeDefinitions order.
2928 RecordData TentativeDefinitions;
2929 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2930 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2931 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2932 }
2933
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002934 // Build a record containing all of the file scoped decls in this file.
2935 RecordData UnusedFileScopedDecls;
2936 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2937 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2938 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002939 }
2940
Sebastian Redl08aca90252010-08-05 18:21:25 +00002941 // We write the entire table, overwriting the tables from the chain.
2942 RecordData WeakUndeclaredIdentifiers;
2943 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2944 WeakUndeclaredIdentifiers.push_back(
2945 SemaRef.WeakUndeclaredIdentifiers.size());
2946 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2947 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2948 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2949 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2950 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2951 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2952 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2953 }
2954 }
2955
Sebastian Redl98912122010-07-27 23:01:28 +00002956 // Build a record containing all of the locally-scoped external
2957 // declarations in this header file. Generally, this record will be
2958 // empty.
2959 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002960 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl98912122010-07-27 23:01:28 +00002961 // nondeterminstic!
2962 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2963 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2964 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2965 TD != TDEnd; ++TD) {
2966 if (TD->second->getPCHLevel() == 0)
2967 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2968 }
2969
2970 // Build a record containing all of the ext_vector declarations.
2971 RecordData ExtVectorDecls;
2972 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2973 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2974 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2975 }
2976
Sebastian Redl08aca90252010-08-05 18:21:25 +00002977 // Build a record containing all of the VTable uses information.
2978 // We write everything here, because it's too hard to determine whether
2979 // a use is new to this part.
2980 RecordData VTableUses;
2981 if (!SemaRef.VTableUses.empty()) {
2982 VTableUses.push_back(SemaRef.VTableUses.size());
2983 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2984 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2985 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2986 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2987 }
2988 }
2989
2990 // Build a record containing all of dynamic classes declarations.
2991 RecordData DynamicClasses;
2992 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2993 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
2994 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2995
2996 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002997 RecordData PendingInstantiations;
Sebastian Redl08aca90252010-08-05 18:21:25 +00002998 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002999 I = SemaRef.PendingInstantiations.begin(),
3000 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl08aca90252010-08-05 18:21:25 +00003001 if (I->first->getPCHLevel() == 0) {
Chandler Carruth54080172010-08-25 08:44:16 +00003002 AddDeclRef(I->first, PendingInstantiations);
3003 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003004 }
3005 }
3006 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3007 "There are local ones at end of translation unit!");
3008
3009 // Build a record containing some declaration references.
3010 // It's not worth the effort to avoid duplication here.
3011 RecordData SemaDeclRefs;
3012 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3013 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3014 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3015 }
3016
Sebastian Redl539c5062010-08-18 23:57:32 +00003017 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl143413f2010-07-12 22:02:52 +00003018 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003019 for (DeclsToRewriteTy::iterator
3020 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
3021 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl143413f2010-07-12 22:02:52 +00003022 while (!DeclTypesToEmit.empty()) {
3023 DeclOrType DOT = DeclTypesToEmit.front();
3024 DeclTypesToEmit.pop();
3025 if (DOT.isType())
3026 WriteType(DOT.getType());
3027 else
3028 WriteDecl(Context, DOT.getDecl());
3029 }
3030 Stream.ExitBlock();
3031
Sebastian Redl98912122010-07-27 23:01:28 +00003032 WritePreprocessor(PP);
Sebastian Redl51c79d82010-08-04 22:21:29 +00003033 WriteSelectors(SemaRef);
3034 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlff4a2952010-07-23 23:49:55 +00003035 WriteIdentifierTable(PP);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003036 WriteFPPragmaOptions(SemaRef.getFPOptions());
3037 WriteOpenCLExtensions(SemaRef);
3038
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003039 WriteTypeDeclOffsets();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003040 // FIXME: For chained PCH only write the new mappings (we currently
3041 // write all of them again).
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003042 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl98912122010-07-27 23:01:28 +00003043
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003044 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003045 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003046 RecordData FirstLatestDeclIDs;
3047 for (FirstLatestDeclMap::iterator
3048 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
3049 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
3050 "Expected first & second to be in different PCHs");
3051 AddDeclRef(I->first, FirstLatestDeclIDs);
3052 AddDeclRef(I->second, FirstLatestDeclIDs);
3053 }
3054 if (!FirstLatestDeclIDs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003055 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003056
Sebastian Redl98912122010-07-27 23:01:28 +00003057 // Write the record containing external, unnamed definitions.
3058 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003059 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00003060
3061 // Write the record containing tentative definitions.
3062 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003063 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00003064
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003065 // Write the record containing unused file scoped decls.
3066 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003067 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003068
Sebastian Redl08aca90252010-08-05 18:21:25 +00003069 // Write the record containing weak undeclared identifiers.
3070 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003071 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl08aca90252010-08-05 18:21:25 +00003072 WeakUndeclaredIdentifiers);
3073
Sebastian Redl98912122010-07-27 23:01:28 +00003074 // Write the record containing locally-scoped external definitions.
3075 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003076 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl98912122010-07-27 23:01:28 +00003077 LocallyScopedExternalDecls);
3078
3079 // Write the record containing ext_vector type names.
3080 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003081 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003082
Sebastian Redl08aca90252010-08-05 18:21:25 +00003083 // Write the record containing VTable uses information.
3084 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003085 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003086
3087 // Write the record containing dynamic classes declarations.
3088 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003089 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003090
3091 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003092 if (!PendingInstantiations.empty())
3093 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003094
3095 // Write the record containing declaration references of Sema.
3096 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003097 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl98912122010-07-27 23:01:28 +00003098
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003099 // Write the updates to DeclContexts.
3100 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3101 I = UpdatedDeclContexts.begin(),
3102 E = UpdatedDeclContexts.end();
Sebastian Redla4071b42010-08-24 00:50:09 +00003103 I != E; ++I)
3104 WriteDeclContextVisibleUpdate(*I);
3105
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003106 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003107
Sebastian Redl98912122010-07-27 23:01:28 +00003108 Record.clear();
3109 Record.push_back(NumStatements);
3110 Record.push_back(NumMacros);
3111 Record.push_back(NumLexicalDeclContexts);
3112 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003113 WriteDeclReplacementsBlock();
Sebastian Redl539c5062010-08-18 23:57:32 +00003114 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl143413f2010-07-12 22:02:52 +00003115 Stream.ExitBlock();
3116}
3117
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003118void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003119 if (DeclUpdates.empty())
3120 return;
3121
3122 RecordData OffsetsRecord;
3123 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
3124 for (DeclUpdateMap::iterator
3125 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3126 const Decl *D = I->first;
3127 UpdateRecord &URec = I->second;
3128
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00003129 if (DeclsToRewrite.count(D))
3130 continue; // The decl will be written completely,no need to store updates.
3131
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003132 uint64_t Offset = Stream.GetCurrentBitNo();
3133 Stream.EmitRecord(DECL_UPDATES, URec);
3134
3135 OffsetsRecord.push_back(GetDeclRef(D));
3136 OffsetsRecord.push_back(Offset);
3137 }
3138 Stream.ExitBlock();
3139 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3140}
3141
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003142void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003143 if (ReplacedDecls.empty())
3144 return;
3145
3146 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003147 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003148 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
3149 Record.push_back(I->first);
3150 Record.push_back(I->second);
3151 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003152 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003153}
3154
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003155void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003156 Record.push_back(Loc.getRawEncoding());
3157}
3158
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003159void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003160 AddSourceLocation(Range.getBegin(), Record);
3161 AddSourceLocation(Range.getEnd(), Record);
3162}
3163
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003164void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003165 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00003166 const uint64_t *Words = Value.getRawData();
3167 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003168}
3169
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003170void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00003171 Record.push_back(Value.isUnsigned());
3172 AddAPInt(Value, Record);
3173}
3174
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003175void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00003176 AddAPInt(Value.bitcastToAPInt(), Record);
3177}
3178
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003179void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003180 Record.push_back(getIdentifierRef(II));
3181}
3182
Sebastian Redl539c5062010-08-18 23:57:32 +00003183IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003184 if (II == 0)
3185 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003186
Sebastian Redl539c5062010-08-18 23:57:32 +00003187 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003188 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00003189 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003190 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003191}
3192
Sebastian Redl50e26582010-09-15 19:54:06 +00003193MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregoraae92242010-03-19 21:51:54 +00003194 if (MD == 0)
3195 return 0;
Sebastian Redl50e26582010-09-15 19:54:06 +00003196
3197 MacroID &ID = MacroDefinitions[MD];
Douglas Gregoraae92242010-03-19 21:51:54 +00003198 if (ID == 0)
Douglas Gregor91096292010-10-02 19:29:26 +00003199 ID = NextMacroID++;
Douglas Gregoraae92242010-03-19 21:51:54 +00003200 return ID;
3201}
3202
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003203void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003204 Record.push_back(getSelectorRef(SelRef));
3205}
3206
Sebastian Redl539c5062010-08-18 23:57:32 +00003207SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003208 if (Sel.getAsOpaquePtr() == 0) {
3209 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00003210 }
3211
Sebastian Redl539c5062010-08-18 23:57:32 +00003212 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00003213 if (SID == 0 && Chain) {
3214 // This might trigger a ReadSelector callback, which will set the ID for
3215 // this selector.
3216 Chain->LoadSelector(Sel);
3217 }
Steve Naroff2ddea052009-04-23 10:39:46 +00003218 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003219 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00003220 }
Sebastian Redl834bb972010-08-04 17:20:04 +00003221 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00003222}
3223
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003224void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00003225 AddDeclRef(Temp->getDestructor(), Record);
3226}
3227
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003228void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3229 CXXBaseSpecifier const *BasesEnd,
3230 RecordDataImpl &Record) {
3231 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3232 CXXBaseSpecifiersToWrite.push_back(
3233 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3234 Bases, BasesEnd));
3235 Record.push_back(NextCXXBaseSpecifiersID++);
3236}
3237
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003238void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003239 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003240 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003241 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00003242 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003243 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00003244 break;
3245 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003246 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00003247 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003248 case TemplateArgument::Template:
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003249 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
3250 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003251 break;
3252 case TemplateArgument::TemplateExpansion:
3253 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
3254 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003255 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003256 break;
John McCall0ad16662009-10-29 08:12:44 +00003257 case TemplateArgument::Null:
3258 case TemplateArgument::Integral:
3259 case TemplateArgument::Declaration:
3260 case TemplateArgument::Pack:
3261 break;
3262 }
3263}
3264
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003265void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003266 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003267 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003268
3269 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3270 bool InfoHasSameExpr
3271 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3272 Record.push_back(InfoHasSameExpr);
3273 if (InfoHasSameExpr)
3274 return; // Avoid storing the same expr twice.
3275 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003276 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3277 Record);
3278}
3279
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003280void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3281 RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00003282 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00003283 AddTypeRef(QualType(), Record);
3284 return;
3285 }
3286
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003287 AddTypeLoc(TInfo->getTypeLoc(), Record);
3288}
3289
3290void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3291 AddTypeRef(TL.getType(), Record);
3292
John McCall8f115c62009-10-16 21:56:05 +00003293 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003294 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003295 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00003296}
3297
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003298void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00003299 Record.push_back(GetOrCreateTypeID(T));
3300}
3301
3302TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003303 return MakeTypeID(T,
3304 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3305}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003306
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003307TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003308 return MakeTypeID(T,
3309 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003310}
3311
3312TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3313 if (T.isNull())
3314 return TypeIdx();
3315 assert(!T.getLocalFastQualifiers());
3316
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00003317 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003318 if (Idx.getIndex() == 0) {
Douglas Gregor1970d882009-04-26 03:49:13 +00003319 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00003320 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003321 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003322 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00003323 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003324 return Idx;
3325}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003326
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003327TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003328 if (T.isNull())
3329 return TypeIdx();
3330 assert(!T.getLocalFastQualifiers());
3331
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003332 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3333 assert(I != TypeIdxs.end() && "Type not emitted!");
3334 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003335}
3336
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003337void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003338 Record.push_back(GetDeclRef(D));
3339}
3340
Sebastian Redl539c5062010-08-18 23:57:32 +00003341DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003342 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003343 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003344 }
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003345 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00003346 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00003347 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003348 // We haven't seen this declaration before. Give it a new ID and
3349 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00003350 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003351 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003352 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3353 // We don't add it to the replacement collection here, because we don't
3354 // have the offset yet.
3355 DeclTypesToEmit.push(const_cast<Decl *>(D));
3356 // Reset the flag, so that we don't add this decl multiple times.
3357 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003358 }
3359
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003360 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003361}
3362
Sebastian Redl539c5062010-08-18 23:57:32 +00003363DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00003364 if (D == 0)
3365 return 0;
3366
3367 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3368 return DeclIDs[D];
3369}
3370
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003371void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00003372 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003373 Record.push_back(Name.getNameKind());
3374 switch (Name.getNameKind()) {
3375 case DeclarationName::Identifier:
3376 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3377 break;
3378
3379 case DeclarationName::ObjCZeroArgSelector:
3380 case DeclarationName::ObjCOneArgSelector:
3381 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00003382 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003383 break;
3384
3385 case DeclarationName::CXXConstructorName:
3386 case DeclarationName::CXXDestructorName:
3387 case DeclarationName::CXXConversionFunctionName:
3388 AddTypeRef(Name.getCXXNameType(), Record);
3389 break;
3390
3391 case DeclarationName::CXXOperatorName:
3392 Record.push_back(Name.getCXXOverloadedOperator());
3393 break;
3394
Alexis Hunt3d221f22009-11-29 07:34:05 +00003395 case DeclarationName::CXXLiteralOperatorName:
3396 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3397 break;
3398
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003399 case DeclarationName::CXXUsingDirective:
3400 // No extra data to emit
3401 break;
3402 }
3403}
Chris Lattnerca025db2010-05-07 21:43:38 +00003404
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003405void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003406 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003407 switch (Name.getNameKind()) {
3408 case DeclarationName::CXXConstructorName:
3409 case DeclarationName::CXXDestructorName:
3410 case DeclarationName::CXXConversionFunctionName:
3411 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3412 break;
3413
3414 case DeclarationName::CXXOperatorName:
3415 AddSourceLocation(
3416 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3417 Record);
3418 AddSourceLocation(
3419 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3420 Record);
3421 break;
3422
3423 case DeclarationName::CXXLiteralOperatorName:
3424 AddSourceLocation(
3425 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3426 Record);
3427 break;
3428
3429 case DeclarationName::Identifier:
3430 case DeclarationName::ObjCZeroArgSelector:
3431 case DeclarationName::ObjCOneArgSelector:
3432 case DeclarationName::ObjCMultiArgSelector:
3433 case DeclarationName::CXXUsingDirective:
3434 break;
3435 }
3436}
3437
3438void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003439 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003440 AddDeclarationName(NameInfo.getName(), Record);
3441 AddSourceLocation(NameInfo.getLoc(), Record);
3442 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3443}
3444
3445void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003446 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00003447 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003448 Record.push_back(Info.NumTemplParamLists);
3449 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3450 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3451}
3452
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003453void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003454 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003455 // Nested name specifiers usually aren't too long. I think that 8 would
3456 // typically accomodate the vast majority.
3457 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3458
3459 // Push each of the NNS's onto a stack for serialization in reverse order.
3460 while (NNS) {
3461 NestedNames.push_back(NNS);
3462 NNS = NNS->getPrefix();
3463 }
3464
3465 Record.push_back(NestedNames.size());
3466 while(!NestedNames.empty()) {
3467 NNS = NestedNames.pop_back_val();
3468 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3469 Record.push_back(Kind);
3470 switch (Kind) {
3471 case NestedNameSpecifier::Identifier:
3472 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3473 break;
3474
3475 case NestedNameSpecifier::Namespace:
3476 AddDeclRef(NNS->getAsNamespace(), Record);
3477 break;
3478
Douglas Gregor7b26ff92011-02-24 02:36:08 +00003479 case NestedNameSpecifier::NamespaceAlias:
3480 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
3481 break;
3482
Chris Lattnerca025db2010-05-07 21:43:38 +00003483 case NestedNameSpecifier::TypeSpec:
3484 case NestedNameSpecifier::TypeSpecWithTemplate:
3485 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3486 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3487 break;
3488
3489 case NestedNameSpecifier::Global:
3490 // Don't need to write an associated value.
3491 break;
3492 }
3493 }
3494}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003495
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003496void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
3497 RecordDataImpl &Record) {
3498 // Nested name specifiers usually aren't too long. I think that 8 would
3499 // typically accomodate the vast majority.
3500 llvm::SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
3501
3502 // Push each of the nested-name-specifiers's onto a stack for
3503 // serialization in reverse order.
3504 while (NNS) {
3505 NestedNames.push_back(NNS);
3506 NNS = NNS.getPrefix();
3507 }
3508
3509 Record.push_back(NestedNames.size());
3510 while(!NestedNames.empty()) {
3511 NNS = NestedNames.pop_back_val();
3512 NestedNameSpecifier::SpecifierKind Kind
3513 = NNS.getNestedNameSpecifier()->getKind();
3514 Record.push_back(Kind);
3515 switch (Kind) {
3516 case NestedNameSpecifier::Identifier:
3517 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
3518 AddSourceRange(NNS.getLocalSourceRange(), Record);
3519 break;
3520
3521 case NestedNameSpecifier::Namespace:
3522 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
3523 AddSourceRange(NNS.getLocalSourceRange(), Record);
3524 break;
3525
3526 case NestedNameSpecifier::NamespaceAlias:
3527 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
3528 AddSourceRange(NNS.getLocalSourceRange(), Record);
3529 break;
3530
3531 case NestedNameSpecifier::TypeSpec:
3532 case NestedNameSpecifier::TypeSpecWithTemplate:
3533 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3534 AddTypeLoc(NNS.getTypeLoc(), Record);
3535 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3536 break;
3537
3538 case NestedNameSpecifier::Global:
3539 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3540 break;
3541 }
3542 }
3543}
3544
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003545void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003546 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003547 Record.push_back(Kind);
3548 switch (Kind) {
3549 case TemplateName::Template:
3550 AddDeclRef(Name.getAsTemplateDecl(), Record);
3551 break;
3552
3553 case TemplateName::OverloadedTemplate: {
3554 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3555 Record.push_back(OvT->size());
3556 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3557 I != E; ++I)
3558 AddDeclRef(*I, Record);
3559 break;
3560 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003561
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003562 case TemplateName::QualifiedTemplate: {
3563 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3564 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3565 Record.push_back(QualT->hasTemplateKeyword());
3566 AddDeclRef(QualT->getTemplateDecl(), Record);
3567 break;
3568 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003569
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003570 case TemplateName::DependentTemplate: {
3571 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3572 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3573 Record.push_back(DepT->isIdentifier());
3574 if (DepT->isIdentifier())
3575 AddIdentifierRef(DepT->getIdentifier(), Record);
3576 else
3577 Record.push_back(DepT->getOperator());
3578 break;
3579 }
Douglas Gregor5590be02011-01-15 06:45:20 +00003580
3581 case TemplateName::SubstTemplateTemplateParmPack: {
3582 SubstTemplateTemplateParmPackStorage *SubstPack
3583 = Name.getAsSubstTemplateTemplateParmPack();
3584 AddDeclRef(SubstPack->getParameterPack(), Record);
3585 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
3586 break;
3587 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003588 }
3589}
3590
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003591void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003592 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003593 Record.push_back(Arg.getKind());
3594 switch (Arg.getKind()) {
3595 case TemplateArgument::Null:
3596 break;
3597 case TemplateArgument::Type:
3598 AddTypeRef(Arg.getAsType(), Record);
3599 break;
3600 case TemplateArgument::Declaration:
3601 AddDeclRef(Arg.getAsDecl(), Record);
3602 break;
3603 case TemplateArgument::Integral:
3604 AddAPSInt(*Arg.getAsIntegral(), Record);
3605 AddTypeRef(Arg.getIntegralType(), Record);
3606 break;
3607 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00003608 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3609 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003610 case TemplateArgument::TemplateExpansion:
3611 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregore1d60df2011-01-14 23:41:42 +00003612 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3613 Record.push_back(*NumExpansions + 1);
3614 else
3615 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003616 break;
3617 case TemplateArgument::Expression:
3618 AddStmt(Arg.getAsExpr());
3619 break;
3620 case TemplateArgument::Pack:
3621 Record.push_back(Arg.pack_size());
3622 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3623 I != E; ++I)
3624 AddTemplateArgument(*I, Record);
3625 break;
3626 }
3627}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003628
3629void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003630ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003631 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003632 assert(TemplateParams && "No TemplateParams!");
3633 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3634 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3635 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3636 Record.push_back(TemplateParams->size());
3637 for (TemplateParameterList::const_iterator
3638 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3639 P != PEnd; ++P)
3640 AddDeclRef(*P, Record);
3641}
3642
3643/// \brief Emit a template argument list.
3644void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003645ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003646 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003647 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003648 Record.push_back(TemplateArgs->size());
3649 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003650 AddTemplateArgument(TemplateArgs->get(i), Record);
3651}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003652
3653
3654void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003655ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003656 Record.push_back(Set.size());
3657 for (UnresolvedSetImpl::const_iterator
3658 I = Set.begin(), E = Set.end(); I != E; ++I) {
3659 AddDeclRef(I.getDecl(), Record);
3660 Record.push_back(I.getAccess());
3661 }
3662}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003663
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003664void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003665 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003666 Record.push_back(Base.isVirtual());
3667 Record.push_back(Base.isBaseOfClass());
3668 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00003669 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00003670 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003671 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00003672 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3673 : SourceLocation(),
3674 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003675}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003676
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003677void ASTWriter::FlushCXXBaseSpecifiers() {
3678 RecordData Record;
3679 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3680 Record.clear();
3681
3682 // Record the offset of this base-specifier set.
3683 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3684 if (Index == CXXBaseSpecifiersOffsets.size())
3685 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3686 else {
3687 if (Index > CXXBaseSpecifiersOffsets.size())
3688 CXXBaseSpecifiersOffsets.resize(Index + 1);
3689 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3690 }
3691
3692 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3693 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3694 Record.push_back(BEnd - B);
3695 for (; B != BEnd; ++B)
3696 AddCXXBaseSpecifier(*B, Record);
3697 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00003698
3699 // Flush any expressions that were written as part of the base specifiers.
3700 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003701 }
3702
3703 CXXBaseSpecifiersToWrite.clear();
3704}
3705
Alexis Hunt1d792652011-01-08 20:30:50 +00003706void ASTWriter::AddCXXCtorInitializers(
3707 const CXXCtorInitializer * const *CtorInitializers,
3708 unsigned NumCtorInitializers,
3709 RecordDataImpl &Record) {
3710 Record.push_back(NumCtorInitializers);
3711 for (unsigned i=0; i != NumCtorInitializers; ++i) {
3712 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003713
3714 Record.push_back(Init->isBaseInitializer());
3715 if (Init->isBaseInitializer()) {
3716 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3717 Record.push_back(Init->isBaseVirtual());
3718 } else {
Francois Pichetd583da02010-12-04 09:14:42 +00003719 Record.push_back(Init->isIndirectMemberInitializer());
3720 if (Init->isIndirectMemberInitializer())
3721 AddDeclRef(Init->getIndirectMember(), Record);
3722 else
3723 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003724 }
Francois Pichetd583da02010-12-04 09:14:42 +00003725
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003726 AddSourceLocation(Init->getMemberLocation(), Record);
3727 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003728 AddSourceLocation(Init->getLParenLoc(), Record);
3729 AddSourceLocation(Init->getRParenLoc(), Record);
3730 Record.push_back(Init->isWritten());
3731 if (Init->isWritten()) {
3732 Record.push_back(Init->getSourceOrder());
3733 } else {
3734 Record.push_back(Init->getNumArrayIndices());
3735 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3736 AddDeclRef(Init->getArrayIndex(i), Record);
3737 }
3738 }
3739}
3740
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003741void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3742 assert(D->DefinitionData);
3743 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3744 Record.push_back(Data.UserDeclaredConstructor);
3745 Record.push_back(Data.UserDeclaredCopyConstructor);
3746 Record.push_back(Data.UserDeclaredCopyAssignment);
3747 Record.push_back(Data.UserDeclaredDestructor);
3748 Record.push_back(Data.Aggregate);
3749 Record.push_back(Data.PlainOldData);
3750 Record.push_back(Data.Empty);
3751 Record.push_back(Data.Polymorphic);
3752 Record.push_back(Data.Abstract);
3753 Record.push_back(Data.HasTrivialConstructor);
3754 Record.push_back(Data.HasTrivialCopyConstructor);
3755 Record.push_back(Data.HasTrivialCopyAssignment);
3756 Record.push_back(Data.HasTrivialDestructor);
3757 Record.push_back(Data.ComputedVisibleConversions);
3758 Record.push_back(Data.DeclaredDefaultConstructor);
3759 Record.push_back(Data.DeclaredCopyConstructor);
3760 Record.push_back(Data.DeclaredCopyAssignment);
3761 Record.push_back(Data.DeclaredDestructor);
3762
3763 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003764 if (Data.NumBases > 0)
3765 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3766 Record);
3767
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003768 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3769 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003770 if (Data.NumVBases > 0)
3771 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3772 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003773
3774 AddUnresolvedSet(Data.Conversions, Record);
3775 AddUnresolvedSet(Data.VisibleConversions, Record);
3776 // Data.Definition is the owning decl, no need to write it.
3777 AddDeclRef(Data.FirstFriend, Record);
3778}
3779
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003780void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00003781 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003782 assert(!Chain && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00003783 assert(FirstDeclID == NextDeclID &&
3784 FirstTypeID == NextTypeID &&
3785 FirstIdentID == NextIdentID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00003786 FirstSelectorID == NextSelectorID &&
Douglas Gregor91096292010-10-02 19:29:26 +00003787 FirstMacroID == NextMacroID &&
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003788 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00003789 "Setting chain after writing has started.");
3790 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003791
3792 FirstDeclID += Chain->getTotalNumDecls();
3793 FirstTypeID += Chain->getTotalNumTypes();
3794 FirstIdentID += Chain->getTotalNumIdentifiers();
3795 FirstSelectorID += Chain->getTotalNumSelectors();
3796 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003797 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003798 NextDeclID = FirstDeclID;
3799 NextTypeID = FirstTypeID;
3800 NextIdentID = FirstIdentID;
3801 NextSelectorID = FirstSelectorID;
3802 NextMacroID = FirstMacroID;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003803 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00003804}
3805
Sebastian Redl539c5062010-08-18 23:57:32 +00003806void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00003807 IdentifierIDs[II] = ID;
Douglas Gregor68051a72011-02-11 00:26:14 +00003808 if (II->hasMacroDefinition())
3809 DeserializedMacroNames.push_back(II);
Sebastian Redlff4a2952010-07-23 23:49:55 +00003810}
3811
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003812void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003813 // Always take the highest-numbered type index. This copes with an interesting
3814 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003815 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003816 // keep the higher-numbered entry so that we can properly write it out to
3817 // the AST file.
3818 TypeIdx &StoredIdx = TypeIdxs[T];
3819 if (Idx.getIndex() >= StoredIdx.getIndex())
3820 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003821}
3822
Sebastian Redl539c5062010-08-18 23:57:32 +00003823void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003824 DeclIDs[D] = ID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003825}
Sebastian Redl834bb972010-08-04 17:20:04 +00003826
Sebastian Redl539c5062010-08-18 23:57:32 +00003827void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003828 SelectorIDs[S] = ID;
3829}
Douglas Gregor91096292010-10-02 19:29:26 +00003830
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003831void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00003832 MacroDefinition *MD) {
3833 MacroDefinitions[MD] = ID;
3834}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003835
3836void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3837 assert(D->isDefinition());
3838 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3839 // We are interested when a PCH decl is modified.
3840 if (RD->getPCHLevel() > 0) {
3841 // A forward reference was mutated into a definition. Rewrite it.
3842 // FIXME: This happens during template instantiation, should we
3843 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003844 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003845 }
3846
3847 for (CXXRecordDecl::redecl_iterator
3848 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3849 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3850 if (Redecl == RD)
3851 continue;
3852
3853 // We are interested when a PCH decl is modified.
3854 if (Redecl->getPCHLevel() > 0) {
3855 UpdateRecord &Record = DeclUpdates[Redecl];
3856 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3857 assert(Redecl->DefinitionData);
3858 assert(Redecl->DefinitionData->Definition == D);
3859 AddDeclRef(D, Record); // the DefinitionDecl
3860 }
3861 }
3862 }
3863}
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003864void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3865 // TU and namespaces are handled elsewhere.
3866 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3867 return;
3868
3869 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3870 return; // Not a source decl added to a DeclContext from PCH.
3871
3872 AddUpdatedDeclContext(DC);
3873}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003874
3875void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3876 assert(D->isImplicit());
3877 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3878 return; // Not a source member added to a class from PCH.
3879 if (!isa<CXXMethodDecl>(D))
3880 return; // We are interested in lazily declared implicit methods.
3881
3882 // A decl coming from PCH was modified.
3883 assert(RD->isDefinition());
3884 UpdateRecord &Record = DeclUpdates[RD];
3885 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3886 AddDeclRef(D, Record);
3887}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003888
3889void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3890 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00003891 // The specializations set is kept in the canonical template.
3892 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003893 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3894 return; // Not a source specialization added to a template from PCH.
3895
3896 UpdateRecord &Record = DeclUpdates[TD];
3897 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3898 AddDeclRef(D, Record);
3899}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003900
3901ASTSerializationListener::~ASTSerializationListener() { }