blob: be415015ee70d95019e65ab412583a4098f0f046 [file] [log] [blame]
Sebastian Redld6522cf2010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redl55c0ad52010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregoref84c4b2009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl1914c6f2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Douglas Gregorf88e35b2010-11-30 06:16:57 +000015#include "clang/Serialization/ASTSerializationListener.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000016#include "ASTCommon.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000017#include "clang/Sema/Sema.h"
18#include "clang/Sema/IdentifierResolver.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclContextInternals.h"
John McCall19c1bfd2010-08-25 05:32:35 +000022#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000023#include "clang/AST/DeclFriend.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000024#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000025#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000026#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000027#include "clang/AST/TypeLocVisitor.h"
Sebastian Redlf5b13462010-08-18 23:57:17 +000028#include "clang/Serialization/ASTReader.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000029#include "clang/Lex/MacroInfo.h"
Douglas Gregoraae92242010-03-19 21:51:54 +000030#include "clang/Lex/PreprocessingRecord.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000031#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000032#include "clang/Lex/HeaderSearch.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000033#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000034#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000035#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000036#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000037#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000038#include "clang/Basic/TargetInfo.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000039#include "clang/Basic/Version.h"
Douglas Gregor20b2ebd2011-03-23 00:50:03 +000040#include "clang/Basic/VersionTuple.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000041#include "llvm/ADT/APFloat.h"
42#include "llvm/ADT/APInt.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000043#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000044#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencer740857f2010-12-21 16:45:57 +000045#include "llvm/Support/FileSystem.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000046#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000047#include "llvm/Support/Path.h"
Chris Lattner225dd6c2009-04-11 18:40:46 +000048#include <cstdio>
Douglas Gregor09b69892011-02-10 17:09:37 +000049#include <string.h>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000050using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000051using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000052
Sebastian Redl3df5a082010-07-30 17:03:48 +000053template <typename T, typename Allocator>
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000054static llvm::StringRef data(const std::vector<T, Allocator> &v) {
55 if (v.empty()) return llvm::StringRef();
56 return llvm::StringRef(reinterpret_cast<const char*>(&v[0]),
57 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000058}
Benjamin Kramerd47a12a2011-04-24 17:44:50 +000059
60template <typename T>
61static llvm::StringRef data(const llvm::SmallVectorImpl<T> &v) {
62 return llvm::StringRef(reinterpret_cast<const char*>(v.data()),
63 sizeof(T) * v.size());
Sebastian Redl3df5a082010-07-30 17:03:48 +000064}
65
Douglas Gregoref84c4b2009-04-09 22:27:44 +000066//===----------------------------------------------------------------------===//
67// Type serialization
68//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000069
Douglas Gregoref84c4b2009-04-09 22:27:44 +000070namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000071 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000072 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000073 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000074
75 public:
76 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000077 TypeCode Code;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000078
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000079 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000080 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000081
82 void VisitArrayType(const ArrayType *T);
83 void VisitFunctionType(const FunctionType *T);
84 void VisitTagType(const TagType *T);
85
86#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
87#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000088#include "clang/AST/TypeNodes.def"
89 };
90}
91
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000092void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000093 assert(false && "Built-in types are never serialized");
94}
95
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000096void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000097 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000098 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000099}
100
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000101void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000102 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000103 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000104}
105
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000106void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000107 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000108 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000109}
110
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000111void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000112 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
113 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl539c5062010-08-18 23:57:32 +0000114 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000115}
116
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000117void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smith0f538462011-04-12 10:38:03 +0000118 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000119 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000120}
121
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000122void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000123 Writer.AddTypeRef(T->getPointeeType(), Record);
124 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000125 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000126}
127
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000128void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000129 Writer.AddTypeRef(T->getElementType(), Record);
130 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000131 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000132}
133
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000134void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000135 VisitArrayType(T);
136 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000137 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000138}
139
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000140void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000141 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000142 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000143}
144
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000145void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000146 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000147 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
148 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000149 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000150 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000151}
152
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000153void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000154 Writer.AddTypeRef(T->getElementType(), Record);
155 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000156 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000157 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000158}
159
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000160void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000161 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000162 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000163}
164
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000165void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000166 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000167 FunctionType::ExtInfo C = T->getExtInfo();
168 Record.push_back(C.getNoReturn());
Eli Friedmanc5b20b52011-04-09 08:18:08 +0000169 Record.push_back(C.getHasRegParm());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000170 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000171 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000172 Record.push_back(C.getCC());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000173}
174
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000175void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000176 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000177 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000178}
179
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000180void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000181 VisitFunctionType(T);
182 Record.push_back(T->getNumArgs());
183 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
184 Writer.AddTypeRef(T->getArgType(I), Record);
185 Record.push_back(T->isVariadic());
186 Record.push_back(T->getTypeQuals());
Douglas Gregordb9d6642011-01-26 05:01:58 +0000187 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redlfa453cf2011-03-12 11:50:43 +0000188 Record.push_back(T->getExceptionSpecType());
189 if (T->getExceptionSpecType() == EST_Dynamic) {
190 Record.push_back(T->getNumExceptions());
191 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
192 Writer.AddTypeRef(T->getExceptionType(I), Record);
193 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
194 Writer.AddStmt(T->getNoexceptExpr());
195 }
Sebastian Redl539c5062010-08-18 23:57:32 +0000196 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000197}
198
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000199void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000200 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000201 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000202}
John McCallb96ec562009-12-04 22:46:56 +0000203
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000204void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000205 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000206 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
207 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000208 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000209}
210
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000211void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000212 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000213 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000214}
215
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000216void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000217 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000218 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000219}
220
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000221void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson81df7b82009-06-24 19:06:50 +0000222 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000223 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000224}
225
Richard Smith30482bc2011-02-20 03:19:35 +0000226void ASTTypeWriter::VisitAutoType(const AutoType *T) {
227 Writer.AddTypeRef(T->getDeducedType(), Record);
228 Code = TYPE_AUTO;
229}
230
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000231void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000232 Record.push_back(T->isDependentType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000233 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000234 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000235 "Cannot serialize in the middle of a type definition");
236}
237
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000238void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000239 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000240 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000241}
242
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000243void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000244 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000245 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000246}
247
John McCall81904512011-01-06 01:58:22 +0000248void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
249 Writer.AddTypeRef(T->getModifiedType(), Record);
250 Writer.AddTypeRef(T->getEquivalentType(), Record);
251 Record.push_back(T->getAttrKind());
252 Code = TYPE_ATTRIBUTED;
253}
254
Mike Stump11289f42009-09-09 15:08:12 +0000255void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000256ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000257 const SubstTemplateTypeParmType *T) {
258 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
259 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000260 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000261}
262
263void
Douglas Gregorada4b792011-01-14 02:55:32 +0000264ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
265 const SubstTemplateTypeParmPackType *T) {
266 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
267 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
268 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
269}
270
271void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000272ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000273 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000274 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000275 Writer.AddTemplateName(T->getTemplateName(), Record);
276 Record.push_back(T->getNumArgs());
277 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
278 ArgI != ArgE; ++ArgI)
279 Writer.AddTemplateArgument(*ArgI, Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000280 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
281 : T->getCanonicalTypeInternal(),
282 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000283 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000284}
285
286void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000287ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000288 VisitArrayType(T);
289 Writer.AddStmt(T->getSizeExpr());
290 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000291 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000292}
293
294void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000295ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000296 const DependentSizedExtVectorType *T) {
297 // FIXME: Serialize this type (C++ only)
298 assert(false && "Cannot serialize dependent sized extended vector types");
299}
300
301void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000302ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000303 Record.push_back(T->getDepth());
304 Record.push_back(T->getIndex());
305 Record.push_back(T->isParameterPack());
Chandler Carruth08836322011-05-01 00:51:33 +0000306 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000307 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000308}
309
310void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000311ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000312 Record.push_back(T->getKeyword());
313 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
314 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000315 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
316 : T->getCanonicalTypeInternal(),
317 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000318 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000319}
320
321void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000322ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000323 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000324 Record.push_back(T->getKeyword());
325 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
326 Writer.AddIdentifierRef(T->getIdentifier(), Record);
327 Record.push_back(T->getNumArgs());
328 for (DependentTemplateSpecializationType::iterator
329 I = T->begin(), E = T->end(); I != E; ++I)
330 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000331 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000332}
333
Douglas Gregord2fa7662010-12-20 02:24:11 +0000334void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
335 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000336 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
337 Record.push_back(*NumExpansions + 1);
338 else
339 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000340 Code = TYPE_PACK_EXPANSION;
341}
342
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000343void ASTTypeWriter::VisitParenType(const ParenType *T) {
344 Writer.AddTypeRef(T->getInnerType(), Record);
345 Code = TYPE_PAREN;
346}
347
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000348void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000349 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000350 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
351 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000352 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000353}
354
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000355void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCalle78aac42010-03-10 03:28:59 +0000356 Writer.AddDeclRef(T->getDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000357 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000358 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000359}
360
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000361void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1c283312010-08-11 12:19:30 +0000362 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000363 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000364}
365
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000366void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000367 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000368 Record.push_back(T->getNumProtocols());
John McCall8b07ec22010-05-15 11:32:37 +0000369 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000370 E = T->qual_end(); I != E; ++I)
371 Writer.AddDeclRef(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000372 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000373}
374
Steve Narofffb4330f2009-06-17 22:40:22 +0000375void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000376ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000377 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000378 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000379}
380
John McCall8f115c62009-10-16 21:56:05 +0000381namespace {
382
383class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000384 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000385 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000386
387public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000388 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000389 : Writer(Writer), Record(Record) { }
390
John McCall17001972009-10-18 01:05:36 +0000391#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000392#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000393 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000394#include "clang/AST/TypeLocNodes.def"
395
John McCall17001972009-10-18 01:05:36 +0000396 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
397 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000398};
399
400}
401
John McCall17001972009-10-18 01:05:36 +0000402void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
403 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000404}
John McCall17001972009-10-18 01:05:36 +0000405void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000406 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
407 if (TL.needsExtraLocalData()) {
408 Record.push_back(TL.getWrittenTypeSpec());
409 Record.push_back(TL.getWrittenSignSpec());
410 Record.push_back(TL.getWrittenWidthSpec());
411 Record.push_back(TL.hasModeAttr());
412 }
John McCall8f115c62009-10-16 21:56:05 +0000413}
John McCall17001972009-10-18 01:05:36 +0000414void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
415 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000416}
John McCall17001972009-10-18 01:05:36 +0000417void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
418 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000419}
John McCall17001972009-10-18 01:05:36 +0000420void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
421 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000422}
John McCall17001972009-10-18 01:05:36 +0000423void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
424 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000425}
John McCall17001972009-10-18 01:05:36 +0000426void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
427 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000428}
John McCall17001972009-10-18 01:05:36 +0000429void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
430 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnara509357842011-03-05 14:42:21 +0000431 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000432}
John McCall17001972009-10-18 01:05:36 +0000433void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
434 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
435 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
436 Record.push_back(TL.getSizeExpr() ? 1 : 0);
437 if (TL.getSizeExpr())
438 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000439}
John McCall17001972009-10-18 01:05:36 +0000440void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
441 VisitArrayTypeLoc(TL);
442}
443void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
444 VisitArrayTypeLoc(TL);
445}
446void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
447 VisitArrayTypeLoc(TL);
448}
449void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
450 DependentSizedArrayTypeLoc TL) {
451 VisitArrayTypeLoc(TL);
452}
453void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
454 DependentSizedExtVectorTypeLoc TL) {
455 Writer.AddSourceLocation(TL.getNameLoc(), Record);
456}
457void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
458 Writer.AddSourceLocation(TL.getNameLoc(), Record);
459}
460void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
461 Writer.AddSourceLocation(TL.getNameLoc(), Record);
462}
463void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000464 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
465 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Douglas Gregor7fb25412010-10-01 18:44:50 +0000466 Record.push_back(TL.getTrailingReturn());
John McCall17001972009-10-18 01:05:36 +0000467 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
468 Writer.AddDeclRef(TL.getArg(i), Record);
469}
470void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
471 VisitFunctionTypeLoc(TL);
472}
473void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
474 VisitFunctionTypeLoc(TL);
475}
John McCallb96ec562009-12-04 22:46:56 +0000476void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
477 Writer.AddSourceLocation(TL.getNameLoc(), Record);
478}
John McCall17001972009-10-18 01:05:36 +0000479void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
480 Writer.AddSourceLocation(TL.getNameLoc(), Record);
481}
482void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000483 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
484 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
485 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000486}
487void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000488 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
489 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
490 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
491 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000492}
493void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
494 Writer.AddSourceLocation(TL.getNameLoc(), Record);
495}
Richard Smith30482bc2011-02-20 03:19:35 +0000496void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
497 Writer.AddSourceLocation(TL.getNameLoc(), Record);
498}
John McCall17001972009-10-18 01:05:36 +0000499void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
500 Writer.AddSourceLocation(TL.getNameLoc(), Record);
501}
502void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
503 Writer.AddSourceLocation(TL.getNameLoc(), Record);
504}
John McCall81904512011-01-06 01:58:22 +0000505void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
506 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
507 if (TL.hasAttrOperand()) {
508 SourceRange range = TL.getAttrOperandParensRange();
509 Writer.AddSourceLocation(range.getBegin(), Record);
510 Writer.AddSourceLocation(range.getEnd(), Record);
511 }
512 if (TL.hasAttrExprOperand()) {
513 Expr *operand = TL.getAttrExprOperand();
514 Record.push_back(operand ? 1 : 0);
515 if (operand) Writer.AddStmt(operand);
516 } else if (TL.hasAttrEnumOperand()) {
517 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
518 }
519}
John McCall17001972009-10-18 01:05:36 +0000520void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
521 Writer.AddSourceLocation(TL.getNameLoc(), Record);
522}
John McCallcebee162009-10-18 09:09:24 +0000523void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
524 SubstTemplateTypeParmTypeLoc TL) {
525 Writer.AddSourceLocation(TL.getNameLoc(), Record);
526}
Douglas Gregorada4b792011-01-14 02:55:32 +0000527void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
528 SubstTemplateTypeParmPackTypeLoc TL) {
529 Writer.AddSourceLocation(TL.getNameLoc(), Record);
530}
John McCall17001972009-10-18 01:05:36 +0000531void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
532 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +0000533 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
534 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
535 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
536 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000537 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
538 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000539}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000540void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
541 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
542 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
543}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000544void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000545 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000546 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000547}
John McCalle78aac42010-03-10 03:28:59 +0000548void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
549 Writer.AddSourceLocation(TL.getNameLoc(), Record);
550}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000551void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000552 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000553 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000554 Writer.AddSourceLocation(TL.getNameLoc(), Record);
555}
John McCallc392f372010-06-11 00:33:02 +0000556void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
557 DependentTemplateSpecializationTypeLoc TL) {
558 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000559 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000560 Writer.AddSourceLocation(TL.getNameLoc(), Record);
561 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
562 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
563 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000564 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
565 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000566}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000567void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
568 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
569}
John McCall17001972009-10-18 01:05:36 +0000570void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
571 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000572}
573void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
574 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000575 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
576 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
577 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
578 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000579}
John McCallfc93cf92009-10-22 22:37:11 +0000580void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
581 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000582}
John McCall8f115c62009-10-16 21:56:05 +0000583
Chris Lattner19cea4e2009-04-22 05:57:30 +0000584//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000585// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000586//===----------------------------------------------------------------------===//
587
Chris Lattner28fa4e62009-04-26 22:26:21 +0000588static void EmitBlockID(unsigned ID, const char *Name,
589 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000590 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000591 Record.clear();
592 Record.push_back(ID);
593 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
594
595 // Emit the block name if present.
596 if (Name == 0 || Name[0] == 0) return;
597 Record.clear();
598 while (*Name)
599 Record.push_back(*Name++);
600 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
601}
602
603static void EmitRecordID(unsigned ID, const char *Name,
604 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000605 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000606 Record.clear();
607 Record.push_back(ID);
608 while (*Name)
609 Record.push_back(*Name++);
610 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000611}
612
613static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000614 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000615#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000616 RECORD(STMT_STOP);
617 RECORD(STMT_NULL_PTR);
618 RECORD(STMT_NULL);
619 RECORD(STMT_COMPOUND);
620 RECORD(STMT_CASE);
621 RECORD(STMT_DEFAULT);
622 RECORD(STMT_LABEL);
623 RECORD(STMT_IF);
624 RECORD(STMT_SWITCH);
625 RECORD(STMT_WHILE);
626 RECORD(STMT_DO);
627 RECORD(STMT_FOR);
628 RECORD(STMT_GOTO);
629 RECORD(STMT_INDIRECT_GOTO);
630 RECORD(STMT_CONTINUE);
631 RECORD(STMT_BREAK);
632 RECORD(STMT_RETURN);
633 RECORD(STMT_DECL);
634 RECORD(STMT_ASM);
635 RECORD(EXPR_PREDEFINED);
636 RECORD(EXPR_DECL_REF);
637 RECORD(EXPR_INTEGER_LITERAL);
638 RECORD(EXPR_FLOATING_LITERAL);
639 RECORD(EXPR_IMAGINARY_LITERAL);
640 RECORD(EXPR_STRING_LITERAL);
641 RECORD(EXPR_CHARACTER_LITERAL);
642 RECORD(EXPR_PAREN);
643 RECORD(EXPR_UNARY_OPERATOR);
644 RECORD(EXPR_SIZEOF_ALIGN_OF);
645 RECORD(EXPR_ARRAY_SUBSCRIPT);
646 RECORD(EXPR_CALL);
647 RECORD(EXPR_MEMBER);
648 RECORD(EXPR_BINARY_OPERATOR);
649 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
650 RECORD(EXPR_CONDITIONAL_OPERATOR);
651 RECORD(EXPR_IMPLICIT_CAST);
652 RECORD(EXPR_CSTYLE_CAST);
653 RECORD(EXPR_COMPOUND_LITERAL);
654 RECORD(EXPR_EXT_VECTOR_ELEMENT);
655 RECORD(EXPR_INIT_LIST);
656 RECORD(EXPR_DESIGNATED_INIT);
657 RECORD(EXPR_IMPLICIT_VALUE_INIT);
658 RECORD(EXPR_VA_ARG);
659 RECORD(EXPR_ADDR_LABEL);
660 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000661 RECORD(EXPR_CHOOSE);
662 RECORD(EXPR_GNU_NULL);
663 RECORD(EXPR_SHUFFLE_VECTOR);
664 RECORD(EXPR_BLOCK);
665 RECORD(EXPR_BLOCK_DECL_REF);
Peter Collingbourne91147592011-04-15 00:35:48 +0000666 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000667 RECORD(EXPR_OBJC_STRING_LITERAL);
668 RECORD(EXPR_OBJC_ENCODE);
669 RECORD(EXPR_OBJC_SELECTOR_EXPR);
670 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
671 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
672 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
673 RECORD(EXPR_OBJC_KVC_REF_EXPR);
674 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000675 RECORD(STMT_OBJC_FOR_COLLECTION);
676 RECORD(STMT_OBJC_CATCH);
677 RECORD(STMT_OBJC_FINALLY);
678 RECORD(STMT_OBJC_AT_TRY);
679 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
680 RECORD(STMT_OBJC_AT_THROW);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000681 RECORD(EXPR_CXX_OPERATOR_CALL);
682 RECORD(EXPR_CXX_CONSTRUCT);
683 RECORD(EXPR_CXX_STATIC_CAST);
684 RECORD(EXPR_CXX_DYNAMIC_CAST);
685 RECORD(EXPR_CXX_REINTERPRET_CAST);
686 RECORD(EXPR_CXX_CONST_CAST);
687 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
688 RECORD(EXPR_CXX_BOOL_LITERAL);
689 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000690 RECORD(EXPR_CXX_TYPEID_EXPR);
691 RECORD(EXPR_CXX_TYPEID_TYPE);
692 RECORD(EXPR_CXX_UUIDOF_EXPR);
693 RECORD(EXPR_CXX_UUIDOF_TYPE);
694 RECORD(EXPR_CXX_THIS);
695 RECORD(EXPR_CXX_THROW);
696 RECORD(EXPR_CXX_DEFAULT_ARG);
697 RECORD(EXPR_CXX_BIND_TEMPORARY);
698 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
699 RECORD(EXPR_CXX_NEW);
700 RECORD(EXPR_CXX_DELETE);
701 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
702 RECORD(EXPR_EXPR_WITH_CLEANUPS);
703 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
704 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
705 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
706 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
707 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
708 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
709 RECORD(EXPR_CXX_NOEXCEPT);
710 RECORD(EXPR_OPAQUE_VALUE);
711 RECORD(EXPR_BINARY_TYPE_TRAIT);
712 RECORD(EXPR_PACK_EXPANSION);
713 RECORD(EXPR_SIZEOF_PACK);
714 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000715 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000716#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000717}
Mike Stump11289f42009-09-09 15:08:12 +0000718
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000719void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000720 RecordData Record;
721 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000722
Sebastian Redl539c5062010-08-18 23:57:32 +0000723#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
724#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000725
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000726 // AST Top-Level Block.
Sebastian Redlf1642042010-08-18 23:57:22 +0000727 BLOCK(AST_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000728 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000729 RECORD(TYPE_OFFSET);
730 RECORD(DECL_OFFSET);
731 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000732 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000733 RECORD(IDENTIFIER_OFFSET);
734 RECORD(IDENTIFIER_TABLE);
735 RECORD(EXTERNAL_DEFINITIONS);
736 RECORD(SPECIAL_TYPES);
737 RECORD(STATISTICS);
738 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000739 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000740 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
741 RECORD(SELECTOR_OFFSETS);
742 RECORD(METHOD_POOL);
743 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000744 RECORD(SOURCE_LOCATION_OFFSETS);
745 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000746 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000747 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek17437132010-01-22 20:59:36 +0000748 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregoraae92242010-03-19 21:51:54 +0000749 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redl595c5132010-07-08 22:01:51 +0000750 RECORD(CHAINED_METADATA);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000751 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000752 RECORD(TU_UPDATE_LEXICAL);
753 RECORD(REDECLS_UPDATE_LATEST);
754 RECORD(SEMA_DECL_REFS);
755 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
756 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
757 RECORD(DECL_REPLACEMENTS);
758 RECORD(UPDATE_VISIBLE);
759 RECORD(DECL_UPDATE_OFFSETS);
760 RECORD(DECL_UPDATES);
761 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
762 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000763 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000764 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000765 RECORD(FP_PRAGMA_OPTIONS);
766 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +0000767 RECORD(DELEGATING_CTORS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000768
Chris Lattner28fa4e62009-04-26 22:26:21 +0000769 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000770 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000771 RECORD(SM_SLOC_FILE_ENTRY);
772 RECORD(SM_SLOC_BUFFER_ENTRY);
773 RECORD(SM_SLOC_BUFFER_BLOB);
774 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
775 RECORD(SM_LINE_TABLE);
Mike Stump11289f42009-09-09 15:08:12 +0000776
Chris Lattner28fa4e62009-04-26 22:26:21 +0000777 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000778 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000779 RECORD(PP_MACRO_OBJECT_LIKE);
780 RECORD(PP_MACRO_FUNCTION_LIKE);
781 RECORD(PP_TOKEN);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000782
Douglas Gregor12bfa382009-10-17 00:13:19 +0000783 // Decls and Types block.
784 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000785 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000786 RECORD(TYPE_COMPLEX);
787 RECORD(TYPE_POINTER);
788 RECORD(TYPE_BLOCK_POINTER);
789 RECORD(TYPE_LVALUE_REFERENCE);
790 RECORD(TYPE_RVALUE_REFERENCE);
791 RECORD(TYPE_MEMBER_POINTER);
792 RECORD(TYPE_CONSTANT_ARRAY);
793 RECORD(TYPE_INCOMPLETE_ARRAY);
794 RECORD(TYPE_VARIABLE_ARRAY);
795 RECORD(TYPE_VECTOR);
796 RECORD(TYPE_EXT_VECTOR);
797 RECORD(TYPE_FUNCTION_PROTO);
798 RECORD(TYPE_FUNCTION_NO_PROTO);
799 RECORD(TYPE_TYPEDEF);
800 RECORD(TYPE_TYPEOF_EXPR);
801 RECORD(TYPE_TYPEOF);
802 RECORD(TYPE_RECORD);
803 RECORD(TYPE_ENUM);
804 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000805 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000806 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000807 RECORD(TYPE_DECLTYPE);
808 RECORD(TYPE_ELABORATED);
809 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
810 RECORD(TYPE_UNRESOLVED_USING);
811 RECORD(TYPE_INJECTED_CLASS_NAME);
812 RECORD(TYPE_OBJC_OBJECT);
813 RECORD(TYPE_TEMPLATE_TYPE_PARM);
814 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
815 RECORD(TYPE_DEPENDENT_NAME);
816 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
817 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
818 RECORD(TYPE_PAREN);
819 RECORD(TYPE_PACK_EXPANSION);
820 RECORD(TYPE_ATTRIBUTED);
821 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000822 RECORD(DECL_TRANSLATION_UNIT);
823 RECORD(DECL_TYPEDEF);
824 RECORD(DECL_ENUM);
825 RECORD(DECL_RECORD);
826 RECORD(DECL_ENUM_CONSTANT);
827 RECORD(DECL_FUNCTION);
828 RECORD(DECL_OBJC_METHOD);
829 RECORD(DECL_OBJC_INTERFACE);
830 RECORD(DECL_OBJC_PROTOCOL);
831 RECORD(DECL_OBJC_IVAR);
832 RECORD(DECL_OBJC_AT_DEFS_FIELD);
833 RECORD(DECL_OBJC_CLASS);
834 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
835 RECORD(DECL_OBJC_CATEGORY);
836 RECORD(DECL_OBJC_CATEGORY_IMPL);
837 RECORD(DECL_OBJC_IMPLEMENTATION);
838 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
839 RECORD(DECL_OBJC_PROPERTY);
840 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000841 RECORD(DECL_FIELD);
842 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000843 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000844 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000845 RECORD(DECL_FILE_SCOPE_ASM);
846 RECORD(DECL_BLOCK);
847 RECORD(DECL_CONTEXT_LEXICAL);
848 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000849 RECORD(DECL_NAMESPACE);
850 RECORD(DECL_NAMESPACE_ALIAS);
851 RECORD(DECL_USING);
852 RECORD(DECL_USING_SHADOW);
853 RECORD(DECL_USING_DIRECTIVE);
854 RECORD(DECL_UNRESOLVED_USING_VALUE);
855 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
856 RECORD(DECL_LINKAGE_SPEC);
857 RECORD(DECL_CXX_RECORD);
858 RECORD(DECL_CXX_METHOD);
859 RECORD(DECL_CXX_CONSTRUCTOR);
860 RECORD(DECL_CXX_DESTRUCTOR);
861 RECORD(DECL_CXX_CONVERSION);
862 RECORD(DECL_ACCESS_SPEC);
863 RECORD(DECL_FRIEND);
864 RECORD(DECL_FRIEND_TEMPLATE);
865 RECORD(DECL_CLASS_TEMPLATE);
866 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
867 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
868 RECORD(DECL_FUNCTION_TEMPLATE);
869 RECORD(DECL_TEMPLATE_TYPE_PARM);
870 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
871 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
872 RECORD(DECL_STATIC_ASSERT);
873 RECORD(DECL_CXX_BASE_SPECIFIERS);
874 RECORD(DECL_INDIRECTFIELD);
875 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
876
Douglas Gregor92a96f52011-02-08 21:58:10 +0000877 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
878 RECORD(PPD_MACRO_INSTANTIATION);
879 RECORD(PPD_MACRO_DEFINITION);
880 RECORD(PPD_INCLUSION_DIRECTIVE);
881
Douglas Gregor12bfa382009-10-17 00:13:19 +0000882 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000883 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000884#undef RECORD
885#undef BLOCK
886 Stream.ExitBlock();
887}
888
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000889/// \brief Adjusts the given filename to only write out the portion of the
890/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000891///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000892/// \param Filename the file name to adjust.
893///
894/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
895/// the returned filename will be adjusted by this system root.
896///
897/// \returns either the original filename (if it needs no adjustment) or the
898/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000899static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000900adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
901 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000902
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000903 if (!isysroot)
904 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000905
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000906 // Verify that the filename and the system root have the same prefix.
907 unsigned Pos = 0;
908 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
909 if (Filename[Pos] != isysroot[Pos])
910 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000911
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000912 // We hit the end of the filename before we hit the end of the system root.
913 if (!Filename[Pos])
914 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000915
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000916 // If the file name has a '/' at the current position, skip over the '/'.
917 // We distinguish sysroot-based includes from absolute includes by the
918 // absence of '/' at the beginning of sysroot-based includes.
919 if (Filename[Pos] == '/')
920 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000921
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000922 return Filename + Pos;
923}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000924
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000925/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000926void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot,
927 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000928 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000929
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000930 // Metadata
931 const TargetInfo &Target = Context.Target;
932 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000933 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl539c5062010-08-18 23:57:32 +0000934 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000935 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
936 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000937 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
938 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
939 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000940 // Target triple or chained PCH name
941 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000942 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000943
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000944 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000945 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
946 Record.push_back(VERSION_MAJOR);
947 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000948 Record.push_back(CLANG_VERSION_MAJOR);
949 Record.push_back(CLANG_VERSION_MINOR);
950 Record.push_back(isysroot != 0);
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000951 // FIXME: This writes the absolute path for chained headers.
952 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
953 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump11289f42009-09-09 15:08:12 +0000954
Douglas Gregor45fe0362009-05-12 01:31:05 +0000955 // Original file name
956 SourceManager &SM = Context.getSourceManager();
957 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
958 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000959 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregor45fe0362009-05-12 01:31:05 +0000960 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
961 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
962
Michael J. Spencer740857f2010-12-21 16:45:57 +0000963 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +0000964
Michael J. Spencer740857f2010-12-21 16:45:57 +0000965 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000966
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +0000967 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000968 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000969 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000970 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000971 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000972 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000973 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000974
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000975 // Original PCH directory
976 if (!OutputFile.empty() && OutputFile != "-") {
977 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
978 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
979 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
980 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
981
982 llvm::SmallString<128> OutputPath(OutputFile);
983
984 llvm::sys::fs::make_absolute(OutputPath);
985 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
986
987 RecordData Record;
988 Record.push_back(ORIGINAL_PCH_DIR);
989 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
990 }
991
Ted Kremenek18e066f2010-01-22 22:12:47 +0000992 // Repository branch/version information.
993 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000994 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenek18e066f2010-01-22 22:12:47 +0000995 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
996 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000997 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +0000998 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +0000999 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
1000 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +00001001}
1002
1003/// \brief Write the LangOptions structure.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001004void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001005 RecordData Record;
1006 Record.push_back(LangOpts.Trigraphs);
1007 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1008 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1009 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1010 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carruthe03aa552010-04-17 20:17:31 +00001011 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor55abb232009-04-10 20:39:37 +00001012 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1013 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1014 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1015 Record.push_back(LangOpts.C99); // C99 Support
Peter Collingbournea686b5f2011-04-15 00:35:23 +00001016 Record.push_back(LangOpts.C1X); // C1X Support
Douglas Gregor55abb232009-04-10 20:39:37 +00001017 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +00001018 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
1019 // already saved elsewhere.
Douglas Gregor55abb232009-04-10 20:39:37 +00001020 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1021 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +00001022 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +00001023
Douglas Gregor55abb232009-04-10 20:39:37 +00001024 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1025 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001026 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian45878032010-02-09 19:31:38 +00001027 // modern abi enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001028 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian45878032010-02-09 19:31:38 +00001029 // modern abi enabled.
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +00001030 Record.push_back(LangOpts.AppleKext); // Apple's kernel extensions ABI
Ted Kremenek1d56c9e2010-12-23 21:35:43 +00001031 Record.push_back(LangOpts.ObjCDefaultSynthProperties); // Objective-C auto-synthesized
1032 // properties enabled.
Fariborz Jahanian62c56022010-04-22 21:01:59 +00001033 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump11289f42009-09-09 15:08:12 +00001034
Douglas Gregor55abb232009-04-10 20:39:37 +00001035 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +00001036 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1037 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00001038 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +00001039 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Anders Carlssonce8dd3a2011-02-19 23:53:54 +00001040 Record.push_back(LangOpts.ObjCExceptions);
Anders Carlsson6bbd2682011-02-23 03:04:54 +00001041 Record.push_back(LangOpts.CXXExceptions);
1042 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor55abb232009-04-10 20:39:37 +00001043
Douglas Gregordbe39272011-02-01 15:15:22 +00001044 Record.push_back(LangOpts.MSBitfields); // MS-compatible structure layout
Douglas Gregor55abb232009-04-10 20:39:37 +00001045 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1046 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1047 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1048
Chris Lattner258172e2009-04-27 07:35:58 +00001049 // Whether static initializers are protected by locks.
1050 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00001051 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +00001052 Record.push_back(LangOpts.Blocks); // block extension to C
1053 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1054 // they are unused.
1055 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1056 // (modulo the platform support).
1057
Chris Lattner51924e512010-06-26 21:25:03 +00001058 Record.push_back(LangOpts.getSignedOverflowBehavior());
1059 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor55abb232009-04-10 20:39:37 +00001060
1061 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +00001062 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +00001063 // defined.
1064 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1065 // opposed to __DYNAMIC__).
1066 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1067
1068 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1069 // used (instead of C99 semantics).
1070 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Chandler Carruth7ffce732011-04-23 20:05:38 +00001071 Record.push_back(LangOpts.Deprecated); // Should __DEPRECATED be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +00001072 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
1073 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +00001074 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
1075 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +00001076 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Argyrios Kyrtzidisa88942a2011-01-15 02:56:16 +00001077 Record.push_back(LangOpts.ShortEnums); // Should the enum type be equivalent
1078 // to the smallest integer type with
1079 // enough room.
Douglas Gregor55abb232009-04-10 20:39:37 +00001080 Record.push_back(LangOpts.getGCMode());
1081 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +00001082 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +00001083 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00001084 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +00001085 Record.push_back(LangOpts.CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +00001086 Record.push_back(LangOpts.CatchUndefined);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001087 Record.push_back(LangOpts.DefaultFPContract);
Anders Carlsson9cedbef2009-08-22 22:30:33 +00001088 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +00001089 Record.push_back(LangOpts.SpellChecking);
Roman Divacky65b88cd2011-03-01 17:40:53 +00001090 Record.push_back(LangOpts.MRTD);
Sebastian Redl539c5062010-08-18 23:57:32 +00001091 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +00001092}
1093
Douglas Gregora7f71a92009-04-10 03:52:48 +00001094//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00001095// stat cache Serialization
1096//===----------------------------------------------------------------------===//
1097
1098namespace {
1099// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001100class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001101public:
1102 typedef const char * key_type;
1103 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001104
Chris Lattner2a6fa472010-11-23 19:28:12 +00001105 typedef struct stat data_type;
1106 typedef const data_type &data_type_ref;
Douglas Gregorc5046832009-04-27 18:38:38 +00001107
1108 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001109 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001110 }
Mike Stump11289f42009-09-09 15:08:12 +00001111
1112 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +00001113 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1114 data_type_ref Data) {
1115 unsigned StrLen = strlen(path);
1116 clang::io::Emit16(Out, StrLen);
Chris Lattner2a6fa472010-11-23 19:28:12 +00001117 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregorc5046832009-04-27 18:38:38 +00001118 clang::io::Emit8(Out, DataLen);
1119 return std::make_pair(StrLen + 1, DataLen);
1120 }
Mike Stump11289f42009-09-09 15:08:12 +00001121
Douglas Gregorc5046832009-04-27 18:38:38 +00001122 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1123 Out.write(path, KeyLen);
1124 }
Mike Stump11289f42009-09-09 15:08:12 +00001125
Chris Lattner2a6fa472010-11-23 19:28:12 +00001126 void EmitData(llvm::raw_ostream &Out, key_type_ref,
Douglas Gregorc5046832009-04-27 18:38:38 +00001127 data_type_ref Data, unsigned DataLen) {
1128 using namespace clang::io;
1129 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +00001130
Chris Lattner2a6fa472010-11-23 19:28:12 +00001131 Emit32(Out, (uint32_t) Data.st_ino);
1132 Emit32(Out, (uint32_t) Data.st_dev);
1133 Emit16(Out, (uint16_t) Data.st_mode);
1134 Emit64(Out, (uint64_t) Data.st_mtime);
1135 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregorc5046832009-04-27 18:38:38 +00001136
1137 assert(Out.tell() - Start == DataLen && "Wrong data length");
1138 }
1139};
1140} // end anonymous namespace
1141
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001142/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001143void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001144 // Build the on-disk hash table containing information about every
1145 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001146 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +00001147 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001148 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +00001149 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001150 Stat != StatEnd; ++Stat, ++NumStatEntries) {
1151 const char *Filename = Stat->first();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001152 Generator.insert(Filename, Stat->second);
1153 }
Mike Stump11289f42009-09-09 15:08:12 +00001154
Douglas Gregorc5046832009-04-27 18:38:38 +00001155 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001156 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +00001157 uint32_t BucketOffset;
1158 {
1159 llvm::raw_svector_ostream Out(StatCacheData);
1160 // Make sure that no bucket is at offset 0
1161 clang::io::Emit32(Out, 0);
1162 BucketOffset = Generator.Emit(Out);
1163 }
1164
1165 // Create a blob abbreviation
1166 using namespace llvm;
1167 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001168 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +00001169 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1170 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1171 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1172 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1173
1174 // Write the stat cache
1175 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001176 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +00001177 Record.push_back(BucketOffset);
1178 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001179 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +00001180}
1181
1182//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +00001183// Source Manager Serialization
1184//===----------------------------------------------------------------------===//
1185
1186/// \brief Create an abbreviation for the SLocEntry that refers to a
1187/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001188static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001189 using namespace llvm;
1190 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001191 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001192 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1193 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1194 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1195 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001196 // FileEntry fields.
1197 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora7f71a92009-04-10 03:52:48 +00001199 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
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.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001205static unsigned CreateSLocBufferAbbrev(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_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001209 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1210 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1211 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1212 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1213 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001214 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001215}
1216
1217/// \brief Create an abbreviation for the SLocEntry that refers to a
1218/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001219static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001220 using namespace llvm;
1221 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001222 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001223 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001224 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001225}
1226
1227/// \brief Create an abbreviation for the SLocEntry that refers to an
1228/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001229static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001230 using namespace llvm;
1231 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001232 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001233 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1234 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1235 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1236 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001237 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001238 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001239}
1240
Douglas Gregor09b69892011-02-10 17:09:37 +00001241namespace {
1242 // Trait used for the on-disk hash table of header search information.
1243 class HeaderFileInfoTrait {
1244 ASTWriter &Writer;
1245 HeaderSearch &HS;
1246
1247 public:
1248 HeaderFileInfoTrait(ASTWriter &Writer, HeaderSearch &HS)
1249 : Writer(Writer), HS(HS) { }
1250
1251 typedef const char *key_type;
1252 typedef key_type key_type_ref;
1253
1254 typedef HeaderFileInfo data_type;
1255 typedef const data_type &data_type_ref;
1256
1257 static unsigned ComputeHash(const char *path) {
1258 // The hash is based only on the filename portion of the key, so that the
1259 // reader can match based on filenames when symlinking or excess path
1260 // elements ("foo/../", "../") change the form of the name. However,
1261 // complete path is still the key.
1262 return llvm::HashString(llvm::sys::path::filename(path));
1263 }
1264
1265 std::pair<unsigned,unsigned>
1266 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1267 data_type_ref Data) {
1268 unsigned StrLen = strlen(path);
1269 clang::io::Emit16(Out, StrLen);
1270 unsigned DataLen = 1 + 2 + 4;
1271 clang::io::Emit8(Out, DataLen);
1272 return std::make_pair(StrLen + 1, DataLen);
1273 }
1274
1275 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1276 Out.write(path, KeyLen);
1277 }
1278
1279 void EmitData(llvm::raw_ostream &Out, key_type_ref,
1280 data_type_ref Data, unsigned DataLen) {
1281 using namespace clang::io;
1282 uint64_t Start = Out.tell(); (void)Start;
1283
Douglas Gregor37aa4932011-05-04 00:14:37 +00001284 unsigned char Flags = (Data.isImport << 4)
1285 | (Data.isPragmaOnce << 3)
Douglas Gregor09b69892011-02-10 17:09:37 +00001286 | (Data.DirInfo << 1)
1287 | Data.Resolved;
1288 Emit8(Out, (uint8_t)Flags);
1289 Emit16(Out, (uint16_t) Data.NumIncludes);
1290
1291 if (!Data.ControllingMacro)
1292 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1293 else
1294 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
1295 assert(Out.tell() - Start == DataLen && "Wrong data length");
1296 }
1297 };
1298} // end anonymous namespace
1299
1300/// \brief Write the header search block for the list of files that
1301///
1302/// \param HS The header search structure to save.
1303///
1304/// \param Chain Whether we're creating a chained AST file.
1305void ASTWriter::WriteHeaderSearch(HeaderSearch &HS, const char* isysroot) {
1306 llvm::SmallVector<const FileEntry *, 16> FilesByUID;
1307 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1308
1309 if (FilesByUID.size() > HS.header_file_size())
1310 FilesByUID.resize(HS.header_file_size());
1311
1312 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1313 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
1314 llvm::SmallVector<const char *, 4> SavedStrings;
1315 unsigned NumHeaderSearchEntries = 0;
1316 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1317 const FileEntry *File = FilesByUID[UID];
1318 if (!File)
1319 continue;
1320
1321 const HeaderFileInfo &HFI = HS.header_file_begin()[UID];
1322 if (HFI.External && Chain)
1323 continue;
1324
1325 // Turn the file name into an absolute path, if it isn't already.
1326 const char *Filename = File->getName();
1327 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1328
1329 // If we performed any translation on the file name at all, we need to
1330 // save this string, since the generator will refer to it later.
1331 if (Filename != File->getName()) {
1332 Filename = strdup(Filename);
1333 SavedStrings.push_back(Filename);
1334 }
1335
1336 Generator.insert(Filename, HFI, GeneratorTrait);
1337 ++NumHeaderSearchEntries;
1338 }
1339
1340 // Create the on-disk hash table in a buffer.
1341 llvm::SmallString<4096> TableData;
1342 uint32_t BucketOffset;
1343 {
1344 llvm::raw_svector_ostream Out(TableData);
1345 // Make sure that no bucket is at offset 0
1346 clang::io::Emit32(Out, 0);
1347 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1348 }
1349
1350 // Create a blob abbreviation
1351 using namespace llvm;
1352 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1353 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1354 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1355 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1356 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1357 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1358
1359 // Write the stat cache
1360 RecordData Record;
1361 Record.push_back(HEADER_SEARCH_TABLE);
1362 Record.push_back(BucketOffset);
1363 Record.push_back(NumHeaderSearchEntries);
1364 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1365
1366 // Free all of the strings we had to duplicate.
1367 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1368 free((void*)SavedStrings[I]);
1369}
1370
Douglas Gregora7f71a92009-04-10 03:52:48 +00001371/// \brief Writes the block containing the serialized form of the
1372/// source manager.
1373///
1374/// TODO: We should probably use an on-disk hash table (stored in a
1375/// blob), indexed based on the file name, so that we only create
1376/// entries for files that we actually need. In the common case (no
1377/// errors), we probably won't have to create file entries for any of
1378/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001379void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001380 const Preprocessor &PP,
1381 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001382 RecordData Record;
1383
Chris Lattner0910e3b2009-04-10 17:16:57 +00001384 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001385 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001386
1387 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001388 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1389 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1390 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1391 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001392
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001393 // Write the line table.
1394 if (SourceMgr.hasLineTable()) {
1395 LineTableInfo &LineTable = SourceMgr.getLineTable();
1396
1397 // Emit the file names
1398 Record.push_back(LineTable.getNumFilenames());
1399 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1400 // Emit the file name
1401 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001402 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001403 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1404 Record.push_back(FilenameLen);
1405 if (FilenameLen)
1406 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1407 }
Mike Stump11289f42009-09-09 15:08:12 +00001408
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001409 // Emit the line entries
1410 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1411 L != LEnd; ++L) {
1412 // Emit the file ID
1413 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +00001414
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001415 // Emit the line entries
1416 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +00001417 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001418 LEEnd = L->second.end();
1419 LE != LEEnd; ++LE) {
1420 Record.push_back(LE->FileOffset);
1421 Record.push_back(LE->LineNo);
1422 Record.push_back(LE->FilenameID);
1423 Record.push_back((unsigned)LE->FileKind);
1424 Record.push_back(LE->IncludeOffset);
1425 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001426 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001427 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001428 }
1429
Douglas Gregor258ae542009-04-27 06:38:32 +00001430 // Write out the source location entry table. We skip the first
1431 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001432 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001433 RecordData PreloadSLocs;
Sebastian Redl5c415f32010-07-22 17:01:13 +00001434 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1435 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1436 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1437 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001438 // Get this source location entry.
1439 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001440
Douglas Gregor258ae542009-04-27 06:38:32 +00001441 // Record the offset of this source-location entry.
1442 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1443
1444 // Figure out which record code to use.
1445 unsigned Code;
1446 if (SLoc->isFile()) {
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001447 if (SLoc->getFile().getContentCache()->OrigEntry)
Sebastian Redl539c5062010-08-18 23:57:32 +00001448 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001449 else
Sebastian Redl539c5062010-08-18 23:57:32 +00001450 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001451 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001452 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001453 Record.clear();
1454 Record.push_back(Code);
1455
1456 Record.push_back(SLoc->getOffset());
1457 if (SLoc->isFile()) {
1458 const SrcMgr::FileInfo &File = SLoc->getFile();
1459 Record.push_back(File.getIncludeLoc().getRawEncoding());
1460 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1461 Record.push_back(File.hasLineDirectives());
1462
1463 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001464 if (Content->OrigEntry) {
1465 assert(Content->OrigEntry == Content->ContentsEntry &&
1466 "Writing to AST an overriden file is not supported");
1467
Douglas Gregor258ae542009-04-27 06:38:32 +00001468 // The source location entry is a file. The blob associated
1469 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001470
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001471 // Emit size/modification time for this file.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001472 Record.push_back(Content->OrigEntry->getSize());
1473 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001474
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001475 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001476 const char *Filename = Content->OrigEntry->getName();
Michael J. Spencer740857f2010-12-21 16:45:57 +00001477 llvm::SmallString<128> FilePath(Filename);
Anders Carlssona4267052011-03-08 16:04:35 +00001478
1479 // Ask the file manager to fixup the relative path for us. This will
1480 // honor the working directory.
1481 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1482
1483 // FIXME: This call to make_absolute shouldn't be necessary, the
1484 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencer740857f2010-12-21 16:45:57 +00001485 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001486 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001487
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001488 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001489 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001490 } else {
1491 // The source location entry is a buffer. The blob associated
1492 // with this entry contains the contents of the buffer.
1493
1494 // We add one to the size so that we capture the trailing NULL
1495 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1496 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001497 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001498 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001499 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001500 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1501 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001502 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001503 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001504 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001505 llvm::StringRef(Buffer->getBufferStart(),
1506 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001507
1508 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl5c415f32010-07-22 17:01:13 +00001509 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001510 }
1511 } else {
1512 // The source location entry is an instantiation.
1513 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1514 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1515 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1516 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1517
1518 // Compute the token length for this macro expansion.
1519 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001520 if (I + 1 != N)
1521 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001522 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1523 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1524 }
1525 }
1526
Douglas Gregor8f45df52009-04-16 22:23:12 +00001527 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001528
1529 if (SLocEntryOffsets.empty())
1530 return;
1531
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001532 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001533 // table is used for lazily loading source-location information.
1534 using namespace llvm;
1535 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001536 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001537 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1538 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1539 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1540 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001541
Douglas Gregor258ae542009-04-27 06:38:32 +00001542 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001543 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001544 Record.push_back(SLocEntryOffsets.size());
Sebastian Redlc1d035f2010-09-22 20:19:08 +00001545 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1546 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001547 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001548
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001549 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001550 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001551 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001552}
1553
Douglas Gregorc5046832009-04-27 18:38:38 +00001554//===----------------------------------------------------------------------===//
1555// Preprocessor Serialization
1556//===----------------------------------------------------------------------===//
1557
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001558static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1559 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1560 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1561 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1562 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1563 return X.first->getName().compare(Y.first->getName());
1564}
1565
Chris Lattnereeffaef2009-04-10 17:15:23 +00001566/// \brief Writes the block containing the serialized form of the
1567/// preprocessor.
1568///
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001569void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001570 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001571
Chris Lattner0af3ba12009-04-13 01:29:17 +00001572 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1573 if (PP.getCounterValue() != 0) {
1574 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001575 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001576 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001577 }
1578
1579 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001580 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001581
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001582 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001583 // FIXME: use diagnostics subsystem for localization etc.
1584 if (PP.SawDateOrTime())
1585 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001586
Douglas Gregor796d76a2010-10-20 22:00:55 +00001587
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001588 // Loop over all the macro definitions that are live at the end of the file,
1589 // emitting each to the PP section.
Douglas Gregoraae92242010-03-19 21:51:54 +00001590 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001591
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001592 // Construct the list of macro definitions that need to be serialized.
1593 llvm::SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
1594 MacrosToEmit;
1595 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor68051a72011-02-11 00:26:14 +00001596 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1597 E = PP.macro_end(Chain == 0);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001598 I != E; ++I) {
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001599 MacroDefinitionsSeen.insert(I->first);
1600 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1601 }
1602
1603 // Sort the set of macro definitions that need to be serialized by the
1604 // name of the macro, to provide a stable ordering.
1605 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1606 &compareMacroDefinitions);
1607
Douglas Gregor68051a72011-02-11 00:26:14 +00001608 // Resolve any identifiers that defined macros at the time they were
1609 // deserialized, adding them to the list of macros to emit (if appropriate).
1610 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1611 IdentifierInfo *Name
1612 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1613 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1614 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1615 }
1616
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001617 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1618 const IdentifierInfo *Name = MacrosToEmit[I].first;
1619 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor68051a72011-02-11 00:26:14 +00001620 if (!MI)
1621 continue;
1622
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001623 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001624 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001625 // Also skip macros from a AST file if we're chaining.
Douglas Gregoreb114da2010-10-01 01:03:07 +00001626
1627 // FIXME: There is a (probably minor) optimization we could do here, if
1628 // the macro comes from the original PCH but the identifier comes from a
1629 // chained PCH, by storing the offset into the original PCH rather than
1630 // writing the macro definition a second time.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001631 if (MI->isBuiltinMacro() ||
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001632 (Chain && Name->isFromAST() && MI->isFromAST()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001633 continue;
1634
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001635 AddIdentifierRef(Name, Record);
1636 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001637 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1638 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001639
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001640 unsigned Code;
1641 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001642 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001643 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001644 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001645
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001646 Record.push_back(MI->isC99Varargs());
1647 Record.push_back(MI->isGNUVarargs());
1648 Record.push_back(MI->getNumArgs());
1649 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1650 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001651 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001652 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001653
Douglas Gregoraae92242010-03-19 21:51:54 +00001654 // If we have a detailed preprocessing record, record the macro definition
1655 // ID that corresponds to this macro.
1656 if (PPRec)
1657 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001658
Douglas Gregor8f45df52009-04-16 22:23:12 +00001659 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001660 Record.clear();
1661
Chris Lattner2199f5b2009-04-10 18:08:30 +00001662 // Emit the tokens array.
1663 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1664 // Note that we know that the preprocessor does not have any annotation
1665 // tokens in it because they are created by the parser, and thus can't be
1666 // in a macro definition.
1667 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001668
Chris Lattner2199f5b2009-04-10 18:08:30 +00001669 Record.push_back(Tok.getLocation().getRawEncoding());
1670 Record.push_back(Tok.getLength());
1671
Chris Lattner2199f5b2009-04-10 18:08:30 +00001672 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1673 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001674 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001675 // FIXME: Should translate token kind to a stable encoding.
1676 Record.push_back(Tok.getKind());
1677 // FIXME: Should translate token flags to a stable encoding.
1678 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001679
Sebastian Redl539c5062010-08-18 23:57:32 +00001680 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001681 Record.clear();
1682 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001683 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001684 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001685 Stream.ExitBlock();
1686
1687 if (PPRec)
1688 WritePreprocessorDetail(*PPRec);
1689}
1690
1691void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
1692 if (PPRec.begin(Chain) == PPRec.end(Chain))
1693 return;
1694
1695 // Enter the preprocessor block.
1696 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001697
Douglas Gregoraae92242010-03-19 21:51:54 +00001698 // If the preprocessor has a preprocessing record, emit it.
1699 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001700 using namespace llvm;
1701
1702 // Set up the abbreviation for
1703 unsigned InclusionAbbrev = 0;
1704 {
1705 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1706 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
1707 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1708 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1709 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1710 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1711 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1712 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1713 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1714 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1715 }
1716
1717 unsigned IndexBase = Chain ? PPRec.getNumPreallocatedEntities() : 0;
1718 RecordData Record;
1719 for (PreprocessingRecord::iterator E = PPRec.begin(Chain),
1720 EEnd = PPRec.end(Chain);
1721 E != EEnd; ++E) {
1722 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001723
Douglas Gregor92a96f52011-02-08 21:58:10 +00001724 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1725 // Record this macro definition's location.
1726 MacroID ID = getMacroDefinitionID(MD);
1727
1728 // Don't write the macro definition if it is from another AST file.
1729 if (ID < FirstMacroID)
Douglas Gregoraae92242010-03-19 21:51:54 +00001730 continue;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001731
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001732 // Notify the serialization listener that we're serializing this entity.
1733 if (SerializationListener)
1734 SerializationListener->SerializedPreprocessedEntity(*E,
Douglas Gregor92a96f52011-02-08 21:58:10 +00001735 Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001736
Douglas Gregor92a96f52011-02-08 21:58:10 +00001737 unsigned Position = ID - FirstMacroID;
1738 if (Position != MacroDefinitionOffsets.size()) {
1739 if (Position > MacroDefinitionOffsets.size())
1740 MacroDefinitionOffsets.resize(Position + 1);
1741
1742 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
1743 } else
1744 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001745
Douglas Gregor92a96f52011-02-08 21:58:10 +00001746 Record.push_back(IndexBase + NumPreprocessingRecords++);
1747 Record.push_back(ID);
1748 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1749 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1750 AddIdentifierRef(MD->getName(), Record);
1751 AddSourceLocation(MD->getLocation(), Record);
1752 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1753 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00001754 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001755
Douglas Gregor92a96f52011-02-08 21:58:10 +00001756 // Notify the serialization listener that we're serializing this entity.
1757 if (SerializationListener)
1758 SerializationListener->SerializedPreprocessedEntity(*E,
1759 Stream.GetCurrentBitNo());
1760
1761 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1762 Record.push_back(IndexBase + NumPreprocessingRecords++);
1763 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1764 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1765 AddIdentifierRef(MI->getName(), Record);
1766 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1767 Stream.EmitRecord(PPD_MACRO_INSTANTIATION, Record);
1768 continue;
1769 }
1770
1771 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1772 Record.push_back(PPD_INCLUSION_DIRECTIVE);
1773 Record.push_back(IndexBase + NumPreprocessingRecords++);
1774 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1775 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1776 Record.push_back(ID->getFileName().size());
1777 Record.push_back(ID->wasInQuotes());
1778 Record.push_back(static_cast<unsigned>(ID->getKind()));
1779 llvm::SmallString<64> Buffer;
1780 Buffer += ID->getFileName();
1781 Buffer += ID->getFile()->getName();
1782 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1783 continue;
1784 }
1785
1786 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1787 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001788 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001789
Douglas Gregoraae92242010-03-19 21:51:54 +00001790 // Write the offsets table for the preprocessing record.
1791 if (NumPreprocessingRecords > 0) {
1792 // Write the offsets table for identifier IDs.
1793 using namespace llvm;
1794 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001795 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregoraae92242010-03-19 21:51:54 +00001796 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1797 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1798 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1799 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001800
Douglas Gregoraae92242010-03-19 21:51:54 +00001801 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001802 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001803 Record.push_back(NumPreprocessingRecords);
1804 Record.push_back(MacroDefinitionOffsets.size());
1805 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001806 data(MacroDefinitionOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00001807 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001808}
1809
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001810void ASTWriter::WritePragmaDiagnosticMappings(const Diagnostic &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001811 RecordData Record;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001812 for (Diagnostic::DiagStatePointsTy::const_iterator
1813 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1814 I != E; ++I) {
1815 const Diagnostic::DiagStatePoint &point = *I;
1816 if (point.Loc.isInvalid())
1817 continue;
1818
1819 Record.push_back(point.Loc.getRawEncoding());
1820 for (Diagnostic::DiagState::iterator
1821 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
1822 unsigned diag = I->first, map = I->second;
1823 if (map & 0x10) { // mapping from a diagnostic pragma.
1824 Record.push_back(diag);
1825 Record.push_back(map & 0x7);
1826 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001827 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001828 Record.push_back(-1); // mark the end of the diag/map pairs for this
1829 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001830 }
1831
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00001832 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001833 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001834}
1835
Anders Carlsson9bb83e82011-03-06 18:41:18 +00001836void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
1837 if (CXXBaseSpecifiersOffsets.empty())
1838 return;
1839
1840 RecordData Record;
1841
1842 // Create a blob abbreviation for the C++ base specifiers offsets.
1843 using namespace llvm;
1844
1845 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1846 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
1847 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
1848 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1849 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1850
1851 // Write the selector offsets table.
1852 Record.clear();
1853 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
1854 Record.push_back(CXXBaseSpecifiersOffsets.size());
1855 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001856 data(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00001857}
1858
Douglas Gregorc5046832009-04-27 18:38:38 +00001859//===----------------------------------------------------------------------===//
1860// Type Serialization
1861//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001862
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001863/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001864void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00001865 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001866 if (Idx.getIndex() == 0) // we haven't seen this type before.
1867 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00001868
Douglas Gregor9b3932c2010-10-05 18:37:06 +00001869 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00001870
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001871 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001872 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001873 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001874 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001875 else if (TypeOffsets.size() < Index) {
1876 TypeOffsets.resize(Index + 1);
1877 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001878 }
1879
1880 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001881
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001882 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001883 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001884
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001885 if (T.hasLocalNonFastQualifiers()) {
1886 Qualifiers Qs = T.getLocalQualifiers();
1887 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001888 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001889 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00001890 } else {
1891 switch (T->getTypeClass()) {
1892 // For all of the concrete, non-dependent types, call the
1893 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001894#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001895 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001896#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001897#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001898 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001899 }
1900
1901 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001902 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001903
1904 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001905 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001906}
1907
Douglas Gregorc5046832009-04-27 18:38:38 +00001908//===----------------------------------------------------------------------===//
1909// Declaration Serialization
1910//===----------------------------------------------------------------------===//
1911
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001912/// \brief Write the block containing all of the declaration IDs
1913/// lexically declared within the given DeclContext.
1914///
1915/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1916/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001917uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001918 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001919 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001920 return 0;
1921
Douglas Gregor8f45df52009-04-16 22:23:12 +00001922 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001923 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001924 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001925 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001926 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1927 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001928 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001929
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001930 ++NumLexicalDeclContexts;
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001931 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001932 return Offset;
1933}
1934
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001935void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001936 using namespace llvm;
1937 RecordData Record;
1938
1939 // Write the type offsets array
1940 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001941 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001942 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1943 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1944 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1945 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001946 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001947 Record.push_back(TypeOffsets.size());
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001948 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001949
1950 // Write the declaration offsets array
1951 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001952 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001953 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1954 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1955 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1956 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001957 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001958 Record.push_back(DeclOffsets.size());
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001959 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001960}
1961
Douglas Gregorc5046832009-04-27 18:38:38 +00001962//===----------------------------------------------------------------------===//
1963// Global Method Pool and Selector Serialization
1964//===----------------------------------------------------------------------===//
1965
Douglas Gregore84a9da2009-04-20 20:36:09 +00001966namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001967// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001968class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001969 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001970
1971public:
1972 typedef Selector key_type;
1973 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001974
Sebastian Redl834bb972010-08-04 17:20:04 +00001975 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00001976 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00001977 ObjCMethodList Instance, Factory;
1978 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00001979 typedef const data_type& data_type_ref;
1980
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001981 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001982
Douglas Gregorc78d3462009-04-24 21:10:55 +00001983 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00001984 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001985 }
Mike Stump11289f42009-09-09 15:08:12 +00001986
1987 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001988 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1989 data_type_ref Methods) {
1990 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1991 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00001992 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1993 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001994 Method = Method->Next)
1995 if (Method->Method)
1996 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00001997 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001998 Method = Method->Next)
1999 if (Method->Method)
2000 DataLen += 4;
2001 clang::io::Emit16(Out, DataLen);
2002 return std::make_pair(KeyLen, DataLen);
2003 }
Mike Stump11289f42009-09-09 15:08:12 +00002004
Douglas Gregor95c13f52009-04-25 17:48:32 +00002005 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00002006 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002007 assert((Start >> 32) == 0 && "Selector key offset too large");
2008 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002009 unsigned N = Sel.getNumArgs();
2010 clang::io::Emit16(Out, N);
2011 if (N == 0)
2012 N = 1;
2013 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00002014 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002015 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2016 }
Mike Stump11289f42009-09-09 15:08:12 +00002017
Douglas Gregorc78d3462009-04-24 21:10:55 +00002018 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002019 data_type_ref Methods, unsigned DataLen) {
2020 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00002021 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002022 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002023 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002024 Method = Method->Next)
2025 if (Method->Method)
2026 ++NumInstanceMethods;
2027
2028 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002029 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002030 Method = Method->Next)
2031 if (Method->Method)
2032 ++NumFactoryMethods;
2033
2034 clang::io::Emit16(Out, NumInstanceMethods);
2035 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00002036 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002037 Method = Method->Next)
2038 if (Method->Method)
2039 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002040 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002041 Method = Method->Next)
2042 if (Method->Method)
2043 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002044
2045 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002046 }
2047};
2048} // end anonymous namespace
2049
Sebastian Redla19a67f2010-08-03 21:58:15 +00002050/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002051///
2052/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002053/// in an on-disk hash table indexed by the selector. The hash table also
2054/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002055void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002056 using namespace llvm;
2057
Sebastian Redla19a67f2010-08-03 21:58:15 +00002058 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002059 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002060 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002061 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002062 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002063 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002064 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002065 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002066
Sebastian Redla19a67f2010-08-03 21:58:15 +00002067 // Create the on-disk hash table representation. We walk through every
2068 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002069 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002070 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002071 I = SelectorIDs.begin(), E = SelectorIDs.end();
2072 I != E; ++I) {
2073 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002074 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002075 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002076 I->second,
2077 ObjCMethodList(),
2078 ObjCMethodList()
2079 };
2080 if (F != SemaRef.MethodPool.end()) {
2081 Data.Instance = F->second.first;
2082 Data.Factory = F->second.second;
2083 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002084 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002085 // changed.
2086 if (Chain && I->second < FirstSelectorID) {
2087 // Selector already exists. Did it change?
2088 bool changed = false;
2089 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2090 M = M->Next) {
2091 if (M->Method->getPCHLevel() == 0)
2092 changed = true;
2093 }
2094 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2095 M = M->Next) {
2096 if (M->Method->getPCHLevel() == 0)
2097 changed = true;
2098 }
2099 if (!changed)
2100 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002101 } else if (Data.Instance.Method || Data.Factory.Method) {
2102 // A new method pool entry.
2103 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002104 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002105 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002106 }
2107
Douglas Gregorc78d3462009-04-24 21:10:55 +00002108 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002109 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002110 uint32_t BucketOffset;
2111 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002112 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002113 llvm::raw_svector_ostream Out(MethodPool);
2114 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002115 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002116 BucketOffset = Generator.Emit(Out, Trait);
2117 }
2118
2119 // Create a blob abbreviation
2120 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002121 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002122 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002123 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002124 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2125 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2126
Douglas Gregor95c13f52009-04-25 17:48:32 +00002127 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002128 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002129 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002130 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002131 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002132 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002133
2134 // Create a blob abbreviation for the selector table offsets.
2135 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002136 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002137 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor95c13f52009-04-25 17:48:32 +00002138 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2139 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2140
2141 // Write the selector offsets table.
2142 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002143 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002144 Record.push_back(SelectorOffsets.size());
2145 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002146 data(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002147 }
2148}
2149
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002150/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002151void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002152 using namespace llvm;
2153 if (SemaRef.ReferencedSelectors.empty())
2154 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002155
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002156 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002157
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002158 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002159 // very tricky to fix, and given that @selector shouldn't really appear in
2160 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002161 for (DenseMap<Selector, SourceLocation>::iterator S =
2162 SemaRef.ReferencedSelectors.begin(),
2163 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2164 Selector Sel = (*S).first;
2165 SourceLocation Loc = (*S).second;
2166 AddSelectorRef(Sel, Record);
2167 AddSourceLocation(Loc, Record);
2168 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002169 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002170}
2171
Douglas Gregorc5046832009-04-27 18:38:38 +00002172//===----------------------------------------------------------------------===//
2173// Identifier Table Serialization
2174//===----------------------------------------------------------------------===//
2175
Douglas Gregorc78d3462009-04-24 21:10:55 +00002176namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002177class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002178 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002179 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002180
Douglas Gregor1d583f22009-04-28 21:18:29 +00002181 /// \brief Determines whether this is an "interesting" identifier
2182 /// that needs a full IdentifierInfo structure written into the hash
2183 /// table.
2184 static bool isInterestingIdentifier(const IdentifierInfo *II) {
2185 return II->isPoisoned() ||
2186 II->isExtensionToken() ||
2187 II->hasMacroDefinition() ||
2188 II->getObjCOrBuiltinID() ||
2189 II->getFETokenInfo<void>();
2190 }
2191
Douglas Gregore84a9da2009-04-20 20:36:09 +00002192public:
2193 typedef const IdentifierInfo* key_type;
2194 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002195
Sebastian Redl539c5062010-08-18 23:57:32 +00002196 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002197 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002198
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002199 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00002200 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00002201
2202 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00002203 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00002204 }
Mike Stump11289f42009-09-09 15:08:12 +00002205
2206 std::pair<unsigned,unsigned>
2207 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002208 IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002209 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002210 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
2211 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00002212 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00002213 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00002214 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00002215 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002216 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2217 DEnd = IdentifierResolver::end();
2218 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00002219 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00002220 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00002221 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00002222 // We emit the key length after the data length so that every
2223 // string is preceded by a 16-bit length. This matches the PTH
2224 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00002225 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002226 return std::make_pair(KeyLen, DataLen);
2227 }
Mike Stump11289f42009-09-09 15:08:12 +00002228
2229 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00002230 unsigned KeyLen) {
2231 // Record the location of the key data. This is used when generating
2232 // the mapping from persistent IDs to strings.
2233 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002234 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002235 }
Mike Stump11289f42009-09-09 15:08:12 +00002236
2237 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002238 IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00002239 if (!isInterestingIdentifier(II)) {
2240 clang::io::Emit32(Out, ID << 1);
2241 return;
2242 }
Douglas Gregorb9256522009-04-28 21:32:13 +00002243
Douglas Gregor1d583f22009-04-28 21:18:29 +00002244 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002245 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002246 bool hasMacroDefinition =
2247 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00002248 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00002249 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002250 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
2251 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2252 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00002253 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002254 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00002255 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002256
Douglas Gregorc3366a52009-04-21 23:56:24 +00002257 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00002258 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00002259
Douglas Gregora868bbd2009-04-21 22:25:48 +00002260 // Emit the declaration IDs in reverse order, because the
2261 // IdentifierResolver provides the declarations as they would be
2262 // visible (e.g., the function "stat" would come before the struct
2263 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2264 // adds declarations to the end of the list (so we need to see the
2265 // struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00002266 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump11289f42009-09-09 15:08:12 +00002267 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00002268 IdentifierResolver::end());
2269 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
2270 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00002271 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00002272 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002273 }
2274};
2275} // end anonymous namespace
2276
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002277/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002278///
2279/// The identifier table consists of a blob containing string data
2280/// (the actual identifiers themselves) and a separate "offsets" index
2281/// that maps identifier IDs to locations within the blob.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002282void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002283 using namespace llvm;
2284
2285 // Create and write out the blob that contains the identifier
2286 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002287 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002288 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002289 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump11289f42009-09-09 15:08:12 +00002290
Douglas Gregore6648fb2009-04-28 20:33:11 +00002291 // Look for any identifiers that were named while processing the
2292 // headers, but are otherwise not needed. We add these to the hash
2293 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002294 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00002295 // file.
2296 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2297 IDEnd = PP.getIdentifierTable().end();
2298 ID != IDEnd; ++ID)
2299 getIdentifierRef(ID->second);
2300
Sebastian Redlff4a2952010-07-23 23:49:55 +00002301 // Create the on-disk hash table representation. We only store offsets
2302 // for identifiers that appear here for the first time.
2303 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002304 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002305 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2306 ID != IDEnd; ++ID) {
2307 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002308 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002309 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002310 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002311
Douglas Gregore84a9da2009-04-20 20:36:09 +00002312 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002313 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00002314 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002315 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002316 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002317 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002318 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002319 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002320 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002321 }
2322
2323 // Create a blob abbreviation
2324 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002325 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00002326 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002327 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00002328 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002329
2330 // Write the identifier table
2331 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002332 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002333 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002334 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002335 }
2336
2337 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00002338 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002339 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00002340 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2341 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2342 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2343
2344 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002345 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00002346 Record.push_back(IdentifierOffsets.size());
2347 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002348 data(IdentifierOffsets));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002349}
2350
Douglas Gregorc5046832009-04-27 18:38:38 +00002351//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002352// DeclContext's Name Lookup Table Serialization
2353//===----------------------------------------------------------------------===//
2354
2355namespace {
2356// Trait used for the on-disk hash table used in the method pool.
2357class ASTDeclContextNameLookupTrait {
2358 ASTWriter &Writer;
2359
2360public:
2361 typedef DeclarationName key_type;
2362 typedef key_type key_type_ref;
2363
2364 typedef DeclContext::lookup_result data_type;
2365 typedef const data_type& data_type_ref;
2366
2367 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2368
2369 unsigned ComputeHash(DeclarationName Name) {
2370 llvm::FoldingSetNodeID ID;
2371 ID.AddInteger(Name.getNameKind());
2372
2373 switch (Name.getNameKind()) {
2374 case DeclarationName::Identifier:
2375 ID.AddString(Name.getAsIdentifierInfo()->getName());
2376 break;
2377 case DeclarationName::ObjCZeroArgSelector:
2378 case DeclarationName::ObjCOneArgSelector:
2379 case DeclarationName::ObjCMultiArgSelector:
2380 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2381 break;
2382 case DeclarationName::CXXConstructorName:
2383 case DeclarationName::CXXDestructorName:
2384 case DeclarationName::CXXConversionFunctionName:
2385 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
2386 break;
2387 case DeclarationName::CXXOperatorName:
2388 ID.AddInteger(Name.getCXXOverloadedOperator());
2389 break;
2390 case DeclarationName::CXXLiteralOperatorName:
2391 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2392 case DeclarationName::CXXUsingDirective:
2393 break;
2394 }
2395
2396 return ID.ComputeHash();
2397 }
2398
2399 std::pair<unsigned,unsigned>
2400 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2401 data_type_ref Lookup) {
2402 unsigned KeyLen = 1;
2403 switch (Name.getNameKind()) {
2404 case DeclarationName::Identifier:
2405 case DeclarationName::ObjCZeroArgSelector:
2406 case DeclarationName::ObjCOneArgSelector:
2407 case DeclarationName::ObjCMultiArgSelector:
2408 case DeclarationName::CXXConstructorName:
2409 case DeclarationName::CXXDestructorName:
2410 case DeclarationName::CXXConversionFunctionName:
2411 case DeclarationName::CXXLiteralOperatorName:
2412 KeyLen += 4;
2413 break;
2414 case DeclarationName::CXXOperatorName:
2415 KeyLen += 1;
2416 break;
2417 case DeclarationName::CXXUsingDirective:
2418 break;
2419 }
2420 clang::io::Emit16(Out, KeyLen);
2421
2422 // 2 bytes for num of decls and 4 for each DeclID.
2423 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2424 clang::io::Emit16(Out, DataLen);
2425
2426 return std::make_pair(KeyLen, DataLen);
2427 }
2428
2429 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2430 using namespace clang::io;
2431
2432 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2433 Emit8(Out, Name.getNameKind());
2434 switch (Name.getNameKind()) {
2435 case DeclarationName::Identifier:
2436 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2437 break;
2438 case DeclarationName::ObjCZeroArgSelector:
2439 case DeclarationName::ObjCOneArgSelector:
2440 case DeclarationName::ObjCMultiArgSelector:
2441 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2442 break;
2443 case DeclarationName::CXXConstructorName:
2444 case DeclarationName::CXXDestructorName:
2445 case DeclarationName::CXXConversionFunctionName:
2446 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2447 break;
2448 case DeclarationName::CXXOperatorName:
2449 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2450 Emit8(Out, Name.getCXXOverloadedOperator());
2451 break;
2452 case DeclarationName::CXXLiteralOperatorName:
2453 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2454 break;
2455 case DeclarationName::CXXUsingDirective:
2456 break;
2457 }
2458 }
2459
2460 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2461 data_type Lookup, unsigned DataLen) {
2462 uint64_t Start = Out.tell(); (void)Start;
2463 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2464 for (; Lookup.first != Lookup.second; ++Lookup.first)
2465 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2466
2467 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2468 }
2469};
2470} // end anonymous namespace
2471
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002472/// \brief Write the block containing all of the declaration IDs
2473/// visible from the given DeclContext.
2474///
2475/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002476/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002477uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2478 DeclContext *DC) {
2479 if (DC->getPrimaryContext() != DC)
2480 return 0;
2481
2482 // Since there is no name lookup into functions or methods, don't bother to
2483 // build a visible-declarations table for these entities.
2484 if (DC->isFunctionOrMethod())
2485 return 0;
2486
2487 // If not in C++, we perform name lookup for the translation unit via the
2488 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2489 // FIXME: In C++ we need the visible declarations in order to "see" the
2490 // friend declarations, is there a way to do this without writing the table ?
2491 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2492 return 0;
2493
2494 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00002495 if (DC->hasExternalVisibleStorage())
2496 DC->MaterializeVisibleDeclsFromExternalStorage();
2497 else
2498 DC->lookup(DeclarationName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002499
2500 // Serialize the contents of the mapping used for lookup. Note that,
2501 // although we have two very different code paths, the serialized
2502 // representation is the same for both cases: a declaration name,
2503 // followed by a size, followed by references to the visible
2504 // declarations that have that name.
2505 uint64_t Offset = Stream.GetCurrentBitNo();
2506 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2507 if (!Map || Map->empty())
2508 return 0;
2509
2510 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2511 ASTDeclContextNameLookupTrait Trait(*this);
2512
2513 // Create the on-disk hash table representation.
2514 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2515 D != DEnd; ++D) {
2516 DeclarationName Name = D->first;
2517 DeclContext::lookup_result Result = D->second.getLookupResult();
2518 Generator.insert(Name, Result, Trait);
2519 }
2520
2521 // Create the on-disk hash table in a buffer.
2522 llvm::SmallString<4096> LookupTable;
2523 uint32_t BucketOffset;
2524 {
2525 llvm::raw_svector_ostream Out(LookupTable);
2526 // Make sure that no bucket is at offset 0
2527 clang::io::Emit32(Out, 0);
2528 BucketOffset = Generator.Emit(Out, Trait);
2529 }
2530
2531 // Write the lookup table
2532 RecordData Record;
2533 Record.push_back(DECL_CONTEXT_VISIBLE);
2534 Record.push_back(BucketOffset);
2535 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2536 LookupTable.str());
2537
2538 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2539 ++NumVisibleDeclContexts;
2540 return Offset;
2541}
2542
Sebastian Redla4071b42010-08-24 00:50:09 +00002543/// \brief Write an UPDATE_VISIBLE block for the given context.
2544///
2545/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2546/// DeclContext in a dependent AST file. As such, they only exist for the TU
2547/// (in C++) and for namespaces.
2548void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00002549 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2550 if (!Map || Map->empty())
2551 return;
2552
2553 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2554 ASTDeclContextNameLookupTrait Trait(*this);
2555
2556 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002557 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2558 D != DEnd; ++D) {
2559 DeclarationName Name = D->first;
2560 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002561 // For any name that appears in this table, the results are complete, i.e.
2562 // they overwrite results from previous PCHs. Merging is always a mess.
2563 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002564 }
2565
2566 // Create the on-disk hash table in a buffer.
2567 llvm::SmallString<4096> LookupTable;
2568 uint32_t BucketOffset;
2569 {
2570 llvm::raw_svector_ostream Out(LookupTable);
2571 // Make sure that no bucket is at offset 0
2572 clang::io::Emit32(Out, 0);
2573 BucketOffset = Generator.Emit(Out, Trait);
2574 }
2575
2576 // Write the lookup table
2577 RecordData Record;
2578 Record.push_back(UPDATE_VISIBLE);
2579 Record.push_back(getDeclID(cast<Decl>(DC)));
2580 Record.push_back(BucketOffset);
2581 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2582}
2583
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002584/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2585void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2586 RecordData Record;
2587 Record.push_back(Opts.fp_contract);
2588 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2589}
2590
2591/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2592void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
2593 if (!SemaRef.Context.getLangOptions().OpenCL)
2594 return;
2595
2596 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2597 RecordData Record;
2598#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2599#include "clang/Basic/OpenCLExtensions.def"
2600 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2601}
2602
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002603//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00002604// General Serialization Routines
2605//===----------------------------------------------------------------------===//
2606
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002607/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002608void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00002609 Record.push_back(Attrs.size());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002610 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2611 const Attr * A = *i;
2612 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2613 AddSourceLocation(A->getLocation(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002614
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002615#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00002616
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002617 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002618}
2619
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002620void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002621 Record.push_back(Str.size());
2622 Record.insert(Record.end(), Str.begin(), Str.end());
2623}
2624
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002625void ASTWriter::AddVersionTuple(const VersionTuple &Version,
2626 RecordDataImpl &Record) {
2627 Record.push_back(Version.getMajor());
2628 if (llvm::Optional<unsigned> Minor = Version.getMinor())
2629 Record.push_back(*Minor + 1);
2630 else
2631 Record.push_back(0);
2632 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
2633 Record.push_back(*Subminor + 1);
2634 else
2635 Record.push_back(0);
2636}
2637
Douglas Gregore84a9da2009-04-20 20:36:09 +00002638/// \brief Note that the identifier II occurs at the given offset
2639/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002640void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002641 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002642 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00002643 // up earlier in the chain and thus don't need an offset.
2644 if (ID >= FirstIdentID)
2645 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002646}
2647
Douglas Gregor95c13f52009-04-25 17:48:32 +00002648/// \brief Note that the selector Sel occurs at the given offset
2649/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002650void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00002651 unsigned ID = SelectorIDs[Sel];
2652 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00002653 // Don't record offsets for selectors that are also available in a different
2654 // file.
2655 if (ID < FirstSelectorID)
2656 return;
2657 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002658}
2659
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002660ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00002661 : Stream(Stream), Chain(0), SerializationListener(0),
2662 FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00002663 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002664 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor91096292010-10-02 19:29:26 +00002665 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2666 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002667 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002668 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
2669 NextCXXBaseSpecifiersID(1)
2670{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002671}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002672
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002673void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002674 const std::string &OutputFile,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002675 const char *isysroot) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002676 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002677 Stream.Emit((unsigned)'C', 8);
2678 Stream.Emit((unsigned)'P', 8);
2679 Stream.Emit((unsigned)'C', 8);
2680 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002681
Chris Lattner28fa4e62009-04-26 22:26:21 +00002682 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002683
Sebastian Redl143413f2010-07-12 22:02:52 +00002684 if (Chain)
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002685 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002686 else
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002687 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile);
Sebastian Redl143413f2010-07-12 22:02:52 +00002688}
2689
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002690void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002691 const char *isysroot,
2692 const std::string &OutputFile) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002693 using namespace llvm;
2694
2695 ASTContext &Context = SemaRef.Context;
2696 Preprocessor &PP = SemaRef.PP;
2697
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002698 // The translation unit is the first declaration we'll emit.
2699 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlff4a2952010-07-23 23:49:55 +00002700 ++NextDeclID;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002701 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002702
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002703 // Make sure that we emit IdentifierInfos (and any attached
2704 // declarations) for builtins.
2705 {
2706 IdentifierTable &Table = PP.getIdentifierTable();
2707 llvm::SmallVector<const char *, 32> BuiltinNames;
2708 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2709 Context.getLangOptions().NoBuiltin);
2710 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2711 getIdentifierRef(&Table.get(BuiltinNames[I]));
2712 }
2713
Chris Lattner0c797362009-09-08 18:19:27 +00002714 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002715 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002716 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002717 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00002718 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2719 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00002720 }
Douglas Gregord4df8652009-04-22 22:02:47 +00002721
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002722 // Build a record containing all of the file scoped decls in this file.
2723 RecordData UnusedFileScopedDecls;
2724 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2725 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002726
Alexis Hunt27a761d2011-05-04 23:29:54 +00002727 RecordData DelegatingCtorDecls;
2728 for (unsigned i=0, e = SemaRef.DelegatingCtorDecls.size(); i != e; ++i)
2729 AddDeclRef(SemaRef.DelegatingCtorDecls[i], DelegatingCtorDecls);
2730
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002731 RecordData WeakUndeclaredIdentifiers;
2732 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2733 WeakUndeclaredIdentifiers.push_back(
2734 SemaRef.WeakUndeclaredIdentifiers.size());
2735 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2736 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2737 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2738 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2739 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2740 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2741 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2742 }
2743 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002744
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002745 // Build a record containing all of the locally-scoped external
2746 // declarations in this header file. Generally, this record will be
2747 // empty.
2748 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002749 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00002750 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002751 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002752 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2753 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2754 TD != TDEnd; ++TD)
2755 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2756
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002757 // Build a record containing all of the ext_vector declarations.
2758 RecordData ExtVectorDecls;
2759 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2760 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2761
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002762 // Build a record containing all of the VTable uses information.
2763 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00002764 if (!SemaRef.VTableUses.empty()) {
2765 VTableUses.push_back(SemaRef.VTableUses.size());
2766 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2767 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2768 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2769 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2770 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002771 }
2772
2773 // Build a record containing all of dynamic classes declarations.
2774 RecordData DynamicClasses;
2775 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2776 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2777
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002778 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002779 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002780 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002781 I = SemaRef.PendingInstantiations.begin(),
2782 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2783 AddDeclRef(I->first, PendingInstantiations);
2784 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002785 }
2786 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2787 "There are local ones at end of translation unit!");
2788
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002789 // Build a record containing some declaration references.
2790 RecordData SemaDeclRefs;
2791 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2792 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2793 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2794 }
2795
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002796 RecordData CUDASpecialDeclRefs;
2797 if (Context.getcudaConfigureCallDecl()) {
2798 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
2799 }
2800
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002801 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002802 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002803 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002804 WriteMetadata(Context, isysroot, OutputFile);
Sebastian Redl143413f2010-07-12 22:02:52 +00002805 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002806 if (StatCalls && !isysroot)
Douglas Gregor11cfd942010-07-12 23:48:14 +00002807 WriteStatCache(*StatCalls);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002808 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc277ad12009-07-18 15:33:26 +00002809 // Write the record of special types.
2810 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002811
Steve Naroffc277ad12009-07-18 15:33:26 +00002812 AddTypeRef(Context.getBuiltinVaListType(), Record);
2813 AddTypeRef(Context.getObjCIdType(), Record);
2814 AddTypeRef(Context.getObjCSelType(), Record);
2815 AddTypeRef(Context.getObjCProtoType(), Record);
2816 AddTypeRef(Context.getObjCClassType(), Record);
2817 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2818 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2819 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002820 AddTypeRef(Context.getjmp_bufType(), Record);
2821 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002822 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2823 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpd0153282009-10-20 02:12:22 +00002824 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002825 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002826 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2827 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002828 Record.push_back(Context.isInt128Installed());
Richard Smith02e85f32011-04-14 22:09:26 +00002829 AddTypeRef(Context.AutoDeductTy, Record);
2830 AddTypeRef(Context.AutoRRefDeductTy, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +00002831 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002832
Douglas Gregor1970d882009-04-26 03:49:13 +00002833 // Keep writing types and declarations until all types and
2834 // declarations have been written.
Sebastian Redl539c5062010-08-18 23:57:32 +00002835 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002836 WriteDeclsBlockAbbrevs();
2837 while (!DeclTypesToEmit.empty()) {
2838 DeclOrType DOT = DeclTypesToEmit.front();
2839 DeclTypesToEmit.pop();
2840 if (DOT.isType())
2841 WriteType(DOT.getType());
2842 else
2843 WriteDecl(Context, DOT.getDecl());
2844 }
2845 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002846
Douglas Gregor45053152009-10-17 17:25:45 +00002847 WritePreprocessor(PP);
Douglas Gregor09b69892011-02-10 17:09:37 +00002848 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00002849 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002850 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002851 WriteIdentifierTable(PP);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002852 WriteFPPragmaOptions(SemaRef.getFPOptions());
2853 WriteOpenCLExtensions(SemaRef);
Douglas Gregor745ed142009-04-25 18:35:21 +00002854
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002855 WriteTypeDeclOffsets();
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002856 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00002857
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002858 WriteCXXBaseSpecifiersOffsets();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002859
Douglas Gregord4df8652009-04-22 22:02:47 +00002860 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002861 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002862 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002863
2864 // Write the record containing tentative definitions.
2865 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002866 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002867
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002868 // Write the record containing unused file scoped decls.
2869 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002870 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002871
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002872 // Write the record containing weak undeclared identifiers.
2873 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002874 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002875 WeakUndeclaredIdentifiers);
2876
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002877 // Write the record containing locally-scoped external definitions.
2878 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002879 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002880 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002881
2882 // Write the record containing ext_vector type names.
2883 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002884 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002885
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002886 // Write the record containing VTable uses information.
2887 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002888 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002889
2890 // Write the record containing dynamic classes declarations.
2891 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002892 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002893
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002894 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002895 if (!PendingInstantiations.empty())
2896 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002897
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002898 // Write the record containing declaration references of Sema.
2899 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002900 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002901
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002902 // Write the record containing CUDA-specific declaration references.
2903 if (!CUDASpecialDeclRefs.empty())
2904 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00002905
2906 // Write the delegating constructors.
2907 if (!DelegatingCtorDecls.empty())
2908 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002909
Douglas Gregor08f01292009-04-17 22:13:46 +00002910 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002911 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002912 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002913 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002914 Record.push_back(NumLexicalDeclContexts);
2915 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00002916 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002917 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002918}
2919
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002920void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002921 const char *isysroot) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002922 using namespace llvm;
2923
2924 ASTContext &Context = SemaRef.Context;
2925 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002926
Sebastian Redl143413f2010-07-12 22:02:52 +00002927 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002928 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002929 WriteMetadata(Context, isysroot, "");
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002930 if (StatCalls && !isysroot)
2931 WriteStatCache(*StatCalls);
2932 // FIXME: Source manager block should only write new stuff, which could be
2933 // done by tracking the largest ID in the chain
2934 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002935
2936 // The special types are in the chained PCH.
2937
2938 // We don't start with the translation unit, but with its decls that
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002939 // don't come from the chained PCH.
Sebastian Redl143413f2010-07-12 22:02:52 +00002940 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002941 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002942 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2943 E = TU->noload_decls_end();
Sebastian Redl143413f2010-07-12 22:02:52 +00002944 I != E; ++I) {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002945 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002946 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002947 else if ((*I)->isChangedSinceDeserialization())
2948 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl143413f2010-07-12 22:02:52 +00002949 }
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002950 // We also need to write a lexical updates block for the TU.
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002951 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002952 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002953 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2954 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2955 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002956 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002957 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002958 data(NewGlobalDecls));
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00002959 // And a visible updates block for the DeclContexts.
2960 Abv = new llvm::BitCodeAbbrev();
2961 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2962 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2963 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2964 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2965 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2966 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl143413f2010-07-12 22:02:52 +00002967
Sebastian Redl98912122010-07-27 23:01:28 +00002968 // Build a record containing all of the new tentative definitions in this
2969 // file, in TentativeDefinitions order.
2970 RecordData TentativeDefinitions;
2971 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2972 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2973 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2974 }
2975
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002976 // Build a record containing all of the file scoped decls in this file.
2977 RecordData UnusedFileScopedDecls;
2978 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2979 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2980 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002981 }
2982
Alexis Hunt27a761d2011-05-04 23:29:54 +00002983 // Build a record containing all of the delegating constructor decls in this
2984 // file.
2985 RecordData DelegatingCtorDecls;
2986 for (unsigned i=0, e = SemaRef.DelegatingCtorDecls.size(); i != e; ++i) {
2987 if (SemaRef.DelegatingCtorDecls[i]->getPCHLevel() == 0)
2988 AddDeclRef(SemaRef.DelegatingCtorDecls[i], DelegatingCtorDecls);
2989 }
2990
Sebastian Redl08aca90252010-08-05 18:21:25 +00002991 // We write the entire table, overwriting the tables from the chain.
2992 RecordData WeakUndeclaredIdentifiers;
2993 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2994 WeakUndeclaredIdentifiers.push_back(
2995 SemaRef.WeakUndeclaredIdentifiers.size());
2996 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2997 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2998 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2999 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3000 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3001 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3002 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3003 }
3004 }
3005
Sebastian Redl98912122010-07-27 23:01:28 +00003006 // Build a record containing all of the locally-scoped external
3007 // declarations in this header file. Generally, this record will be
3008 // empty.
3009 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003010 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl98912122010-07-27 23:01:28 +00003011 // nondeterminstic!
3012 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
3013 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3014 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
3015 TD != TDEnd; ++TD) {
3016 if (TD->second->getPCHLevel() == 0)
3017 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3018 }
3019
3020 // Build a record containing all of the ext_vector declarations.
3021 RecordData ExtVectorDecls;
3022 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
3023 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
3024 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
3025 }
3026
Sebastian Redl08aca90252010-08-05 18:21:25 +00003027 // Build a record containing all of the VTable uses information.
3028 // We write everything here, because it's too hard to determine whether
3029 // a use is new to this part.
3030 RecordData VTableUses;
3031 if (!SemaRef.VTableUses.empty()) {
3032 VTableUses.push_back(SemaRef.VTableUses.size());
3033 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3034 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3035 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3036 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3037 }
3038 }
3039
3040 // Build a record containing all of dynamic classes declarations.
3041 RecordData DynamicClasses;
3042 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
3043 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
3044 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
3045
3046 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003047 RecordData PendingInstantiations;
Sebastian Redl08aca90252010-08-05 18:21:25 +00003048 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00003049 I = SemaRef.PendingInstantiations.begin(),
3050 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl14afaf02011-04-24 16:27:30 +00003051 AddDeclRef(I->first, PendingInstantiations);
3052 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003053 }
3054 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3055 "There are local ones at end of translation unit!");
3056
3057 // Build a record containing some declaration references.
3058 // It's not worth the effort to avoid duplication here.
3059 RecordData SemaDeclRefs;
3060 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3061 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3062 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3063 }
3064
Sebastian Redl539c5062010-08-18 23:57:32 +00003065 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl143413f2010-07-12 22:02:52 +00003066 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003067 for (DeclsToRewriteTy::iterator
3068 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
3069 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl143413f2010-07-12 22:02:52 +00003070 while (!DeclTypesToEmit.empty()) {
3071 DeclOrType DOT = DeclTypesToEmit.front();
3072 DeclTypesToEmit.pop();
3073 if (DOT.isType())
3074 WriteType(DOT.getType());
3075 else
3076 WriteDecl(Context, DOT.getDecl());
3077 }
3078 Stream.ExitBlock();
3079
Sebastian Redl98912122010-07-27 23:01:28 +00003080 WritePreprocessor(PP);
Sebastian Redl51c79d82010-08-04 22:21:29 +00003081 WriteSelectors(SemaRef);
3082 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlff4a2952010-07-23 23:49:55 +00003083 WriteIdentifierTable(PP);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003084 WriteFPPragmaOptions(SemaRef.getFPOptions());
3085 WriteOpenCLExtensions(SemaRef);
3086
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003087 WriteTypeDeclOffsets();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003088 // FIXME: For chained PCH only write the new mappings (we currently
3089 // write all of them again).
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003090 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl98912122010-07-27 23:01:28 +00003091
Anders Carlsson9bb83e82011-03-06 18:41:18 +00003092 WriteCXXBaseSpecifiersOffsets();
3093
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003094 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003095 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003096 RecordData FirstLatestDeclIDs;
3097 for (FirstLatestDeclMap::iterator
3098 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
3099 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
3100 "Expected first & second to be in different PCHs");
3101 AddDeclRef(I->first, FirstLatestDeclIDs);
3102 AddDeclRef(I->second, FirstLatestDeclIDs);
3103 }
3104 if (!FirstLatestDeclIDs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003105 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003106
Sebastian Redl98912122010-07-27 23:01:28 +00003107 // Write the record containing external, unnamed definitions.
3108 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003109 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00003110
3111 // Write the record containing tentative definitions.
3112 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003113 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00003114
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003115 // Write the record containing unused file scoped decls.
3116 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003117 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003118
Sebastian Redl08aca90252010-08-05 18:21:25 +00003119 // Write the record containing weak undeclared identifiers.
3120 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003121 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl08aca90252010-08-05 18:21:25 +00003122 WeakUndeclaredIdentifiers);
3123
Sebastian Redl98912122010-07-27 23:01:28 +00003124 // Write the record containing locally-scoped external definitions.
3125 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003126 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl98912122010-07-27 23:01:28 +00003127 LocallyScopedExternalDecls);
3128
3129 // Write the record containing ext_vector type names.
3130 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003131 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003132
Sebastian Redl08aca90252010-08-05 18:21:25 +00003133 // Write the record containing VTable uses information.
3134 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003135 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003136
3137 // Write the record containing dynamic classes declarations.
3138 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003139 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003140
3141 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003142 if (!PendingInstantiations.empty())
3143 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003144
3145 // Write the record containing declaration references of Sema.
3146 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003147 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003148
3149 // Write the delegating constructors.
3150 if (!DelegatingCtorDecls.empty())
3151 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003152
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003153 // Write the updates to DeclContexts.
3154 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3155 I = UpdatedDeclContexts.begin(),
3156 E = UpdatedDeclContexts.end();
Sebastian Redla4071b42010-08-24 00:50:09 +00003157 I != E; ++I)
3158 WriteDeclContextVisibleUpdate(*I);
3159
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003160 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003161
Sebastian Redl98912122010-07-27 23:01:28 +00003162 Record.clear();
3163 Record.push_back(NumStatements);
3164 Record.push_back(NumMacros);
3165 Record.push_back(NumLexicalDeclContexts);
3166 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003167 WriteDeclReplacementsBlock();
Sebastian Redl539c5062010-08-18 23:57:32 +00003168 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl143413f2010-07-12 22:02:52 +00003169 Stream.ExitBlock();
3170}
3171
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003172void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003173 if (DeclUpdates.empty())
3174 return;
3175
3176 RecordData OffsetsRecord;
3177 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
3178 for (DeclUpdateMap::iterator
3179 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3180 const Decl *D = I->first;
3181 UpdateRecord &URec = I->second;
3182
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00003183 if (DeclsToRewrite.count(D))
3184 continue; // The decl will be written completely,no need to store updates.
3185
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003186 uint64_t Offset = Stream.GetCurrentBitNo();
3187 Stream.EmitRecord(DECL_UPDATES, URec);
3188
3189 OffsetsRecord.push_back(GetDeclRef(D));
3190 OffsetsRecord.push_back(Offset);
3191 }
3192 Stream.ExitBlock();
3193 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3194}
3195
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003196void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003197 if (ReplacedDecls.empty())
3198 return;
3199
3200 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003201 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003202 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
3203 Record.push_back(I->first);
3204 Record.push_back(I->second);
3205 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003206 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003207}
3208
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003209void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003210 Record.push_back(Loc.getRawEncoding());
3211}
3212
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003213void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003214 AddSourceLocation(Range.getBegin(), Record);
3215 AddSourceLocation(Range.getEnd(), Record);
3216}
3217
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003218void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003219 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00003220 const uint64_t *Words = Value.getRawData();
3221 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003222}
3223
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003224void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00003225 Record.push_back(Value.isUnsigned());
3226 AddAPInt(Value, Record);
3227}
3228
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003229void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00003230 AddAPInt(Value.bitcastToAPInt(), Record);
3231}
3232
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003233void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003234 Record.push_back(getIdentifierRef(II));
3235}
3236
Sebastian Redl539c5062010-08-18 23:57:32 +00003237IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003238 if (II == 0)
3239 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003240
Sebastian Redl539c5062010-08-18 23:57:32 +00003241 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003242 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00003243 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003244 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003245}
3246
Sebastian Redl50e26582010-09-15 19:54:06 +00003247MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregoraae92242010-03-19 21:51:54 +00003248 if (MD == 0)
3249 return 0;
Sebastian Redl50e26582010-09-15 19:54:06 +00003250
3251 MacroID &ID = MacroDefinitions[MD];
Douglas Gregoraae92242010-03-19 21:51:54 +00003252 if (ID == 0)
Douglas Gregor91096292010-10-02 19:29:26 +00003253 ID = NextMacroID++;
Douglas Gregoraae92242010-03-19 21:51:54 +00003254 return ID;
3255}
3256
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003257void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003258 Record.push_back(getSelectorRef(SelRef));
3259}
3260
Sebastian Redl539c5062010-08-18 23:57:32 +00003261SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003262 if (Sel.getAsOpaquePtr() == 0) {
3263 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00003264 }
3265
Sebastian Redl539c5062010-08-18 23:57:32 +00003266 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00003267 if (SID == 0 && Chain) {
3268 // This might trigger a ReadSelector callback, which will set the ID for
3269 // this selector.
3270 Chain->LoadSelector(Sel);
3271 }
Steve Naroff2ddea052009-04-23 10:39:46 +00003272 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003273 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00003274 }
Sebastian Redl834bb972010-08-04 17:20:04 +00003275 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00003276}
3277
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003278void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00003279 AddDeclRef(Temp->getDestructor(), Record);
3280}
3281
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003282void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3283 CXXBaseSpecifier const *BasesEnd,
3284 RecordDataImpl &Record) {
3285 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3286 CXXBaseSpecifiersToWrite.push_back(
3287 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3288 Bases, BasesEnd));
3289 Record.push_back(NextCXXBaseSpecifiersID++);
3290}
3291
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003292void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003293 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003294 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003295 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00003296 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003297 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00003298 break;
3299 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003300 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00003301 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003302 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00003303 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003304 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003305 break;
3306 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00003307 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003308 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003309 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003310 break;
John McCall0ad16662009-10-29 08:12:44 +00003311 case TemplateArgument::Null:
3312 case TemplateArgument::Integral:
3313 case TemplateArgument::Declaration:
3314 case TemplateArgument::Pack:
3315 break;
3316 }
3317}
3318
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003319void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003320 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003321 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003322
3323 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3324 bool InfoHasSameExpr
3325 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3326 Record.push_back(InfoHasSameExpr);
3327 if (InfoHasSameExpr)
3328 return; // Avoid storing the same expr twice.
3329 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003330 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3331 Record);
3332}
3333
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003334void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3335 RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00003336 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00003337 AddTypeRef(QualType(), Record);
3338 return;
3339 }
3340
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003341 AddTypeLoc(TInfo->getTypeLoc(), Record);
3342}
3343
3344void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3345 AddTypeRef(TL.getType(), Record);
3346
John McCall8f115c62009-10-16 21:56:05 +00003347 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003348 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003349 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00003350}
3351
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003352void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00003353 Record.push_back(GetOrCreateTypeID(T));
3354}
3355
3356TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003357 return MakeTypeID(T,
3358 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3359}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003360
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003361TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003362 return MakeTypeID(T,
3363 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003364}
3365
3366TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3367 if (T.isNull())
3368 return TypeIdx();
3369 assert(!T.getLocalFastQualifiers());
3370
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00003371 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003372 if (Idx.getIndex() == 0) {
Douglas Gregor1970d882009-04-26 03:49:13 +00003373 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00003374 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003375 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003376 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00003377 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003378 return Idx;
3379}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003380
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003381TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003382 if (T.isNull())
3383 return TypeIdx();
3384 assert(!T.getLocalFastQualifiers());
3385
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003386 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3387 assert(I != TypeIdxs.end() && "Type not emitted!");
3388 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003389}
3390
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003391void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003392 Record.push_back(GetDeclRef(D));
3393}
3394
Sebastian Redl539c5062010-08-18 23:57:32 +00003395DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003396 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003397 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003398 }
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003399 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00003400 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00003401 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003402 // We haven't seen this declaration before. Give it a new ID and
3403 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00003404 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003405 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003406 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3407 // We don't add it to the replacement collection here, because we don't
3408 // have the offset yet.
3409 DeclTypesToEmit.push(const_cast<Decl *>(D));
3410 // Reset the flag, so that we don't add this decl multiple times.
3411 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003412 }
3413
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003414 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003415}
3416
Sebastian Redl539c5062010-08-18 23:57:32 +00003417DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00003418 if (D == 0)
3419 return 0;
3420
3421 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3422 return DeclIDs[D];
3423}
3424
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003425void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00003426 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003427 Record.push_back(Name.getNameKind());
3428 switch (Name.getNameKind()) {
3429 case DeclarationName::Identifier:
3430 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3431 break;
3432
3433 case DeclarationName::ObjCZeroArgSelector:
3434 case DeclarationName::ObjCOneArgSelector:
3435 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00003436 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003437 break;
3438
3439 case DeclarationName::CXXConstructorName:
3440 case DeclarationName::CXXDestructorName:
3441 case DeclarationName::CXXConversionFunctionName:
3442 AddTypeRef(Name.getCXXNameType(), Record);
3443 break;
3444
3445 case DeclarationName::CXXOperatorName:
3446 Record.push_back(Name.getCXXOverloadedOperator());
3447 break;
3448
Alexis Hunt3d221f22009-11-29 07:34:05 +00003449 case DeclarationName::CXXLiteralOperatorName:
3450 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3451 break;
3452
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003453 case DeclarationName::CXXUsingDirective:
3454 // No extra data to emit
3455 break;
3456 }
3457}
Chris Lattnerca025db2010-05-07 21:43:38 +00003458
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003459void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003460 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003461 switch (Name.getNameKind()) {
3462 case DeclarationName::CXXConstructorName:
3463 case DeclarationName::CXXDestructorName:
3464 case DeclarationName::CXXConversionFunctionName:
3465 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3466 break;
3467
3468 case DeclarationName::CXXOperatorName:
3469 AddSourceLocation(
3470 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3471 Record);
3472 AddSourceLocation(
3473 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3474 Record);
3475 break;
3476
3477 case DeclarationName::CXXLiteralOperatorName:
3478 AddSourceLocation(
3479 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3480 Record);
3481 break;
3482
3483 case DeclarationName::Identifier:
3484 case DeclarationName::ObjCZeroArgSelector:
3485 case DeclarationName::ObjCOneArgSelector:
3486 case DeclarationName::ObjCMultiArgSelector:
3487 case DeclarationName::CXXUsingDirective:
3488 break;
3489 }
3490}
3491
3492void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003493 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003494 AddDeclarationName(NameInfo.getName(), Record);
3495 AddSourceLocation(NameInfo.getLoc(), Record);
3496 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3497}
3498
3499void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003500 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00003501 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003502 Record.push_back(Info.NumTemplParamLists);
3503 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3504 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3505}
3506
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003507void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003508 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003509 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00003510 // typically accommodate the vast majority.
Chris Lattnerca025db2010-05-07 21:43:38 +00003511 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3512
3513 // Push each of the NNS's onto a stack for serialization in reverse order.
3514 while (NNS) {
3515 NestedNames.push_back(NNS);
3516 NNS = NNS->getPrefix();
3517 }
3518
3519 Record.push_back(NestedNames.size());
3520 while(!NestedNames.empty()) {
3521 NNS = NestedNames.pop_back_val();
3522 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3523 Record.push_back(Kind);
3524 switch (Kind) {
3525 case NestedNameSpecifier::Identifier:
3526 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3527 break;
3528
3529 case NestedNameSpecifier::Namespace:
3530 AddDeclRef(NNS->getAsNamespace(), Record);
3531 break;
3532
Douglas Gregor7b26ff92011-02-24 02:36:08 +00003533 case NestedNameSpecifier::NamespaceAlias:
3534 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
3535 break;
3536
Chris Lattnerca025db2010-05-07 21:43:38 +00003537 case NestedNameSpecifier::TypeSpec:
3538 case NestedNameSpecifier::TypeSpecWithTemplate:
3539 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3540 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3541 break;
3542
3543 case NestedNameSpecifier::Global:
3544 // Don't need to write an associated value.
3545 break;
3546 }
3547 }
3548}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003549
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003550void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
3551 RecordDataImpl &Record) {
3552 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00003553 // typically accommodate the vast majority.
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003554 llvm::SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
3555
3556 // Push each of the nested-name-specifiers's onto a stack for
3557 // serialization in reverse order.
3558 while (NNS) {
3559 NestedNames.push_back(NNS);
3560 NNS = NNS.getPrefix();
3561 }
3562
3563 Record.push_back(NestedNames.size());
3564 while(!NestedNames.empty()) {
3565 NNS = NestedNames.pop_back_val();
3566 NestedNameSpecifier::SpecifierKind Kind
3567 = NNS.getNestedNameSpecifier()->getKind();
3568 Record.push_back(Kind);
3569 switch (Kind) {
3570 case NestedNameSpecifier::Identifier:
3571 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
3572 AddSourceRange(NNS.getLocalSourceRange(), Record);
3573 break;
3574
3575 case NestedNameSpecifier::Namespace:
3576 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
3577 AddSourceRange(NNS.getLocalSourceRange(), Record);
3578 break;
3579
3580 case NestedNameSpecifier::NamespaceAlias:
3581 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
3582 AddSourceRange(NNS.getLocalSourceRange(), Record);
3583 break;
3584
3585 case NestedNameSpecifier::TypeSpec:
3586 case NestedNameSpecifier::TypeSpecWithTemplate:
3587 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3588 AddTypeLoc(NNS.getTypeLoc(), Record);
3589 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3590 break;
3591
3592 case NestedNameSpecifier::Global:
3593 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3594 break;
3595 }
3596 }
3597}
3598
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003599void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003600 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003601 Record.push_back(Kind);
3602 switch (Kind) {
3603 case TemplateName::Template:
3604 AddDeclRef(Name.getAsTemplateDecl(), Record);
3605 break;
3606
3607 case TemplateName::OverloadedTemplate: {
3608 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3609 Record.push_back(OvT->size());
3610 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3611 I != E; ++I)
3612 AddDeclRef(*I, Record);
3613 break;
3614 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003615
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003616 case TemplateName::QualifiedTemplate: {
3617 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3618 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3619 Record.push_back(QualT->hasTemplateKeyword());
3620 AddDeclRef(QualT->getTemplateDecl(), Record);
3621 break;
3622 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003623
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003624 case TemplateName::DependentTemplate: {
3625 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3626 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3627 Record.push_back(DepT->isIdentifier());
3628 if (DepT->isIdentifier())
3629 AddIdentifierRef(DepT->getIdentifier(), Record);
3630 else
3631 Record.push_back(DepT->getOperator());
3632 break;
3633 }
Douglas Gregor5590be02011-01-15 06:45:20 +00003634
3635 case TemplateName::SubstTemplateTemplateParmPack: {
3636 SubstTemplateTemplateParmPackStorage *SubstPack
3637 = Name.getAsSubstTemplateTemplateParmPack();
3638 AddDeclRef(SubstPack->getParameterPack(), Record);
3639 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
3640 break;
3641 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003642 }
3643}
3644
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003645void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003646 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003647 Record.push_back(Arg.getKind());
3648 switch (Arg.getKind()) {
3649 case TemplateArgument::Null:
3650 break;
3651 case TemplateArgument::Type:
3652 AddTypeRef(Arg.getAsType(), Record);
3653 break;
3654 case TemplateArgument::Declaration:
3655 AddDeclRef(Arg.getAsDecl(), Record);
3656 break;
3657 case TemplateArgument::Integral:
3658 AddAPSInt(*Arg.getAsIntegral(), Record);
3659 AddTypeRef(Arg.getIntegralType(), Record);
3660 break;
3661 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00003662 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3663 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003664 case TemplateArgument::TemplateExpansion:
3665 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregore1d60df2011-01-14 23:41:42 +00003666 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3667 Record.push_back(*NumExpansions + 1);
3668 else
3669 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003670 break;
3671 case TemplateArgument::Expression:
3672 AddStmt(Arg.getAsExpr());
3673 break;
3674 case TemplateArgument::Pack:
3675 Record.push_back(Arg.pack_size());
3676 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3677 I != E; ++I)
3678 AddTemplateArgument(*I, Record);
3679 break;
3680 }
3681}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003682
3683void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003684ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003685 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003686 assert(TemplateParams && "No TemplateParams!");
3687 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3688 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3689 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3690 Record.push_back(TemplateParams->size());
3691 for (TemplateParameterList::const_iterator
3692 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3693 P != PEnd; ++P)
3694 AddDeclRef(*P, Record);
3695}
3696
3697/// \brief Emit a template argument list.
3698void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003699ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003700 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003701 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003702 Record.push_back(TemplateArgs->size());
3703 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003704 AddTemplateArgument(TemplateArgs->get(i), Record);
3705}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003706
3707
3708void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003709ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003710 Record.push_back(Set.size());
3711 for (UnresolvedSetImpl::const_iterator
3712 I = Set.begin(), E = Set.end(); I != E; ++I) {
3713 AddDeclRef(I.getDecl(), Record);
3714 Record.push_back(I.getAccess());
3715 }
3716}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003717
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003718void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003719 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003720 Record.push_back(Base.isVirtual());
3721 Record.push_back(Base.isBaseOfClass());
3722 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00003723 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00003724 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003725 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00003726 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3727 : SourceLocation(),
3728 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003729}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003730
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003731void ASTWriter::FlushCXXBaseSpecifiers() {
3732 RecordData Record;
3733 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3734 Record.clear();
3735
3736 // Record the offset of this base-specifier set.
3737 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3738 if (Index == CXXBaseSpecifiersOffsets.size())
3739 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3740 else {
3741 if (Index > CXXBaseSpecifiersOffsets.size())
3742 CXXBaseSpecifiersOffsets.resize(Index + 1);
3743 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3744 }
3745
3746 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3747 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3748 Record.push_back(BEnd - B);
3749 for (; B != BEnd; ++B)
3750 AddCXXBaseSpecifier(*B, Record);
3751 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00003752
3753 // Flush any expressions that were written as part of the base specifiers.
3754 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003755 }
3756
3757 CXXBaseSpecifiersToWrite.clear();
3758}
3759
Alexis Hunt1d792652011-01-08 20:30:50 +00003760void ASTWriter::AddCXXCtorInitializers(
3761 const CXXCtorInitializer * const *CtorInitializers,
3762 unsigned NumCtorInitializers,
3763 RecordDataImpl &Record) {
3764 Record.push_back(NumCtorInitializers);
3765 for (unsigned i=0; i != NumCtorInitializers; ++i) {
3766 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003767
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003768 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00003769 Record.push_back(CTOR_INITIALIZER_BASE);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003770 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3771 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00003772 } else if (Init->isDelegatingInitializer()) {
3773 Record.push_back(CTOR_INITIALIZER_DELEGATING);
3774 AddDeclRef(Init->getTargetConstructor(), Record);
3775 } else if (Init->isMemberInitializer()){
3776 Record.push_back(CTOR_INITIALIZER_MEMBER);
3777 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003778 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00003779 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
3780 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003781 }
Francois Pichetd583da02010-12-04 09:14:42 +00003782
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003783 AddSourceLocation(Init->getMemberLocation(), Record);
3784 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003785 AddSourceLocation(Init->getLParenLoc(), Record);
3786 AddSourceLocation(Init->getRParenLoc(), Record);
3787 Record.push_back(Init->isWritten());
3788 if (Init->isWritten()) {
3789 Record.push_back(Init->getSourceOrder());
3790 } else {
3791 Record.push_back(Init->getNumArrayIndices());
3792 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3793 AddDeclRef(Init->getArrayIndex(i), Record);
3794 }
3795 }
3796}
3797
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003798void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3799 assert(D->DefinitionData);
3800 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3801 Record.push_back(Data.UserDeclaredConstructor);
3802 Record.push_back(Data.UserDeclaredCopyConstructor);
3803 Record.push_back(Data.UserDeclaredCopyAssignment);
3804 Record.push_back(Data.UserDeclaredDestructor);
3805 Record.push_back(Data.Aggregate);
3806 Record.push_back(Data.PlainOldData);
3807 Record.push_back(Data.Empty);
3808 Record.push_back(Data.Polymorphic);
3809 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00003810 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00003811 Record.push_back(Data.HasNoNonEmptyBases);
3812 Record.push_back(Data.HasPrivateFields);
3813 Record.push_back(Data.HasProtectedFields);
3814 Record.push_back(Data.HasPublicFields);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003815 Record.push_back(Data.HasTrivialConstructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00003816 Record.push_back(Data.HasConstExprNonCopyMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003817 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruthad7d4042011-04-23 23:10:33 +00003818 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003819 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruthad7d4042011-04-23 23:10:33 +00003820 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003821 Record.push_back(Data.HasTrivialDestructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00003822 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003823 Record.push_back(Data.ComputedVisibleConversions);
3824 Record.push_back(Data.DeclaredDefaultConstructor);
3825 Record.push_back(Data.DeclaredCopyConstructor);
3826 Record.push_back(Data.DeclaredCopyAssignment);
3827 Record.push_back(Data.DeclaredDestructor);
3828
3829 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003830 if (Data.NumBases > 0)
3831 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3832 Record);
3833
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003834 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3835 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003836 if (Data.NumVBases > 0)
3837 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3838 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003839
3840 AddUnresolvedSet(Data.Conversions, Record);
3841 AddUnresolvedSet(Data.VisibleConversions, Record);
3842 // Data.Definition is the owning decl, no need to write it.
3843 AddDeclRef(Data.FirstFriend, Record);
3844}
3845
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003846void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00003847 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003848 assert(!Chain && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00003849 assert(FirstDeclID == NextDeclID &&
3850 FirstTypeID == NextTypeID &&
3851 FirstIdentID == NextIdentID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00003852 FirstSelectorID == NextSelectorID &&
Douglas Gregor91096292010-10-02 19:29:26 +00003853 FirstMacroID == NextMacroID &&
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003854 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00003855 "Setting chain after writing has started.");
3856 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003857
3858 FirstDeclID += Chain->getTotalNumDecls();
3859 FirstTypeID += Chain->getTotalNumTypes();
3860 FirstIdentID += Chain->getTotalNumIdentifiers();
3861 FirstSelectorID += Chain->getTotalNumSelectors();
3862 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003863 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003864 NextDeclID = FirstDeclID;
3865 NextTypeID = FirstTypeID;
3866 NextIdentID = FirstIdentID;
3867 NextSelectorID = FirstSelectorID;
3868 NextMacroID = FirstMacroID;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003869 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00003870}
3871
Sebastian Redl539c5062010-08-18 23:57:32 +00003872void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00003873 IdentifierIDs[II] = ID;
Douglas Gregor68051a72011-02-11 00:26:14 +00003874 if (II->hasMacroDefinition())
3875 DeserializedMacroNames.push_back(II);
Sebastian Redlff4a2952010-07-23 23:49:55 +00003876}
3877
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003878void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003879 // Always take the highest-numbered type index. This copes with an interesting
3880 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003881 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003882 // keep the higher-numbered entry so that we can properly write it out to
3883 // the AST file.
3884 TypeIdx &StoredIdx = TypeIdxs[T];
3885 if (Idx.getIndex() >= StoredIdx.getIndex())
3886 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003887}
3888
Sebastian Redl539c5062010-08-18 23:57:32 +00003889void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003890 DeclIDs[D] = ID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003891}
Sebastian Redl834bb972010-08-04 17:20:04 +00003892
Sebastian Redl539c5062010-08-18 23:57:32 +00003893void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003894 SelectorIDs[S] = ID;
3895}
Douglas Gregor91096292010-10-02 19:29:26 +00003896
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003897void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00003898 MacroDefinition *MD) {
3899 MacroDefinitions[MD] = ID;
3900}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003901
3902void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3903 assert(D->isDefinition());
3904 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3905 // We are interested when a PCH decl is modified.
3906 if (RD->getPCHLevel() > 0) {
3907 // A forward reference was mutated into a definition. Rewrite it.
3908 // FIXME: This happens during template instantiation, should we
3909 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003910 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003911 }
3912
3913 for (CXXRecordDecl::redecl_iterator
3914 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3915 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3916 if (Redecl == RD)
3917 continue;
3918
3919 // We are interested when a PCH decl is modified.
3920 if (Redecl->getPCHLevel() > 0) {
3921 UpdateRecord &Record = DeclUpdates[Redecl];
3922 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3923 assert(Redecl->DefinitionData);
3924 assert(Redecl->DefinitionData->Definition == D);
3925 AddDeclRef(D, Record); // the DefinitionDecl
3926 }
3927 }
3928 }
3929}
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003930void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3931 // TU and namespaces are handled elsewhere.
3932 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3933 return;
3934
3935 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3936 return; // Not a source decl added to a DeclContext from PCH.
3937
3938 AddUpdatedDeclContext(DC);
3939}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003940
3941void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3942 assert(D->isImplicit());
3943 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3944 return; // Not a source member added to a class from PCH.
3945 if (!isa<CXXMethodDecl>(D))
3946 return; // We are interested in lazily declared implicit methods.
3947
3948 // A decl coming from PCH was modified.
3949 assert(RD->isDefinition());
3950 UpdateRecord &Record = DeclUpdates[RD];
3951 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3952 AddDeclRef(D, Record);
3953}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003954
3955void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3956 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00003957 // The specializations set is kept in the canonical template.
3958 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003959 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3960 return; // Not a source specialization added to a template from PCH.
3961
3962 UpdateRecord &Record = DeclUpdates[TD];
3963 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3964 AddDeclRef(D, Record);
3965}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003966
Sebastian Redl9ab988f2011-04-14 14:07:59 +00003967void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
3968 const FunctionDecl *D) {
3969 // The specializations set is kept in the canonical template.
3970 TD = TD->getCanonicalDecl();
3971 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3972 return; // Not a source specialization added to a template from PCH.
3973
3974 UpdateRecord &Record = DeclUpdates[TD];
3975 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3976 AddDeclRef(D, Record);
3977}
3978
Sebastian Redlab238a72011-04-24 16:28:06 +00003979void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
3980 if (D->getPCHLevel() == 0)
3981 return; // Declaration not imported from PCH.
3982
3983 // Implicit decl from a PCH was defined.
3984 // FIXME: Should implicit definition be a separate FunctionDecl?
3985 RewriteDecl(D);
3986}
3987
Sebastian Redl2ac2c722011-04-29 08:19:30 +00003988void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
3989 if (D->getPCHLevel() == 0)
3990 return;
3991
3992 // Since the actual instantiation is delayed, this really means that we need
3993 // to update the instantiation location.
3994 UpdateRecord &Record = DeclUpdates[D];
3995 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
3996 AddSourceLocation(
3997 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
3998}
3999
Douglas Gregorf88e35b2010-11-30 06:16:57 +00004000ASTSerializationListener::~ASTSerializationListener() { }