blob: 1cb195dd310ac632c33890289f244b17aeed849e [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);
Abramo Bagnara509357842011-03-05 14:42:21 +0000421 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000422}
John McCall17001972009-10-18 01:05:36 +0000423void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
424 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
425 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
426 Record.push_back(TL.getSizeExpr() ? 1 : 0);
427 if (TL.getSizeExpr())
428 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000429}
John McCall17001972009-10-18 01:05:36 +0000430void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
431 VisitArrayTypeLoc(TL);
432}
433void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
434 VisitArrayTypeLoc(TL);
435}
436void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
437 VisitArrayTypeLoc(TL);
438}
439void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
440 DependentSizedArrayTypeLoc TL) {
441 VisitArrayTypeLoc(TL);
442}
443void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
444 DependentSizedExtVectorTypeLoc TL) {
445 Writer.AddSourceLocation(TL.getNameLoc(), Record);
446}
447void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
448 Writer.AddSourceLocation(TL.getNameLoc(), Record);
449}
450void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
451 Writer.AddSourceLocation(TL.getNameLoc(), Record);
452}
453void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
454 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
455 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Douglas Gregor7fb25412010-10-01 18:44:50 +0000456 Record.push_back(TL.getTrailingReturn());
John McCall17001972009-10-18 01:05:36 +0000457 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
458 Writer.AddDeclRef(TL.getArg(i), Record);
459}
460void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
461 VisitFunctionTypeLoc(TL);
462}
463void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
464 VisitFunctionTypeLoc(TL);
465}
John McCallb96ec562009-12-04 22:46:56 +0000466void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
467 Writer.AddSourceLocation(TL.getNameLoc(), Record);
468}
John McCall17001972009-10-18 01:05:36 +0000469void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
470 Writer.AddSourceLocation(TL.getNameLoc(), Record);
471}
472void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000473 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
474 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
475 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000476}
477void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000478 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
479 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
480 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
481 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000482}
483void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
484 Writer.AddSourceLocation(TL.getNameLoc(), Record);
485}
Richard Smith30482bc2011-02-20 03:19:35 +0000486void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
487 Writer.AddSourceLocation(TL.getNameLoc(), Record);
488}
John McCall17001972009-10-18 01:05:36 +0000489void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
490 Writer.AddSourceLocation(TL.getNameLoc(), Record);
491}
492void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
493 Writer.AddSourceLocation(TL.getNameLoc(), Record);
494}
John McCall81904512011-01-06 01:58:22 +0000495void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
496 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
497 if (TL.hasAttrOperand()) {
498 SourceRange range = TL.getAttrOperandParensRange();
499 Writer.AddSourceLocation(range.getBegin(), Record);
500 Writer.AddSourceLocation(range.getEnd(), Record);
501 }
502 if (TL.hasAttrExprOperand()) {
503 Expr *operand = TL.getAttrExprOperand();
504 Record.push_back(operand ? 1 : 0);
505 if (operand) Writer.AddStmt(operand);
506 } else if (TL.hasAttrEnumOperand()) {
507 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
508 }
509}
John McCall17001972009-10-18 01:05:36 +0000510void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
511 Writer.AddSourceLocation(TL.getNameLoc(), Record);
512}
John McCallcebee162009-10-18 09:09:24 +0000513void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
514 SubstTemplateTypeParmTypeLoc TL) {
515 Writer.AddSourceLocation(TL.getNameLoc(), Record);
516}
Douglas Gregorada4b792011-01-14 02:55:32 +0000517void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
518 SubstTemplateTypeParmPackTypeLoc TL) {
519 Writer.AddSourceLocation(TL.getNameLoc(), Record);
520}
John McCall17001972009-10-18 01:05:36 +0000521void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
522 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +0000523 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
524 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
525 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
526 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000527 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
528 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000529}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000530void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
531 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
532 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
533}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000534void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000535 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000536 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000537}
John McCalle78aac42010-03-10 03:28:59 +0000538void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
539 Writer.AddSourceLocation(TL.getNameLoc(), Record);
540}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000541void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000542 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000543 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000544 Writer.AddSourceLocation(TL.getNameLoc(), Record);
545}
John McCallc392f372010-06-11 00:33:02 +0000546void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
547 DependentTemplateSpecializationTypeLoc TL) {
548 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000549 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000550 Writer.AddSourceLocation(TL.getNameLoc(), Record);
551 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
552 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
553 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000554 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
555 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000556}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000557void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
558 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
559}
John McCall17001972009-10-18 01:05:36 +0000560void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
561 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000562}
563void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
564 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000565 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
566 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
567 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
568 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000569}
John McCallfc93cf92009-10-22 22:37:11 +0000570void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
571 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000572}
John McCall8f115c62009-10-16 21:56:05 +0000573
Chris Lattner19cea4e2009-04-22 05:57:30 +0000574//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000575// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000576//===----------------------------------------------------------------------===//
577
Chris Lattner28fa4e62009-04-26 22:26:21 +0000578static void EmitBlockID(unsigned ID, const char *Name,
579 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000580 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000581 Record.clear();
582 Record.push_back(ID);
583 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
584
585 // Emit the block name if present.
586 if (Name == 0 || Name[0] == 0) return;
587 Record.clear();
588 while (*Name)
589 Record.push_back(*Name++);
590 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
591}
592
593static void EmitRecordID(unsigned ID, const char *Name,
594 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000595 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000596 Record.clear();
597 Record.push_back(ID);
598 while (*Name)
599 Record.push_back(*Name++);
600 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000601}
602
603static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000604 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000605#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000606 RECORD(STMT_STOP);
607 RECORD(STMT_NULL_PTR);
608 RECORD(STMT_NULL);
609 RECORD(STMT_COMPOUND);
610 RECORD(STMT_CASE);
611 RECORD(STMT_DEFAULT);
612 RECORD(STMT_LABEL);
613 RECORD(STMT_IF);
614 RECORD(STMT_SWITCH);
615 RECORD(STMT_WHILE);
616 RECORD(STMT_DO);
617 RECORD(STMT_FOR);
618 RECORD(STMT_GOTO);
619 RECORD(STMT_INDIRECT_GOTO);
620 RECORD(STMT_CONTINUE);
621 RECORD(STMT_BREAK);
622 RECORD(STMT_RETURN);
623 RECORD(STMT_DECL);
624 RECORD(STMT_ASM);
625 RECORD(EXPR_PREDEFINED);
626 RECORD(EXPR_DECL_REF);
627 RECORD(EXPR_INTEGER_LITERAL);
628 RECORD(EXPR_FLOATING_LITERAL);
629 RECORD(EXPR_IMAGINARY_LITERAL);
630 RECORD(EXPR_STRING_LITERAL);
631 RECORD(EXPR_CHARACTER_LITERAL);
632 RECORD(EXPR_PAREN);
633 RECORD(EXPR_UNARY_OPERATOR);
634 RECORD(EXPR_SIZEOF_ALIGN_OF);
635 RECORD(EXPR_ARRAY_SUBSCRIPT);
636 RECORD(EXPR_CALL);
637 RECORD(EXPR_MEMBER);
638 RECORD(EXPR_BINARY_OPERATOR);
639 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
640 RECORD(EXPR_CONDITIONAL_OPERATOR);
641 RECORD(EXPR_IMPLICIT_CAST);
642 RECORD(EXPR_CSTYLE_CAST);
643 RECORD(EXPR_COMPOUND_LITERAL);
644 RECORD(EXPR_EXT_VECTOR_ELEMENT);
645 RECORD(EXPR_INIT_LIST);
646 RECORD(EXPR_DESIGNATED_INIT);
647 RECORD(EXPR_IMPLICIT_VALUE_INIT);
648 RECORD(EXPR_VA_ARG);
649 RECORD(EXPR_ADDR_LABEL);
650 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000651 RECORD(EXPR_CHOOSE);
652 RECORD(EXPR_GNU_NULL);
653 RECORD(EXPR_SHUFFLE_VECTOR);
654 RECORD(EXPR_BLOCK);
655 RECORD(EXPR_BLOCK_DECL_REF);
656 RECORD(EXPR_OBJC_STRING_LITERAL);
657 RECORD(EXPR_OBJC_ENCODE);
658 RECORD(EXPR_OBJC_SELECTOR_EXPR);
659 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
660 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
661 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
662 RECORD(EXPR_OBJC_KVC_REF_EXPR);
663 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000664 RECORD(STMT_OBJC_FOR_COLLECTION);
665 RECORD(STMT_OBJC_CATCH);
666 RECORD(STMT_OBJC_FINALLY);
667 RECORD(STMT_OBJC_AT_TRY);
668 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
669 RECORD(STMT_OBJC_AT_THROW);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000670 RECORD(EXPR_CXX_OPERATOR_CALL);
671 RECORD(EXPR_CXX_CONSTRUCT);
672 RECORD(EXPR_CXX_STATIC_CAST);
673 RECORD(EXPR_CXX_DYNAMIC_CAST);
674 RECORD(EXPR_CXX_REINTERPRET_CAST);
675 RECORD(EXPR_CXX_CONST_CAST);
676 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
677 RECORD(EXPR_CXX_BOOL_LITERAL);
678 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000679 RECORD(EXPR_CXX_TYPEID_EXPR);
680 RECORD(EXPR_CXX_TYPEID_TYPE);
681 RECORD(EXPR_CXX_UUIDOF_EXPR);
682 RECORD(EXPR_CXX_UUIDOF_TYPE);
683 RECORD(EXPR_CXX_THIS);
684 RECORD(EXPR_CXX_THROW);
685 RECORD(EXPR_CXX_DEFAULT_ARG);
686 RECORD(EXPR_CXX_BIND_TEMPORARY);
687 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
688 RECORD(EXPR_CXX_NEW);
689 RECORD(EXPR_CXX_DELETE);
690 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
691 RECORD(EXPR_EXPR_WITH_CLEANUPS);
692 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
693 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
694 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
695 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
696 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
697 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
698 RECORD(EXPR_CXX_NOEXCEPT);
699 RECORD(EXPR_OPAQUE_VALUE);
700 RECORD(EXPR_BINARY_TYPE_TRAIT);
701 RECORD(EXPR_PACK_EXPANSION);
702 RECORD(EXPR_SIZEOF_PACK);
703 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000704 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000705#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000706}
Mike Stump11289f42009-09-09 15:08:12 +0000707
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000708void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000709 RecordData Record;
710 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000711
Sebastian Redl539c5062010-08-18 23:57:32 +0000712#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
713#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000714
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000715 // AST Top-Level Block.
Sebastian Redlf1642042010-08-18 23:57:22 +0000716 BLOCK(AST_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000717 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000718 RECORD(TYPE_OFFSET);
719 RECORD(DECL_OFFSET);
720 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000721 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000722 RECORD(IDENTIFIER_OFFSET);
723 RECORD(IDENTIFIER_TABLE);
724 RECORD(EXTERNAL_DEFINITIONS);
725 RECORD(SPECIAL_TYPES);
726 RECORD(STATISTICS);
727 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000728 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000729 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
730 RECORD(SELECTOR_OFFSETS);
731 RECORD(METHOD_POOL);
732 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000733 RECORD(SOURCE_LOCATION_OFFSETS);
734 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000735 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000736 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek17437132010-01-22 20:59:36 +0000737 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregoraae92242010-03-19 21:51:54 +0000738 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redl595c5132010-07-08 22:01:51 +0000739 RECORD(CHAINED_METADATA);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000740 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000741 RECORD(TU_UPDATE_LEXICAL);
742 RECORD(REDECLS_UPDATE_LATEST);
743 RECORD(SEMA_DECL_REFS);
744 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
745 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
746 RECORD(DECL_REPLACEMENTS);
747 RECORD(UPDATE_VISIBLE);
748 RECORD(DECL_UPDATE_OFFSETS);
749 RECORD(DECL_UPDATES);
750 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
751 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000752 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000753 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000754 RECORD(FP_PRAGMA_OPTIONS);
755 RECORD(OPENCL_EXTENSIONS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000756
Chris Lattner28fa4e62009-04-26 22:26:21 +0000757 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000758 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000759 RECORD(SM_SLOC_FILE_ENTRY);
760 RECORD(SM_SLOC_BUFFER_ENTRY);
761 RECORD(SM_SLOC_BUFFER_BLOB);
762 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
763 RECORD(SM_LINE_TABLE);
Mike Stump11289f42009-09-09 15:08:12 +0000764
Chris Lattner28fa4e62009-04-26 22:26:21 +0000765 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000766 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000767 RECORD(PP_MACRO_OBJECT_LIKE);
768 RECORD(PP_MACRO_FUNCTION_LIKE);
769 RECORD(PP_TOKEN);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000770
Douglas Gregor12bfa382009-10-17 00:13:19 +0000771 // Decls and Types block.
772 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000773 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000774 RECORD(TYPE_COMPLEX);
775 RECORD(TYPE_POINTER);
776 RECORD(TYPE_BLOCK_POINTER);
777 RECORD(TYPE_LVALUE_REFERENCE);
778 RECORD(TYPE_RVALUE_REFERENCE);
779 RECORD(TYPE_MEMBER_POINTER);
780 RECORD(TYPE_CONSTANT_ARRAY);
781 RECORD(TYPE_INCOMPLETE_ARRAY);
782 RECORD(TYPE_VARIABLE_ARRAY);
783 RECORD(TYPE_VECTOR);
784 RECORD(TYPE_EXT_VECTOR);
785 RECORD(TYPE_FUNCTION_PROTO);
786 RECORD(TYPE_FUNCTION_NO_PROTO);
787 RECORD(TYPE_TYPEDEF);
788 RECORD(TYPE_TYPEOF_EXPR);
789 RECORD(TYPE_TYPEOF);
790 RECORD(TYPE_RECORD);
791 RECORD(TYPE_ENUM);
792 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000793 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000794 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000795 RECORD(TYPE_DECLTYPE);
796 RECORD(TYPE_ELABORATED);
797 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
798 RECORD(TYPE_UNRESOLVED_USING);
799 RECORD(TYPE_INJECTED_CLASS_NAME);
800 RECORD(TYPE_OBJC_OBJECT);
801 RECORD(TYPE_TEMPLATE_TYPE_PARM);
802 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
803 RECORD(TYPE_DEPENDENT_NAME);
804 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
805 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
806 RECORD(TYPE_PAREN);
807 RECORD(TYPE_PACK_EXPANSION);
808 RECORD(TYPE_ATTRIBUTED);
809 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000810 RECORD(DECL_TRANSLATION_UNIT);
811 RECORD(DECL_TYPEDEF);
812 RECORD(DECL_ENUM);
813 RECORD(DECL_RECORD);
814 RECORD(DECL_ENUM_CONSTANT);
815 RECORD(DECL_FUNCTION);
816 RECORD(DECL_OBJC_METHOD);
817 RECORD(DECL_OBJC_INTERFACE);
818 RECORD(DECL_OBJC_PROTOCOL);
819 RECORD(DECL_OBJC_IVAR);
820 RECORD(DECL_OBJC_AT_DEFS_FIELD);
821 RECORD(DECL_OBJC_CLASS);
822 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
823 RECORD(DECL_OBJC_CATEGORY);
824 RECORD(DECL_OBJC_CATEGORY_IMPL);
825 RECORD(DECL_OBJC_IMPLEMENTATION);
826 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
827 RECORD(DECL_OBJC_PROPERTY);
828 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000829 RECORD(DECL_FIELD);
830 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000831 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000832 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000833 RECORD(DECL_FILE_SCOPE_ASM);
834 RECORD(DECL_BLOCK);
835 RECORD(DECL_CONTEXT_LEXICAL);
836 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000837 RECORD(DECL_NAMESPACE);
838 RECORD(DECL_NAMESPACE_ALIAS);
839 RECORD(DECL_USING);
840 RECORD(DECL_USING_SHADOW);
841 RECORD(DECL_USING_DIRECTIVE);
842 RECORD(DECL_UNRESOLVED_USING_VALUE);
843 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
844 RECORD(DECL_LINKAGE_SPEC);
845 RECORD(DECL_CXX_RECORD);
846 RECORD(DECL_CXX_METHOD);
847 RECORD(DECL_CXX_CONSTRUCTOR);
848 RECORD(DECL_CXX_DESTRUCTOR);
849 RECORD(DECL_CXX_CONVERSION);
850 RECORD(DECL_ACCESS_SPEC);
851 RECORD(DECL_FRIEND);
852 RECORD(DECL_FRIEND_TEMPLATE);
853 RECORD(DECL_CLASS_TEMPLATE);
854 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
855 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
856 RECORD(DECL_FUNCTION_TEMPLATE);
857 RECORD(DECL_TEMPLATE_TYPE_PARM);
858 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
859 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
860 RECORD(DECL_STATIC_ASSERT);
861 RECORD(DECL_CXX_BASE_SPECIFIERS);
862 RECORD(DECL_INDIRECTFIELD);
863 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
864
Douglas Gregor92a96f52011-02-08 21:58:10 +0000865 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
866 RECORD(PPD_MACRO_INSTANTIATION);
867 RECORD(PPD_MACRO_DEFINITION);
868 RECORD(PPD_INCLUSION_DIRECTIVE);
869
Douglas Gregor12bfa382009-10-17 00:13:19 +0000870 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000871 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000872#undef RECORD
873#undef BLOCK
874 Stream.ExitBlock();
875}
876
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000877/// \brief Adjusts the given filename to only write out the portion of the
878/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000879///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000880/// \param Filename the file name to adjust.
881///
882/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
883/// the returned filename will be adjusted by this system root.
884///
885/// \returns either the original filename (if it needs no adjustment) or the
886/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000887static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000888adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
889 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000890
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000891 if (!isysroot)
892 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000893
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000894 // Verify that the filename and the system root have the same prefix.
895 unsigned Pos = 0;
896 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
897 if (Filename[Pos] != isysroot[Pos])
898 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000899
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000900 // We hit the end of the filename before we hit the end of the system root.
901 if (!Filename[Pos])
902 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000903
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000904 // If the file name has a '/' at the current position, skip over the '/'.
905 // We distinguish sysroot-based includes from absolute includes by the
906 // absence of '/' at the beginning of sysroot-based includes.
907 if (Filename[Pos] == '/')
908 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000909
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000910 return Filename + Pos;
911}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000912
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000913/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000914void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot,
915 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000916 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000917
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000918 // Metadata
919 const TargetInfo &Target = Context.Target;
920 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000921 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl539c5062010-08-18 23:57:32 +0000922 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000923 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
924 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000925 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
926 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
927 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000928 // Target triple or chained PCH name
929 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000930 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000931
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000932 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000933 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
934 Record.push_back(VERSION_MAJOR);
935 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000936 Record.push_back(CLANG_VERSION_MAJOR);
937 Record.push_back(CLANG_VERSION_MINOR);
938 Record.push_back(isysroot != 0);
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000939 // FIXME: This writes the absolute path for chained headers.
940 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
941 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump11289f42009-09-09 15:08:12 +0000942
Douglas Gregor45fe0362009-05-12 01:31:05 +0000943 // Original file name
944 SourceManager &SM = Context.getSourceManager();
945 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
946 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000947 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregor45fe0362009-05-12 01:31:05 +0000948 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
949 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
950
Michael J. Spencer740857f2010-12-21 16:45:57 +0000951 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +0000952
Michael J. Spencer740857f2010-12-21 16:45:57 +0000953 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000954
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +0000955 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000956 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000957 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000958 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000959 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000960 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000961 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000962
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000963 // Original PCH directory
964 if (!OutputFile.empty() && OutputFile != "-") {
965 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
966 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
967 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
968 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
969
970 llvm::SmallString<128> OutputPath(OutputFile);
971
972 llvm::sys::fs::make_absolute(OutputPath);
973 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
974
975 RecordData Record;
976 Record.push_back(ORIGINAL_PCH_DIR);
977 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
978 }
979
Ted Kremenek18e066f2010-01-22 22:12:47 +0000980 // Repository branch/version information.
981 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000982 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenek18e066f2010-01-22 22:12:47 +0000983 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
984 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000985 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +0000986 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +0000987 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
988 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000989}
990
991/// \brief Write the LangOptions structure.
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000992void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor55abb232009-04-10 20:39:37 +0000993 RecordData Record;
994 Record.push_back(LangOpts.Trigraphs);
995 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
996 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
997 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
998 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carruthe03aa552010-04-17 20:17:31 +0000999 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor55abb232009-04-10 20:39:37 +00001000 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1001 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1002 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1003 Record.push_back(LangOpts.C99); // C99 Support
1004 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +00001005 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
1006 // already saved elsewhere.
Douglas Gregor55abb232009-04-10 20:39:37 +00001007 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1008 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +00001009 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +00001010
Douglas Gregor55abb232009-04-10 20:39:37 +00001011 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1012 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001013 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian45878032010-02-09 19:31:38 +00001014 // modern abi enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001015 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian45878032010-02-09 19:31:38 +00001016 // modern abi enabled.
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +00001017 Record.push_back(LangOpts.AppleKext); // Apple's kernel extensions ABI
Ted Kremenek1d56c9e2010-12-23 21:35:43 +00001018 Record.push_back(LangOpts.ObjCDefaultSynthProperties); // Objective-C auto-synthesized
1019 // properties enabled.
Fariborz Jahanian62c56022010-04-22 21:01:59 +00001020 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump11289f42009-09-09 15:08:12 +00001021
Douglas Gregor55abb232009-04-10 20:39:37 +00001022 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +00001023 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1024 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00001025 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +00001026 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Anders Carlssonce8dd3a2011-02-19 23:53:54 +00001027 Record.push_back(LangOpts.ObjCExceptions);
Anders Carlsson6bbd2682011-02-23 03:04:54 +00001028 Record.push_back(LangOpts.CXXExceptions);
1029 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor55abb232009-04-10 20:39:37 +00001030
Douglas Gregordbe39272011-02-01 15:15:22 +00001031 Record.push_back(LangOpts.MSBitfields); // MS-compatible structure layout
Douglas Gregor55abb232009-04-10 20:39:37 +00001032 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1033 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1034 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1035
Chris Lattner258172e2009-04-27 07:35:58 +00001036 // Whether static initializers are protected by locks.
1037 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00001038 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +00001039 Record.push_back(LangOpts.Blocks); // block extension to C
1040 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1041 // they are unused.
1042 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1043 // (modulo the platform support).
1044
Chris Lattner51924e512010-06-26 21:25:03 +00001045 Record.push_back(LangOpts.getSignedOverflowBehavior());
1046 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor55abb232009-04-10 20:39:37 +00001047
1048 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +00001049 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +00001050 // defined.
1051 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1052 // opposed to __DYNAMIC__).
1053 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1054
1055 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1056 // used (instead of C99 semantics).
1057 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +00001058 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
1059 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +00001060 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
1061 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +00001062 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Argyrios Kyrtzidisa88942a2011-01-15 02:56:16 +00001063 Record.push_back(LangOpts.ShortEnums); // Should the enum type be equivalent
1064 // to the smallest integer type with
1065 // enough room.
Douglas Gregor55abb232009-04-10 20:39:37 +00001066 Record.push_back(LangOpts.getGCMode());
1067 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +00001068 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +00001069 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00001070 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +00001071 Record.push_back(LangOpts.CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +00001072 Record.push_back(LangOpts.CatchUndefined);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001073 Record.push_back(LangOpts.DefaultFPContract);
Anders Carlsson9cedbef2009-08-22 22:30:33 +00001074 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +00001075 Record.push_back(LangOpts.SpellChecking);
Roman Divacky65b88cd2011-03-01 17:40:53 +00001076 Record.push_back(LangOpts.MRTD);
Sebastian Redl539c5062010-08-18 23:57:32 +00001077 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +00001078}
1079
Douglas Gregora7f71a92009-04-10 03:52:48 +00001080//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00001081// stat cache Serialization
1082//===----------------------------------------------------------------------===//
1083
1084namespace {
1085// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001086class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001087public:
1088 typedef const char * key_type;
1089 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001090
Chris Lattner2a6fa472010-11-23 19:28:12 +00001091 typedef struct stat data_type;
1092 typedef const data_type &data_type_ref;
Douglas Gregorc5046832009-04-27 18:38:38 +00001093
1094 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001095 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001096 }
Mike Stump11289f42009-09-09 15:08:12 +00001097
1098 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +00001099 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1100 data_type_ref Data) {
1101 unsigned StrLen = strlen(path);
1102 clang::io::Emit16(Out, StrLen);
Chris Lattner2a6fa472010-11-23 19:28:12 +00001103 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregorc5046832009-04-27 18:38:38 +00001104 clang::io::Emit8(Out, DataLen);
1105 return std::make_pair(StrLen + 1, DataLen);
1106 }
Mike Stump11289f42009-09-09 15:08:12 +00001107
Douglas Gregorc5046832009-04-27 18:38:38 +00001108 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1109 Out.write(path, KeyLen);
1110 }
Mike Stump11289f42009-09-09 15:08:12 +00001111
Chris Lattner2a6fa472010-11-23 19:28:12 +00001112 void EmitData(llvm::raw_ostream &Out, key_type_ref,
Douglas Gregorc5046832009-04-27 18:38:38 +00001113 data_type_ref Data, unsigned DataLen) {
1114 using namespace clang::io;
1115 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +00001116
Chris Lattner2a6fa472010-11-23 19:28:12 +00001117 Emit32(Out, (uint32_t) Data.st_ino);
1118 Emit32(Out, (uint32_t) Data.st_dev);
1119 Emit16(Out, (uint16_t) Data.st_mode);
1120 Emit64(Out, (uint64_t) Data.st_mtime);
1121 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregorc5046832009-04-27 18:38:38 +00001122
1123 assert(Out.tell() - Start == DataLen && "Wrong data length");
1124 }
1125};
1126} // end anonymous namespace
1127
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001128/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001129void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001130 // Build the on-disk hash table containing information about every
1131 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001132 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +00001133 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001134 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +00001135 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001136 Stat != StatEnd; ++Stat, ++NumStatEntries) {
1137 const char *Filename = Stat->first();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001138 Generator.insert(Filename, Stat->second);
1139 }
Mike Stump11289f42009-09-09 15:08:12 +00001140
Douglas Gregorc5046832009-04-27 18:38:38 +00001141 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001142 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +00001143 uint32_t BucketOffset;
1144 {
1145 llvm::raw_svector_ostream Out(StatCacheData);
1146 // Make sure that no bucket is at offset 0
1147 clang::io::Emit32(Out, 0);
1148 BucketOffset = Generator.Emit(Out);
1149 }
1150
1151 // Create a blob abbreviation
1152 using namespace llvm;
1153 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001154 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +00001155 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1156 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1157 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1158 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1159
1160 // Write the stat cache
1161 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001162 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +00001163 Record.push_back(BucketOffset);
1164 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001165 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +00001166}
1167
1168//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +00001169// Source Manager Serialization
1170//===----------------------------------------------------------------------===//
1171
1172/// \brief Create an abbreviation for the SLocEntry that refers to a
1173/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001174static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001175 using namespace llvm;
1176 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001177 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001178 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1179 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1180 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1181 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001182 // FileEntry fields.
1183 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1184 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora7f71a92009-04-10 03:52:48 +00001185 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001186 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001187}
1188
1189/// \brief Create an abbreviation for the SLocEntry that refers to a
1190/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001191static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001192 using namespace llvm;
1193 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001194 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001195 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1196 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1197 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1199 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001200 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001201}
1202
1203/// \brief Create an abbreviation for the SLocEntry that refers to a
1204/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001205static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001206 using namespace llvm;
1207 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001208 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001209 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001210 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001211}
1212
1213/// \brief Create an abbreviation for the SLocEntry that refers to an
1214/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001215static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001216 using namespace llvm;
1217 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001218 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001219 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1220 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1221 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1222 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001223 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001224 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001225}
1226
Douglas Gregor09b69892011-02-10 17:09:37 +00001227namespace {
1228 // Trait used for the on-disk hash table of header search information.
1229 class HeaderFileInfoTrait {
1230 ASTWriter &Writer;
1231 HeaderSearch &HS;
1232
1233 public:
1234 HeaderFileInfoTrait(ASTWriter &Writer, HeaderSearch &HS)
1235 : Writer(Writer), HS(HS) { }
1236
1237 typedef const char *key_type;
1238 typedef key_type key_type_ref;
1239
1240 typedef HeaderFileInfo data_type;
1241 typedef const data_type &data_type_ref;
1242
1243 static unsigned ComputeHash(const char *path) {
1244 // The hash is based only on the filename portion of the key, so that the
1245 // reader can match based on filenames when symlinking or excess path
1246 // elements ("foo/../", "../") change the form of the name. However,
1247 // complete path is still the key.
1248 return llvm::HashString(llvm::sys::path::filename(path));
1249 }
1250
1251 std::pair<unsigned,unsigned>
1252 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1253 data_type_ref Data) {
1254 unsigned StrLen = strlen(path);
1255 clang::io::Emit16(Out, StrLen);
1256 unsigned DataLen = 1 + 2 + 4;
1257 clang::io::Emit8(Out, DataLen);
1258 return std::make_pair(StrLen + 1, DataLen);
1259 }
1260
1261 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1262 Out.write(path, KeyLen);
1263 }
1264
1265 void EmitData(llvm::raw_ostream &Out, key_type_ref,
1266 data_type_ref Data, unsigned DataLen) {
1267 using namespace clang::io;
1268 uint64_t Start = Out.tell(); (void)Start;
1269
1270 unsigned char Flags = (Data.isImport << 3)
1271 | (Data.DirInfo << 1)
1272 | Data.Resolved;
1273 Emit8(Out, (uint8_t)Flags);
1274 Emit16(Out, (uint16_t) Data.NumIncludes);
1275
1276 if (!Data.ControllingMacro)
1277 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1278 else
1279 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
1280 assert(Out.tell() - Start == DataLen && "Wrong data length");
1281 }
1282 };
1283} // end anonymous namespace
1284
1285/// \brief Write the header search block for the list of files that
1286///
1287/// \param HS The header search structure to save.
1288///
1289/// \param Chain Whether we're creating a chained AST file.
1290void ASTWriter::WriteHeaderSearch(HeaderSearch &HS, const char* isysroot) {
1291 llvm::SmallVector<const FileEntry *, 16> FilesByUID;
1292 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1293
1294 if (FilesByUID.size() > HS.header_file_size())
1295 FilesByUID.resize(HS.header_file_size());
1296
1297 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1298 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
1299 llvm::SmallVector<const char *, 4> SavedStrings;
1300 unsigned NumHeaderSearchEntries = 0;
1301 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1302 const FileEntry *File = FilesByUID[UID];
1303 if (!File)
1304 continue;
1305
1306 const HeaderFileInfo &HFI = HS.header_file_begin()[UID];
1307 if (HFI.External && Chain)
1308 continue;
1309
1310 // Turn the file name into an absolute path, if it isn't already.
1311 const char *Filename = File->getName();
1312 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1313
1314 // If we performed any translation on the file name at all, we need to
1315 // save this string, since the generator will refer to it later.
1316 if (Filename != File->getName()) {
1317 Filename = strdup(Filename);
1318 SavedStrings.push_back(Filename);
1319 }
1320
1321 Generator.insert(Filename, HFI, GeneratorTrait);
1322 ++NumHeaderSearchEntries;
1323 }
1324
1325 // Create the on-disk hash table in a buffer.
1326 llvm::SmallString<4096> TableData;
1327 uint32_t BucketOffset;
1328 {
1329 llvm::raw_svector_ostream Out(TableData);
1330 // Make sure that no bucket is at offset 0
1331 clang::io::Emit32(Out, 0);
1332 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1333 }
1334
1335 // Create a blob abbreviation
1336 using namespace llvm;
1337 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1338 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1339 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1340 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1341 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1342 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1343
1344 // Write the stat cache
1345 RecordData Record;
1346 Record.push_back(HEADER_SEARCH_TABLE);
1347 Record.push_back(BucketOffset);
1348 Record.push_back(NumHeaderSearchEntries);
1349 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1350
1351 // Free all of the strings we had to duplicate.
1352 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1353 free((void*)SavedStrings[I]);
1354}
1355
Douglas Gregora7f71a92009-04-10 03:52:48 +00001356/// \brief Writes the block containing the serialized form of the
1357/// source manager.
1358///
1359/// TODO: We should probably use an on-disk hash table (stored in a
1360/// blob), indexed based on the file name, so that we only create
1361/// entries for files that we actually need. In the common case (no
1362/// errors), we probably won't have to create file entries for any of
1363/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001364void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001365 const Preprocessor &PP,
1366 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001367 RecordData Record;
1368
Chris Lattner0910e3b2009-04-10 17:16:57 +00001369 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001370 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001371
1372 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001373 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1374 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1375 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1376 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001377
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001378 // Write the line table.
1379 if (SourceMgr.hasLineTable()) {
1380 LineTableInfo &LineTable = SourceMgr.getLineTable();
1381
1382 // Emit the file names
1383 Record.push_back(LineTable.getNumFilenames());
1384 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1385 // Emit the file name
1386 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001387 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001388 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1389 Record.push_back(FilenameLen);
1390 if (FilenameLen)
1391 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1392 }
Mike Stump11289f42009-09-09 15:08:12 +00001393
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001394 // Emit the line entries
1395 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1396 L != LEnd; ++L) {
1397 // Emit the file ID
1398 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +00001399
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001400 // Emit the line entries
1401 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +00001402 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001403 LEEnd = L->second.end();
1404 LE != LEEnd; ++LE) {
1405 Record.push_back(LE->FileOffset);
1406 Record.push_back(LE->LineNo);
1407 Record.push_back(LE->FilenameID);
1408 Record.push_back((unsigned)LE->FileKind);
1409 Record.push_back(LE->IncludeOffset);
1410 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001411 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001412 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001413 }
1414
Douglas Gregor258ae542009-04-27 06:38:32 +00001415 // Write out the source location entry table. We skip the first
1416 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001417 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001418 RecordData PreloadSLocs;
Sebastian Redl5c415f32010-07-22 17:01:13 +00001419 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1420 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1421 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1422 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001423 // Get this source location entry.
1424 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001425
Douglas Gregor258ae542009-04-27 06:38:32 +00001426 // Record the offset of this source-location entry.
1427 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1428
1429 // Figure out which record code to use.
1430 unsigned Code;
1431 if (SLoc->isFile()) {
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001432 if (SLoc->getFile().getContentCache()->OrigEntry)
Sebastian Redl539c5062010-08-18 23:57:32 +00001433 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001434 else
Sebastian Redl539c5062010-08-18 23:57:32 +00001435 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001436 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001437 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001438 Record.clear();
1439 Record.push_back(Code);
1440
1441 Record.push_back(SLoc->getOffset());
1442 if (SLoc->isFile()) {
1443 const SrcMgr::FileInfo &File = SLoc->getFile();
1444 Record.push_back(File.getIncludeLoc().getRawEncoding());
1445 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1446 Record.push_back(File.hasLineDirectives());
1447
1448 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001449 if (Content->OrigEntry) {
1450 assert(Content->OrigEntry == Content->ContentsEntry &&
1451 "Writing to AST an overriden file is not supported");
1452
Douglas Gregor258ae542009-04-27 06:38:32 +00001453 // The source location entry is a file. The blob associated
1454 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001455
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001456 // Emit size/modification time for this file.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001457 Record.push_back(Content->OrigEntry->getSize());
1458 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001459
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001460 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001461 const char *Filename = Content->OrigEntry->getName();
Michael J. Spencer740857f2010-12-21 16:45:57 +00001462 llvm::SmallString<128> FilePath(Filename);
Anders Carlssona4267052011-03-08 16:04:35 +00001463
1464 // Ask the file manager to fixup the relative path for us. This will
1465 // honor the working directory.
1466 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1467
1468 // FIXME: This call to make_absolute shouldn't be necessary, the
1469 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencer740857f2010-12-21 16:45:57 +00001470 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001471 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001472
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001473 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001474 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001475 } else {
1476 // The source location entry is a buffer. The blob associated
1477 // with this entry contains the contents of the buffer.
1478
1479 // We add one to the size so that we capture the trailing NULL
1480 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1481 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001482 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001483 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001484 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001485 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1486 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001487 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001488 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001489 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001490 llvm::StringRef(Buffer->getBufferStart(),
1491 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001492
1493 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl5c415f32010-07-22 17:01:13 +00001494 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001495 }
1496 } else {
1497 // The source location entry is an instantiation.
1498 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1499 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1500 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1501 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1502
1503 // Compute the token length for this macro expansion.
1504 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001505 if (I + 1 != N)
1506 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001507 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1508 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1509 }
1510 }
1511
Douglas Gregor8f45df52009-04-16 22:23:12 +00001512 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001513
1514 if (SLocEntryOffsets.empty())
1515 return;
1516
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001517 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001518 // table is used for lazily loading source-location information.
1519 using namespace llvm;
1520 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001521 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001522 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1523 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1524 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1525 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001526
Douglas Gregor258ae542009-04-27 06:38:32 +00001527 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001528 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001529 Record.push_back(SLocEntryOffsets.size());
Sebastian Redlc1d035f2010-09-22 20:19:08 +00001530 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1531 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Douglas Gregor258ae542009-04-27 06:38:32 +00001532 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001533 (const char *)data(SLocEntryOffsets),
Chris Lattner12d61d32009-04-27 19:01:47 +00001534 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001535
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001536 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001537 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001538 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001539}
1540
Douglas Gregorc5046832009-04-27 18:38:38 +00001541//===----------------------------------------------------------------------===//
1542// Preprocessor Serialization
1543//===----------------------------------------------------------------------===//
1544
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001545static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1546 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1547 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1548 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1549 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1550 return X.first->getName().compare(Y.first->getName());
1551}
1552
Chris Lattnereeffaef2009-04-10 17:15:23 +00001553/// \brief Writes the block containing the serialized form of the
1554/// preprocessor.
1555///
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001556void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001557 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001558
Chris Lattner0af3ba12009-04-13 01:29:17 +00001559 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1560 if (PP.getCounterValue() != 0) {
1561 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001562 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001563 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001564 }
1565
1566 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001567 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001568
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001569 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001570 // FIXME: use diagnostics subsystem for localization etc.
1571 if (PP.SawDateOrTime())
1572 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001573
Douglas Gregor796d76a2010-10-20 22:00:55 +00001574
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001575 // Loop over all the macro definitions that are live at the end of the file,
1576 // emitting each to the PP section.
Douglas Gregoraae92242010-03-19 21:51:54 +00001577 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001578
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001579 // Construct the list of macro definitions that need to be serialized.
1580 llvm::SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
1581 MacrosToEmit;
1582 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor68051a72011-02-11 00:26:14 +00001583 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1584 E = PP.macro_end(Chain == 0);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001585 I != E; ++I) {
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001586 MacroDefinitionsSeen.insert(I->first);
1587 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1588 }
1589
1590 // Sort the set of macro definitions that need to be serialized by the
1591 // name of the macro, to provide a stable ordering.
1592 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1593 &compareMacroDefinitions);
1594
Douglas Gregor68051a72011-02-11 00:26:14 +00001595 // Resolve any identifiers that defined macros at the time they were
1596 // deserialized, adding them to the list of macros to emit (if appropriate).
1597 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1598 IdentifierInfo *Name
1599 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1600 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1601 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1602 }
1603
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001604 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1605 const IdentifierInfo *Name = MacrosToEmit[I].first;
1606 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor68051a72011-02-11 00:26:14 +00001607 if (!MI)
1608 continue;
1609
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001610 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001611 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001612 // Also skip macros from a AST file if we're chaining.
Douglas Gregoreb114da2010-10-01 01:03:07 +00001613
1614 // FIXME: There is a (probably minor) optimization we could do here, if
1615 // the macro comes from the original PCH but the identifier comes from a
1616 // chained PCH, by storing the offset into the original PCH rather than
1617 // writing the macro definition a second time.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001618 if (MI->isBuiltinMacro() ||
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001619 (Chain && Name->isFromAST() && MI->isFromAST()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001620 continue;
1621
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001622 AddIdentifierRef(Name, Record);
1623 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001624 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1625 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001626
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001627 unsigned Code;
1628 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001629 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001630 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001631 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001632
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001633 Record.push_back(MI->isC99Varargs());
1634 Record.push_back(MI->isGNUVarargs());
1635 Record.push_back(MI->getNumArgs());
1636 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1637 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001638 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001639 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001640
Douglas Gregoraae92242010-03-19 21:51:54 +00001641 // If we have a detailed preprocessing record, record the macro definition
1642 // ID that corresponds to this macro.
1643 if (PPRec)
1644 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001645
Douglas Gregor8f45df52009-04-16 22:23:12 +00001646 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001647 Record.clear();
1648
Chris Lattner2199f5b2009-04-10 18:08:30 +00001649 // Emit the tokens array.
1650 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1651 // Note that we know that the preprocessor does not have any annotation
1652 // tokens in it because they are created by the parser, and thus can't be
1653 // in a macro definition.
1654 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001655
Chris Lattner2199f5b2009-04-10 18:08:30 +00001656 Record.push_back(Tok.getLocation().getRawEncoding());
1657 Record.push_back(Tok.getLength());
1658
Chris Lattner2199f5b2009-04-10 18:08:30 +00001659 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1660 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001661 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001662 // FIXME: Should translate token kind to a stable encoding.
1663 Record.push_back(Tok.getKind());
1664 // FIXME: Should translate token flags to a stable encoding.
1665 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001666
Sebastian Redl539c5062010-08-18 23:57:32 +00001667 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001668 Record.clear();
1669 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001670 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001671 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001672 Stream.ExitBlock();
1673
1674 if (PPRec)
1675 WritePreprocessorDetail(*PPRec);
1676}
1677
1678void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
1679 if (PPRec.begin(Chain) == PPRec.end(Chain))
1680 return;
1681
1682 // Enter the preprocessor block.
1683 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001684
Douglas Gregoraae92242010-03-19 21:51:54 +00001685 // If the preprocessor has a preprocessing record, emit it.
1686 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001687 using namespace llvm;
1688
1689 // Set up the abbreviation for
1690 unsigned InclusionAbbrev = 0;
1691 {
1692 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1693 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
1694 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1695 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1696 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1697 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1698 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1699 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1700 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1701 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1702 }
1703
1704 unsigned IndexBase = Chain ? PPRec.getNumPreallocatedEntities() : 0;
1705 RecordData Record;
1706 for (PreprocessingRecord::iterator E = PPRec.begin(Chain),
1707 EEnd = PPRec.end(Chain);
1708 E != EEnd; ++E) {
1709 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001710
Douglas Gregor92a96f52011-02-08 21:58:10 +00001711 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1712 // Record this macro definition's location.
1713 MacroID ID = getMacroDefinitionID(MD);
1714
1715 // Don't write the macro definition if it is from another AST file.
1716 if (ID < FirstMacroID)
Douglas Gregoraae92242010-03-19 21:51:54 +00001717 continue;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001718
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001719 // Notify the serialization listener that we're serializing this entity.
1720 if (SerializationListener)
1721 SerializationListener->SerializedPreprocessedEntity(*E,
Douglas Gregor92a96f52011-02-08 21:58:10 +00001722 Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001723
Douglas Gregor92a96f52011-02-08 21:58:10 +00001724 unsigned Position = ID - FirstMacroID;
1725 if (Position != MacroDefinitionOffsets.size()) {
1726 if (Position > MacroDefinitionOffsets.size())
1727 MacroDefinitionOffsets.resize(Position + 1);
1728
1729 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
1730 } else
1731 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001732
Douglas Gregor92a96f52011-02-08 21:58:10 +00001733 Record.push_back(IndexBase + NumPreprocessingRecords++);
1734 Record.push_back(ID);
1735 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1736 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1737 AddIdentifierRef(MD->getName(), Record);
1738 AddSourceLocation(MD->getLocation(), Record);
1739 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1740 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00001741 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001742
Douglas Gregor92a96f52011-02-08 21:58:10 +00001743 // Notify the serialization listener that we're serializing this entity.
1744 if (SerializationListener)
1745 SerializationListener->SerializedPreprocessedEntity(*E,
1746 Stream.GetCurrentBitNo());
1747
1748 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1749 Record.push_back(IndexBase + NumPreprocessingRecords++);
1750 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1751 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1752 AddIdentifierRef(MI->getName(), Record);
1753 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1754 Stream.EmitRecord(PPD_MACRO_INSTANTIATION, Record);
1755 continue;
1756 }
1757
1758 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1759 Record.push_back(PPD_INCLUSION_DIRECTIVE);
1760 Record.push_back(IndexBase + NumPreprocessingRecords++);
1761 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1762 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1763 Record.push_back(ID->getFileName().size());
1764 Record.push_back(ID->wasInQuotes());
1765 Record.push_back(static_cast<unsigned>(ID->getKind()));
1766 llvm::SmallString<64> Buffer;
1767 Buffer += ID->getFileName();
1768 Buffer += ID->getFile()->getName();
1769 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1770 continue;
1771 }
1772
1773 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1774 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001775 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001776
Douglas Gregoraae92242010-03-19 21:51:54 +00001777 // Write the offsets table for the preprocessing record.
1778 if (NumPreprocessingRecords > 0) {
1779 // Write the offsets table for identifier IDs.
1780 using namespace llvm;
1781 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001782 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregoraae92242010-03-19 21:51:54 +00001783 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1784 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1785 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1786 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001787
Douglas Gregoraae92242010-03-19 21:51:54 +00001788 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001789 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001790 Record.push_back(NumPreprocessingRecords);
1791 Record.push_back(MacroDefinitionOffsets.size());
1792 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001793 (const char *)data(MacroDefinitionOffsets),
Douglas Gregoraae92242010-03-19 21:51:54 +00001794 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1795 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001796}
1797
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001798void ASTWriter::WritePragmaDiagnosticMappings(const Diagnostic &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001799 RecordData Record;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001800 for (Diagnostic::DiagStatePointsTy::const_iterator
1801 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1802 I != E; ++I) {
1803 const Diagnostic::DiagStatePoint &point = *I;
1804 if (point.Loc.isInvalid())
1805 continue;
1806
1807 Record.push_back(point.Loc.getRawEncoding());
1808 for (Diagnostic::DiagState::iterator
1809 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
1810 unsigned diag = I->first, map = I->second;
1811 if (map & 0x10) { // mapping from a diagnostic pragma.
1812 Record.push_back(diag);
1813 Record.push_back(map & 0x7);
1814 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001815 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001816 Record.push_back(-1); // mark the end of the diag/map pairs for this
1817 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001818 }
1819
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00001820 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001821 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001822}
1823
Anders Carlsson9bb83e82011-03-06 18:41:18 +00001824void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
1825 if (CXXBaseSpecifiersOffsets.empty())
1826 return;
1827
1828 RecordData Record;
1829
1830 // Create a blob abbreviation for the C++ base specifiers offsets.
1831 using namespace llvm;
1832
1833 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1834 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
1835 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
1836 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1837 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1838
1839 // Write the selector offsets table.
1840 Record.clear();
1841 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
1842 Record.push_back(CXXBaseSpecifiersOffsets.size());
1843 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
1844 (const char *)CXXBaseSpecifiersOffsets.data(),
1845 CXXBaseSpecifiersOffsets.size() * sizeof(uint32_t));
1846}
1847
Douglas Gregorc5046832009-04-27 18:38:38 +00001848//===----------------------------------------------------------------------===//
1849// Type Serialization
1850//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001851
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001852/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001853void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00001854 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001855 if (Idx.getIndex() == 0) // we haven't seen this type before.
1856 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00001857
Douglas Gregor9b3932c2010-10-05 18:37:06 +00001858 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00001859
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001860 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001861 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001862 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001863 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001864 else if (TypeOffsets.size() < Index) {
1865 TypeOffsets.resize(Index + 1);
1866 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001867 }
1868
1869 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001870
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001871 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001872 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001873
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001874 if (T.hasLocalNonFastQualifiers()) {
1875 Qualifiers Qs = T.getLocalQualifiers();
1876 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001877 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001878 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00001879 } else {
1880 switch (T->getTypeClass()) {
1881 // For all of the concrete, non-dependent types, call the
1882 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001883#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001884 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001885#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001886#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001887 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001888 }
1889
1890 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001891 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001892
1893 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001894 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001895}
1896
Douglas Gregorc5046832009-04-27 18:38:38 +00001897//===----------------------------------------------------------------------===//
1898// Declaration Serialization
1899//===----------------------------------------------------------------------===//
1900
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001901/// \brief Write the block containing all of the declaration IDs
1902/// lexically declared within the given DeclContext.
1903///
1904/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1905/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001906uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001907 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001908 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001909 return 0;
1910
Douglas Gregor8f45df52009-04-16 22:23:12 +00001911 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001912 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001913 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001914 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001915 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1916 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001917 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001918
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001919 ++NumLexicalDeclContexts;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001920 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001921 reinterpret_cast<char*>(Decls.data()),
1922 Decls.size() * sizeof(KindDeclIDPair));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001923 return Offset;
1924}
1925
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001926void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001927 using namespace llvm;
1928 RecordData Record;
1929
1930 // Write the type offsets array
1931 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001932 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001933 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1934 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1935 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1936 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001937 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001938 Record.push_back(TypeOffsets.size());
1939 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001940 (const char *)data(TypeOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001941 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1942
1943 // Write the declaration offsets array
1944 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001945 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001946 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1947 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1948 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1949 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001950 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001951 Record.push_back(DeclOffsets.size());
1952 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001953 (const char *)data(DeclOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001954 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1955}
1956
Douglas Gregorc5046832009-04-27 18:38:38 +00001957//===----------------------------------------------------------------------===//
1958// Global Method Pool and Selector Serialization
1959//===----------------------------------------------------------------------===//
1960
Douglas Gregore84a9da2009-04-20 20:36:09 +00001961namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001962// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001963class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001964 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001965
1966public:
1967 typedef Selector key_type;
1968 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001969
Sebastian Redl834bb972010-08-04 17:20:04 +00001970 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00001971 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00001972 ObjCMethodList Instance, Factory;
1973 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00001974 typedef const data_type& data_type_ref;
1975
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001976 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001977
Douglas Gregorc78d3462009-04-24 21:10:55 +00001978 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00001979 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001980 }
Mike Stump11289f42009-09-09 15:08:12 +00001981
1982 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001983 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1984 data_type_ref Methods) {
1985 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1986 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00001987 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1988 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001989 Method = Method->Next)
1990 if (Method->Method)
1991 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00001992 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001993 Method = Method->Next)
1994 if (Method->Method)
1995 DataLen += 4;
1996 clang::io::Emit16(Out, DataLen);
1997 return std::make_pair(KeyLen, DataLen);
1998 }
Mike Stump11289f42009-09-09 15:08:12 +00001999
Douglas Gregor95c13f52009-04-25 17:48:32 +00002000 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00002001 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002002 assert((Start >> 32) == 0 && "Selector key offset too large");
2003 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002004 unsigned N = Sel.getNumArgs();
2005 clang::io::Emit16(Out, N);
2006 if (N == 0)
2007 N = 1;
2008 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00002009 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002010 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2011 }
Mike Stump11289f42009-09-09 15:08:12 +00002012
Douglas Gregorc78d3462009-04-24 21:10:55 +00002013 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002014 data_type_ref Methods, unsigned DataLen) {
2015 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00002016 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002017 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002018 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002019 Method = Method->Next)
2020 if (Method->Method)
2021 ++NumInstanceMethods;
2022
2023 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002024 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002025 Method = Method->Next)
2026 if (Method->Method)
2027 ++NumFactoryMethods;
2028
2029 clang::io::Emit16(Out, NumInstanceMethods);
2030 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00002031 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002032 Method = Method->Next)
2033 if (Method->Method)
2034 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002035 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002036 Method = Method->Next)
2037 if (Method->Method)
2038 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002039
2040 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002041 }
2042};
2043} // end anonymous namespace
2044
Sebastian Redla19a67f2010-08-03 21:58:15 +00002045/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002046///
2047/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002048/// in an on-disk hash table indexed by the selector. The hash table also
2049/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002050void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002051 using namespace llvm;
2052
Sebastian Redla19a67f2010-08-03 21:58:15 +00002053 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002054 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002055 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002056 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002057 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002058 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002059 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002060 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002061
Sebastian Redla19a67f2010-08-03 21:58:15 +00002062 // Create the on-disk hash table representation. We walk through every
2063 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002064 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002065 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002066 I = SelectorIDs.begin(), E = SelectorIDs.end();
2067 I != E; ++I) {
2068 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002069 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002070 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002071 I->second,
2072 ObjCMethodList(),
2073 ObjCMethodList()
2074 };
2075 if (F != SemaRef.MethodPool.end()) {
2076 Data.Instance = F->second.first;
2077 Data.Factory = F->second.second;
2078 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002079 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002080 // changed.
2081 if (Chain && I->second < FirstSelectorID) {
2082 // Selector already exists. Did it change?
2083 bool changed = false;
2084 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2085 M = M->Next) {
2086 if (M->Method->getPCHLevel() == 0)
2087 changed = true;
2088 }
2089 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2090 M = M->Next) {
2091 if (M->Method->getPCHLevel() == 0)
2092 changed = true;
2093 }
2094 if (!changed)
2095 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002096 } else if (Data.Instance.Method || Data.Factory.Method) {
2097 // A new method pool entry.
2098 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002099 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002100 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002101 }
2102
Douglas Gregorc78d3462009-04-24 21:10:55 +00002103 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002104 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002105 uint32_t BucketOffset;
2106 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002107 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002108 llvm::raw_svector_ostream Out(MethodPool);
2109 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002110 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002111 BucketOffset = Generator.Emit(Out, Trait);
2112 }
2113
2114 // Create a blob abbreviation
2115 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002116 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002117 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002118 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002119 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2120 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2121
Douglas Gregor95c13f52009-04-25 17:48:32 +00002122 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002123 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002124 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002125 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002126 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002127 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002128
2129 // Create a blob abbreviation for the selector table offsets.
2130 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002131 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002132 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor95c13f52009-04-25 17:48:32 +00002133 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2134 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2135
2136 // Write the selector offsets table.
2137 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002138 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002139 Record.push_back(SelectorOffsets.size());
2140 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00002141 (const char *)data(SelectorOffsets),
Douglas Gregor95c13f52009-04-25 17:48:32 +00002142 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002143 }
2144}
2145
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002146/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002147void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002148 using namespace llvm;
2149 if (SemaRef.ReferencedSelectors.empty())
2150 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002151
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002152 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002153
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002154 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002155 // very tricky to fix, and given that @selector shouldn't really appear in
2156 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002157 for (DenseMap<Selector, SourceLocation>::iterator S =
2158 SemaRef.ReferencedSelectors.begin(),
2159 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2160 Selector Sel = (*S).first;
2161 SourceLocation Loc = (*S).second;
2162 AddSelectorRef(Sel, Record);
2163 AddSourceLocation(Loc, Record);
2164 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002165 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002166}
2167
Douglas Gregorc5046832009-04-27 18:38:38 +00002168//===----------------------------------------------------------------------===//
2169// Identifier Table Serialization
2170//===----------------------------------------------------------------------===//
2171
Douglas Gregorc78d3462009-04-24 21:10:55 +00002172namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002173class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002174 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002175 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002176
Douglas Gregor1d583f22009-04-28 21:18:29 +00002177 /// \brief Determines whether this is an "interesting" identifier
2178 /// that needs a full IdentifierInfo structure written into the hash
2179 /// table.
2180 static bool isInterestingIdentifier(const IdentifierInfo *II) {
2181 return II->isPoisoned() ||
2182 II->isExtensionToken() ||
2183 II->hasMacroDefinition() ||
2184 II->getObjCOrBuiltinID() ||
2185 II->getFETokenInfo<void>();
2186 }
2187
Douglas Gregore84a9da2009-04-20 20:36:09 +00002188public:
2189 typedef const IdentifierInfo* key_type;
2190 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002191
Sebastian Redl539c5062010-08-18 23:57:32 +00002192 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002193 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002194
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002195 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00002196 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00002197
2198 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00002199 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00002200 }
Mike Stump11289f42009-09-09 15:08:12 +00002201
2202 std::pair<unsigned,unsigned>
2203 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002204 IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002205 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002206 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
2207 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00002208 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00002209 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00002210 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00002211 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002212 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2213 DEnd = IdentifierResolver::end();
2214 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00002215 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00002216 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00002217 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00002218 // We emit the key length after the data length so that every
2219 // string is preceded by a 16-bit length. This matches the PTH
2220 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00002221 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002222 return std::make_pair(KeyLen, DataLen);
2223 }
Mike Stump11289f42009-09-09 15:08:12 +00002224
2225 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00002226 unsigned KeyLen) {
2227 // Record the location of the key data. This is used when generating
2228 // the mapping from persistent IDs to strings.
2229 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002230 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002231 }
Mike Stump11289f42009-09-09 15:08:12 +00002232
2233 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002234 IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00002235 if (!isInterestingIdentifier(II)) {
2236 clang::io::Emit32(Out, ID << 1);
2237 return;
2238 }
Douglas Gregorb9256522009-04-28 21:32:13 +00002239
Douglas Gregor1d583f22009-04-28 21:18:29 +00002240 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002241 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002242 bool hasMacroDefinition =
2243 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00002244 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00002245 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002246 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
2247 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2248 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00002249 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002250 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00002251 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002252
Douglas Gregorc3366a52009-04-21 23:56:24 +00002253 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00002254 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00002255
Douglas Gregora868bbd2009-04-21 22:25:48 +00002256 // Emit the declaration IDs in reverse order, because the
2257 // IdentifierResolver provides the declarations as they would be
2258 // visible (e.g., the function "stat" would come before the struct
2259 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2260 // adds declarations to the end of the list (so we need to see the
2261 // struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00002262 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump11289f42009-09-09 15:08:12 +00002263 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00002264 IdentifierResolver::end());
2265 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
2266 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00002267 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00002268 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002269 }
2270};
2271} // end anonymous namespace
2272
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002273/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002274///
2275/// The identifier table consists of a blob containing string data
2276/// (the actual identifiers themselves) and a separate "offsets" index
2277/// that maps identifier IDs to locations within the blob.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002278void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002279 using namespace llvm;
2280
2281 // Create and write out the blob that contains the identifier
2282 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002283 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002284 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002285 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump11289f42009-09-09 15:08:12 +00002286
Douglas Gregore6648fb2009-04-28 20:33:11 +00002287 // Look for any identifiers that were named while processing the
2288 // headers, but are otherwise not needed. We add these to the hash
2289 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002290 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00002291 // file.
2292 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2293 IDEnd = PP.getIdentifierTable().end();
2294 ID != IDEnd; ++ID)
2295 getIdentifierRef(ID->second);
2296
Sebastian Redlff4a2952010-07-23 23:49:55 +00002297 // Create the on-disk hash table representation. We only store offsets
2298 // for identifiers that appear here for the first time.
2299 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002300 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002301 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2302 ID != IDEnd; ++ID) {
2303 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002304 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002305 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002306 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002307
Douglas Gregore84a9da2009-04-20 20:36:09 +00002308 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002309 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00002310 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002311 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002312 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002313 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002314 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002315 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002316 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002317 }
2318
2319 // Create a blob abbreviation
2320 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002321 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00002322 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002323 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00002324 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002325
2326 // Write the identifier table
2327 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002328 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002329 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002330 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002331 }
2332
2333 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00002334 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002335 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00002336 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2337 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2338 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2339
2340 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002341 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00002342 Record.push_back(IdentifierOffsets.size());
2343 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00002344 (const char *)data(IdentifierOffsets),
Douglas Gregor0e149972009-04-25 19:10:14 +00002345 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002346}
2347
Douglas Gregorc5046832009-04-27 18:38:38 +00002348//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002349// DeclContext's Name Lookup Table Serialization
2350//===----------------------------------------------------------------------===//
2351
2352namespace {
2353// Trait used for the on-disk hash table used in the method pool.
2354class ASTDeclContextNameLookupTrait {
2355 ASTWriter &Writer;
2356
2357public:
2358 typedef DeclarationName key_type;
2359 typedef key_type key_type_ref;
2360
2361 typedef DeclContext::lookup_result data_type;
2362 typedef const data_type& data_type_ref;
2363
2364 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2365
2366 unsigned ComputeHash(DeclarationName Name) {
2367 llvm::FoldingSetNodeID ID;
2368 ID.AddInteger(Name.getNameKind());
2369
2370 switch (Name.getNameKind()) {
2371 case DeclarationName::Identifier:
2372 ID.AddString(Name.getAsIdentifierInfo()->getName());
2373 break;
2374 case DeclarationName::ObjCZeroArgSelector:
2375 case DeclarationName::ObjCOneArgSelector:
2376 case DeclarationName::ObjCMultiArgSelector:
2377 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2378 break;
2379 case DeclarationName::CXXConstructorName:
2380 case DeclarationName::CXXDestructorName:
2381 case DeclarationName::CXXConversionFunctionName:
2382 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
2383 break;
2384 case DeclarationName::CXXOperatorName:
2385 ID.AddInteger(Name.getCXXOverloadedOperator());
2386 break;
2387 case DeclarationName::CXXLiteralOperatorName:
2388 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2389 case DeclarationName::CXXUsingDirective:
2390 break;
2391 }
2392
2393 return ID.ComputeHash();
2394 }
2395
2396 std::pair<unsigned,unsigned>
2397 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2398 data_type_ref Lookup) {
2399 unsigned KeyLen = 1;
2400 switch (Name.getNameKind()) {
2401 case DeclarationName::Identifier:
2402 case DeclarationName::ObjCZeroArgSelector:
2403 case DeclarationName::ObjCOneArgSelector:
2404 case DeclarationName::ObjCMultiArgSelector:
2405 case DeclarationName::CXXConstructorName:
2406 case DeclarationName::CXXDestructorName:
2407 case DeclarationName::CXXConversionFunctionName:
2408 case DeclarationName::CXXLiteralOperatorName:
2409 KeyLen += 4;
2410 break;
2411 case DeclarationName::CXXOperatorName:
2412 KeyLen += 1;
2413 break;
2414 case DeclarationName::CXXUsingDirective:
2415 break;
2416 }
2417 clang::io::Emit16(Out, KeyLen);
2418
2419 // 2 bytes for num of decls and 4 for each DeclID.
2420 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2421 clang::io::Emit16(Out, DataLen);
2422
2423 return std::make_pair(KeyLen, DataLen);
2424 }
2425
2426 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2427 using namespace clang::io;
2428
2429 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2430 Emit8(Out, Name.getNameKind());
2431 switch (Name.getNameKind()) {
2432 case DeclarationName::Identifier:
2433 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2434 break;
2435 case DeclarationName::ObjCZeroArgSelector:
2436 case DeclarationName::ObjCOneArgSelector:
2437 case DeclarationName::ObjCMultiArgSelector:
2438 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2439 break;
2440 case DeclarationName::CXXConstructorName:
2441 case DeclarationName::CXXDestructorName:
2442 case DeclarationName::CXXConversionFunctionName:
2443 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2444 break;
2445 case DeclarationName::CXXOperatorName:
2446 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2447 Emit8(Out, Name.getCXXOverloadedOperator());
2448 break;
2449 case DeclarationName::CXXLiteralOperatorName:
2450 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2451 break;
2452 case DeclarationName::CXXUsingDirective:
2453 break;
2454 }
2455 }
2456
2457 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2458 data_type Lookup, unsigned DataLen) {
2459 uint64_t Start = Out.tell(); (void)Start;
2460 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2461 for (; Lookup.first != Lookup.second; ++Lookup.first)
2462 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2463
2464 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2465 }
2466};
2467} // end anonymous namespace
2468
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002469/// \brief Write the block containing all of the declaration IDs
2470/// visible from the given DeclContext.
2471///
2472/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002473/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002474uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2475 DeclContext *DC) {
2476 if (DC->getPrimaryContext() != DC)
2477 return 0;
2478
2479 // Since there is no name lookup into functions or methods, don't bother to
2480 // build a visible-declarations table for these entities.
2481 if (DC->isFunctionOrMethod())
2482 return 0;
2483
2484 // If not in C++, we perform name lookup for the translation unit via the
2485 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2486 // FIXME: In C++ we need the visible declarations in order to "see" the
2487 // friend declarations, is there a way to do this without writing the table ?
2488 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2489 return 0;
2490
2491 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00002492 if (DC->hasExternalVisibleStorage())
2493 DC->MaterializeVisibleDeclsFromExternalStorage();
2494 else
2495 DC->lookup(DeclarationName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002496
2497 // Serialize the contents of the mapping used for lookup. Note that,
2498 // although we have two very different code paths, the serialized
2499 // representation is the same for both cases: a declaration name,
2500 // followed by a size, followed by references to the visible
2501 // declarations that have that name.
2502 uint64_t Offset = Stream.GetCurrentBitNo();
2503 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2504 if (!Map || Map->empty())
2505 return 0;
2506
2507 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2508 ASTDeclContextNameLookupTrait Trait(*this);
2509
2510 // Create the on-disk hash table representation.
2511 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2512 D != DEnd; ++D) {
2513 DeclarationName Name = D->first;
2514 DeclContext::lookup_result Result = D->second.getLookupResult();
2515 Generator.insert(Name, Result, Trait);
2516 }
2517
2518 // Create the on-disk hash table in a buffer.
2519 llvm::SmallString<4096> LookupTable;
2520 uint32_t BucketOffset;
2521 {
2522 llvm::raw_svector_ostream Out(LookupTable);
2523 // Make sure that no bucket is at offset 0
2524 clang::io::Emit32(Out, 0);
2525 BucketOffset = Generator.Emit(Out, Trait);
2526 }
2527
2528 // Write the lookup table
2529 RecordData Record;
2530 Record.push_back(DECL_CONTEXT_VISIBLE);
2531 Record.push_back(BucketOffset);
2532 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2533 LookupTable.str());
2534
2535 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2536 ++NumVisibleDeclContexts;
2537 return Offset;
2538}
2539
Sebastian Redla4071b42010-08-24 00:50:09 +00002540/// \brief Write an UPDATE_VISIBLE block for the given context.
2541///
2542/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2543/// DeclContext in a dependent AST file. As such, they only exist for the TU
2544/// (in C++) and for namespaces.
2545void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00002546 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2547 if (!Map || Map->empty())
2548 return;
2549
2550 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2551 ASTDeclContextNameLookupTrait Trait(*this);
2552
2553 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002554 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2555 D != DEnd; ++D) {
2556 DeclarationName Name = D->first;
2557 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002558 // For any name that appears in this table, the results are complete, i.e.
2559 // they overwrite results from previous PCHs. Merging is always a mess.
2560 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002561 }
2562
2563 // Create the on-disk hash table in a buffer.
2564 llvm::SmallString<4096> LookupTable;
2565 uint32_t BucketOffset;
2566 {
2567 llvm::raw_svector_ostream Out(LookupTable);
2568 // Make sure that no bucket is at offset 0
2569 clang::io::Emit32(Out, 0);
2570 BucketOffset = Generator.Emit(Out, Trait);
2571 }
2572
2573 // Write the lookup table
2574 RecordData Record;
2575 Record.push_back(UPDATE_VISIBLE);
2576 Record.push_back(getDeclID(cast<Decl>(DC)));
2577 Record.push_back(BucketOffset);
2578 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2579}
2580
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002581/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2582void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2583 RecordData Record;
2584 Record.push_back(Opts.fp_contract);
2585 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2586}
2587
2588/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2589void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
2590 if (!SemaRef.Context.getLangOptions().OpenCL)
2591 return;
2592
2593 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2594 RecordData Record;
2595#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2596#include "clang/Basic/OpenCLExtensions.def"
2597 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2598}
2599
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002600//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00002601// General Serialization Routines
2602//===----------------------------------------------------------------------===//
2603
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002604/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002605void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00002606 Record.push_back(Attrs.size());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002607 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2608 const Attr * A = *i;
2609 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2610 AddSourceLocation(A->getLocation(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002611
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002612#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00002613
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002614 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002615}
2616
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002617void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002618 Record.push_back(Str.size());
2619 Record.insert(Record.end(), Str.begin(), Str.end());
2620}
2621
Douglas Gregore84a9da2009-04-20 20:36:09 +00002622/// \brief Note that the identifier II occurs at the given offset
2623/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002624void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002625 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002626 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00002627 // up earlier in the chain and thus don't need an offset.
2628 if (ID >= FirstIdentID)
2629 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002630}
2631
Douglas Gregor95c13f52009-04-25 17:48:32 +00002632/// \brief Note that the selector Sel occurs at the given offset
2633/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002634void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00002635 unsigned ID = SelectorIDs[Sel];
2636 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00002637 // Don't record offsets for selectors that are also available in a different
2638 // file.
2639 if (ID < FirstSelectorID)
2640 return;
2641 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002642}
2643
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002644ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00002645 : Stream(Stream), Chain(0), SerializationListener(0),
2646 FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00002647 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002648 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor91096292010-10-02 19:29:26 +00002649 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2650 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002651 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002652 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
2653 NextCXXBaseSpecifiersID(1)
2654{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002655}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002656
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002657void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002658 const std::string &OutputFile,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002659 const char *isysroot) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002660 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002661 Stream.Emit((unsigned)'C', 8);
2662 Stream.Emit((unsigned)'P', 8);
2663 Stream.Emit((unsigned)'C', 8);
2664 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002665
Chris Lattner28fa4e62009-04-26 22:26:21 +00002666 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002667
Sebastian Redl143413f2010-07-12 22:02:52 +00002668 if (Chain)
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002669 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002670 else
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002671 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile);
Sebastian Redl143413f2010-07-12 22:02:52 +00002672}
2673
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002674void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002675 const char *isysroot,
2676 const std::string &OutputFile) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002677 using namespace llvm;
2678
2679 ASTContext &Context = SemaRef.Context;
2680 Preprocessor &PP = SemaRef.PP;
2681
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002682 // The translation unit is the first declaration we'll emit.
2683 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlff4a2952010-07-23 23:49:55 +00002684 ++NextDeclID;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002685 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002686
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002687 // Make sure that we emit IdentifierInfos (and any attached
2688 // declarations) for builtins.
2689 {
2690 IdentifierTable &Table = PP.getIdentifierTable();
2691 llvm::SmallVector<const char *, 32> BuiltinNames;
2692 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2693 Context.getLangOptions().NoBuiltin);
2694 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2695 getIdentifierRef(&Table.get(BuiltinNames[I]));
2696 }
2697
Chris Lattner0c797362009-09-08 18:19:27 +00002698 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002699 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002700 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002701 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00002702 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2703 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00002704 }
Douglas Gregord4df8652009-04-22 22:02:47 +00002705
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002706 // Build a record containing all of the file scoped decls in this file.
2707 RecordData UnusedFileScopedDecls;
2708 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2709 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002710
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002711 RecordData WeakUndeclaredIdentifiers;
2712 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2713 WeakUndeclaredIdentifiers.push_back(
2714 SemaRef.WeakUndeclaredIdentifiers.size());
2715 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2716 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2717 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2718 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2719 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2720 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2721 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2722 }
2723 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002724
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002725 // Build a record containing all of the locally-scoped external
2726 // declarations in this header file. Generally, this record will be
2727 // empty.
2728 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002729 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00002730 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002731 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002732 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2733 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2734 TD != TDEnd; ++TD)
2735 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2736
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002737 // Build a record containing all of the ext_vector declarations.
2738 RecordData ExtVectorDecls;
2739 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2740 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2741
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002742 // Build a record containing all of the VTable uses information.
2743 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00002744 if (!SemaRef.VTableUses.empty()) {
2745 VTableUses.push_back(SemaRef.VTableUses.size());
2746 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2747 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2748 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2749 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2750 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002751 }
2752
2753 // Build a record containing all of dynamic classes declarations.
2754 RecordData DynamicClasses;
2755 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2756 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2757
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002758 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002759 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002760 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002761 I = SemaRef.PendingInstantiations.begin(),
2762 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2763 AddDeclRef(I->first, PendingInstantiations);
2764 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002765 }
2766 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2767 "There are local ones at end of translation unit!");
2768
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002769 // Build a record containing some declaration references.
2770 RecordData SemaDeclRefs;
2771 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2772 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2773 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2774 }
2775
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002776 RecordData CUDASpecialDeclRefs;
2777 if (Context.getcudaConfigureCallDecl()) {
2778 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
2779 }
2780
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002781 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002782 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002783 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002784 WriteMetadata(Context, isysroot, OutputFile);
Sebastian Redl143413f2010-07-12 22:02:52 +00002785 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002786 if (StatCalls && !isysroot)
Douglas Gregor11cfd942010-07-12 23:48:14 +00002787 WriteStatCache(*StatCalls);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002788 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc277ad12009-07-18 15:33:26 +00002789 // Write the record of special types.
2790 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002791
Steve Naroffc277ad12009-07-18 15:33:26 +00002792 AddTypeRef(Context.getBuiltinVaListType(), Record);
2793 AddTypeRef(Context.getObjCIdType(), Record);
2794 AddTypeRef(Context.getObjCSelType(), Record);
2795 AddTypeRef(Context.getObjCProtoType(), Record);
2796 AddTypeRef(Context.getObjCClassType(), Record);
2797 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2798 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2799 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002800 AddTypeRef(Context.getjmp_bufType(), Record);
2801 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002802 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2803 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpd0153282009-10-20 02:12:22 +00002804 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002805 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002806 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2807 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002808 Record.push_back(Context.isInt128Installed());
Sebastian Redl539c5062010-08-18 23:57:32 +00002809 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002810
Douglas Gregor1970d882009-04-26 03:49:13 +00002811 // Keep writing types and declarations until all types and
2812 // declarations have been written.
Sebastian Redl539c5062010-08-18 23:57:32 +00002813 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002814 WriteDeclsBlockAbbrevs();
2815 while (!DeclTypesToEmit.empty()) {
2816 DeclOrType DOT = DeclTypesToEmit.front();
2817 DeclTypesToEmit.pop();
2818 if (DOT.isType())
2819 WriteType(DOT.getType());
2820 else
2821 WriteDecl(Context, DOT.getDecl());
2822 }
2823 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002824
Douglas Gregor45053152009-10-17 17:25:45 +00002825 WritePreprocessor(PP);
Douglas Gregor09b69892011-02-10 17:09:37 +00002826 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00002827 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002828 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002829 WriteIdentifierTable(PP);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002830 WriteFPPragmaOptions(SemaRef.getFPOptions());
2831 WriteOpenCLExtensions(SemaRef);
Douglas Gregor745ed142009-04-25 18:35:21 +00002832
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002833 WriteTypeDeclOffsets();
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002834 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00002835
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002836 WriteCXXBaseSpecifiersOffsets();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002837
Douglas Gregord4df8652009-04-22 22:02:47 +00002838 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002839 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002840 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002841
2842 // Write the record containing tentative definitions.
2843 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002844 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002845
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002846 // Write the record containing unused file scoped decls.
2847 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002848 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002849
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002850 // Write the record containing weak undeclared identifiers.
2851 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002852 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002853 WeakUndeclaredIdentifiers);
2854
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002855 // Write the record containing locally-scoped external definitions.
2856 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002857 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002858 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002859
2860 // Write the record containing ext_vector type names.
2861 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002862 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002863
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002864 // Write the record containing VTable uses information.
2865 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002866 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002867
2868 // Write the record containing dynamic classes declarations.
2869 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002870 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002871
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002872 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002873 if (!PendingInstantiations.empty())
2874 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002875
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002876 // Write the record containing declaration references of Sema.
2877 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002878 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002879
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002880 // Write the record containing CUDA-specific declaration references.
2881 if (!CUDASpecialDeclRefs.empty())
2882 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
2883
Douglas Gregor08f01292009-04-17 22:13:46 +00002884 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002885 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002886 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002887 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002888 Record.push_back(NumLexicalDeclContexts);
2889 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00002890 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002891 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002892}
2893
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002894void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002895 const char *isysroot) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002896 using namespace llvm;
2897
2898 ASTContext &Context = SemaRef.Context;
2899 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002900
Sebastian Redl143413f2010-07-12 22:02:52 +00002901 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002902 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002903 WriteMetadata(Context, isysroot, "");
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002904 if (StatCalls && !isysroot)
2905 WriteStatCache(*StatCalls);
2906 // FIXME: Source manager block should only write new stuff, which could be
2907 // done by tracking the largest ID in the chain
2908 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002909
2910 // The special types are in the chained PCH.
2911
2912 // We don't start with the translation unit, but with its decls that
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002913 // don't come from the chained PCH.
Sebastian Redl143413f2010-07-12 22:02:52 +00002914 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002915 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002916 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2917 E = TU->noload_decls_end();
Sebastian Redl143413f2010-07-12 22:02:52 +00002918 I != E; ++I) {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002919 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002920 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002921 else if ((*I)->isChangedSinceDeserialization())
2922 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl143413f2010-07-12 22:02:52 +00002923 }
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002924 // We also need to write a lexical updates block for the TU.
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002925 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002926 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002927 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2928 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2929 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002930 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002931 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2932 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002933 NewGlobalDecls.size() * sizeof(KindDeclIDPair));
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00002934 // And a visible updates block for the DeclContexts.
2935 Abv = new llvm::BitCodeAbbrev();
2936 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2937 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2938 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2939 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2940 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2941 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl143413f2010-07-12 22:02:52 +00002942
Sebastian Redl98912122010-07-27 23:01:28 +00002943 // Build a record containing all of the new tentative definitions in this
2944 // file, in TentativeDefinitions order.
2945 RecordData TentativeDefinitions;
2946 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2947 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2948 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2949 }
2950
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002951 // Build a record containing all of the file scoped decls in this file.
2952 RecordData UnusedFileScopedDecls;
2953 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2954 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2955 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002956 }
2957
Sebastian Redl08aca90252010-08-05 18:21:25 +00002958 // We write the entire table, overwriting the tables from the chain.
2959 RecordData WeakUndeclaredIdentifiers;
2960 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2961 WeakUndeclaredIdentifiers.push_back(
2962 SemaRef.WeakUndeclaredIdentifiers.size());
2963 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2964 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2965 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2966 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2967 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2968 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2969 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2970 }
2971 }
2972
Sebastian Redl98912122010-07-27 23:01:28 +00002973 // Build a record containing all of the locally-scoped external
2974 // declarations in this header file. Generally, this record will be
2975 // empty.
2976 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002977 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl98912122010-07-27 23:01:28 +00002978 // nondeterminstic!
2979 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2980 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2981 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2982 TD != TDEnd; ++TD) {
2983 if (TD->second->getPCHLevel() == 0)
2984 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2985 }
2986
2987 // Build a record containing all of the ext_vector declarations.
2988 RecordData ExtVectorDecls;
2989 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2990 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2991 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2992 }
2993
Sebastian Redl08aca90252010-08-05 18:21:25 +00002994 // Build a record containing all of the VTable uses information.
2995 // We write everything here, because it's too hard to determine whether
2996 // a use is new to this part.
2997 RecordData VTableUses;
2998 if (!SemaRef.VTableUses.empty()) {
2999 VTableUses.push_back(SemaRef.VTableUses.size());
3000 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3001 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3002 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3003 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3004 }
3005 }
3006
3007 // Build a record containing all of dynamic classes declarations.
3008 RecordData DynamicClasses;
3009 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
3010 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
3011 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
3012
3013 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003014 RecordData PendingInstantiations;
Sebastian Redl08aca90252010-08-05 18:21:25 +00003015 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00003016 I = SemaRef.PendingInstantiations.begin(),
3017 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl08aca90252010-08-05 18:21:25 +00003018 if (I->first->getPCHLevel() == 0) {
Chandler Carruth54080172010-08-25 08:44:16 +00003019 AddDeclRef(I->first, PendingInstantiations);
3020 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003021 }
3022 }
3023 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3024 "There are local ones at end of translation unit!");
3025
3026 // Build a record containing some declaration references.
3027 // It's not worth the effort to avoid duplication here.
3028 RecordData SemaDeclRefs;
3029 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3030 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3031 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3032 }
3033
Sebastian Redl539c5062010-08-18 23:57:32 +00003034 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl143413f2010-07-12 22:02:52 +00003035 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003036 for (DeclsToRewriteTy::iterator
3037 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
3038 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl143413f2010-07-12 22:02:52 +00003039 while (!DeclTypesToEmit.empty()) {
3040 DeclOrType DOT = DeclTypesToEmit.front();
3041 DeclTypesToEmit.pop();
3042 if (DOT.isType())
3043 WriteType(DOT.getType());
3044 else
3045 WriteDecl(Context, DOT.getDecl());
3046 }
3047 Stream.ExitBlock();
3048
Sebastian Redl98912122010-07-27 23:01:28 +00003049 WritePreprocessor(PP);
Sebastian Redl51c79d82010-08-04 22:21:29 +00003050 WriteSelectors(SemaRef);
3051 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlff4a2952010-07-23 23:49:55 +00003052 WriteIdentifierTable(PP);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003053 WriteFPPragmaOptions(SemaRef.getFPOptions());
3054 WriteOpenCLExtensions(SemaRef);
3055
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003056 WriteTypeDeclOffsets();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003057 // FIXME: For chained PCH only write the new mappings (we currently
3058 // write all of them again).
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003059 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl98912122010-07-27 23:01:28 +00003060
Anders Carlsson9bb83e82011-03-06 18:41:18 +00003061 WriteCXXBaseSpecifiersOffsets();
3062
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003063 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003064 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003065 RecordData FirstLatestDeclIDs;
3066 for (FirstLatestDeclMap::iterator
3067 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
3068 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
3069 "Expected first & second to be in different PCHs");
3070 AddDeclRef(I->first, FirstLatestDeclIDs);
3071 AddDeclRef(I->second, FirstLatestDeclIDs);
3072 }
3073 if (!FirstLatestDeclIDs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003074 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003075
Sebastian Redl98912122010-07-27 23:01:28 +00003076 // Write the record containing external, unnamed definitions.
3077 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003078 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00003079
3080 // Write the record containing tentative definitions.
3081 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003082 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00003083
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003084 // Write the record containing unused file scoped decls.
3085 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003086 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003087
Sebastian Redl08aca90252010-08-05 18:21:25 +00003088 // Write the record containing weak undeclared identifiers.
3089 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003090 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl08aca90252010-08-05 18:21:25 +00003091 WeakUndeclaredIdentifiers);
3092
Sebastian Redl98912122010-07-27 23:01:28 +00003093 // Write the record containing locally-scoped external definitions.
3094 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003095 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl98912122010-07-27 23:01:28 +00003096 LocallyScopedExternalDecls);
3097
3098 // Write the record containing ext_vector type names.
3099 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003100 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003101
Sebastian Redl08aca90252010-08-05 18:21:25 +00003102 // Write the record containing VTable uses information.
3103 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003104 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003105
3106 // Write the record containing dynamic classes declarations.
3107 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003108 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003109
3110 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003111 if (!PendingInstantiations.empty())
3112 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003113
3114 // Write the record containing declaration references of Sema.
3115 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003116 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl98912122010-07-27 23:01:28 +00003117
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003118 // Write the updates to DeclContexts.
3119 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3120 I = UpdatedDeclContexts.begin(),
3121 E = UpdatedDeclContexts.end();
Sebastian Redla4071b42010-08-24 00:50:09 +00003122 I != E; ++I)
3123 WriteDeclContextVisibleUpdate(*I);
3124
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003125 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003126
Sebastian Redl98912122010-07-27 23:01:28 +00003127 Record.clear();
3128 Record.push_back(NumStatements);
3129 Record.push_back(NumMacros);
3130 Record.push_back(NumLexicalDeclContexts);
3131 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003132 WriteDeclReplacementsBlock();
Sebastian Redl539c5062010-08-18 23:57:32 +00003133 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl143413f2010-07-12 22:02:52 +00003134 Stream.ExitBlock();
3135}
3136
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003137void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003138 if (DeclUpdates.empty())
3139 return;
3140
3141 RecordData OffsetsRecord;
3142 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
3143 for (DeclUpdateMap::iterator
3144 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3145 const Decl *D = I->first;
3146 UpdateRecord &URec = I->second;
3147
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00003148 if (DeclsToRewrite.count(D))
3149 continue; // The decl will be written completely,no need to store updates.
3150
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003151 uint64_t Offset = Stream.GetCurrentBitNo();
3152 Stream.EmitRecord(DECL_UPDATES, URec);
3153
3154 OffsetsRecord.push_back(GetDeclRef(D));
3155 OffsetsRecord.push_back(Offset);
3156 }
3157 Stream.ExitBlock();
3158 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3159}
3160
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003161void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003162 if (ReplacedDecls.empty())
3163 return;
3164
3165 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003166 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003167 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
3168 Record.push_back(I->first);
3169 Record.push_back(I->second);
3170 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003171 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003172}
3173
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003174void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003175 Record.push_back(Loc.getRawEncoding());
3176}
3177
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003178void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003179 AddSourceLocation(Range.getBegin(), Record);
3180 AddSourceLocation(Range.getEnd(), Record);
3181}
3182
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003183void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003184 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00003185 const uint64_t *Words = Value.getRawData();
3186 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003187}
3188
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003189void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00003190 Record.push_back(Value.isUnsigned());
3191 AddAPInt(Value, Record);
3192}
3193
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003194void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00003195 AddAPInt(Value.bitcastToAPInt(), Record);
3196}
3197
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003198void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003199 Record.push_back(getIdentifierRef(II));
3200}
3201
Sebastian Redl539c5062010-08-18 23:57:32 +00003202IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003203 if (II == 0)
3204 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003205
Sebastian Redl539c5062010-08-18 23:57:32 +00003206 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003207 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00003208 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003209 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003210}
3211
Sebastian Redl50e26582010-09-15 19:54:06 +00003212MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregoraae92242010-03-19 21:51:54 +00003213 if (MD == 0)
3214 return 0;
Sebastian Redl50e26582010-09-15 19:54:06 +00003215
3216 MacroID &ID = MacroDefinitions[MD];
Douglas Gregoraae92242010-03-19 21:51:54 +00003217 if (ID == 0)
Douglas Gregor91096292010-10-02 19:29:26 +00003218 ID = NextMacroID++;
Douglas Gregoraae92242010-03-19 21:51:54 +00003219 return ID;
3220}
3221
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003222void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003223 Record.push_back(getSelectorRef(SelRef));
3224}
3225
Sebastian Redl539c5062010-08-18 23:57:32 +00003226SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003227 if (Sel.getAsOpaquePtr() == 0) {
3228 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00003229 }
3230
Sebastian Redl539c5062010-08-18 23:57:32 +00003231 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00003232 if (SID == 0 && Chain) {
3233 // This might trigger a ReadSelector callback, which will set the ID for
3234 // this selector.
3235 Chain->LoadSelector(Sel);
3236 }
Steve Naroff2ddea052009-04-23 10:39:46 +00003237 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003238 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00003239 }
Sebastian Redl834bb972010-08-04 17:20:04 +00003240 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00003241}
3242
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003243void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00003244 AddDeclRef(Temp->getDestructor(), Record);
3245}
3246
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003247void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3248 CXXBaseSpecifier const *BasesEnd,
3249 RecordDataImpl &Record) {
3250 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3251 CXXBaseSpecifiersToWrite.push_back(
3252 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3253 Bases, BasesEnd));
3254 Record.push_back(NextCXXBaseSpecifiersID++);
3255}
3256
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003257void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003258 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003259 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003260 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00003261 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003262 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00003263 break;
3264 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003265 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00003266 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003267 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00003268 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003269 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003270 break;
3271 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00003272 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003273 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003274 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003275 break;
John McCall0ad16662009-10-29 08:12:44 +00003276 case TemplateArgument::Null:
3277 case TemplateArgument::Integral:
3278 case TemplateArgument::Declaration:
3279 case TemplateArgument::Pack:
3280 break;
3281 }
3282}
3283
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003284void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003285 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003286 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003287
3288 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3289 bool InfoHasSameExpr
3290 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3291 Record.push_back(InfoHasSameExpr);
3292 if (InfoHasSameExpr)
3293 return; // Avoid storing the same expr twice.
3294 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003295 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3296 Record);
3297}
3298
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003299void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3300 RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00003301 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00003302 AddTypeRef(QualType(), Record);
3303 return;
3304 }
3305
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003306 AddTypeLoc(TInfo->getTypeLoc(), Record);
3307}
3308
3309void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3310 AddTypeRef(TL.getType(), Record);
3311
John McCall8f115c62009-10-16 21:56:05 +00003312 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003313 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003314 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00003315}
3316
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003317void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00003318 Record.push_back(GetOrCreateTypeID(T));
3319}
3320
3321TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003322 return MakeTypeID(T,
3323 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3324}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003325
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003326TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003327 return MakeTypeID(T,
3328 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003329}
3330
3331TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3332 if (T.isNull())
3333 return TypeIdx();
3334 assert(!T.getLocalFastQualifiers());
3335
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00003336 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003337 if (Idx.getIndex() == 0) {
Douglas Gregor1970d882009-04-26 03:49:13 +00003338 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00003339 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003340 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003341 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00003342 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003343 return Idx;
3344}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003345
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003346TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003347 if (T.isNull())
3348 return TypeIdx();
3349 assert(!T.getLocalFastQualifiers());
3350
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003351 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3352 assert(I != TypeIdxs.end() && "Type not emitted!");
3353 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003354}
3355
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003356void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003357 Record.push_back(GetDeclRef(D));
3358}
3359
Sebastian Redl539c5062010-08-18 23:57:32 +00003360DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003361 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003362 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003363 }
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003364 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00003365 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00003366 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003367 // We haven't seen this declaration before. Give it a new ID and
3368 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00003369 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003370 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003371 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3372 // We don't add it to the replacement collection here, because we don't
3373 // have the offset yet.
3374 DeclTypesToEmit.push(const_cast<Decl *>(D));
3375 // Reset the flag, so that we don't add this decl multiple times.
3376 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003377 }
3378
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003379 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003380}
3381
Sebastian Redl539c5062010-08-18 23:57:32 +00003382DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00003383 if (D == 0)
3384 return 0;
3385
3386 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3387 return DeclIDs[D];
3388}
3389
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003390void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00003391 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003392 Record.push_back(Name.getNameKind());
3393 switch (Name.getNameKind()) {
3394 case DeclarationName::Identifier:
3395 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3396 break;
3397
3398 case DeclarationName::ObjCZeroArgSelector:
3399 case DeclarationName::ObjCOneArgSelector:
3400 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00003401 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003402 break;
3403
3404 case DeclarationName::CXXConstructorName:
3405 case DeclarationName::CXXDestructorName:
3406 case DeclarationName::CXXConversionFunctionName:
3407 AddTypeRef(Name.getCXXNameType(), Record);
3408 break;
3409
3410 case DeclarationName::CXXOperatorName:
3411 Record.push_back(Name.getCXXOverloadedOperator());
3412 break;
3413
Alexis Hunt3d221f22009-11-29 07:34:05 +00003414 case DeclarationName::CXXLiteralOperatorName:
3415 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3416 break;
3417
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003418 case DeclarationName::CXXUsingDirective:
3419 // No extra data to emit
3420 break;
3421 }
3422}
Chris Lattnerca025db2010-05-07 21:43:38 +00003423
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003424void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003425 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003426 switch (Name.getNameKind()) {
3427 case DeclarationName::CXXConstructorName:
3428 case DeclarationName::CXXDestructorName:
3429 case DeclarationName::CXXConversionFunctionName:
3430 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3431 break;
3432
3433 case DeclarationName::CXXOperatorName:
3434 AddSourceLocation(
3435 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3436 Record);
3437 AddSourceLocation(
3438 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3439 Record);
3440 break;
3441
3442 case DeclarationName::CXXLiteralOperatorName:
3443 AddSourceLocation(
3444 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3445 Record);
3446 break;
3447
3448 case DeclarationName::Identifier:
3449 case DeclarationName::ObjCZeroArgSelector:
3450 case DeclarationName::ObjCOneArgSelector:
3451 case DeclarationName::ObjCMultiArgSelector:
3452 case DeclarationName::CXXUsingDirective:
3453 break;
3454 }
3455}
3456
3457void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003458 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003459 AddDeclarationName(NameInfo.getName(), Record);
3460 AddSourceLocation(NameInfo.getLoc(), Record);
3461 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3462}
3463
3464void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003465 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00003466 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003467 Record.push_back(Info.NumTemplParamLists);
3468 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3469 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3470}
3471
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003472void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003473 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003474 // Nested name specifiers usually aren't too long. I think that 8 would
3475 // typically accomodate the vast majority.
3476 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3477
3478 // Push each of the NNS's onto a stack for serialization in reverse order.
3479 while (NNS) {
3480 NestedNames.push_back(NNS);
3481 NNS = NNS->getPrefix();
3482 }
3483
3484 Record.push_back(NestedNames.size());
3485 while(!NestedNames.empty()) {
3486 NNS = NestedNames.pop_back_val();
3487 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3488 Record.push_back(Kind);
3489 switch (Kind) {
3490 case NestedNameSpecifier::Identifier:
3491 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3492 break;
3493
3494 case NestedNameSpecifier::Namespace:
3495 AddDeclRef(NNS->getAsNamespace(), Record);
3496 break;
3497
Douglas Gregor7b26ff92011-02-24 02:36:08 +00003498 case NestedNameSpecifier::NamespaceAlias:
3499 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
3500 break;
3501
Chris Lattnerca025db2010-05-07 21:43:38 +00003502 case NestedNameSpecifier::TypeSpec:
3503 case NestedNameSpecifier::TypeSpecWithTemplate:
3504 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3505 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3506 break;
3507
3508 case NestedNameSpecifier::Global:
3509 // Don't need to write an associated value.
3510 break;
3511 }
3512 }
3513}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003514
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003515void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
3516 RecordDataImpl &Record) {
3517 // Nested name specifiers usually aren't too long. I think that 8 would
3518 // typically accomodate the vast majority.
3519 llvm::SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
3520
3521 // Push each of the nested-name-specifiers's onto a stack for
3522 // serialization in reverse order.
3523 while (NNS) {
3524 NestedNames.push_back(NNS);
3525 NNS = NNS.getPrefix();
3526 }
3527
3528 Record.push_back(NestedNames.size());
3529 while(!NestedNames.empty()) {
3530 NNS = NestedNames.pop_back_val();
3531 NestedNameSpecifier::SpecifierKind Kind
3532 = NNS.getNestedNameSpecifier()->getKind();
3533 Record.push_back(Kind);
3534 switch (Kind) {
3535 case NestedNameSpecifier::Identifier:
3536 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
3537 AddSourceRange(NNS.getLocalSourceRange(), Record);
3538 break;
3539
3540 case NestedNameSpecifier::Namespace:
3541 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
3542 AddSourceRange(NNS.getLocalSourceRange(), Record);
3543 break;
3544
3545 case NestedNameSpecifier::NamespaceAlias:
3546 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
3547 AddSourceRange(NNS.getLocalSourceRange(), Record);
3548 break;
3549
3550 case NestedNameSpecifier::TypeSpec:
3551 case NestedNameSpecifier::TypeSpecWithTemplate:
3552 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3553 AddTypeLoc(NNS.getTypeLoc(), Record);
3554 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3555 break;
3556
3557 case NestedNameSpecifier::Global:
3558 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3559 break;
3560 }
3561 }
3562}
3563
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003564void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003565 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003566 Record.push_back(Kind);
3567 switch (Kind) {
3568 case TemplateName::Template:
3569 AddDeclRef(Name.getAsTemplateDecl(), Record);
3570 break;
3571
3572 case TemplateName::OverloadedTemplate: {
3573 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3574 Record.push_back(OvT->size());
3575 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3576 I != E; ++I)
3577 AddDeclRef(*I, Record);
3578 break;
3579 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003580
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003581 case TemplateName::QualifiedTemplate: {
3582 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3583 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3584 Record.push_back(QualT->hasTemplateKeyword());
3585 AddDeclRef(QualT->getTemplateDecl(), Record);
3586 break;
3587 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003588
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003589 case TemplateName::DependentTemplate: {
3590 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3591 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3592 Record.push_back(DepT->isIdentifier());
3593 if (DepT->isIdentifier())
3594 AddIdentifierRef(DepT->getIdentifier(), Record);
3595 else
3596 Record.push_back(DepT->getOperator());
3597 break;
3598 }
Douglas Gregor5590be02011-01-15 06:45:20 +00003599
3600 case TemplateName::SubstTemplateTemplateParmPack: {
3601 SubstTemplateTemplateParmPackStorage *SubstPack
3602 = Name.getAsSubstTemplateTemplateParmPack();
3603 AddDeclRef(SubstPack->getParameterPack(), Record);
3604 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
3605 break;
3606 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003607 }
3608}
3609
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003610void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003611 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003612 Record.push_back(Arg.getKind());
3613 switch (Arg.getKind()) {
3614 case TemplateArgument::Null:
3615 break;
3616 case TemplateArgument::Type:
3617 AddTypeRef(Arg.getAsType(), Record);
3618 break;
3619 case TemplateArgument::Declaration:
3620 AddDeclRef(Arg.getAsDecl(), Record);
3621 break;
3622 case TemplateArgument::Integral:
3623 AddAPSInt(*Arg.getAsIntegral(), Record);
3624 AddTypeRef(Arg.getIntegralType(), Record);
3625 break;
3626 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00003627 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3628 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003629 case TemplateArgument::TemplateExpansion:
3630 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregore1d60df2011-01-14 23:41:42 +00003631 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3632 Record.push_back(*NumExpansions + 1);
3633 else
3634 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003635 break;
3636 case TemplateArgument::Expression:
3637 AddStmt(Arg.getAsExpr());
3638 break;
3639 case TemplateArgument::Pack:
3640 Record.push_back(Arg.pack_size());
3641 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3642 I != E; ++I)
3643 AddTemplateArgument(*I, Record);
3644 break;
3645 }
3646}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003647
3648void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003649ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003650 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003651 assert(TemplateParams && "No TemplateParams!");
3652 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3653 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3654 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3655 Record.push_back(TemplateParams->size());
3656 for (TemplateParameterList::const_iterator
3657 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3658 P != PEnd; ++P)
3659 AddDeclRef(*P, Record);
3660}
3661
3662/// \brief Emit a template argument list.
3663void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003664ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003665 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003666 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003667 Record.push_back(TemplateArgs->size());
3668 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003669 AddTemplateArgument(TemplateArgs->get(i), Record);
3670}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003671
3672
3673void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003674ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003675 Record.push_back(Set.size());
3676 for (UnresolvedSetImpl::const_iterator
3677 I = Set.begin(), E = Set.end(); I != E; ++I) {
3678 AddDeclRef(I.getDecl(), Record);
3679 Record.push_back(I.getAccess());
3680 }
3681}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003682
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003683void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003684 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003685 Record.push_back(Base.isVirtual());
3686 Record.push_back(Base.isBaseOfClass());
3687 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00003688 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00003689 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003690 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00003691 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3692 : SourceLocation(),
3693 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003694}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003695
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003696void ASTWriter::FlushCXXBaseSpecifiers() {
3697 RecordData Record;
3698 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3699 Record.clear();
3700
3701 // Record the offset of this base-specifier set.
3702 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3703 if (Index == CXXBaseSpecifiersOffsets.size())
3704 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3705 else {
3706 if (Index > CXXBaseSpecifiersOffsets.size())
3707 CXXBaseSpecifiersOffsets.resize(Index + 1);
3708 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3709 }
3710
3711 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3712 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3713 Record.push_back(BEnd - B);
3714 for (; B != BEnd; ++B)
3715 AddCXXBaseSpecifier(*B, Record);
3716 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00003717
3718 // Flush any expressions that were written as part of the base specifiers.
3719 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003720 }
3721
3722 CXXBaseSpecifiersToWrite.clear();
3723}
3724
Alexis Hunt1d792652011-01-08 20:30:50 +00003725void ASTWriter::AddCXXCtorInitializers(
3726 const CXXCtorInitializer * const *CtorInitializers,
3727 unsigned NumCtorInitializers,
3728 RecordDataImpl &Record) {
3729 Record.push_back(NumCtorInitializers);
3730 for (unsigned i=0; i != NumCtorInitializers; ++i) {
3731 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003732
3733 Record.push_back(Init->isBaseInitializer());
3734 if (Init->isBaseInitializer()) {
3735 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3736 Record.push_back(Init->isBaseVirtual());
3737 } else {
Francois Pichetd583da02010-12-04 09:14:42 +00003738 Record.push_back(Init->isIndirectMemberInitializer());
3739 if (Init->isIndirectMemberInitializer())
3740 AddDeclRef(Init->getIndirectMember(), Record);
3741 else
3742 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003743 }
Francois Pichetd583da02010-12-04 09:14:42 +00003744
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003745 AddSourceLocation(Init->getMemberLocation(), Record);
3746 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003747 AddSourceLocation(Init->getLParenLoc(), Record);
3748 AddSourceLocation(Init->getRParenLoc(), Record);
3749 Record.push_back(Init->isWritten());
3750 if (Init->isWritten()) {
3751 Record.push_back(Init->getSourceOrder());
3752 } else {
3753 Record.push_back(Init->getNumArrayIndices());
3754 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3755 AddDeclRef(Init->getArrayIndex(i), Record);
3756 }
3757 }
3758}
3759
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003760void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3761 assert(D->DefinitionData);
3762 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3763 Record.push_back(Data.UserDeclaredConstructor);
3764 Record.push_back(Data.UserDeclaredCopyConstructor);
3765 Record.push_back(Data.UserDeclaredCopyAssignment);
3766 Record.push_back(Data.UserDeclaredDestructor);
3767 Record.push_back(Data.Aggregate);
3768 Record.push_back(Data.PlainOldData);
3769 Record.push_back(Data.Empty);
3770 Record.push_back(Data.Polymorphic);
3771 Record.push_back(Data.Abstract);
3772 Record.push_back(Data.HasTrivialConstructor);
3773 Record.push_back(Data.HasTrivialCopyConstructor);
3774 Record.push_back(Data.HasTrivialCopyAssignment);
3775 Record.push_back(Data.HasTrivialDestructor);
3776 Record.push_back(Data.ComputedVisibleConversions);
3777 Record.push_back(Data.DeclaredDefaultConstructor);
3778 Record.push_back(Data.DeclaredCopyConstructor);
3779 Record.push_back(Data.DeclaredCopyAssignment);
3780 Record.push_back(Data.DeclaredDestructor);
3781
3782 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003783 if (Data.NumBases > 0)
3784 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3785 Record);
3786
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003787 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3788 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003789 if (Data.NumVBases > 0)
3790 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3791 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003792
3793 AddUnresolvedSet(Data.Conversions, Record);
3794 AddUnresolvedSet(Data.VisibleConversions, Record);
3795 // Data.Definition is the owning decl, no need to write it.
3796 AddDeclRef(Data.FirstFriend, Record);
3797}
3798
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003799void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00003800 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003801 assert(!Chain && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00003802 assert(FirstDeclID == NextDeclID &&
3803 FirstTypeID == NextTypeID &&
3804 FirstIdentID == NextIdentID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00003805 FirstSelectorID == NextSelectorID &&
Douglas Gregor91096292010-10-02 19:29:26 +00003806 FirstMacroID == NextMacroID &&
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003807 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00003808 "Setting chain after writing has started.");
3809 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003810
3811 FirstDeclID += Chain->getTotalNumDecls();
3812 FirstTypeID += Chain->getTotalNumTypes();
3813 FirstIdentID += Chain->getTotalNumIdentifiers();
3814 FirstSelectorID += Chain->getTotalNumSelectors();
3815 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003816 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003817 NextDeclID = FirstDeclID;
3818 NextTypeID = FirstTypeID;
3819 NextIdentID = FirstIdentID;
3820 NextSelectorID = FirstSelectorID;
3821 NextMacroID = FirstMacroID;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003822 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00003823}
3824
Sebastian Redl539c5062010-08-18 23:57:32 +00003825void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00003826 IdentifierIDs[II] = ID;
Douglas Gregor68051a72011-02-11 00:26:14 +00003827 if (II->hasMacroDefinition())
3828 DeserializedMacroNames.push_back(II);
Sebastian Redlff4a2952010-07-23 23:49:55 +00003829}
3830
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003831void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003832 // Always take the highest-numbered type index. This copes with an interesting
3833 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003834 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003835 // keep the higher-numbered entry so that we can properly write it out to
3836 // the AST file.
3837 TypeIdx &StoredIdx = TypeIdxs[T];
3838 if (Idx.getIndex() >= StoredIdx.getIndex())
3839 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003840}
3841
Sebastian Redl539c5062010-08-18 23:57:32 +00003842void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003843 DeclIDs[D] = ID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003844}
Sebastian Redl834bb972010-08-04 17:20:04 +00003845
Sebastian Redl539c5062010-08-18 23:57:32 +00003846void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003847 SelectorIDs[S] = ID;
3848}
Douglas Gregor91096292010-10-02 19:29:26 +00003849
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003850void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00003851 MacroDefinition *MD) {
3852 MacroDefinitions[MD] = ID;
3853}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003854
3855void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3856 assert(D->isDefinition());
3857 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3858 // We are interested when a PCH decl is modified.
3859 if (RD->getPCHLevel() > 0) {
3860 // A forward reference was mutated into a definition. Rewrite it.
3861 // FIXME: This happens during template instantiation, should we
3862 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003863 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003864 }
3865
3866 for (CXXRecordDecl::redecl_iterator
3867 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3868 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3869 if (Redecl == RD)
3870 continue;
3871
3872 // We are interested when a PCH decl is modified.
3873 if (Redecl->getPCHLevel() > 0) {
3874 UpdateRecord &Record = DeclUpdates[Redecl];
3875 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3876 assert(Redecl->DefinitionData);
3877 assert(Redecl->DefinitionData->Definition == D);
3878 AddDeclRef(D, Record); // the DefinitionDecl
3879 }
3880 }
3881 }
3882}
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003883void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3884 // TU and namespaces are handled elsewhere.
3885 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3886 return;
3887
3888 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3889 return; // Not a source decl added to a DeclContext from PCH.
3890
3891 AddUpdatedDeclContext(DC);
3892}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003893
3894void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3895 assert(D->isImplicit());
3896 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3897 return; // Not a source member added to a class from PCH.
3898 if (!isa<CXXMethodDecl>(D))
3899 return; // We are interested in lazily declared implicit methods.
3900
3901 // A decl coming from PCH was modified.
3902 assert(RD->isDefinition());
3903 UpdateRecord &Record = DeclUpdates[RD];
3904 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3905 AddDeclRef(D, Record);
3906}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003907
3908void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3909 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00003910 // The specializations set is kept in the canonical template.
3911 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003912 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3913 return; // Not a source specialization added to a template from PCH.
3914
3915 UpdateRecord &Record = DeclUpdates[TD];
3916 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3917 AddDeclRef(D, Record);
3918}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003919
3920ASTSerializationListener::~ASTSerializationListener() { }