blob: 1c62bfd72abcadbc1b84c26e9f244d6951260bec [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);
Richard Smith3f1b5d02011-05-05 21:57:07 +0000280 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
281 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000282 : T->getCanonicalTypeInternal(),
283 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000284 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000285}
286
287void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000288ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000289 VisitArrayType(T);
290 Writer.AddStmt(T->getSizeExpr());
291 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000292 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000293}
294
295void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000296ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000297 const DependentSizedExtVectorType *T) {
298 // FIXME: Serialize this type (C++ only)
299 assert(false && "Cannot serialize dependent sized extended vector types");
300}
301
302void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000303ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000304 Record.push_back(T->getDepth());
305 Record.push_back(T->getIndex());
306 Record.push_back(T->isParameterPack());
Chandler Carruth08836322011-05-01 00:51:33 +0000307 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000308 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000309}
310
311void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000312ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000313 Record.push_back(T->getKeyword());
314 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
315 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000316 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
317 : T->getCanonicalTypeInternal(),
318 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000319 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000320}
321
322void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000323ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000324 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000325 Record.push_back(T->getKeyword());
326 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
327 Writer.AddIdentifierRef(T->getIdentifier(), Record);
328 Record.push_back(T->getNumArgs());
329 for (DependentTemplateSpecializationType::iterator
330 I = T->begin(), E = T->end(); I != E; ++I)
331 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000332 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000333}
334
Douglas Gregord2fa7662010-12-20 02:24:11 +0000335void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
336 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregor0dca5fd2011-01-14 17:04:44 +0000337 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
338 Record.push_back(*NumExpansions + 1);
339 else
340 Record.push_back(0);
Douglas Gregord2fa7662010-12-20 02:24:11 +0000341 Code = TYPE_PACK_EXPANSION;
342}
343
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000344void ASTTypeWriter::VisitParenType(const ParenType *T) {
345 Writer.AddTypeRef(T->getInnerType(), Record);
346 Code = TYPE_PAREN;
347}
348
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000349void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000350 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000351 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
352 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000353 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000354}
355
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000356void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCalle78aac42010-03-10 03:28:59 +0000357 Writer.AddDeclRef(T->getDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000358 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000359 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000360}
361
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000362void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1c283312010-08-11 12:19:30 +0000363 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000364 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000365}
366
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000367void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000368 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000369 Record.push_back(T->getNumProtocols());
John McCall8b07ec22010-05-15 11:32:37 +0000370 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000371 E = T->qual_end(); I != E; ++I)
372 Writer.AddDeclRef(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000373 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000374}
375
Steve Narofffb4330f2009-06-17 22:40:22 +0000376void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000377ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000378 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000379 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000380}
381
John McCall8f115c62009-10-16 21:56:05 +0000382namespace {
383
384class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000385 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000386 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000387
388public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000389 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000390 : Writer(Writer), Record(Record) { }
391
John McCall17001972009-10-18 01:05:36 +0000392#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000393#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000394 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000395#include "clang/AST/TypeLocNodes.def"
396
John McCall17001972009-10-18 01:05:36 +0000397 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
398 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000399};
400
401}
402
John McCall17001972009-10-18 01:05:36 +0000403void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
404 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000405}
John McCall17001972009-10-18 01:05:36 +0000406void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000407 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
408 if (TL.needsExtraLocalData()) {
409 Record.push_back(TL.getWrittenTypeSpec());
410 Record.push_back(TL.getWrittenSignSpec());
411 Record.push_back(TL.getWrittenWidthSpec());
412 Record.push_back(TL.hasModeAttr());
413 }
John McCall8f115c62009-10-16 21:56:05 +0000414}
John McCall17001972009-10-18 01:05:36 +0000415void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
416 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000417}
John McCall17001972009-10-18 01:05:36 +0000418void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
419 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000420}
John McCall17001972009-10-18 01:05:36 +0000421void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
422 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000423}
John McCall17001972009-10-18 01:05:36 +0000424void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
425 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000426}
John McCall17001972009-10-18 01:05:36 +0000427void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
428 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000429}
John McCall17001972009-10-18 01:05:36 +0000430void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
431 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnara509357842011-03-05 14:42:21 +0000432 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000433}
John McCall17001972009-10-18 01:05:36 +0000434void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
435 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
436 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
437 Record.push_back(TL.getSizeExpr() ? 1 : 0);
438 if (TL.getSizeExpr())
439 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000440}
John McCall17001972009-10-18 01:05:36 +0000441void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
442 VisitArrayTypeLoc(TL);
443}
444void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
445 VisitArrayTypeLoc(TL);
446}
447void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
448 VisitArrayTypeLoc(TL);
449}
450void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
451 DependentSizedArrayTypeLoc TL) {
452 VisitArrayTypeLoc(TL);
453}
454void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
455 DependentSizedExtVectorTypeLoc TL) {
456 Writer.AddSourceLocation(TL.getNameLoc(), Record);
457}
458void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
459 Writer.AddSourceLocation(TL.getNameLoc(), Record);
460}
461void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
462 Writer.AddSourceLocation(TL.getNameLoc(), Record);
463}
464void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +0000465 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
466 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Douglas Gregor7fb25412010-10-01 18:44:50 +0000467 Record.push_back(TL.getTrailingReturn());
John McCall17001972009-10-18 01:05:36 +0000468 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
469 Writer.AddDeclRef(TL.getArg(i), Record);
470}
471void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
472 VisitFunctionTypeLoc(TL);
473}
474void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
475 VisitFunctionTypeLoc(TL);
476}
John McCallb96ec562009-12-04 22:46:56 +0000477void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
478 Writer.AddSourceLocation(TL.getNameLoc(), Record);
479}
John McCall17001972009-10-18 01:05:36 +0000480void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
481 Writer.AddSourceLocation(TL.getNameLoc(), Record);
482}
483void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000484 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
485 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
486 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000487}
488void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000489 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
490 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
491 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
492 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000493}
494void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
495 Writer.AddSourceLocation(TL.getNameLoc(), Record);
496}
Richard Smith30482bc2011-02-20 03:19:35 +0000497void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
498 Writer.AddSourceLocation(TL.getNameLoc(), Record);
499}
John McCall17001972009-10-18 01:05:36 +0000500void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
501 Writer.AddSourceLocation(TL.getNameLoc(), Record);
502}
503void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
504 Writer.AddSourceLocation(TL.getNameLoc(), Record);
505}
John McCall81904512011-01-06 01:58:22 +0000506void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
507 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
508 if (TL.hasAttrOperand()) {
509 SourceRange range = TL.getAttrOperandParensRange();
510 Writer.AddSourceLocation(range.getBegin(), Record);
511 Writer.AddSourceLocation(range.getEnd(), Record);
512 }
513 if (TL.hasAttrExprOperand()) {
514 Expr *operand = TL.getAttrExprOperand();
515 Record.push_back(operand ? 1 : 0);
516 if (operand) Writer.AddStmt(operand);
517 } else if (TL.hasAttrEnumOperand()) {
518 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
519 }
520}
John McCall17001972009-10-18 01:05:36 +0000521void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
522 Writer.AddSourceLocation(TL.getNameLoc(), Record);
523}
John McCallcebee162009-10-18 09:09:24 +0000524void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
525 SubstTemplateTypeParmTypeLoc TL) {
526 Writer.AddSourceLocation(TL.getNameLoc(), Record);
527}
Douglas Gregorada4b792011-01-14 02:55:32 +0000528void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
529 SubstTemplateTypeParmPackTypeLoc TL) {
530 Writer.AddSourceLocation(TL.getNameLoc(), Record);
531}
John McCall17001972009-10-18 01:05:36 +0000532void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
533 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +0000534 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
535 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
536 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
537 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000538 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
539 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000540}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000541void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
542 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
543 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
544}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000545void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000546 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor844cb502011-03-01 18:12:44 +0000547 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000548}
John McCalle78aac42010-03-10 03:28:59 +0000549void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
550 Writer.AddSourceLocation(TL.getNameLoc(), Record);
551}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000552void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000553 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor3d0da5f2011-03-01 01:34:45 +0000554 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000555 Writer.AddSourceLocation(TL.getNameLoc(), Record);
556}
John McCallc392f372010-06-11 00:33:02 +0000557void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
558 DependentTemplateSpecializationTypeLoc TL) {
559 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregora7a795b2011-03-01 20:11:18 +0000560 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCallc392f372010-06-11 00:33:02 +0000561 Writer.AddSourceLocation(TL.getNameLoc(), Record);
562 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
563 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
564 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000565 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
566 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000567}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000568void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
569 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
570}
John McCall17001972009-10-18 01:05:36 +0000571void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
572 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000573}
574void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
575 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000576 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
577 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
578 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
579 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000580}
John McCallfc93cf92009-10-22 22:37:11 +0000581void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
582 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000583}
John McCall8f115c62009-10-16 21:56:05 +0000584
Chris Lattner19cea4e2009-04-22 05:57:30 +0000585//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000586// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000587//===----------------------------------------------------------------------===//
588
Chris Lattner28fa4e62009-04-26 22:26:21 +0000589static void EmitBlockID(unsigned ID, const char *Name,
590 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000591 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000592 Record.clear();
593 Record.push_back(ID);
594 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
595
596 // Emit the block name if present.
597 if (Name == 0 || Name[0] == 0) return;
598 Record.clear();
599 while (*Name)
600 Record.push_back(*Name++);
601 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
602}
603
604static void EmitRecordID(unsigned ID, const char *Name,
605 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000606 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000607 Record.clear();
608 Record.push_back(ID);
609 while (*Name)
610 Record.push_back(*Name++);
611 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000612}
613
614static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000615 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000616#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000617 RECORD(STMT_STOP);
618 RECORD(STMT_NULL_PTR);
619 RECORD(STMT_NULL);
620 RECORD(STMT_COMPOUND);
621 RECORD(STMT_CASE);
622 RECORD(STMT_DEFAULT);
623 RECORD(STMT_LABEL);
624 RECORD(STMT_IF);
625 RECORD(STMT_SWITCH);
626 RECORD(STMT_WHILE);
627 RECORD(STMT_DO);
628 RECORD(STMT_FOR);
629 RECORD(STMT_GOTO);
630 RECORD(STMT_INDIRECT_GOTO);
631 RECORD(STMT_CONTINUE);
632 RECORD(STMT_BREAK);
633 RECORD(STMT_RETURN);
634 RECORD(STMT_DECL);
635 RECORD(STMT_ASM);
636 RECORD(EXPR_PREDEFINED);
637 RECORD(EXPR_DECL_REF);
638 RECORD(EXPR_INTEGER_LITERAL);
639 RECORD(EXPR_FLOATING_LITERAL);
640 RECORD(EXPR_IMAGINARY_LITERAL);
641 RECORD(EXPR_STRING_LITERAL);
642 RECORD(EXPR_CHARACTER_LITERAL);
643 RECORD(EXPR_PAREN);
644 RECORD(EXPR_UNARY_OPERATOR);
645 RECORD(EXPR_SIZEOF_ALIGN_OF);
646 RECORD(EXPR_ARRAY_SUBSCRIPT);
647 RECORD(EXPR_CALL);
648 RECORD(EXPR_MEMBER);
649 RECORD(EXPR_BINARY_OPERATOR);
650 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
651 RECORD(EXPR_CONDITIONAL_OPERATOR);
652 RECORD(EXPR_IMPLICIT_CAST);
653 RECORD(EXPR_CSTYLE_CAST);
654 RECORD(EXPR_COMPOUND_LITERAL);
655 RECORD(EXPR_EXT_VECTOR_ELEMENT);
656 RECORD(EXPR_INIT_LIST);
657 RECORD(EXPR_DESIGNATED_INIT);
658 RECORD(EXPR_IMPLICIT_VALUE_INIT);
659 RECORD(EXPR_VA_ARG);
660 RECORD(EXPR_ADDR_LABEL);
661 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000662 RECORD(EXPR_CHOOSE);
663 RECORD(EXPR_GNU_NULL);
664 RECORD(EXPR_SHUFFLE_VECTOR);
665 RECORD(EXPR_BLOCK);
666 RECORD(EXPR_BLOCK_DECL_REF);
Peter Collingbourne91147592011-04-15 00:35:48 +0000667 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000668 RECORD(EXPR_OBJC_STRING_LITERAL);
669 RECORD(EXPR_OBJC_ENCODE);
670 RECORD(EXPR_OBJC_SELECTOR_EXPR);
671 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
672 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
673 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
674 RECORD(EXPR_OBJC_KVC_REF_EXPR);
675 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000676 RECORD(STMT_OBJC_FOR_COLLECTION);
677 RECORD(STMT_OBJC_CATCH);
678 RECORD(STMT_OBJC_FINALLY);
679 RECORD(STMT_OBJC_AT_TRY);
680 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
681 RECORD(STMT_OBJC_AT_THROW);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000682 RECORD(EXPR_CXX_OPERATOR_CALL);
683 RECORD(EXPR_CXX_CONSTRUCT);
684 RECORD(EXPR_CXX_STATIC_CAST);
685 RECORD(EXPR_CXX_DYNAMIC_CAST);
686 RECORD(EXPR_CXX_REINTERPRET_CAST);
687 RECORD(EXPR_CXX_CONST_CAST);
688 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
689 RECORD(EXPR_CXX_BOOL_LITERAL);
690 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000691 RECORD(EXPR_CXX_TYPEID_EXPR);
692 RECORD(EXPR_CXX_TYPEID_TYPE);
693 RECORD(EXPR_CXX_UUIDOF_EXPR);
694 RECORD(EXPR_CXX_UUIDOF_TYPE);
695 RECORD(EXPR_CXX_THIS);
696 RECORD(EXPR_CXX_THROW);
697 RECORD(EXPR_CXX_DEFAULT_ARG);
698 RECORD(EXPR_CXX_BIND_TEMPORARY);
699 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
700 RECORD(EXPR_CXX_NEW);
701 RECORD(EXPR_CXX_DELETE);
702 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
703 RECORD(EXPR_EXPR_WITH_CLEANUPS);
704 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
705 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
706 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
707 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
708 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
709 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
710 RECORD(EXPR_CXX_NOEXCEPT);
711 RECORD(EXPR_OPAQUE_VALUE);
712 RECORD(EXPR_BINARY_TYPE_TRAIT);
713 RECORD(EXPR_PACK_EXPANSION);
714 RECORD(EXPR_SIZEOF_PACK);
715 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbourne41f85462011-02-09 21:07:24 +0000716 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000717#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000718}
Mike Stump11289f42009-09-09 15:08:12 +0000719
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000720void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000721 RecordData Record;
722 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000723
Sebastian Redl539c5062010-08-18 23:57:32 +0000724#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
725#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000726
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000727 // AST Top-Level Block.
Sebastian Redlf1642042010-08-18 23:57:22 +0000728 BLOCK(AST_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000729 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000730 RECORD(TYPE_OFFSET);
731 RECORD(DECL_OFFSET);
732 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000733 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000734 RECORD(IDENTIFIER_OFFSET);
735 RECORD(IDENTIFIER_TABLE);
736 RECORD(EXTERNAL_DEFINITIONS);
737 RECORD(SPECIAL_TYPES);
738 RECORD(STATISTICS);
739 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000740 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000741 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
742 RECORD(SELECTOR_OFFSETS);
743 RECORD(METHOD_POOL);
744 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000745 RECORD(SOURCE_LOCATION_OFFSETS);
746 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000747 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000748 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek17437132010-01-22 20:59:36 +0000749 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregoraae92242010-03-19 21:51:54 +0000750 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redl595c5132010-07-08 22:01:51 +0000751 RECORD(CHAINED_METADATA);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000752 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000753 RECORD(TU_UPDATE_LEXICAL);
754 RECORD(REDECLS_UPDATE_LATEST);
755 RECORD(SEMA_DECL_REFS);
756 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
757 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
758 RECORD(DECL_REPLACEMENTS);
759 RECORD(UPDATE_VISIBLE);
760 RECORD(DECL_UPDATE_OFFSETS);
761 RECORD(DECL_UPDATES);
762 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
763 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000764 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000765 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne5df20e02011-02-15 19:46:30 +0000766 RECORD(FP_PRAGMA_OPTIONS);
767 RECORD(OPENCL_EXTENSIONS);
Alexis Hunt27a761d2011-05-04 23:29:54 +0000768 RECORD(DELEGATING_CTORS);
Douglas Gregor09b69892011-02-10 17:09:37 +0000769
Chris Lattner28fa4e62009-04-26 22:26:21 +0000770 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000771 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000772 RECORD(SM_SLOC_FILE_ENTRY);
773 RECORD(SM_SLOC_BUFFER_ENTRY);
774 RECORD(SM_SLOC_BUFFER_BLOB);
775 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
776 RECORD(SM_LINE_TABLE);
Mike Stump11289f42009-09-09 15:08:12 +0000777
Chris Lattner28fa4e62009-04-26 22:26:21 +0000778 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000779 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000780 RECORD(PP_MACRO_OBJECT_LIKE);
781 RECORD(PP_MACRO_FUNCTION_LIKE);
782 RECORD(PP_TOKEN);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000783
Douglas Gregor12bfa382009-10-17 00:13:19 +0000784 // Decls and Types block.
785 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000786 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000787 RECORD(TYPE_COMPLEX);
788 RECORD(TYPE_POINTER);
789 RECORD(TYPE_BLOCK_POINTER);
790 RECORD(TYPE_LVALUE_REFERENCE);
791 RECORD(TYPE_RVALUE_REFERENCE);
792 RECORD(TYPE_MEMBER_POINTER);
793 RECORD(TYPE_CONSTANT_ARRAY);
794 RECORD(TYPE_INCOMPLETE_ARRAY);
795 RECORD(TYPE_VARIABLE_ARRAY);
796 RECORD(TYPE_VECTOR);
797 RECORD(TYPE_EXT_VECTOR);
798 RECORD(TYPE_FUNCTION_PROTO);
799 RECORD(TYPE_FUNCTION_NO_PROTO);
800 RECORD(TYPE_TYPEDEF);
801 RECORD(TYPE_TYPEOF_EXPR);
802 RECORD(TYPE_TYPEOF);
803 RECORD(TYPE_RECORD);
804 RECORD(TYPE_ENUM);
805 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000806 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000807 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000808 RECORD(TYPE_DECLTYPE);
809 RECORD(TYPE_ELABORATED);
810 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
811 RECORD(TYPE_UNRESOLVED_USING);
812 RECORD(TYPE_INJECTED_CLASS_NAME);
813 RECORD(TYPE_OBJC_OBJECT);
814 RECORD(TYPE_TEMPLATE_TYPE_PARM);
815 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
816 RECORD(TYPE_DEPENDENT_NAME);
817 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
818 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
819 RECORD(TYPE_PAREN);
820 RECORD(TYPE_PACK_EXPANSION);
821 RECORD(TYPE_ATTRIBUTED);
822 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000823 RECORD(DECL_TRANSLATION_UNIT);
824 RECORD(DECL_TYPEDEF);
825 RECORD(DECL_ENUM);
826 RECORD(DECL_RECORD);
827 RECORD(DECL_ENUM_CONSTANT);
828 RECORD(DECL_FUNCTION);
829 RECORD(DECL_OBJC_METHOD);
830 RECORD(DECL_OBJC_INTERFACE);
831 RECORD(DECL_OBJC_PROTOCOL);
832 RECORD(DECL_OBJC_IVAR);
833 RECORD(DECL_OBJC_AT_DEFS_FIELD);
834 RECORD(DECL_OBJC_CLASS);
835 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
836 RECORD(DECL_OBJC_CATEGORY);
837 RECORD(DECL_OBJC_CATEGORY_IMPL);
838 RECORD(DECL_OBJC_IMPLEMENTATION);
839 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
840 RECORD(DECL_OBJC_PROPERTY);
841 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000842 RECORD(DECL_FIELD);
843 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000844 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000845 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000846 RECORD(DECL_FILE_SCOPE_ASM);
847 RECORD(DECL_BLOCK);
848 RECORD(DECL_CONTEXT_LEXICAL);
849 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor0beaec02011-02-08 16:34:17 +0000850 RECORD(DECL_NAMESPACE);
851 RECORD(DECL_NAMESPACE_ALIAS);
852 RECORD(DECL_USING);
853 RECORD(DECL_USING_SHADOW);
854 RECORD(DECL_USING_DIRECTIVE);
855 RECORD(DECL_UNRESOLVED_USING_VALUE);
856 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
857 RECORD(DECL_LINKAGE_SPEC);
858 RECORD(DECL_CXX_RECORD);
859 RECORD(DECL_CXX_METHOD);
860 RECORD(DECL_CXX_CONSTRUCTOR);
861 RECORD(DECL_CXX_DESTRUCTOR);
862 RECORD(DECL_CXX_CONVERSION);
863 RECORD(DECL_ACCESS_SPEC);
864 RECORD(DECL_FRIEND);
865 RECORD(DECL_FRIEND_TEMPLATE);
866 RECORD(DECL_CLASS_TEMPLATE);
867 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
868 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
869 RECORD(DECL_FUNCTION_TEMPLATE);
870 RECORD(DECL_TEMPLATE_TYPE_PARM);
871 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
872 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
873 RECORD(DECL_STATIC_ASSERT);
874 RECORD(DECL_CXX_BASE_SPECIFIERS);
875 RECORD(DECL_INDIRECTFIELD);
876 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
877
Douglas Gregor92a96f52011-02-08 21:58:10 +0000878 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
879 RECORD(PPD_MACRO_INSTANTIATION);
880 RECORD(PPD_MACRO_DEFINITION);
881 RECORD(PPD_INCLUSION_DIRECTIVE);
882
Douglas Gregor12bfa382009-10-17 00:13:19 +0000883 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000884 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000885#undef RECORD
886#undef BLOCK
887 Stream.ExitBlock();
888}
889
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000890/// \brief Adjusts the given filename to only write out the portion of the
891/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000892///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000893/// \param Filename the file name to adjust.
894///
895/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
896/// the returned filename will be adjusted by this system root.
897///
898/// \returns either the original filename (if it needs no adjustment) or the
899/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000900static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000901adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
902 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000903
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000904 if (!isysroot)
905 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000906
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000907 // Verify that the filename and the system root have the same prefix.
908 unsigned Pos = 0;
909 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
910 if (Filename[Pos] != isysroot[Pos])
911 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000912
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000913 // We hit the end of the filename before we hit the end of the system root.
914 if (!Filename[Pos])
915 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000916
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000917 // If the file name has a '/' at the current position, skip over the '/'.
918 // We distinguish sysroot-based includes from absolute includes by the
919 // absence of '/' at the beginning of sysroot-based includes.
920 if (Filename[Pos] == '/')
921 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000922
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000923 return Filename + Pos;
924}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000925
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000926/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000927void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot,
928 const std::string &OutputFile) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000929 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000930
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000931 // Metadata
932 const TargetInfo &Target = Context.Target;
933 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000934 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl539c5062010-08-18 23:57:32 +0000935 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000936 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
937 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000938 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
939 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
940 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000941 // Target triple or chained PCH name
942 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000943 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000944
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000945 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000946 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
947 Record.push_back(VERSION_MAJOR);
948 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000949 Record.push_back(CLANG_VERSION_MAJOR);
950 Record.push_back(CLANG_VERSION_MINOR);
951 Record.push_back(isysroot != 0);
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000952 // FIXME: This writes the absolute path for chained headers.
953 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
954 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump11289f42009-09-09 15:08:12 +0000955
Douglas Gregor45fe0362009-05-12 01:31:05 +0000956 // Original file name
957 SourceManager &SM = Context.getSourceManager();
958 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
959 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000960 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregor45fe0362009-05-12 01:31:05 +0000961 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
962 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
963
Michael J. Spencer740857f2010-12-21 16:45:57 +0000964 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +0000965
Michael J. Spencer740857f2010-12-21 16:45:57 +0000966 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000967
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +0000968 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000969 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000970 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000971 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000972 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000973 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000974 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000975
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +0000976 // Original PCH directory
977 if (!OutputFile.empty() && OutputFile != "-") {
978 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
979 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
980 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
981 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
982
983 llvm::SmallString<128> OutputPath(OutputFile);
984
985 llvm::sys::fs::make_absolute(OutputPath);
986 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
987
988 RecordData Record;
989 Record.push_back(ORIGINAL_PCH_DIR);
990 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
991 }
992
Ted Kremenek18e066f2010-01-22 22:12:47 +0000993 // Repository branch/version information.
994 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000995 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenek18e066f2010-01-22 22:12:47 +0000996 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
997 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000998 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +0000999 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +00001000 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
1001 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +00001002}
1003
1004/// \brief Write the LangOptions structure.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001005void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor55abb232009-04-10 20:39:37 +00001006 RecordData Record;
1007 Record.push_back(LangOpts.Trigraphs);
1008 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1009 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1010 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1011 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carruthe03aa552010-04-17 20:17:31 +00001012 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor55abb232009-04-10 20:39:37 +00001013 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1014 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1015 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1016 Record.push_back(LangOpts.C99); // C99 Support
Peter Collingbournea686b5f2011-04-15 00:35:23 +00001017 Record.push_back(LangOpts.C1X); // C1X Support
Douglas Gregor55abb232009-04-10 20:39:37 +00001018 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +00001019 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
1020 // already saved elsewhere.
Douglas Gregor55abb232009-04-10 20:39:37 +00001021 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1022 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +00001023 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +00001024
Douglas Gregor55abb232009-04-10 20:39:37 +00001025 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1026 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001027 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian45878032010-02-09 19:31:38 +00001028 // modern abi enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001029 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian45878032010-02-09 19:31:38 +00001030 // modern abi enabled.
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +00001031 Record.push_back(LangOpts.AppleKext); // Apple's kernel extensions ABI
Ted Kremenek1d56c9e2010-12-23 21:35:43 +00001032 Record.push_back(LangOpts.ObjCDefaultSynthProperties); // Objective-C auto-synthesized
1033 // properties enabled.
Fariborz Jahanian62c56022010-04-22 21:01:59 +00001034 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump11289f42009-09-09 15:08:12 +00001035
Douglas Gregor55abb232009-04-10 20:39:37 +00001036 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +00001037 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1038 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +00001039 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +00001040 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Anders Carlssonce8dd3a2011-02-19 23:53:54 +00001041 Record.push_back(LangOpts.ObjCExceptions);
Anders Carlsson6bbd2682011-02-23 03:04:54 +00001042 Record.push_back(LangOpts.CXXExceptions);
1043 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor55abb232009-04-10 20:39:37 +00001044
Douglas Gregordbe39272011-02-01 15:15:22 +00001045 Record.push_back(LangOpts.MSBitfields); // MS-compatible structure layout
Douglas Gregor55abb232009-04-10 20:39:37 +00001046 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1047 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1048 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1049
Chris Lattner258172e2009-04-27 07:35:58 +00001050 // Whether static initializers are protected by locks.
1051 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +00001052 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +00001053 Record.push_back(LangOpts.Blocks); // block extension to C
1054 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1055 // they are unused.
1056 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1057 // (modulo the platform support).
1058
Chris Lattner51924e512010-06-26 21:25:03 +00001059 Record.push_back(LangOpts.getSignedOverflowBehavior());
1060 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor55abb232009-04-10 20:39:37 +00001061
1062 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +00001063 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +00001064 // defined.
1065 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1066 // opposed to __DYNAMIC__).
1067 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1068
1069 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1070 // used (instead of C99 semantics).
1071 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Chandler Carruth7ffce732011-04-23 20:05:38 +00001072 Record.push_back(LangOpts.Deprecated); // Should __DEPRECATED be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +00001073 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
1074 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +00001075 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
1076 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +00001077 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Argyrios Kyrtzidisa88942a2011-01-15 02:56:16 +00001078 Record.push_back(LangOpts.ShortEnums); // Should the enum type be equivalent
1079 // to the smallest integer type with
1080 // enough room.
Douglas Gregor55abb232009-04-10 20:39:37 +00001081 Record.push_back(LangOpts.getGCMode());
1082 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +00001083 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +00001084 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +00001085 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +00001086 Record.push_back(LangOpts.CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +00001087 Record.push_back(LangOpts.CatchUndefined);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00001088 Record.push_back(LangOpts.DefaultFPContract);
Anders Carlsson9cedbef2009-08-22 22:30:33 +00001089 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +00001090 Record.push_back(LangOpts.SpellChecking);
Roman Divacky65b88cd2011-03-01 17:40:53 +00001091 Record.push_back(LangOpts.MRTD);
Sebastian Redl539c5062010-08-18 23:57:32 +00001092 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +00001093}
1094
Douglas Gregora7f71a92009-04-10 03:52:48 +00001095//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00001096// stat cache Serialization
1097//===----------------------------------------------------------------------===//
1098
1099namespace {
1100// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001101class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +00001102public:
1103 typedef const char * key_type;
1104 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001105
Chris Lattner2a6fa472010-11-23 19:28:12 +00001106 typedef struct stat data_type;
1107 typedef const data_type &data_type_ref;
Douglas Gregorc5046832009-04-27 18:38:38 +00001108
1109 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001110 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +00001111 }
Mike Stump11289f42009-09-09 15:08:12 +00001112
1113 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +00001114 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1115 data_type_ref Data) {
1116 unsigned StrLen = strlen(path);
1117 clang::io::Emit16(Out, StrLen);
Chris Lattner2a6fa472010-11-23 19:28:12 +00001118 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregorc5046832009-04-27 18:38:38 +00001119 clang::io::Emit8(Out, DataLen);
1120 return std::make_pair(StrLen + 1, DataLen);
1121 }
Mike Stump11289f42009-09-09 15:08:12 +00001122
Douglas Gregorc5046832009-04-27 18:38:38 +00001123 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1124 Out.write(path, KeyLen);
1125 }
Mike Stump11289f42009-09-09 15:08:12 +00001126
Chris Lattner2a6fa472010-11-23 19:28:12 +00001127 void EmitData(llvm::raw_ostream &Out, key_type_ref,
Douglas Gregorc5046832009-04-27 18:38:38 +00001128 data_type_ref Data, unsigned DataLen) {
1129 using namespace clang::io;
1130 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +00001131
Chris Lattner2a6fa472010-11-23 19:28:12 +00001132 Emit32(Out, (uint32_t) Data.st_ino);
1133 Emit32(Out, (uint32_t) Data.st_dev);
1134 Emit16(Out, (uint16_t) Data.st_mode);
1135 Emit64(Out, (uint64_t) Data.st_mtime);
1136 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregorc5046832009-04-27 18:38:38 +00001137
1138 assert(Out.tell() - Start == DataLen && "Wrong data length");
1139 }
1140};
1141} // end anonymous namespace
1142
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001143/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001144void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +00001145 // Build the on-disk hash table containing information about every
1146 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001147 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +00001148 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001149 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +00001150 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001151 Stat != StatEnd; ++Stat, ++NumStatEntries) {
1152 const char *Filename = Stat->first();
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001153 Generator.insert(Filename, Stat->second);
1154 }
Mike Stump11289f42009-09-09 15:08:12 +00001155
Douglas Gregorc5046832009-04-27 18:38:38 +00001156 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001157 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +00001158 uint32_t BucketOffset;
1159 {
1160 llvm::raw_svector_ostream Out(StatCacheData);
1161 // Make sure that no bucket is at offset 0
1162 clang::io::Emit32(Out, 0);
1163 BucketOffset = Generator.Emit(Out);
1164 }
1165
1166 // Create a blob abbreviation
1167 using namespace llvm;
1168 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001169 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +00001170 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1171 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1172 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1173 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1174
1175 // Write the stat cache
1176 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001177 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +00001178 Record.push_back(BucketOffset);
1179 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001180 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +00001181}
1182
1183//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +00001184// Source Manager Serialization
1185//===----------------------------------------------------------------------===//
1186
1187/// \brief Create an abbreviation for the SLocEntry that refers to a
1188/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001189static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001190 using namespace llvm;
1191 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001192 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001193 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1194 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1195 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1196 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001197 // FileEntry fields.
1198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1199 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregora7f71a92009-04-10 03:52:48 +00001200 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001201 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001202}
1203
1204/// \brief Create an abbreviation for the SLocEntry that refers to a
1205/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001206static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001207 using namespace llvm;
1208 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001209 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001210 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1211 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1212 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1213 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1214 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001215 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001216}
1217
1218/// \brief Create an abbreviation for the SLocEntry that refers to a
1219/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001220static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001221 using namespace llvm;
1222 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001223 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001224 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001225 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001226}
1227
1228/// \brief Create an abbreviation for the SLocEntry that refers to an
1229/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001230static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001231 using namespace llvm;
1232 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001233 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001234 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1235 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1236 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1237 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001238 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001239 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001240}
1241
Douglas Gregor09b69892011-02-10 17:09:37 +00001242namespace {
1243 // Trait used for the on-disk hash table of header search information.
1244 class HeaderFileInfoTrait {
1245 ASTWriter &Writer;
1246 HeaderSearch &HS;
1247
1248 public:
1249 HeaderFileInfoTrait(ASTWriter &Writer, HeaderSearch &HS)
1250 : Writer(Writer), HS(HS) { }
1251
1252 typedef const char *key_type;
1253 typedef key_type key_type_ref;
1254
1255 typedef HeaderFileInfo data_type;
1256 typedef const data_type &data_type_ref;
1257
1258 static unsigned ComputeHash(const char *path) {
1259 // The hash is based only on the filename portion of the key, so that the
1260 // reader can match based on filenames when symlinking or excess path
1261 // elements ("foo/../", "../") change the form of the name. However,
1262 // complete path is still the key.
1263 return llvm::HashString(llvm::sys::path::filename(path));
1264 }
1265
1266 std::pair<unsigned,unsigned>
1267 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1268 data_type_ref Data) {
1269 unsigned StrLen = strlen(path);
1270 clang::io::Emit16(Out, StrLen);
1271 unsigned DataLen = 1 + 2 + 4;
1272 clang::io::Emit8(Out, DataLen);
1273 return std::make_pair(StrLen + 1, DataLen);
1274 }
1275
1276 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1277 Out.write(path, KeyLen);
1278 }
1279
1280 void EmitData(llvm::raw_ostream &Out, key_type_ref,
1281 data_type_ref Data, unsigned DataLen) {
1282 using namespace clang::io;
1283 uint64_t Start = Out.tell(); (void)Start;
1284
Douglas Gregor37aa4932011-05-04 00:14:37 +00001285 unsigned char Flags = (Data.isImport << 4)
1286 | (Data.isPragmaOnce << 3)
Douglas Gregor09b69892011-02-10 17:09:37 +00001287 | (Data.DirInfo << 1)
1288 | Data.Resolved;
1289 Emit8(Out, (uint8_t)Flags);
1290 Emit16(Out, (uint16_t) Data.NumIncludes);
1291
1292 if (!Data.ControllingMacro)
1293 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1294 else
1295 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
1296 assert(Out.tell() - Start == DataLen && "Wrong data length");
1297 }
1298 };
1299} // end anonymous namespace
1300
1301/// \brief Write the header search block for the list of files that
1302///
1303/// \param HS The header search structure to save.
1304///
1305/// \param Chain Whether we're creating a chained AST file.
1306void ASTWriter::WriteHeaderSearch(HeaderSearch &HS, const char* isysroot) {
1307 llvm::SmallVector<const FileEntry *, 16> FilesByUID;
1308 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1309
1310 if (FilesByUID.size() > HS.header_file_size())
1311 FilesByUID.resize(HS.header_file_size());
1312
1313 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1314 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
1315 llvm::SmallVector<const char *, 4> SavedStrings;
1316 unsigned NumHeaderSearchEntries = 0;
1317 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1318 const FileEntry *File = FilesByUID[UID];
1319 if (!File)
1320 continue;
1321
1322 const HeaderFileInfo &HFI = HS.header_file_begin()[UID];
1323 if (HFI.External && Chain)
1324 continue;
1325
1326 // Turn the file name into an absolute path, if it isn't already.
1327 const char *Filename = File->getName();
1328 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1329
1330 // If we performed any translation on the file name at all, we need to
1331 // save this string, since the generator will refer to it later.
1332 if (Filename != File->getName()) {
1333 Filename = strdup(Filename);
1334 SavedStrings.push_back(Filename);
1335 }
1336
1337 Generator.insert(Filename, HFI, GeneratorTrait);
1338 ++NumHeaderSearchEntries;
1339 }
1340
1341 // Create the on-disk hash table in a buffer.
1342 llvm::SmallString<4096> TableData;
1343 uint32_t BucketOffset;
1344 {
1345 llvm::raw_svector_ostream Out(TableData);
1346 // Make sure that no bucket is at offset 0
1347 clang::io::Emit32(Out, 0);
1348 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1349 }
1350
1351 // Create a blob abbreviation
1352 using namespace llvm;
1353 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1354 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1355 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1356 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1357 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1358 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1359
1360 // Write the stat cache
1361 RecordData Record;
1362 Record.push_back(HEADER_SEARCH_TABLE);
1363 Record.push_back(BucketOffset);
1364 Record.push_back(NumHeaderSearchEntries);
1365 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1366
1367 // Free all of the strings we had to duplicate.
1368 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1369 free((void*)SavedStrings[I]);
1370}
1371
Douglas Gregora7f71a92009-04-10 03:52:48 +00001372/// \brief Writes the block containing the serialized form of the
1373/// source manager.
1374///
1375/// TODO: We should probably use an on-disk hash table (stored in a
1376/// blob), indexed based on the file name, so that we only create
1377/// entries for files that we actually need. In the common case (no
1378/// errors), we probably won't have to create file entries for any of
1379/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001380void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001381 const Preprocessor &PP,
1382 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001383 RecordData Record;
1384
Chris Lattner0910e3b2009-04-10 17:16:57 +00001385 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001386 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001387
1388 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001389 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1390 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1391 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1392 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001393
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001394 // Write the line table.
1395 if (SourceMgr.hasLineTable()) {
1396 LineTableInfo &LineTable = SourceMgr.getLineTable();
1397
1398 // Emit the file names
1399 Record.push_back(LineTable.getNumFilenames());
1400 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1401 // Emit the file name
1402 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001403 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001404 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1405 Record.push_back(FilenameLen);
1406 if (FilenameLen)
1407 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1408 }
Mike Stump11289f42009-09-09 15:08:12 +00001409
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001410 // Emit the line entries
1411 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1412 L != LEnd; ++L) {
1413 // Emit the file ID
1414 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +00001415
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001416 // Emit the line entries
1417 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +00001418 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001419 LEEnd = L->second.end();
1420 LE != LEEnd; ++LE) {
1421 Record.push_back(LE->FileOffset);
1422 Record.push_back(LE->LineNo);
1423 Record.push_back(LE->FilenameID);
1424 Record.push_back((unsigned)LE->FileKind);
1425 Record.push_back(LE->IncludeOffset);
1426 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001427 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001428 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001429 }
1430
Douglas Gregor258ae542009-04-27 06:38:32 +00001431 // Write out the source location entry table. We skip the first
1432 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001433 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001434 RecordData PreloadSLocs;
Sebastian Redl5c415f32010-07-22 17:01:13 +00001435 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1436 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1437 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1438 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001439 // Get this source location entry.
1440 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001441
Douglas Gregor258ae542009-04-27 06:38:32 +00001442 // Record the offset of this source-location entry.
1443 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1444
1445 // Figure out which record code to use.
1446 unsigned Code;
1447 if (SLoc->isFile()) {
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001448 if (SLoc->getFile().getContentCache()->OrigEntry)
Sebastian Redl539c5062010-08-18 23:57:32 +00001449 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001450 else
Sebastian Redl539c5062010-08-18 23:57:32 +00001451 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001452 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001453 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001454 Record.clear();
1455 Record.push_back(Code);
1456
1457 Record.push_back(SLoc->getOffset());
1458 if (SLoc->isFile()) {
1459 const SrcMgr::FileInfo &File = SLoc->getFile();
1460 Record.push_back(File.getIncludeLoc().getRawEncoding());
1461 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1462 Record.push_back(File.hasLineDirectives());
1463
1464 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001465 if (Content->OrigEntry) {
1466 assert(Content->OrigEntry == Content->ContentsEntry &&
1467 "Writing to AST an overriden file is not supported");
1468
Douglas Gregor258ae542009-04-27 06:38:32 +00001469 // The source location entry is a file. The blob associated
1470 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001471
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001472 // Emit size/modification time for this file.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001473 Record.push_back(Content->OrigEntry->getSize());
1474 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001475
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001476 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidis11e6f0a2011-03-05 01:03:53 +00001477 const char *Filename = Content->OrigEntry->getName();
Michael J. Spencer740857f2010-12-21 16:45:57 +00001478 llvm::SmallString<128> FilePath(Filename);
Anders Carlssona4267052011-03-08 16:04:35 +00001479
1480 // Ask the file manager to fixup the relative path for us. This will
1481 // honor the working directory.
1482 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1483
1484 // FIXME: This call to make_absolute shouldn't be necessary, the
1485 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencer740857f2010-12-21 16:45:57 +00001486 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001487 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001488
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001489 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001490 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001491 } else {
1492 // The source location entry is a buffer. The blob associated
1493 // with this entry contains the contents of the buffer.
1494
1495 // We add one to the size so that we capture the trailing NULL
1496 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1497 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001498 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001499 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001500 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001501 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1502 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001503 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001504 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001505 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001506 llvm::StringRef(Buffer->getBufferStart(),
1507 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001508
1509 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl5c415f32010-07-22 17:01:13 +00001510 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001511 }
1512 } else {
1513 // The source location entry is an instantiation.
1514 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1515 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1516 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1517 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1518
1519 // Compute the token length for this macro expansion.
1520 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001521 if (I + 1 != N)
1522 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001523 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1524 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1525 }
1526 }
1527
Douglas Gregor8f45df52009-04-16 22:23:12 +00001528 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001529
1530 if (SLocEntryOffsets.empty())
1531 return;
1532
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001533 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001534 // table is used for lazily loading source-location information.
1535 using namespace llvm;
1536 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001537 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001538 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1539 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1540 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1541 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001542
Douglas Gregor258ae542009-04-27 06:38:32 +00001543 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001544 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001545 Record.push_back(SLocEntryOffsets.size());
Sebastian Redlc1d035f2010-09-22 20:19:08 +00001546 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1547 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001548 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor258ae542009-04-27 06:38:32 +00001549
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001550 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001551 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001552 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001553}
1554
Douglas Gregorc5046832009-04-27 18:38:38 +00001555//===----------------------------------------------------------------------===//
1556// Preprocessor Serialization
1557//===----------------------------------------------------------------------===//
1558
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001559static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1560 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1561 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1562 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1563 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1564 return X.first->getName().compare(Y.first->getName());
1565}
1566
Chris Lattnereeffaef2009-04-10 17:15:23 +00001567/// \brief Writes the block containing the serialized form of the
1568/// preprocessor.
1569///
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001570void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001571 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001572
Chris Lattner0af3ba12009-04-13 01:29:17 +00001573 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1574 if (PP.getCounterValue() != 0) {
1575 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001576 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001577 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001578 }
1579
1580 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001581 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001582
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001583 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001584 // FIXME: use diagnostics subsystem for localization etc.
1585 if (PP.SawDateOrTime())
1586 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001587
Douglas Gregor796d76a2010-10-20 22:00:55 +00001588
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001589 // Loop over all the macro definitions that are live at the end of the file,
1590 // emitting each to the PP section.
Douglas Gregoraae92242010-03-19 21:51:54 +00001591 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001592
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001593 // Construct the list of macro definitions that need to be serialized.
1594 llvm::SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
1595 MacrosToEmit;
1596 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor68051a72011-02-11 00:26:14 +00001597 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1598 E = PP.macro_end(Chain == 0);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001599 I != E; ++I) {
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001600 MacroDefinitionsSeen.insert(I->first);
1601 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1602 }
1603
1604 // Sort the set of macro definitions that need to be serialized by the
1605 // name of the macro, to provide a stable ordering.
1606 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1607 &compareMacroDefinitions);
1608
Douglas Gregor68051a72011-02-11 00:26:14 +00001609 // Resolve any identifiers that defined macros at the time they were
1610 // deserialized, adding them to the list of macros to emit (if appropriate).
1611 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1612 IdentifierInfo *Name
1613 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1614 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1615 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1616 }
1617
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001618 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1619 const IdentifierInfo *Name = MacrosToEmit[I].first;
1620 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor68051a72011-02-11 00:26:14 +00001621 if (!MI)
1622 continue;
1623
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001624 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001625 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001626 // Also skip macros from a AST file if we're chaining.
Douglas Gregoreb114da2010-10-01 01:03:07 +00001627
1628 // FIXME: There is a (probably minor) optimization we could do here, if
1629 // the macro comes from the original PCH but the identifier comes from a
1630 // chained PCH, by storing the offset into the original PCH rather than
1631 // writing the macro definition a second time.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001632 if (MI->isBuiltinMacro() ||
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001633 (Chain && Name->isFromAST() && MI->isFromAST()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001634 continue;
1635
Douglas Gregor2e5571d2011-02-10 18:20:09 +00001636 AddIdentifierRef(Name, Record);
1637 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001638 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1639 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001640
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001641 unsigned Code;
1642 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001643 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001644 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001645 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001646
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001647 Record.push_back(MI->isC99Varargs());
1648 Record.push_back(MI->isGNUVarargs());
1649 Record.push_back(MI->getNumArgs());
1650 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1651 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001652 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001653 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001654
Douglas Gregoraae92242010-03-19 21:51:54 +00001655 // If we have a detailed preprocessing record, record the macro definition
1656 // ID that corresponds to this macro.
1657 if (PPRec)
1658 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001659
Douglas Gregor8f45df52009-04-16 22:23:12 +00001660 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001661 Record.clear();
1662
Chris Lattner2199f5b2009-04-10 18:08:30 +00001663 // Emit the tokens array.
1664 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1665 // Note that we know that the preprocessor does not have any annotation
1666 // tokens in it because they are created by the parser, and thus can't be
1667 // in a macro definition.
1668 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001669
Chris Lattner2199f5b2009-04-10 18:08:30 +00001670 Record.push_back(Tok.getLocation().getRawEncoding());
1671 Record.push_back(Tok.getLength());
1672
Chris Lattner2199f5b2009-04-10 18:08:30 +00001673 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1674 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001675 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001676 // FIXME: Should translate token kind to a stable encoding.
1677 Record.push_back(Tok.getKind());
1678 // FIXME: Should translate token flags to a stable encoding.
1679 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001680
Sebastian Redl539c5062010-08-18 23:57:32 +00001681 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001682 Record.clear();
1683 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001684 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001685 }
Douglas Gregor92a96f52011-02-08 21:58:10 +00001686 Stream.ExitBlock();
1687
1688 if (PPRec)
1689 WritePreprocessorDetail(*PPRec);
1690}
1691
1692void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
1693 if (PPRec.begin(Chain) == PPRec.end(Chain))
1694 return;
1695
1696 // Enter the preprocessor block.
1697 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001698
Douglas Gregoraae92242010-03-19 21:51:54 +00001699 // If the preprocessor has a preprocessing record, emit it.
1700 unsigned NumPreprocessingRecords = 0;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001701 using namespace llvm;
1702
1703 // Set up the abbreviation for
1704 unsigned InclusionAbbrev = 0;
1705 {
1706 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1707 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
1708 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1709 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1710 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1711 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1712 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1713 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1714 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1715 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1716 }
1717
1718 unsigned IndexBase = Chain ? PPRec.getNumPreallocatedEntities() : 0;
1719 RecordData Record;
1720 for (PreprocessingRecord::iterator E = PPRec.begin(Chain),
1721 EEnd = PPRec.end(Chain);
1722 E != EEnd; ++E) {
1723 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001724
Douglas Gregor92a96f52011-02-08 21:58:10 +00001725 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1726 // Record this macro definition's location.
1727 MacroID ID = getMacroDefinitionID(MD);
1728
1729 // Don't write the macro definition if it is from another AST file.
1730 if (ID < FirstMacroID)
Douglas Gregoraae92242010-03-19 21:51:54 +00001731 continue;
Douglas Gregor92a96f52011-02-08 21:58:10 +00001732
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001733 // Notify the serialization listener that we're serializing this entity.
1734 if (SerializationListener)
1735 SerializationListener->SerializedPreprocessedEntity(*E,
Douglas Gregor92a96f52011-02-08 21:58:10 +00001736 Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001737
Douglas Gregor92a96f52011-02-08 21:58:10 +00001738 unsigned Position = ID - FirstMacroID;
1739 if (Position != MacroDefinitionOffsets.size()) {
1740 if (Position > MacroDefinitionOffsets.size())
1741 MacroDefinitionOffsets.resize(Position + 1);
1742
1743 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
1744 } else
1745 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001746
Douglas Gregor92a96f52011-02-08 21:58:10 +00001747 Record.push_back(IndexBase + NumPreprocessingRecords++);
1748 Record.push_back(ID);
1749 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1750 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1751 AddIdentifierRef(MD->getName(), Record);
1752 AddSourceLocation(MD->getLocation(), Record);
1753 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1754 continue;
Douglas Gregoraae92242010-03-19 21:51:54 +00001755 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001756
Douglas Gregor92a96f52011-02-08 21:58:10 +00001757 // Notify the serialization listener that we're serializing this entity.
1758 if (SerializationListener)
1759 SerializationListener->SerializedPreprocessedEntity(*E,
1760 Stream.GetCurrentBitNo());
1761
1762 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1763 Record.push_back(IndexBase + NumPreprocessingRecords++);
1764 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1765 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1766 AddIdentifierRef(MI->getName(), Record);
1767 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1768 Stream.EmitRecord(PPD_MACRO_INSTANTIATION, Record);
1769 continue;
1770 }
1771
1772 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1773 Record.push_back(PPD_INCLUSION_DIRECTIVE);
1774 Record.push_back(IndexBase + NumPreprocessingRecords++);
1775 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1776 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1777 Record.push_back(ID->getFileName().size());
1778 Record.push_back(ID->wasInQuotes());
1779 Record.push_back(static_cast<unsigned>(ID->getKind()));
1780 llvm::SmallString<64> Buffer;
1781 Buffer += ID->getFileName();
1782 Buffer += ID->getFile()->getName();
1783 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1784 continue;
1785 }
1786
1787 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1788 }
Douglas Gregor8f45df52009-04-16 22:23:12 +00001789 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001790
Douglas Gregoraae92242010-03-19 21:51:54 +00001791 // Write the offsets table for the preprocessing record.
1792 if (NumPreprocessingRecords > 0) {
1793 // Write the offsets table for identifier IDs.
1794 using namespace llvm;
1795 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001796 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregoraae92242010-03-19 21:51:54 +00001797 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1798 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1799 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1800 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001801
Douglas Gregoraae92242010-03-19 21:51:54 +00001802 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001803 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001804 Record.push_back(NumPreprocessingRecords);
1805 Record.push_back(MacroDefinitionOffsets.size());
1806 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001807 data(MacroDefinitionOffsets));
Douglas Gregoraae92242010-03-19 21:51:54 +00001808 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001809}
1810
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001811void ASTWriter::WritePragmaDiagnosticMappings(const Diagnostic &Diag) {
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001812 RecordData Record;
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001813 for (Diagnostic::DiagStatePointsTy::const_iterator
1814 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1815 I != E; ++I) {
1816 const Diagnostic::DiagStatePoint &point = *I;
1817 if (point.Loc.isInvalid())
1818 continue;
1819
1820 Record.push_back(point.Loc.getRawEncoding());
1821 for (Diagnostic::DiagState::iterator
1822 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
1823 unsigned diag = I->first, map = I->second;
1824 if (map & 0x10) { // mapping from a diagnostic pragma.
1825 Record.push_back(diag);
1826 Record.push_back(map & 0x7);
1827 }
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001828 }
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001829 Record.push_back(-1); // mark the end of the diag/map pairs for this
1830 // location.
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001831 }
1832
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00001833 if (!Record.empty())
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00001834 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001835}
1836
Anders Carlsson9bb83e82011-03-06 18:41:18 +00001837void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
1838 if (CXXBaseSpecifiersOffsets.empty())
1839 return;
1840
1841 RecordData Record;
1842
1843 // Create a blob abbreviation for the C++ base specifiers offsets.
1844 using namespace llvm;
1845
1846 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1847 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
1848 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
1849 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1850 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1851
1852 // Write the selector offsets table.
1853 Record.clear();
1854 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
1855 Record.push_back(CXXBaseSpecifiersOffsets.size());
1856 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001857 data(CXXBaseSpecifiersOffsets));
Anders Carlsson9bb83e82011-03-06 18:41:18 +00001858}
1859
Douglas Gregorc5046832009-04-27 18:38:38 +00001860//===----------------------------------------------------------------------===//
1861// Type Serialization
1862//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001863
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001864/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001865void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00001866 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001867 if (Idx.getIndex() == 0) // we haven't seen this type before.
1868 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00001869
Douglas Gregor9b3932c2010-10-05 18:37:06 +00001870 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00001871
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001872 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001873 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001874 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001875 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001876 else if (TypeOffsets.size() < Index) {
1877 TypeOffsets.resize(Index + 1);
1878 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001879 }
1880
1881 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001882
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001883 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001884 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001885
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001886 if (T.hasLocalNonFastQualifiers()) {
1887 Qualifiers Qs = T.getLocalQualifiers();
1888 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001889 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001890 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00001891 } else {
1892 switch (T->getTypeClass()) {
1893 // For all of the concrete, non-dependent types, call the
1894 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001895#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001896 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001897#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001898#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001899 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001900 }
1901
1902 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001903 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001904
1905 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001906 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001907}
1908
Douglas Gregorc5046832009-04-27 18:38:38 +00001909//===----------------------------------------------------------------------===//
1910// Declaration Serialization
1911//===----------------------------------------------------------------------===//
1912
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001913/// \brief Write the block containing all of the declaration IDs
1914/// lexically declared within the given DeclContext.
1915///
1916/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1917/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001918uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001919 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001920 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001921 return 0;
1922
Douglas Gregor8f45df52009-04-16 22:23:12 +00001923 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001924 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001925 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001926 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001927 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1928 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001929 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001930
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001931 ++NumLexicalDeclContexts;
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001932 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001933 return Offset;
1934}
1935
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001936void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001937 using namespace llvm;
1938 RecordData Record;
1939
1940 // Write the type offsets array
1941 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001942 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001943 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1944 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1945 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1946 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001947 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001948 Record.push_back(TypeOffsets.size());
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001949 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001950
1951 // Write the declaration offsets array
1952 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001953 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001954 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1955 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1956 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1957 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001958 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001959 Record.push_back(DeclOffsets.size());
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00001960 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001961}
1962
Douglas Gregorc5046832009-04-27 18:38:38 +00001963//===----------------------------------------------------------------------===//
1964// Global Method Pool and Selector Serialization
1965//===----------------------------------------------------------------------===//
1966
Douglas Gregore84a9da2009-04-20 20:36:09 +00001967namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001968// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001969class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001970 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001971
1972public:
1973 typedef Selector key_type;
1974 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001975
Sebastian Redl834bb972010-08-04 17:20:04 +00001976 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00001977 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00001978 ObjCMethodList Instance, Factory;
1979 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00001980 typedef const data_type& data_type_ref;
1981
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001982 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001983
Douglas Gregorc78d3462009-04-24 21:10:55 +00001984 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00001985 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001986 }
Mike Stump11289f42009-09-09 15:08:12 +00001987
1988 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001989 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1990 data_type_ref Methods) {
1991 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1992 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00001993 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1994 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001995 Method = Method->Next)
1996 if (Method->Method)
1997 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00001998 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001999 Method = Method->Next)
2000 if (Method->Method)
2001 DataLen += 4;
2002 clang::io::Emit16(Out, DataLen);
2003 return std::make_pair(KeyLen, DataLen);
2004 }
Mike Stump11289f42009-09-09 15:08:12 +00002005
Douglas Gregor95c13f52009-04-25 17:48:32 +00002006 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00002007 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00002008 assert((Start >> 32) == 0 && "Selector key offset too large");
2009 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002010 unsigned N = Sel.getNumArgs();
2011 clang::io::Emit16(Out, N);
2012 if (N == 0)
2013 N = 1;
2014 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00002015 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00002016 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2017 }
Mike Stump11289f42009-09-09 15:08:12 +00002018
Douglas Gregorc78d3462009-04-24 21:10:55 +00002019 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002020 data_type_ref Methods, unsigned DataLen) {
2021 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00002022 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002023 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002024 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002025 Method = Method->Next)
2026 if (Method->Method)
2027 ++NumInstanceMethods;
2028
2029 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00002030 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002031 Method = Method->Next)
2032 if (Method->Method)
2033 ++NumFactoryMethods;
2034
2035 clang::io::Emit16(Out, NumInstanceMethods);
2036 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00002037 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002038 Method = Method->Next)
2039 if (Method->Method)
2040 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00002041 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002042 Method = Method->Next)
2043 if (Method->Method)
2044 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002045
2046 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00002047 }
2048};
2049} // end anonymous namespace
2050
Sebastian Redla19a67f2010-08-03 21:58:15 +00002051/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002052///
2053/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00002054/// in an on-disk hash table indexed by the selector. The hash table also
2055/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002056void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002057 using namespace llvm;
2058
Sebastian Redla19a67f2010-08-03 21:58:15 +00002059 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00002060 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00002061 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002062 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002063 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00002064 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002065 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002066 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00002067
Sebastian Redla19a67f2010-08-03 21:58:15 +00002068 // Create the on-disk hash table representation. We walk through every
2069 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00002070 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002071 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00002072 I = SelectorIDs.begin(), E = SelectorIDs.end();
2073 I != E; ++I) {
2074 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00002075 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002076 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00002077 I->second,
2078 ObjCMethodList(),
2079 ObjCMethodList()
2080 };
2081 if (F != SemaRef.MethodPool.end()) {
2082 Data.Instance = F->second.first;
2083 Data.Factory = F->second.second;
2084 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002085 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00002086 // changed.
2087 if (Chain && I->second < FirstSelectorID) {
2088 // Selector already exists. Did it change?
2089 bool changed = false;
2090 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2091 M = M->Next) {
2092 if (M->Method->getPCHLevel() == 0)
2093 changed = true;
2094 }
2095 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2096 M = M->Next) {
2097 if (M->Method->getPCHLevel() == 0)
2098 changed = true;
2099 }
2100 if (!changed)
2101 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00002102 } else if (Data.Instance.Method || Data.Factory.Method) {
2103 // A new method pool entry.
2104 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00002105 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002106 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002107 }
2108
Douglas Gregorc78d3462009-04-24 21:10:55 +00002109 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002110 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002111 uint32_t BucketOffset;
2112 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002113 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002114 llvm::raw_svector_ostream Out(MethodPool);
2115 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002116 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002117 BucketOffset = Generator.Emit(Out, Trait);
2118 }
2119
2120 // Create a blob abbreviation
2121 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002122 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002123 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00002124 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002125 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2126 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2127
Douglas Gregor95c13f52009-04-25 17:48:32 +00002128 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00002129 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002130 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002131 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00002132 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002133 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00002134
2135 // Create a blob abbreviation for the selector table offsets.
2136 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002137 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002138 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor95c13f52009-04-25 17:48:32 +00002139 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2140 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2141
2142 // Write the selector offsets table.
2143 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002144 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00002145 Record.push_back(SelectorOffsets.size());
2146 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002147 data(SelectorOffsets));
Douglas Gregorc78d3462009-04-24 21:10:55 +00002148 }
2149}
2150
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002151/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002152void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002153 using namespace llvm;
2154 if (SemaRef.ReferencedSelectors.empty())
2155 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00002156
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002157 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00002158
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002159 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00002160 // very tricky to fix, and given that @selector shouldn't really appear in
2161 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002162 for (DenseMap<Selector, SourceLocation>::iterator S =
2163 SemaRef.ReferencedSelectors.begin(),
2164 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2165 Selector Sel = (*S).first;
2166 SourceLocation Loc = (*S).second;
2167 AddSelectorRef(Sel, Record);
2168 AddSourceLocation(Loc, Record);
2169 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002170 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002171}
2172
Douglas Gregorc5046832009-04-27 18:38:38 +00002173//===----------------------------------------------------------------------===//
2174// Identifier Table Serialization
2175//===----------------------------------------------------------------------===//
2176
Douglas Gregorc78d3462009-04-24 21:10:55 +00002177namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002178class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002179 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00002180 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002181
Douglas Gregor1d583f22009-04-28 21:18:29 +00002182 /// \brief Determines whether this is an "interesting" identifier
2183 /// that needs a full IdentifierInfo structure written into the hash
2184 /// table.
2185 static bool isInterestingIdentifier(const IdentifierInfo *II) {
2186 return II->isPoisoned() ||
2187 II->isExtensionToken() ||
2188 II->hasMacroDefinition() ||
2189 II->getObjCOrBuiltinID() ||
2190 II->getFETokenInfo<void>();
2191 }
2192
Douglas Gregore84a9da2009-04-20 20:36:09 +00002193public:
2194 typedef const IdentifierInfo* key_type;
2195 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002196
Sebastian Redl539c5062010-08-18 23:57:32 +00002197 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002198 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00002199
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002200 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00002201 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00002202
2203 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00002204 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00002205 }
Mike Stump11289f42009-09-09 15:08:12 +00002206
2207 std::pair<unsigned,unsigned>
2208 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002209 IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002210 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002211 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
2212 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00002213 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00002214 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00002215 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00002216 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00002217 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2218 DEnd = IdentifierResolver::end();
2219 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00002220 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00002221 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00002222 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00002223 // We emit the key length after the data length so that every
2224 // string is preceded by a 16-bit length. This matches the PTH
2225 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00002226 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002227 return std::make_pair(KeyLen, DataLen);
2228 }
Mike Stump11289f42009-09-09 15:08:12 +00002229
2230 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00002231 unsigned KeyLen) {
2232 // Record the location of the key data. This is used when generating
2233 // the mapping from persistent IDs to strings.
2234 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00002235 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002236 }
Mike Stump11289f42009-09-09 15:08:12 +00002237
2238 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00002239 IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00002240 if (!isInterestingIdentifier(II)) {
2241 clang::io::Emit32(Out, ID << 1);
2242 return;
2243 }
Douglas Gregorb9256522009-04-28 21:32:13 +00002244
Douglas Gregor1d583f22009-04-28 21:18:29 +00002245 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002246 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002247 bool hasMacroDefinition =
2248 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00002249 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00002250 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002251 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
2252 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2253 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00002254 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00002255 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00002256 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002257
Douglas Gregorc3366a52009-04-21 23:56:24 +00002258 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00002259 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00002260
Douglas Gregora868bbd2009-04-21 22:25:48 +00002261 // Emit the declaration IDs in reverse order, because the
2262 // IdentifierResolver provides the declarations as they would be
2263 // visible (e.g., the function "stat" would come before the struct
2264 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2265 // adds declarations to the end of the list (so we need to see the
2266 // struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00002267 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump11289f42009-09-09 15:08:12 +00002268 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00002269 IdentifierResolver::end());
2270 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
2271 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00002272 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00002273 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002274 }
2275};
2276} // end anonymous namespace
2277
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002278/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002279///
2280/// The identifier table consists of a blob containing string data
2281/// (the actual identifiers themselves) and a separate "offsets" index
2282/// that maps identifier IDs to locations within the blob.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002283void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002284 using namespace llvm;
2285
2286 // Create and write out the blob that contains the identifier
2287 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002288 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002289 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002290 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump11289f42009-09-09 15:08:12 +00002291
Douglas Gregore6648fb2009-04-28 20:33:11 +00002292 // Look for any identifiers that were named while processing the
2293 // headers, but are otherwise not needed. We add these to the hash
2294 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002295 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00002296 // file.
2297 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2298 IDEnd = PP.getIdentifierTable().end();
2299 ID != IDEnd; ++ID)
2300 getIdentifierRef(ID->second);
2301
Sebastian Redlff4a2952010-07-23 23:49:55 +00002302 // Create the on-disk hash table representation. We only store offsets
2303 // for identifiers that appear here for the first time.
2304 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00002305 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002306 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2307 ID != IDEnd; ++ID) {
2308 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002309 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002310 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002311 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002312
Douglas Gregore84a9da2009-04-20 20:36:09 +00002313 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00002314 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00002315 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002316 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002317 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00002318 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002319 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00002320 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002321 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002322 }
2323
2324 // Create a blob abbreviation
2325 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002326 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00002327 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00002328 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00002329 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002330
2331 // Write the identifier table
2332 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002333 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00002334 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00002335 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002336 }
2337
2338 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00002339 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002340 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00002341 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2342 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2343 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2344
2345 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002346 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00002347 Record.push_back(IdentifierOffsets.size());
2348 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002349 data(IdentifierOffsets));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002350}
2351
Douglas Gregorc5046832009-04-27 18:38:38 +00002352//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002353// DeclContext's Name Lookup Table Serialization
2354//===----------------------------------------------------------------------===//
2355
2356namespace {
2357// Trait used for the on-disk hash table used in the method pool.
2358class ASTDeclContextNameLookupTrait {
2359 ASTWriter &Writer;
2360
2361public:
2362 typedef DeclarationName key_type;
2363 typedef key_type key_type_ref;
2364
2365 typedef DeclContext::lookup_result data_type;
2366 typedef const data_type& data_type_ref;
2367
2368 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2369
2370 unsigned ComputeHash(DeclarationName Name) {
2371 llvm::FoldingSetNodeID ID;
2372 ID.AddInteger(Name.getNameKind());
2373
2374 switch (Name.getNameKind()) {
2375 case DeclarationName::Identifier:
2376 ID.AddString(Name.getAsIdentifierInfo()->getName());
2377 break;
2378 case DeclarationName::ObjCZeroArgSelector:
2379 case DeclarationName::ObjCOneArgSelector:
2380 case DeclarationName::ObjCMultiArgSelector:
2381 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2382 break;
2383 case DeclarationName::CXXConstructorName:
2384 case DeclarationName::CXXDestructorName:
2385 case DeclarationName::CXXConversionFunctionName:
2386 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
2387 break;
2388 case DeclarationName::CXXOperatorName:
2389 ID.AddInteger(Name.getCXXOverloadedOperator());
2390 break;
2391 case DeclarationName::CXXLiteralOperatorName:
2392 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2393 case DeclarationName::CXXUsingDirective:
2394 break;
2395 }
2396
2397 return ID.ComputeHash();
2398 }
2399
2400 std::pair<unsigned,unsigned>
2401 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2402 data_type_ref Lookup) {
2403 unsigned KeyLen = 1;
2404 switch (Name.getNameKind()) {
2405 case DeclarationName::Identifier:
2406 case DeclarationName::ObjCZeroArgSelector:
2407 case DeclarationName::ObjCOneArgSelector:
2408 case DeclarationName::ObjCMultiArgSelector:
2409 case DeclarationName::CXXConstructorName:
2410 case DeclarationName::CXXDestructorName:
2411 case DeclarationName::CXXConversionFunctionName:
2412 case DeclarationName::CXXLiteralOperatorName:
2413 KeyLen += 4;
2414 break;
2415 case DeclarationName::CXXOperatorName:
2416 KeyLen += 1;
2417 break;
2418 case DeclarationName::CXXUsingDirective:
2419 break;
2420 }
2421 clang::io::Emit16(Out, KeyLen);
2422
2423 // 2 bytes for num of decls and 4 for each DeclID.
2424 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2425 clang::io::Emit16(Out, DataLen);
2426
2427 return std::make_pair(KeyLen, DataLen);
2428 }
2429
2430 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2431 using namespace clang::io;
2432
2433 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2434 Emit8(Out, Name.getNameKind());
2435 switch (Name.getNameKind()) {
2436 case DeclarationName::Identifier:
2437 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2438 break;
2439 case DeclarationName::ObjCZeroArgSelector:
2440 case DeclarationName::ObjCOneArgSelector:
2441 case DeclarationName::ObjCMultiArgSelector:
2442 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2443 break;
2444 case DeclarationName::CXXConstructorName:
2445 case DeclarationName::CXXDestructorName:
2446 case DeclarationName::CXXConversionFunctionName:
2447 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2448 break;
2449 case DeclarationName::CXXOperatorName:
2450 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2451 Emit8(Out, Name.getCXXOverloadedOperator());
2452 break;
2453 case DeclarationName::CXXLiteralOperatorName:
2454 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2455 break;
2456 case DeclarationName::CXXUsingDirective:
2457 break;
2458 }
2459 }
2460
2461 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2462 data_type Lookup, unsigned DataLen) {
2463 uint64_t Start = Out.tell(); (void)Start;
2464 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2465 for (; Lookup.first != Lookup.second; ++Lookup.first)
2466 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2467
2468 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2469 }
2470};
2471} // end anonymous namespace
2472
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002473/// \brief Write the block containing all of the declaration IDs
2474/// visible from the given DeclContext.
2475///
2476/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002477/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002478uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2479 DeclContext *DC) {
2480 if (DC->getPrimaryContext() != DC)
2481 return 0;
2482
2483 // Since there is no name lookup into functions or methods, don't bother to
2484 // build a visible-declarations table for these entities.
2485 if (DC->isFunctionOrMethod())
2486 return 0;
2487
2488 // If not in C++, we perform name lookup for the translation unit via the
2489 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2490 // FIXME: In C++ we need the visible declarations in order to "see" the
2491 // friend declarations, is there a way to do this without writing the table ?
2492 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2493 return 0;
2494
2495 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00002496 if (DC->hasExternalVisibleStorage())
2497 DC->MaterializeVisibleDeclsFromExternalStorage();
2498 else
2499 DC->lookup(DeclarationName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002500
2501 // Serialize the contents of the mapping used for lookup. Note that,
2502 // although we have two very different code paths, the serialized
2503 // representation is the same for both cases: a declaration name,
2504 // followed by a size, followed by references to the visible
2505 // declarations that have that name.
2506 uint64_t Offset = Stream.GetCurrentBitNo();
2507 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2508 if (!Map || Map->empty())
2509 return 0;
2510
2511 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2512 ASTDeclContextNameLookupTrait Trait(*this);
2513
2514 // Create the on-disk hash table representation.
2515 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2516 D != DEnd; ++D) {
2517 DeclarationName Name = D->first;
2518 DeclContext::lookup_result Result = D->second.getLookupResult();
2519 Generator.insert(Name, Result, Trait);
2520 }
2521
2522 // Create the on-disk hash table in a buffer.
2523 llvm::SmallString<4096> LookupTable;
2524 uint32_t BucketOffset;
2525 {
2526 llvm::raw_svector_ostream Out(LookupTable);
2527 // Make sure that no bucket is at offset 0
2528 clang::io::Emit32(Out, 0);
2529 BucketOffset = Generator.Emit(Out, Trait);
2530 }
2531
2532 // Write the lookup table
2533 RecordData Record;
2534 Record.push_back(DECL_CONTEXT_VISIBLE);
2535 Record.push_back(BucketOffset);
2536 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2537 LookupTable.str());
2538
2539 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2540 ++NumVisibleDeclContexts;
2541 return Offset;
2542}
2543
Sebastian Redla4071b42010-08-24 00:50:09 +00002544/// \brief Write an UPDATE_VISIBLE block for the given context.
2545///
2546/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2547/// DeclContext in a dependent AST file. As such, they only exist for the TU
2548/// (in C++) and for namespaces.
2549void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00002550 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2551 if (!Map || Map->empty())
2552 return;
2553
2554 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2555 ASTDeclContextNameLookupTrait Trait(*this);
2556
2557 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002558 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2559 D != DEnd; ++D) {
2560 DeclarationName Name = D->first;
2561 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002562 // For any name that appears in this table, the results are complete, i.e.
2563 // they overwrite results from previous PCHs. Merging is always a mess.
2564 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002565 }
2566
2567 // Create the on-disk hash table in a buffer.
2568 llvm::SmallString<4096> LookupTable;
2569 uint32_t BucketOffset;
2570 {
2571 llvm::raw_svector_ostream Out(LookupTable);
2572 // Make sure that no bucket is at offset 0
2573 clang::io::Emit32(Out, 0);
2574 BucketOffset = Generator.Emit(Out, Trait);
2575 }
2576
2577 // Write the lookup table
2578 RecordData Record;
2579 Record.push_back(UPDATE_VISIBLE);
2580 Record.push_back(getDeclID(cast<Decl>(DC)));
2581 Record.push_back(BucketOffset);
2582 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2583}
2584
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002585/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2586void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2587 RecordData Record;
2588 Record.push_back(Opts.fp_contract);
2589 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2590}
2591
2592/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2593void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
2594 if (!SemaRef.Context.getLangOptions().OpenCL)
2595 return;
2596
2597 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2598 RecordData Record;
2599#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2600#include "clang/Basic/OpenCLExtensions.def"
2601 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2602}
2603
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002604//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00002605// General Serialization Routines
2606//===----------------------------------------------------------------------===//
2607
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002608/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002609void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00002610 Record.push_back(Attrs.size());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002611 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2612 const Attr * A = *i;
2613 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2614 AddSourceLocation(A->getLocation(), Record);
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002615
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002616#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00002617
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002618 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002619}
2620
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002621void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002622 Record.push_back(Str.size());
2623 Record.insert(Record.end(), Str.begin(), Str.end());
2624}
2625
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002626void ASTWriter::AddVersionTuple(const VersionTuple &Version,
2627 RecordDataImpl &Record) {
2628 Record.push_back(Version.getMajor());
2629 if (llvm::Optional<unsigned> Minor = Version.getMinor())
2630 Record.push_back(*Minor + 1);
2631 else
2632 Record.push_back(0);
2633 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
2634 Record.push_back(*Subminor + 1);
2635 else
2636 Record.push_back(0);
2637}
2638
Douglas Gregore84a9da2009-04-20 20:36:09 +00002639/// \brief Note that the identifier II occurs at the given offset
2640/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002641void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002642 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002643 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00002644 // up earlier in the chain and thus don't need an offset.
2645 if (ID >= FirstIdentID)
2646 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002647}
2648
Douglas Gregor95c13f52009-04-25 17:48:32 +00002649/// \brief Note that the selector Sel occurs at the given offset
2650/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002651void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00002652 unsigned ID = SelectorIDs[Sel];
2653 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00002654 // Don't record offsets for selectors that are also available in a different
2655 // file.
2656 if (ID < FirstSelectorID)
2657 return;
2658 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002659}
2660
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002661ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00002662 : Stream(Stream), Chain(0), SerializationListener(0),
2663 FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00002664 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002665 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor91096292010-10-02 19:29:26 +00002666 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2667 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002668 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002669 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
2670 NextCXXBaseSpecifiersID(1)
2671{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002672}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002673
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002674void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002675 const std::string &OutputFile,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002676 const char *isysroot) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002677 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002678 Stream.Emit((unsigned)'C', 8);
2679 Stream.Emit((unsigned)'P', 8);
2680 Stream.Emit((unsigned)'C', 8);
2681 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002682
Chris Lattner28fa4e62009-04-26 22:26:21 +00002683 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002684
Sebastian Redl143413f2010-07-12 22:02:52 +00002685 if (Chain)
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002686 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002687 else
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002688 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile);
Sebastian Redl143413f2010-07-12 22:02:52 +00002689}
2690
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002691void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002692 const char *isysroot,
2693 const std::string &OutputFile) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002694 using namespace llvm;
2695
2696 ASTContext &Context = SemaRef.Context;
2697 Preprocessor &PP = SemaRef.PP;
2698
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002699 // The translation unit is the first declaration we'll emit.
2700 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlff4a2952010-07-23 23:49:55 +00002701 ++NextDeclID;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002702 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002703
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002704 // Make sure that we emit IdentifierInfos (and any attached
2705 // declarations) for builtins.
2706 {
2707 IdentifierTable &Table = PP.getIdentifierTable();
2708 llvm::SmallVector<const char *, 32> BuiltinNames;
2709 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2710 Context.getLangOptions().NoBuiltin);
2711 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2712 getIdentifierRef(&Table.get(BuiltinNames[I]));
2713 }
2714
Chris Lattner0c797362009-09-08 18:19:27 +00002715 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002716 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002717 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002718 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00002719 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2720 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00002721 }
Douglas Gregord4df8652009-04-22 22:02:47 +00002722
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002723 // Build a record containing all of the file scoped decls in this file.
2724 RecordData UnusedFileScopedDecls;
2725 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2726 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002727
Alexis Hunt27a761d2011-05-04 23:29:54 +00002728 RecordData DelegatingCtorDecls;
2729 for (unsigned i=0, e = SemaRef.DelegatingCtorDecls.size(); i != e; ++i)
2730 AddDeclRef(SemaRef.DelegatingCtorDecls[i], DelegatingCtorDecls);
2731
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002732 RecordData WeakUndeclaredIdentifiers;
2733 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2734 WeakUndeclaredIdentifiers.push_back(
2735 SemaRef.WeakUndeclaredIdentifiers.size());
2736 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2737 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2738 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2739 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2740 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2741 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2742 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2743 }
2744 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002745
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002746 // Build a record containing all of the locally-scoped external
2747 // declarations in this header file. Generally, this record will be
2748 // empty.
2749 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002750 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00002751 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002752 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002753 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2754 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2755 TD != TDEnd; ++TD)
2756 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2757
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002758 // Build a record containing all of the ext_vector declarations.
2759 RecordData ExtVectorDecls;
2760 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2761 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2762
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002763 // Build a record containing all of the VTable uses information.
2764 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00002765 if (!SemaRef.VTableUses.empty()) {
2766 VTableUses.push_back(SemaRef.VTableUses.size());
2767 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2768 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2769 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2770 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2771 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002772 }
2773
2774 // Build a record containing all of dynamic classes declarations.
2775 RecordData DynamicClasses;
2776 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2777 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2778
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002779 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002780 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002781 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002782 I = SemaRef.PendingInstantiations.begin(),
2783 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2784 AddDeclRef(I->first, PendingInstantiations);
2785 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002786 }
2787 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2788 "There are local ones at end of translation unit!");
2789
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002790 // Build a record containing some declaration references.
2791 RecordData SemaDeclRefs;
2792 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2793 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2794 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2795 }
2796
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002797 RecordData CUDASpecialDeclRefs;
2798 if (Context.getcudaConfigureCallDecl()) {
2799 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
2800 }
2801
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002802 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002803 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002804 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002805 WriteMetadata(Context, isysroot, OutputFile);
Sebastian Redl143413f2010-07-12 22:02:52 +00002806 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002807 if (StatCalls && !isysroot)
Douglas Gregor11cfd942010-07-12 23:48:14 +00002808 WriteStatCache(*StatCalls);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002809 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc277ad12009-07-18 15:33:26 +00002810 // Write the record of special types.
2811 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002812
Steve Naroffc277ad12009-07-18 15:33:26 +00002813 AddTypeRef(Context.getBuiltinVaListType(), Record);
2814 AddTypeRef(Context.getObjCIdType(), Record);
2815 AddTypeRef(Context.getObjCSelType(), Record);
2816 AddTypeRef(Context.getObjCProtoType(), Record);
2817 AddTypeRef(Context.getObjCClassType(), Record);
2818 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2819 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2820 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002821 AddTypeRef(Context.getjmp_bufType(), Record);
2822 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002823 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2824 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpd0153282009-10-20 02:12:22 +00002825 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002826 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002827 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2828 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002829 Record.push_back(Context.isInt128Installed());
Richard Smith02e85f32011-04-14 22:09:26 +00002830 AddTypeRef(Context.AutoDeductTy, Record);
2831 AddTypeRef(Context.AutoRRefDeductTy, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +00002832 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002833
Douglas Gregor1970d882009-04-26 03:49:13 +00002834 // Keep writing types and declarations until all types and
2835 // declarations have been written.
Sebastian Redl539c5062010-08-18 23:57:32 +00002836 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002837 WriteDeclsBlockAbbrevs();
2838 while (!DeclTypesToEmit.empty()) {
2839 DeclOrType DOT = DeclTypesToEmit.front();
2840 DeclTypesToEmit.pop();
2841 if (DOT.isType())
2842 WriteType(DOT.getType());
2843 else
2844 WriteDecl(Context, DOT.getDecl());
2845 }
2846 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002847
Douglas Gregor45053152009-10-17 17:25:45 +00002848 WritePreprocessor(PP);
Douglas Gregor09b69892011-02-10 17:09:37 +00002849 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redla19a67f2010-08-03 21:58:15 +00002850 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002851 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002852 WriteIdentifierTable(PP);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00002853 WriteFPPragmaOptions(SemaRef.getFPOptions());
2854 WriteOpenCLExtensions(SemaRef);
Douglas Gregor745ed142009-04-25 18:35:21 +00002855
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002856 WriteTypeDeclOffsets();
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00002857 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00002858
Anders Carlsson9bb83e82011-03-06 18:41:18 +00002859 WriteCXXBaseSpecifiersOffsets();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002860
Douglas Gregord4df8652009-04-22 22:02:47 +00002861 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002862 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002863 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002864
2865 // Write the record containing tentative definitions.
2866 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002867 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002868
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002869 // Write the record containing unused file scoped decls.
2870 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002871 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002872
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002873 // Write the record containing weak undeclared identifiers.
2874 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002875 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002876 WeakUndeclaredIdentifiers);
2877
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002878 // Write the record containing locally-scoped external definitions.
2879 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002880 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002881 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002882
2883 // Write the record containing ext_vector type names.
2884 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002885 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002886
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002887 // Write the record containing VTable uses information.
2888 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002889 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002890
2891 // Write the record containing dynamic classes declarations.
2892 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002893 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002894
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002895 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002896 if (!PendingInstantiations.empty())
2897 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002898
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002899 // Write the record containing declaration references of Sema.
2900 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002901 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002902
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002903 // Write the record containing CUDA-specific declaration references.
2904 if (!CUDASpecialDeclRefs.empty())
2905 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00002906
2907 // Write the delegating constructors.
2908 if (!DelegatingCtorDecls.empty())
2909 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne9e2c81f2011-02-09 21:04:32 +00002910
Douglas Gregor08f01292009-04-17 22:13:46 +00002911 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002912 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002913 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002914 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002915 Record.push_back(NumLexicalDeclContexts);
2916 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00002917 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002918 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002919}
2920
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002921void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002922 const char *isysroot) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002923 using namespace llvm;
2924
2925 ASTContext &Context = SemaRef.Context;
2926 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002927
Sebastian Redl143413f2010-07-12 22:02:52 +00002928 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002929 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis10b23682011-02-15 17:54:22 +00002930 WriteMetadata(Context, isysroot, "");
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002931 if (StatCalls && !isysroot)
2932 WriteStatCache(*StatCalls);
2933 // FIXME: Source manager block should only write new stuff, which could be
2934 // done by tracking the largest ID in the chain
2935 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002936
2937 // The special types are in the chained PCH.
2938
2939 // We don't start with the translation unit, but with its decls that
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002940 // don't come from the chained PCH.
Sebastian Redl143413f2010-07-12 22:02:52 +00002941 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002942 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002943 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2944 E = TU->noload_decls_end();
Sebastian Redl143413f2010-07-12 22:02:52 +00002945 I != E; ++I) {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002946 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002947 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002948 else if ((*I)->isChangedSinceDeserialization())
2949 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl143413f2010-07-12 22:02:52 +00002950 }
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002951 // We also need to write a lexical updates block for the TU.
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002952 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002953 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002954 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2955 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2956 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002957 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002958 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
Benjamin Kramerd47a12a2011-04-24 17:44:50 +00002959 data(NewGlobalDecls));
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00002960 // And a visible updates block for the DeclContexts.
2961 Abv = new llvm::BitCodeAbbrev();
2962 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2963 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2964 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2965 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2966 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2967 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl143413f2010-07-12 22:02:52 +00002968
Sebastian Redl98912122010-07-27 23:01:28 +00002969 // Build a record containing all of the new tentative definitions in this
2970 // file, in TentativeDefinitions order.
2971 RecordData TentativeDefinitions;
2972 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2973 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2974 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2975 }
2976
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002977 // Build a record containing all of the file scoped decls in this file.
2978 RecordData UnusedFileScopedDecls;
2979 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2980 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2981 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002982 }
2983
Alexis Hunt27a761d2011-05-04 23:29:54 +00002984 // Build a record containing all of the delegating constructor decls in this
2985 // file.
2986 RecordData DelegatingCtorDecls;
2987 for (unsigned i=0, e = SemaRef.DelegatingCtorDecls.size(); i != e; ++i) {
2988 if (SemaRef.DelegatingCtorDecls[i]->getPCHLevel() == 0)
2989 AddDeclRef(SemaRef.DelegatingCtorDecls[i], DelegatingCtorDecls);
2990 }
2991
Sebastian Redl08aca90252010-08-05 18:21:25 +00002992 // We write the entire table, overwriting the tables from the chain.
2993 RecordData WeakUndeclaredIdentifiers;
2994 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2995 WeakUndeclaredIdentifiers.push_back(
2996 SemaRef.WeakUndeclaredIdentifiers.size());
2997 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2998 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2999 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3000 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3001 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3002 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3003 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3004 }
3005 }
3006
Sebastian Redl98912122010-07-27 23:01:28 +00003007 // Build a record containing all of the locally-scoped external
3008 // declarations in this header file. Generally, this record will be
3009 // empty.
3010 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003011 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl98912122010-07-27 23:01:28 +00003012 // nondeterminstic!
3013 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
3014 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3015 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
3016 TD != TDEnd; ++TD) {
3017 if (TD->second->getPCHLevel() == 0)
3018 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3019 }
3020
3021 // Build a record containing all of the ext_vector declarations.
3022 RecordData ExtVectorDecls;
3023 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
3024 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
3025 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
3026 }
3027
Sebastian Redl08aca90252010-08-05 18:21:25 +00003028 // Build a record containing all of the VTable uses information.
3029 // We write everything here, because it's too hard to determine whether
3030 // a use is new to this part.
3031 RecordData VTableUses;
3032 if (!SemaRef.VTableUses.empty()) {
3033 VTableUses.push_back(SemaRef.VTableUses.size());
3034 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3035 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3036 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3037 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3038 }
3039 }
3040
3041 // Build a record containing all of dynamic classes declarations.
3042 RecordData DynamicClasses;
3043 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
3044 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
3045 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
3046
3047 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003048 RecordData PendingInstantiations;
Sebastian Redl08aca90252010-08-05 18:21:25 +00003049 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00003050 I = SemaRef.PendingInstantiations.begin(),
3051 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl14afaf02011-04-24 16:27:30 +00003052 AddDeclRef(I->first, PendingInstantiations);
3053 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003054 }
3055 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3056 "There are local ones at end of translation unit!");
3057
3058 // Build a record containing some declaration references.
3059 // It's not worth the effort to avoid duplication here.
3060 RecordData SemaDeclRefs;
3061 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3062 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3063 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3064 }
3065
Sebastian Redl539c5062010-08-18 23:57:32 +00003066 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl143413f2010-07-12 22:02:52 +00003067 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003068 for (DeclsToRewriteTy::iterator
3069 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
3070 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl143413f2010-07-12 22:02:52 +00003071 while (!DeclTypesToEmit.empty()) {
3072 DeclOrType DOT = DeclTypesToEmit.front();
3073 DeclTypesToEmit.pop();
3074 if (DOT.isType())
3075 WriteType(DOT.getType());
3076 else
3077 WriteDecl(Context, DOT.getDecl());
3078 }
3079 Stream.ExitBlock();
3080
Sebastian Redl98912122010-07-27 23:01:28 +00003081 WritePreprocessor(PP);
Sebastian Redl51c79d82010-08-04 22:21:29 +00003082 WriteSelectors(SemaRef);
3083 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlff4a2952010-07-23 23:49:55 +00003084 WriteIdentifierTable(PP);
Peter Collingbourne5df20e02011-02-15 19:46:30 +00003085 WriteFPPragmaOptions(SemaRef.getFPOptions());
3086 WriteOpenCLExtensions(SemaRef);
3087
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003088 WriteTypeDeclOffsets();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00003089 // FIXME: For chained PCH only write the new mappings (we currently
3090 // write all of them again).
Argyrios Kyrtzidis243aedb2011-01-14 20:54:07 +00003091 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl98912122010-07-27 23:01:28 +00003092
Anders Carlsson9bb83e82011-03-06 18:41:18 +00003093 WriteCXXBaseSpecifiersOffsets();
3094
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003095 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00003096 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003097 RecordData FirstLatestDeclIDs;
3098 for (FirstLatestDeclMap::iterator
3099 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
3100 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
3101 "Expected first & second to be in different PCHs");
3102 AddDeclRef(I->first, FirstLatestDeclIDs);
3103 AddDeclRef(I->second, FirstLatestDeclIDs);
3104 }
3105 if (!FirstLatestDeclIDs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003106 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00003107
Sebastian Redl98912122010-07-27 23:01:28 +00003108 // Write the record containing external, unnamed definitions.
3109 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003110 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00003111
3112 // Write the record containing tentative definitions.
3113 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003114 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00003115
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00003116 // Write the record containing unused file scoped decls.
3117 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003118 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003119
Sebastian Redl08aca90252010-08-05 18:21:25 +00003120 // Write the record containing weak undeclared identifiers.
3121 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003122 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl08aca90252010-08-05 18:21:25 +00003123 WeakUndeclaredIdentifiers);
3124
Sebastian Redl98912122010-07-27 23:01:28 +00003125 // Write the record containing locally-scoped external definitions.
3126 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003127 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl98912122010-07-27 23:01:28 +00003128 LocallyScopedExternalDecls);
3129
3130 // Write the record containing ext_vector type names.
3131 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003132 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003133
Sebastian Redl08aca90252010-08-05 18:21:25 +00003134 // Write the record containing VTable uses information.
3135 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003136 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003137
3138 // Write the record containing dynamic classes declarations.
3139 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003140 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003141
3142 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00003143 if (!PendingInstantiations.empty())
3144 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00003145
3146 // Write the record containing declaration references of Sema.
3147 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00003148 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Alexis Hunt27a761d2011-05-04 23:29:54 +00003149
3150 // Write the delegating constructors.
3151 if (!DelegatingCtorDecls.empty())
3152 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00003153
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003154 // Write the updates to DeclContexts.
3155 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3156 I = UpdatedDeclContexts.begin(),
3157 E = UpdatedDeclContexts.end();
Sebastian Redla4071b42010-08-24 00:50:09 +00003158 I != E; ++I)
3159 WriteDeclContextVisibleUpdate(*I);
3160
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003161 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003162
Sebastian Redl98912122010-07-27 23:01:28 +00003163 Record.clear();
3164 Record.push_back(NumStatements);
3165 Record.push_back(NumMacros);
3166 Record.push_back(NumLexicalDeclContexts);
3167 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003168 WriteDeclReplacementsBlock();
Sebastian Redl539c5062010-08-18 23:57:32 +00003169 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl143413f2010-07-12 22:02:52 +00003170 Stream.ExitBlock();
3171}
3172
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003173void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003174 if (DeclUpdates.empty())
3175 return;
3176
3177 RecordData OffsetsRecord;
3178 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
3179 for (DeclUpdateMap::iterator
3180 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3181 const Decl *D = I->first;
3182 UpdateRecord &URec = I->second;
3183
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00003184 if (DeclsToRewrite.count(D))
3185 continue; // The decl will be written completely,no need to store updates.
3186
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003187 uint64_t Offset = Stream.GetCurrentBitNo();
3188 Stream.EmitRecord(DECL_UPDATES, URec);
3189
3190 OffsetsRecord.push_back(GetDeclRef(D));
3191 OffsetsRecord.push_back(Offset);
3192 }
3193 Stream.ExitBlock();
3194 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3195}
3196
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00003197void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003198 if (ReplacedDecls.empty())
3199 return;
3200
3201 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00003202 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003203 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
3204 Record.push_back(I->first);
3205 Record.push_back(I->second);
3206 }
Sebastian Redl539c5062010-08-18 23:57:32 +00003207 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003208}
3209
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003210void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003211 Record.push_back(Loc.getRawEncoding());
3212}
3213
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003214void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003215 AddSourceLocation(Range.getBegin(), Record);
3216 AddSourceLocation(Range.getEnd(), Record);
3217}
3218
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003219void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003220 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00003221 const uint64_t *Words = Value.getRawData();
3222 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003223}
3224
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003225void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00003226 Record.push_back(Value.isUnsigned());
3227 AddAPInt(Value, Record);
3228}
3229
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003230void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00003231 AddAPInt(Value.bitcastToAPInt(), Record);
3232}
3233
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003234void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003235 Record.push_back(getIdentifierRef(II));
3236}
3237
Sebastian Redl539c5062010-08-18 23:57:32 +00003238IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003239 if (II == 0)
3240 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003241
Sebastian Redl539c5062010-08-18 23:57:32 +00003242 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00003243 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00003244 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00003245 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003246}
3247
Sebastian Redl50e26582010-09-15 19:54:06 +00003248MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregoraae92242010-03-19 21:51:54 +00003249 if (MD == 0)
3250 return 0;
Sebastian Redl50e26582010-09-15 19:54:06 +00003251
3252 MacroID &ID = MacroDefinitions[MD];
Douglas Gregoraae92242010-03-19 21:51:54 +00003253 if (ID == 0)
Douglas Gregor91096292010-10-02 19:29:26 +00003254 ID = NextMacroID++;
Douglas Gregoraae92242010-03-19 21:51:54 +00003255 return ID;
3256}
3257
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003258void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003259 Record.push_back(getSelectorRef(SelRef));
3260}
3261
Sebastian Redl539c5062010-08-18 23:57:32 +00003262SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003263 if (Sel.getAsOpaquePtr() == 0) {
3264 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00003265 }
3266
Sebastian Redl539c5062010-08-18 23:57:32 +00003267 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00003268 if (SID == 0 && Chain) {
3269 // This might trigger a ReadSelector callback, which will set the ID for
3270 // this selector.
3271 Chain->LoadSelector(Sel);
3272 }
Steve Naroff2ddea052009-04-23 10:39:46 +00003273 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00003274 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00003275 }
Sebastian Redl834bb972010-08-04 17:20:04 +00003276 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00003277}
3278
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003279void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00003280 AddDeclRef(Temp->getDestructor(), Record);
3281}
3282
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003283void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3284 CXXBaseSpecifier const *BasesEnd,
3285 RecordDataImpl &Record) {
3286 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3287 CXXBaseSpecifiersToWrite.push_back(
3288 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3289 Bases, BasesEnd));
3290 Record.push_back(NextCXXBaseSpecifiersID++);
3291}
3292
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003293void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003294 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003295 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003296 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00003297 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003298 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00003299 break;
3300 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003301 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00003302 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003303 case TemplateArgument::Template:
Douglas Gregor9d802122011-03-02 17:09:35 +00003304 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003305 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003306 break;
3307 case TemplateArgument::TemplateExpansion:
Douglas Gregor9d802122011-03-02 17:09:35 +00003308 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003309 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00003310 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00003311 break;
John McCall0ad16662009-10-29 08:12:44 +00003312 case TemplateArgument::Null:
3313 case TemplateArgument::Integral:
3314 case TemplateArgument::Declaration:
3315 case TemplateArgument::Pack:
3316 break;
3317 }
3318}
3319
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003320void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003321 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003322 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00003323
3324 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3325 bool InfoHasSameExpr
3326 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3327 Record.push_back(InfoHasSameExpr);
3328 if (InfoHasSameExpr)
3329 return; // Avoid storing the same expr twice.
3330 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00003331 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3332 Record);
3333}
3334
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003335void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3336 RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00003337 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00003338 AddTypeRef(QualType(), Record);
3339 return;
3340 }
3341
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003342 AddTypeLoc(TInfo->getTypeLoc(), Record);
3343}
3344
3345void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3346 AddTypeRef(TL.getType(), Record);
3347
John McCall8f115c62009-10-16 21:56:05 +00003348 TypeLocWriter TLW(*this, Record);
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003349 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00003350 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00003351}
3352
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003353void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00003354 Record.push_back(GetOrCreateTypeID(T));
3355}
3356
3357TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003358 return MakeTypeID(T,
3359 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3360}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003361
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003362TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00003363 return MakeTypeID(T,
3364 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003365}
3366
3367TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3368 if (T.isNull())
3369 return TypeIdx();
3370 assert(!T.getLocalFastQualifiers());
3371
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00003372 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003373 if (Idx.getIndex() == 0) {
Douglas Gregor1970d882009-04-26 03:49:13 +00003374 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00003375 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003376 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00003377 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00003378 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003379 return Idx;
3380}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003381
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003382TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00003383 if (T.isNull())
3384 return TypeIdx();
3385 assert(!T.getLocalFastQualifiers());
3386
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00003387 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3388 assert(I != TypeIdxs.end() && "Type not emitted!");
3389 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003390}
3391
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003392void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003393 Record.push_back(GetDeclRef(D));
3394}
3395
Sebastian Redl539c5062010-08-18 23:57:32 +00003396DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003397 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003398 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003399 }
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003400 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00003401 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00003402 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003403 // We haven't seen this declaration before. Give it a new ID and
3404 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00003405 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003406 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003407 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3408 // We don't add it to the replacement collection here, because we don't
3409 // have the offset yet.
3410 DeclTypesToEmit.push(const_cast<Decl *>(D));
3411 // Reset the flag, so that we don't add this decl multiple times.
3412 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003413 }
3414
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003415 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003416}
3417
Sebastian Redl539c5062010-08-18 23:57:32 +00003418DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00003419 if (D == 0)
3420 return 0;
3421
3422 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3423 return DeclIDs[D];
3424}
3425
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003426void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00003427 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003428 Record.push_back(Name.getNameKind());
3429 switch (Name.getNameKind()) {
3430 case DeclarationName::Identifier:
3431 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3432 break;
3433
3434 case DeclarationName::ObjCZeroArgSelector:
3435 case DeclarationName::ObjCOneArgSelector:
3436 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00003437 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003438 break;
3439
3440 case DeclarationName::CXXConstructorName:
3441 case DeclarationName::CXXDestructorName:
3442 case DeclarationName::CXXConversionFunctionName:
3443 AddTypeRef(Name.getCXXNameType(), Record);
3444 break;
3445
3446 case DeclarationName::CXXOperatorName:
3447 Record.push_back(Name.getCXXOverloadedOperator());
3448 break;
3449
Alexis Hunt3d221f22009-11-29 07:34:05 +00003450 case DeclarationName::CXXLiteralOperatorName:
3451 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3452 break;
3453
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003454 case DeclarationName::CXXUsingDirective:
3455 // No extra data to emit
3456 break;
3457 }
3458}
Chris Lattnerca025db2010-05-07 21:43:38 +00003459
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003460void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003461 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003462 switch (Name.getNameKind()) {
3463 case DeclarationName::CXXConstructorName:
3464 case DeclarationName::CXXDestructorName:
3465 case DeclarationName::CXXConversionFunctionName:
3466 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3467 break;
3468
3469 case DeclarationName::CXXOperatorName:
3470 AddSourceLocation(
3471 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3472 Record);
3473 AddSourceLocation(
3474 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3475 Record);
3476 break;
3477
3478 case DeclarationName::CXXLiteralOperatorName:
3479 AddSourceLocation(
3480 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3481 Record);
3482 break;
3483
3484 case DeclarationName::Identifier:
3485 case DeclarationName::ObjCZeroArgSelector:
3486 case DeclarationName::ObjCOneArgSelector:
3487 case DeclarationName::ObjCMultiArgSelector:
3488 case DeclarationName::CXXUsingDirective:
3489 break;
3490 }
3491}
3492
3493void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003494 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003495 AddDeclarationName(NameInfo.getName(), Record);
3496 AddSourceLocation(NameInfo.getLoc(), Record);
3497 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3498}
3499
3500void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003501 RecordDataImpl &Record) {
Douglas Gregor14454802011-02-25 02:25:35 +00003502 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003503 Record.push_back(Info.NumTemplParamLists);
3504 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3505 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3506}
3507
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003508void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003509 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003510 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00003511 // typically accommodate the vast majority.
Chris Lattnerca025db2010-05-07 21:43:38 +00003512 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3513
3514 // Push each of the NNS's onto a stack for serialization in reverse order.
3515 while (NNS) {
3516 NestedNames.push_back(NNS);
3517 NNS = NNS->getPrefix();
3518 }
3519
3520 Record.push_back(NestedNames.size());
3521 while(!NestedNames.empty()) {
3522 NNS = NestedNames.pop_back_val();
3523 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3524 Record.push_back(Kind);
3525 switch (Kind) {
3526 case NestedNameSpecifier::Identifier:
3527 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3528 break;
3529
3530 case NestedNameSpecifier::Namespace:
3531 AddDeclRef(NNS->getAsNamespace(), Record);
3532 break;
3533
Douglas Gregor7b26ff92011-02-24 02:36:08 +00003534 case NestedNameSpecifier::NamespaceAlias:
3535 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
3536 break;
3537
Chris Lattnerca025db2010-05-07 21:43:38 +00003538 case NestedNameSpecifier::TypeSpec:
3539 case NestedNameSpecifier::TypeSpecWithTemplate:
3540 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3541 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3542 break;
3543
3544 case NestedNameSpecifier::Global:
3545 // Don't need to write an associated value.
3546 break;
3547 }
3548 }
3549}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003550
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003551void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
3552 RecordDataImpl &Record) {
3553 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattner57540c52011-04-15 05:22:18 +00003554 // typically accommodate the vast majority.
Douglas Gregora9d87bc2011-02-25 00:36:19 +00003555 llvm::SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
3556
3557 // Push each of the nested-name-specifiers's onto a stack for
3558 // serialization in reverse order.
3559 while (NNS) {
3560 NestedNames.push_back(NNS);
3561 NNS = NNS.getPrefix();
3562 }
3563
3564 Record.push_back(NestedNames.size());
3565 while(!NestedNames.empty()) {
3566 NNS = NestedNames.pop_back_val();
3567 NestedNameSpecifier::SpecifierKind Kind
3568 = NNS.getNestedNameSpecifier()->getKind();
3569 Record.push_back(Kind);
3570 switch (Kind) {
3571 case NestedNameSpecifier::Identifier:
3572 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
3573 AddSourceRange(NNS.getLocalSourceRange(), Record);
3574 break;
3575
3576 case NestedNameSpecifier::Namespace:
3577 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
3578 AddSourceRange(NNS.getLocalSourceRange(), Record);
3579 break;
3580
3581 case NestedNameSpecifier::NamespaceAlias:
3582 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
3583 AddSourceRange(NNS.getLocalSourceRange(), Record);
3584 break;
3585
3586 case NestedNameSpecifier::TypeSpec:
3587 case NestedNameSpecifier::TypeSpecWithTemplate:
3588 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3589 AddTypeLoc(NNS.getTypeLoc(), Record);
3590 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3591 break;
3592
3593 case NestedNameSpecifier::Global:
3594 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3595 break;
3596 }
3597 }
3598}
3599
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003600void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003601 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003602 Record.push_back(Kind);
3603 switch (Kind) {
3604 case TemplateName::Template:
3605 AddDeclRef(Name.getAsTemplateDecl(), Record);
3606 break;
3607
3608 case TemplateName::OverloadedTemplate: {
3609 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3610 Record.push_back(OvT->size());
3611 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3612 I != E; ++I)
3613 AddDeclRef(*I, Record);
3614 break;
3615 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003616
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003617 case TemplateName::QualifiedTemplate: {
3618 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3619 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3620 Record.push_back(QualT->hasTemplateKeyword());
3621 AddDeclRef(QualT->getTemplateDecl(), Record);
3622 break;
3623 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003624
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003625 case TemplateName::DependentTemplate: {
3626 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3627 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3628 Record.push_back(DepT->isIdentifier());
3629 if (DepT->isIdentifier())
3630 AddIdentifierRef(DepT->getIdentifier(), Record);
3631 else
3632 Record.push_back(DepT->getOperator());
3633 break;
3634 }
Douglas Gregor5590be02011-01-15 06:45:20 +00003635
3636 case TemplateName::SubstTemplateTemplateParmPack: {
3637 SubstTemplateTemplateParmPackStorage *SubstPack
3638 = Name.getAsSubstTemplateTemplateParmPack();
3639 AddDeclRef(SubstPack->getParameterPack(), Record);
3640 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
3641 break;
3642 }
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003643 }
3644}
3645
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003646void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003647 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003648 Record.push_back(Arg.getKind());
3649 switch (Arg.getKind()) {
3650 case TemplateArgument::Null:
3651 break;
3652 case TemplateArgument::Type:
3653 AddTypeRef(Arg.getAsType(), Record);
3654 break;
3655 case TemplateArgument::Declaration:
3656 AddDeclRef(Arg.getAsDecl(), Record);
3657 break;
3658 case TemplateArgument::Integral:
3659 AddAPSInt(*Arg.getAsIntegral(), Record);
3660 AddTypeRef(Arg.getIntegralType(), Record);
3661 break;
3662 case TemplateArgument::Template:
Douglas Gregore1d60df2011-01-14 23:41:42 +00003663 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3664 break;
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003665 case TemplateArgument::TemplateExpansion:
3666 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregore1d60df2011-01-14 23:41:42 +00003667 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3668 Record.push_back(*NumExpansions + 1);
3669 else
3670 Record.push_back(0);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003671 break;
3672 case TemplateArgument::Expression:
3673 AddStmt(Arg.getAsExpr());
3674 break;
3675 case TemplateArgument::Pack:
3676 Record.push_back(Arg.pack_size());
3677 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3678 I != E; ++I)
3679 AddTemplateArgument(*I, Record);
3680 break;
3681 }
3682}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003683
3684void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003685ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003686 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003687 assert(TemplateParams && "No TemplateParams!");
3688 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3689 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3690 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3691 Record.push_back(TemplateParams->size());
3692 for (TemplateParameterList::const_iterator
3693 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3694 P != PEnd; ++P)
3695 AddDeclRef(*P, Record);
3696}
3697
3698/// \brief Emit a template argument list.
3699void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003700ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003701 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003702 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003703 Record.push_back(TemplateArgs->size());
3704 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003705 AddTemplateArgument(TemplateArgs->get(i), Record);
3706}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003707
3708
3709void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003710ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003711 Record.push_back(Set.size());
3712 for (UnresolvedSetImpl::const_iterator
3713 I = Set.begin(), E = Set.end(); I != E; ++I) {
3714 AddDeclRef(I.getDecl(), Record);
3715 Record.push_back(I.getAccess());
3716 }
3717}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003718
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003719void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003720 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003721 Record.push_back(Base.isVirtual());
3722 Record.push_back(Base.isBaseOfClass());
3723 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redl08905022011-02-05 19:23:19 +00003724 Record.push_back(Base.getInheritConstructors());
Nick Lewycky19b9f952010-07-26 16:56:01 +00003725 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003726 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00003727 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3728 : SourceLocation(),
3729 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003730}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003731
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003732void ASTWriter::FlushCXXBaseSpecifiers() {
3733 RecordData Record;
3734 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3735 Record.clear();
3736
3737 // Record the offset of this base-specifier set.
3738 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3739 if (Index == CXXBaseSpecifiersOffsets.size())
3740 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3741 else {
3742 if (Index > CXXBaseSpecifiersOffsets.size())
3743 CXXBaseSpecifiersOffsets.resize(Index + 1);
3744 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3745 }
3746
3747 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3748 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3749 Record.push_back(BEnd - B);
3750 for (; B != BEnd; ++B)
3751 AddCXXBaseSpecifier(*B, Record);
3752 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00003753
3754 // Flush any expressions that were written as part of the base specifiers.
3755 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003756 }
3757
3758 CXXBaseSpecifiersToWrite.clear();
3759}
3760
Alexis Hunt1d792652011-01-08 20:30:50 +00003761void ASTWriter::AddCXXCtorInitializers(
3762 const CXXCtorInitializer * const *CtorInitializers,
3763 unsigned NumCtorInitializers,
3764 RecordDataImpl &Record) {
3765 Record.push_back(NumCtorInitializers);
3766 for (unsigned i=0; i != NumCtorInitializers; ++i) {
3767 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003768
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003769 if (Init->isBaseInitializer()) {
Alexis Hunt37a477f2011-05-04 01:19:08 +00003770 Record.push_back(CTOR_INITIALIZER_BASE);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003771 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3772 Record.push_back(Init->isBaseVirtual());
Alexis Hunt37a477f2011-05-04 01:19:08 +00003773 } else if (Init->isDelegatingInitializer()) {
3774 Record.push_back(CTOR_INITIALIZER_DELEGATING);
3775 AddDeclRef(Init->getTargetConstructor(), Record);
3776 } else if (Init->isMemberInitializer()){
3777 Record.push_back(CTOR_INITIALIZER_MEMBER);
3778 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003779 } else {
Alexis Hunt37a477f2011-05-04 01:19:08 +00003780 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
3781 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003782 }
Francois Pichetd583da02010-12-04 09:14:42 +00003783
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003784 AddSourceLocation(Init->getMemberLocation(), Record);
3785 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003786 AddSourceLocation(Init->getLParenLoc(), Record);
3787 AddSourceLocation(Init->getRParenLoc(), Record);
3788 Record.push_back(Init->isWritten());
3789 if (Init->isWritten()) {
3790 Record.push_back(Init->getSourceOrder());
3791 } else {
3792 Record.push_back(Init->getNumArrayIndices());
3793 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3794 AddDeclRef(Init->getArrayIndex(i), Record);
3795 }
3796 }
3797}
3798
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003799void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3800 assert(D->DefinitionData);
3801 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3802 Record.push_back(Data.UserDeclaredConstructor);
3803 Record.push_back(Data.UserDeclaredCopyConstructor);
3804 Record.push_back(Data.UserDeclaredCopyAssignment);
3805 Record.push_back(Data.UserDeclaredDestructor);
3806 Record.push_back(Data.Aggregate);
3807 Record.push_back(Data.PlainOldData);
3808 Record.push_back(Data.Empty);
3809 Record.push_back(Data.Polymorphic);
3810 Record.push_back(Data.Abstract);
Chandler Carruth583edf82011-04-30 10:07:30 +00003811 Record.push_back(Data.IsStandardLayout);
Chandler Carruthb1963742011-04-30 09:17:45 +00003812 Record.push_back(Data.HasNoNonEmptyBases);
3813 Record.push_back(Data.HasPrivateFields);
3814 Record.push_back(Data.HasProtectedFields);
3815 Record.push_back(Data.HasPublicFields);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003816 Record.push_back(Data.HasTrivialConstructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00003817 Record.push_back(Data.HasConstExprNonCopyMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003818 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruthad7d4042011-04-23 23:10:33 +00003819 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003820 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruthad7d4042011-04-23 23:10:33 +00003821 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003822 Record.push_back(Data.HasTrivialDestructor);
Chandler Carruthe71d0622011-04-24 02:49:34 +00003823 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003824 Record.push_back(Data.ComputedVisibleConversions);
3825 Record.push_back(Data.DeclaredDefaultConstructor);
3826 Record.push_back(Data.DeclaredCopyConstructor);
3827 Record.push_back(Data.DeclaredCopyAssignment);
3828 Record.push_back(Data.DeclaredDestructor);
3829
3830 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003831 if (Data.NumBases > 0)
3832 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3833 Record);
3834
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003835 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3836 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003837 if (Data.NumVBases > 0)
3838 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3839 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003840
3841 AddUnresolvedSet(Data.Conversions, Record);
3842 AddUnresolvedSet(Data.VisibleConversions, Record);
3843 // Data.Definition is the owning decl, no need to write it.
3844 AddDeclRef(Data.FirstFriend, Record);
3845}
3846
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003847void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00003848 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003849 assert(!Chain && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00003850 assert(FirstDeclID == NextDeclID &&
3851 FirstTypeID == NextTypeID &&
3852 FirstIdentID == NextIdentID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00003853 FirstSelectorID == NextSelectorID &&
Douglas Gregor91096292010-10-02 19:29:26 +00003854 FirstMacroID == NextMacroID &&
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003855 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00003856 "Setting chain after writing has started.");
3857 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003858
3859 FirstDeclID += Chain->getTotalNumDecls();
3860 FirstTypeID += Chain->getTotalNumTypes();
3861 FirstIdentID += Chain->getTotalNumIdentifiers();
3862 FirstSelectorID += Chain->getTotalNumSelectors();
3863 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003864 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003865 NextDeclID = FirstDeclID;
3866 NextTypeID = FirstTypeID;
3867 NextIdentID = FirstIdentID;
3868 NextSelectorID = FirstSelectorID;
3869 NextMacroID = FirstMacroID;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003870 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00003871}
3872
Sebastian Redl539c5062010-08-18 23:57:32 +00003873void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00003874 IdentifierIDs[II] = ID;
Douglas Gregor68051a72011-02-11 00:26:14 +00003875 if (II->hasMacroDefinition())
3876 DeserializedMacroNames.push_back(II);
Sebastian Redlff4a2952010-07-23 23:49:55 +00003877}
3878
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003879void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003880 // Always take the highest-numbered type index. This copes with an interesting
3881 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003882 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003883 // keep the higher-numbered entry so that we can properly write it out to
3884 // the AST file.
3885 TypeIdx &StoredIdx = TypeIdxs[T];
3886 if (Idx.getIndex() >= StoredIdx.getIndex())
3887 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003888}
3889
Sebastian Redl539c5062010-08-18 23:57:32 +00003890void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003891 DeclIDs[D] = ID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003892}
Sebastian Redl834bb972010-08-04 17:20:04 +00003893
Sebastian Redl539c5062010-08-18 23:57:32 +00003894void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003895 SelectorIDs[S] = ID;
3896}
Douglas Gregor91096292010-10-02 19:29:26 +00003897
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003898void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00003899 MacroDefinition *MD) {
3900 MacroDefinitions[MD] = ID;
3901}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003902
3903void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3904 assert(D->isDefinition());
3905 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3906 // We are interested when a PCH decl is modified.
3907 if (RD->getPCHLevel() > 0) {
3908 // A forward reference was mutated into a definition. Rewrite it.
3909 // FIXME: This happens during template instantiation, should we
3910 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003911 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003912 }
3913
3914 for (CXXRecordDecl::redecl_iterator
3915 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3916 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3917 if (Redecl == RD)
3918 continue;
3919
3920 // We are interested when a PCH decl is modified.
3921 if (Redecl->getPCHLevel() > 0) {
3922 UpdateRecord &Record = DeclUpdates[Redecl];
3923 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3924 assert(Redecl->DefinitionData);
3925 assert(Redecl->DefinitionData->Definition == D);
3926 AddDeclRef(D, Record); // the DefinitionDecl
3927 }
3928 }
3929 }
3930}
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003931void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3932 // TU and namespaces are handled elsewhere.
3933 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3934 return;
3935
3936 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3937 return; // Not a source decl added to a DeclContext from PCH.
3938
3939 AddUpdatedDeclContext(DC);
3940}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003941
3942void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3943 assert(D->isImplicit());
3944 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3945 return; // Not a source member added to a class from PCH.
3946 if (!isa<CXXMethodDecl>(D))
3947 return; // We are interested in lazily declared implicit methods.
3948
3949 // A decl coming from PCH was modified.
3950 assert(RD->isDefinition());
3951 UpdateRecord &Record = DeclUpdates[RD];
3952 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3953 AddDeclRef(D, Record);
3954}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003955
3956void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3957 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00003958 // The specializations set is kept in the canonical template.
3959 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003960 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3961 return; // Not a source specialization added to a template from PCH.
3962
3963 UpdateRecord &Record = DeclUpdates[TD];
3964 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3965 AddDeclRef(D, Record);
3966}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003967
Sebastian Redl9ab988f2011-04-14 14:07:59 +00003968void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
3969 const FunctionDecl *D) {
3970 // The specializations set is kept in the canonical template.
3971 TD = TD->getCanonicalDecl();
3972 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3973 return; // Not a source specialization added to a template from PCH.
3974
3975 UpdateRecord &Record = DeclUpdates[TD];
3976 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3977 AddDeclRef(D, Record);
3978}
3979
Sebastian Redlab238a72011-04-24 16:28:06 +00003980void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
3981 if (D->getPCHLevel() == 0)
3982 return; // Declaration not imported from PCH.
3983
3984 // Implicit decl from a PCH was defined.
3985 // FIXME: Should implicit definition be a separate FunctionDecl?
3986 RewriteDecl(D);
3987}
3988
Sebastian Redl2ac2c722011-04-29 08:19:30 +00003989void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
3990 if (D->getPCHLevel() == 0)
3991 return;
3992
3993 // Since the actual instantiation is delayed, this really means that we need
3994 // to update the instantiation location.
3995 UpdateRecord &Record = DeclUpdates[D];
3996 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
3997 AddSourceLocation(
3998 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
3999}
4000
Douglas Gregorf88e35b2010-11-30 06:16:57 +00004001ASTSerializationListener::~ASTSerializationListener() { }