blob: bf07c52335b0fdd7c66b835e420d009c7c6f462d [file] [log] [blame]
Sebastian Redld6522cf2010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redl55c0ad52010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregoref84c4b2009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl1914c6f2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Douglas Gregorf88e35b2010-11-30 06:16:57 +000015#include "clang/Serialization/ASTSerializationListener.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000016#include "ASTCommon.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000017#include "clang/Sema/Sema.h"
18#include "clang/Sema/IdentifierResolver.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclContextInternals.h"
John McCall19c1bfd2010-08-25 05:32:35 +000022#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000023#include "clang/AST/DeclFriend.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000024#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000025#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000026#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000027#include "clang/AST/TypeLocVisitor.h"
Sebastian Redlf5b13462010-08-18 23:57:17 +000028#include "clang/Serialization/ASTReader.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000029#include "clang/Lex/MacroInfo.h"
Douglas Gregoraae92242010-03-19 21:51:54 +000030#include "clang/Lex/PreprocessingRecord.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000031#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000032#include "clang/Lex/HeaderSearch.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000033#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000034#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000035#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000036#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000037#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000038#include "clang/Basic/TargetInfo.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000039#include "clang/Basic/Version.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000040#include "llvm/ADT/APFloat.h"
41#include "llvm/ADT/APInt.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000042#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000043#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencer740857f2010-12-21 16:45:57 +000044#include "llvm/Support/FileSystem.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000045#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000046#include "llvm/Support/Path.h"
Chris Lattner225dd6c2009-04-11 18:40:46 +000047#include <cstdio>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000048using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000049using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000050
Sebastian Redl3df5a082010-07-30 17:03:48 +000051template <typename T, typename Allocator>
52T *data(std::vector<T, Allocator> &v) {
53 return v.empty() ? 0 : &v.front();
54}
55template <typename T, typename Allocator>
56const T *data(const std::vector<T, Allocator> &v) {
57 return v.empty() ? 0 : &v.front();
58}
59
Douglas Gregoref84c4b2009-04-09 22:27:44 +000060//===----------------------------------------------------------------------===//
61// Type serialization
62//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000063
Douglas Gregoref84c4b2009-04-09 22:27:44 +000064namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000065 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000066 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000067 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000068
69 public:
70 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000071 TypeCode Code;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000072
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000073 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000074 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000075
76 void VisitArrayType(const ArrayType *T);
77 void VisitFunctionType(const FunctionType *T);
78 void VisitTagType(const TagType *T);
79
80#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
81#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000082#include "clang/AST/TypeNodes.def"
83 };
84}
85
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000086void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000087 assert(false && "Built-in types are never serialized");
88}
89
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000090void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000091 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000092 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000093}
94
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000095void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000096 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000097 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000098}
99
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000100void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000101 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000102 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000103}
104
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000105void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000106 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000107 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000108}
109
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000110void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000111 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000112 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000113}
114
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000115void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000116 Writer.AddTypeRef(T->getPointeeType(), Record);
117 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000118 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000119}
120
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000121void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000122 Writer.AddTypeRef(T->getElementType(), Record);
123 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000124 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000125}
126
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000127void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000128 VisitArrayType(T);
129 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000130 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000131}
132
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000133void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000134 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000135 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000136}
137
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000138void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000139 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000140 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
141 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000142 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000143 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000144}
145
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000146void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000147 Writer.AddTypeRef(T->getElementType(), Record);
148 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000149 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000150 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000151}
152
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000153void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000154 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000155 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000156}
157
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000158void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000159 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000160 FunctionType::ExtInfo C = T->getExtInfo();
161 Record.push_back(C.getNoReturn());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000162 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000163 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000164 Record.push_back(C.getCC());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000165}
166
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000167void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000168 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000169 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000170}
171
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000172void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000173 VisitFunctionType(T);
174 Record.push_back(T->getNumArgs());
175 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
176 Writer.AddTypeRef(T->getArgType(I), Record);
177 Record.push_back(T->isVariadic());
178 Record.push_back(T->getTypeQuals());
Sebastian Redl5068f77ac2009-05-27 22:11:52 +0000179 Record.push_back(T->hasExceptionSpec());
180 Record.push_back(T->hasAnyExceptionSpec());
181 Record.push_back(T->getNumExceptions());
182 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
183 Writer.AddTypeRef(T->getExceptionType(I), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000184 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000185}
186
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000187void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000188 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000189 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000190}
John McCallb96ec562009-12-04 22:46:56 +0000191
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000192void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000193 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000194 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
195 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000196 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000197}
198
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000199void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000200 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000201 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000202}
203
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000204void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000205 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000206 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000207}
208
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000209void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson81df7b82009-06-24 19:06:50 +0000210 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000211 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000212}
213
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000214void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000215 Record.push_back(T->isDependentType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000216 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000217 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000218 "Cannot serialize in the middle of a type definition");
219}
220
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000221void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000222 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000223 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000224}
225
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000226void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000227 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000228 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000229}
230
Mike Stump11289f42009-09-09 15:08:12 +0000231void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000232ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000233 const SubstTemplateTypeParmType *T) {
234 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
235 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000236 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000237}
238
239void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000240ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000241 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000242 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000243 Writer.AddTemplateName(T->getTemplateName(), Record);
244 Record.push_back(T->getNumArgs());
245 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
246 ArgI != ArgE; ++ArgI)
247 Writer.AddTemplateArgument(*ArgI, Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000248 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
249 : T->getCanonicalTypeInternal(),
250 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000251 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000252}
253
254void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000255ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000256 VisitArrayType(T);
257 Writer.AddStmt(T->getSizeExpr());
258 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000259 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000260}
261
262void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000263ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000264 const DependentSizedExtVectorType *T) {
265 // FIXME: Serialize this type (C++ only)
266 assert(false && "Cannot serialize dependent sized extended vector types");
267}
268
269void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000270ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000271 Record.push_back(T->getDepth());
272 Record.push_back(T->getIndex());
273 Record.push_back(T->isParameterPack());
274 Writer.AddIdentifierRef(T->getName(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000275 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000276}
277
278void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000279ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000280 Record.push_back(T->getKeyword());
281 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
282 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000283 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
284 : T->getCanonicalTypeInternal(),
285 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000286 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000287}
288
289void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000290ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000291 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000292 Record.push_back(T->getKeyword());
293 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
294 Writer.AddIdentifierRef(T->getIdentifier(), Record);
295 Record.push_back(T->getNumArgs());
296 for (DependentTemplateSpecializationType::iterator
297 I = T->begin(), E = T->end(); I != E; ++I)
298 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000299 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000300}
301
Douglas Gregord2fa7662010-12-20 02:24:11 +0000302void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
303 Writer.AddTypeRef(T->getPattern(), Record);
304 Code = TYPE_PACK_EXPANSION;
305}
306
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000307void ASTTypeWriter::VisitParenType(const ParenType *T) {
308 Writer.AddTypeRef(T->getInnerType(), Record);
309 Code = TYPE_PAREN;
310}
311
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000312void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000313 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000314 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
315 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000316 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000317}
318
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000319void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCalle78aac42010-03-10 03:28:59 +0000320 Writer.AddDeclRef(T->getDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000321 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000322 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000323}
324
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000325void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1c283312010-08-11 12:19:30 +0000326 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000327 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000328}
329
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000330void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000331 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000332 Record.push_back(T->getNumProtocols());
John McCall8b07ec22010-05-15 11:32:37 +0000333 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000334 E = T->qual_end(); I != E; ++I)
335 Writer.AddDeclRef(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000336 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000337}
338
Steve Narofffb4330f2009-06-17 22:40:22 +0000339void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000340ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000341 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000342 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000343}
344
John McCall8f115c62009-10-16 21:56:05 +0000345namespace {
346
347class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000348 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000349 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000350
351public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000352 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000353 : Writer(Writer), Record(Record) { }
354
John McCall17001972009-10-18 01:05:36 +0000355#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000356#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000357 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000358#include "clang/AST/TypeLocNodes.def"
359
John McCall17001972009-10-18 01:05:36 +0000360 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
361 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000362};
363
364}
365
John McCall17001972009-10-18 01:05:36 +0000366void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
367 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000368}
John McCall17001972009-10-18 01:05:36 +0000369void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000370 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
371 if (TL.needsExtraLocalData()) {
372 Record.push_back(TL.getWrittenTypeSpec());
373 Record.push_back(TL.getWrittenSignSpec());
374 Record.push_back(TL.getWrittenWidthSpec());
375 Record.push_back(TL.hasModeAttr());
376 }
John McCall8f115c62009-10-16 21:56:05 +0000377}
John McCall17001972009-10-18 01:05:36 +0000378void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
379 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000380}
John McCall17001972009-10-18 01:05:36 +0000381void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
382 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000383}
John McCall17001972009-10-18 01:05:36 +0000384void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
385 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000386}
John McCall17001972009-10-18 01:05:36 +0000387void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
388 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000389}
John McCall17001972009-10-18 01:05:36 +0000390void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
391 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000392}
John McCall17001972009-10-18 01:05:36 +0000393void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
394 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000395}
John McCall17001972009-10-18 01:05:36 +0000396void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
397 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
398 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
399 Record.push_back(TL.getSizeExpr() ? 1 : 0);
400 if (TL.getSizeExpr())
401 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000402}
John McCall17001972009-10-18 01:05:36 +0000403void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
404 VisitArrayTypeLoc(TL);
405}
406void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
407 VisitArrayTypeLoc(TL);
408}
409void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
410 VisitArrayTypeLoc(TL);
411}
412void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
413 DependentSizedArrayTypeLoc TL) {
414 VisitArrayTypeLoc(TL);
415}
416void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
417 DependentSizedExtVectorTypeLoc TL) {
418 Writer.AddSourceLocation(TL.getNameLoc(), Record);
419}
420void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
421 Writer.AddSourceLocation(TL.getNameLoc(), Record);
422}
423void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
424 Writer.AddSourceLocation(TL.getNameLoc(), Record);
425}
426void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
427 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
428 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Douglas Gregor7fb25412010-10-01 18:44:50 +0000429 Record.push_back(TL.getTrailingReturn());
John McCall17001972009-10-18 01:05:36 +0000430 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
431 Writer.AddDeclRef(TL.getArg(i), Record);
432}
433void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
434 VisitFunctionTypeLoc(TL);
435}
436void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
437 VisitFunctionTypeLoc(TL);
438}
John McCallb96ec562009-12-04 22:46:56 +0000439void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
440 Writer.AddSourceLocation(TL.getNameLoc(), Record);
441}
John McCall17001972009-10-18 01:05:36 +0000442void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
443 Writer.AddSourceLocation(TL.getNameLoc(), Record);
444}
445void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000446 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
447 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
448 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000449}
450void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000451 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
452 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
453 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
454 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000455}
456void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
457 Writer.AddSourceLocation(TL.getNameLoc(), Record);
458}
459void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
460 Writer.AddSourceLocation(TL.getNameLoc(), Record);
461}
462void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
463 Writer.AddSourceLocation(TL.getNameLoc(), Record);
464}
John McCall17001972009-10-18 01:05:36 +0000465void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
466 Writer.AddSourceLocation(TL.getNameLoc(), Record);
467}
John McCallcebee162009-10-18 09:09:24 +0000468void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
469 SubstTemplateTypeParmTypeLoc TL) {
470 Writer.AddSourceLocation(TL.getNameLoc(), Record);
471}
John McCall17001972009-10-18 01:05:36 +0000472void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
473 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +0000474 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
475 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
476 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
477 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000478 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
479 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000480}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000481void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
482 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
483 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
484}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000485void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000486 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
487 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall17001972009-10-18 01:05:36 +0000488}
John McCalle78aac42010-03-10 03:28:59 +0000489void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
490 Writer.AddSourceLocation(TL.getNameLoc(), Record);
491}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000492void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000493 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
494 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall17001972009-10-18 01:05:36 +0000495 Writer.AddSourceLocation(TL.getNameLoc(), Record);
496}
John McCallc392f372010-06-11 00:33:02 +0000497void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
498 DependentTemplateSpecializationTypeLoc TL) {
499 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
500 Writer.AddSourceRange(TL.getQualifierRange(), Record);
501 Writer.AddSourceLocation(TL.getNameLoc(), Record);
502 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
503 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
504 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000505 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
506 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000507}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000508void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
509 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
510}
John McCall17001972009-10-18 01:05:36 +0000511void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
512 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000513}
514void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
515 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000516 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
517 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
518 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
519 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000520}
John McCallfc93cf92009-10-22 22:37:11 +0000521void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
522 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000523}
John McCall8f115c62009-10-16 21:56:05 +0000524
Chris Lattner19cea4e2009-04-22 05:57:30 +0000525//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000526// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000527//===----------------------------------------------------------------------===//
528
Chris Lattner28fa4e62009-04-26 22:26:21 +0000529static void EmitBlockID(unsigned ID, const char *Name,
530 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000531 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000532 Record.clear();
533 Record.push_back(ID);
534 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
535
536 // Emit the block name if present.
537 if (Name == 0 || Name[0] == 0) return;
538 Record.clear();
539 while (*Name)
540 Record.push_back(*Name++);
541 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
542}
543
544static void EmitRecordID(unsigned ID, const char *Name,
545 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000546 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000547 Record.clear();
548 Record.push_back(ID);
549 while (*Name)
550 Record.push_back(*Name++);
551 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000552}
553
554static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000555 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000556#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000557 RECORD(STMT_STOP);
558 RECORD(STMT_NULL_PTR);
559 RECORD(STMT_NULL);
560 RECORD(STMT_COMPOUND);
561 RECORD(STMT_CASE);
562 RECORD(STMT_DEFAULT);
563 RECORD(STMT_LABEL);
564 RECORD(STMT_IF);
565 RECORD(STMT_SWITCH);
566 RECORD(STMT_WHILE);
567 RECORD(STMT_DO);
568 RECORD(STMT_FOR);
569 RECORD(STMT_GOTO);
570 RECORD(STMT_INDIRECT_GOTO);
571 RECORD(STMT_CONTINUE);
572 RECORD(STMT_BREAK);
573 RECORD(STMT_RETURN);
574 RECORD(STMT_DECL);
575 RECORD(STMT_ASM);
576 RECORD(EXPR_PREDEFINED);
577 RECORD(EXPR_DECL_REF);
578 RECORD(EXPR_INTEGER_LITERAL);
579 RECORD(EXPR_FLOATING_LITERAL);
580 RECORD(EXPR_IMAGINARY_LITERAL);
581 RECORD(EXPR_STRING_LITERAL);
582 RECORD(EXPR_CHARACTER_LITERAL);
583 RECORD(EXPR_PAREN);
584 RECORD(EXPR_UNARY_OPERATOR);
585 RECORD(EXPR_SIZEOF_ALIGN_OF);
586 RECORD(EXPR_ARRAY_SUBSCRIPT);
587 RECORD(EXPR_CALL);
588 RECORD(EXPR_MEMBER);
589 RECORD(EXPR_BINARY_OPERATOR);
590 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
591 RECORD(EXPR_CONDITIONAL_OPERATOR);
592 RECORD(EXPR_IMPLICIT_CAST);
593 RECORD(EXPR_CSTYLE_CAST);
594 RECORD(EXPR_COMPOUND_LITERAL);
595 RECORD(EXPR_EXT_VECTOR_ELEMENT);
596 RECORD(EXPR_INIT_LIST);
597 RECORD(EXPR_DESIGNATED_INIT);
598 RECORD(EXPR_IMPLICIT_VALUE_INIT);
599 RECORD(EXPR_VA_ARG);
600 RECORD(EXPR_ADDR_LABEL);
601 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000602 RECORD(EXPR_CHOOSE);
603 RECORD(EXPR_GNU_NULL);
604 RECORD(EXPR_SHUFFLE_VECTOR);
605 RECORD(EXPR_BLOCK);
606 RECORD(EXPR_BLOCK_DECL_REF);
607 RECORD(EXPR_OBJC_STRING_LITERAL);
608 RECORD(EXPR_OBJC_ENCODE);
609 RECORD(EXPR_OBJC_SELECTOR_EXPR);
610 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
611 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
612 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
613 RECORD(EXPR_OBJC_KVC_REF_EXPR);
614 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000615 RECORD(STMT_OBJC_FOR_COLLECTION);
616 RECORD(STMT_OBJC_CATCH);
617 RECORD(STMT_OBJC_FINALLY);
618 RECORD(STMT_OBJC_AT_TRY);
619 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
620 RECORD(STMT_OBJC_AT_THROW);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000621 RECORD(EXPR_CXX_OPERATOR_CALL);
622 RECORD(EXPR_CXX_CONSTRUCT);
623 RECORD(EXPR_CXX_STATIC_CAST);
624 RECORD(EXPR_CXX_DYNAMIC_CAST);
625 RECORD(EXPR_CXX_REINTERPRET_CAST);
626 RECORD(EXPR_CXX_CONST_CAST);
627 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
628 RECORD(EXPR_CXX_BOOL_LITERAL);
629 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000630#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000631}
Mike Stump11289f42009-09-09 15:08:12 +0000632
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000633void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000634 RecordData Record;
635 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000636
Sebastian Redl539c5062010-08-18 23:57:32 +0000637#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
638#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000639
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000640 // AST Top-Level Block.
Sebastian Redlf1642042010-08-18 23:57:22 +0000641 BLOCK(AST_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000642 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000643 RECORD(TYPE_OFFSET);
644 RECORD(DECL_OFFSET);
645 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000646 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000647 RECORD(IDENTIFIER_OFFSET);
648 RECORD(IDENTIFIER_TABLE);
649 RECORD(EXTERNAL_DEFINITIONS);
650 RECORD(SPECIAL_TYPES);
651 RECORD(STATISTICS);
652 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000653 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000654 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
655 RECORD(SELECTOR_OFFSETS);
656 RECORD(METHOD_POOL);
657 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000658 RECORD(SOURCE_LOCATION_OFFSETS);
659 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000660 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000661 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek17437132010-01-22 20:59:36 +0000662 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregoraae92242010-03-19 21:51:54 +0000663 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redl595c5132010-07-08 22:01:51 +0000664 RECORD(CHAINED_METADATA);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000665 RECORD(REFERENCED_SELECTOR_POOL);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000666
Chris Lattner28fa4e62009-04-26 22:26:21 +0000667 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000668 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000669 RECORD(SM_SLOC_FILE_ENTRY);
670 RECORD(SM_SLOC_BUFFER_ENTRY);
671 RECORD(SM_SLOC_BUFFER_BLOB);
672 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
673 RECORD(SM_LINE_TABLE);
Mike Stump11289f42009-09-09 15:08:12 +0000674
Chris Lattner28fa4e62009-04-26 22:26:21 +0000675 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000676 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000677 RECORD(PP_MACRO_OBJECT_LIKE);
678 RECORD(PP_MACRO_FUNCTION_LIKE);
679 RECORD(PP_TOKEN);
Douglas Gregoraae92242010-03-19 21:51:54 +0000680 RECORD(PP_MACRO_INSTANTIATION);
681 RECORD(PP_MACRO_DEFINITION);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000682
Douglas Gregor12bfa382009-10-17 00:13:19 +0000683 // Decls and Types block.
684 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000685 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000686 RECORD(TYPE_COMPLEX);
687 RECORD(TYPE_POINTER);
688 RECORD(TYPE_BLOCK_POINTER);
689 RECORD(TYPE_LVALUE_REFERENCE);
690 RECORD(TYPE_RVALUE_REFERENCE);
691 RECORD(TYPE_MEMBER_POINTER);
692 RECORD(TYPE_CONSTANT_ARRAY);
693 RECORD(TYPE_INCOMPLETE_ARRAY);
694 RECORD(TYPE_VARIABLE_ARRAY);
695 RECORD(TYPE_VECTOR);
696 RECORD(TYPE_EXT_VECTOR);
697 RECORD(TYPE_FUNCTION_PROTO);
698 RECORD(TYPE_FUNCTION_NO_PROTO);
699 RECORD(TYPE_TYPEDEF);
700 RECORD(TYPE_TYPEOF_EXPR);
701 RECORD(TYPE_TYPEOF);
702 RECORD(TYPE_RECORD);
703 RECORD(TYPE_ENUM);
704 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000705 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000706 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000707 RECORD(DECL_TRANSLATION_UNIT);
708 RECORD(DECL_TYPEDEF);
709 RECORD(DECL_ENUM);
710 RECORD(DECL_RECORD);
711 RECORD(DECL_ENUM_CONSTANT);
712 RECORD(DECL_FUNCTION);
713 RECORD(DECL_OBJC_METHOD);
714 RECORD(DECL_OBJC_INTERFACE);
715 RECORD(DECL_OBJC_PROTOCOL);
716 RECORD(DECL_OBJC_IVAR);
717 RECORD(DECL_OBJC_AT_DEFS_FIELD);
718 RECORD(DECL_OBJC_CLASS);
719 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
720 RECORD(DECL_OBJC_CATEGORY);
721 RECORD(DECL_OBJC_CATEGORY_IMPL);
722 RECORD(DECL_OBJC_IMPLEMENTATION);
723 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
724 RECORD(DECL_OBJC_PROPERTY);
725 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000726 RECORD(DECL_FIELD);
727 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000728 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000729 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000730 RECORD(DECL_FILE_SCOPE_ASM);
731 RECORD(DECL_BLOCK);
732 RECORD(DECL_CONTEXT_LEXICAL);
733 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor12bfa382009-10-17 00:13:19 +0000734 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000735 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000736#undef RECORD
737#undef BLOCK
738 Stream.ExitBlock();
739}
740
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000741/// \brief Adjusts the given filename to only write out the portion of the
742/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000743///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000744/// \param Filename the file name to adjust.
745///
746/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
747/// the returned filename will be adjusted by this system root.
748///
749/// \returns either the original filename (if it needs no adjustment) or the
750/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000751static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000752adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
753 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000754
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000755 if (!isysroot)
756 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000757
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000758 // Verify that the filename and the system root have the same prefix.
759 unsigned Pos = 0;
760 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
761 if (Filename[Pos] != isysroot[Pos])
762 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000763
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000764 // We hit the end of the filename before we hit the end of the system root.
765 if (!Filename[Pos])
766 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000767
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000768 // If the file name has a '/' at the current position, skip over the '/'.
769 // We distinguish sysroot-based includes from absolute includes by the
770 // absence of '/' at the beginning of sysroot-based includes.
771 if (Filename[Pos] == '/')
772 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000773
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000774 return Filename + Pos;
775}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000776
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000777/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000778void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000779 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000780
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000781 // Metadata
782 const TargetInfo &Target = Context.Target;
783 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000784 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl539c5062010-08-18 23:57:32 +0000785 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000786 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
787 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000788 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
789 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
790 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000791 // Target triple or chained PCH name
792 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000793 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000794
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000795 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000796 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
797 Record.push_back(VERSION_MAJOR);
798 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000799 Record.push_back(CLANG_VERSION_MAJOR);
800 Record.push_back(CLANG_VERSION_MINOR);
801 Record.push_back(isysroot != 0);
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000802 // FIXME: This writes the absolute path for chained headers.
803 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
804 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump11289f42009-09-09 15:08:12 +0000805
Douglas Gregor45fe0362009-05-12 01:31:05 +0000806 // Original file name
807 SourceManager &SM = Context.getSourceManager();
808 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
809 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000810 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregor45fe0362009-05-12 01:31:05 +0000811 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
812 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
813
Michael J. Spencer740857f2010-12-21 16:45:57 +0000814 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +0000815
Michael J. Spencer740857f2010-12-21 16:45:57 +0000816 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000817
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +0000818 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000819 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000820 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000821 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000822 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000823 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000824 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000825
Ted Kremenek18e066f2010-01-22 22:12:47 +0000826 // Repository branch/version information.
827 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000828 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenek18e066f2010-01-22 22:12:47 +0000829 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
830 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000831 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +0000832 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +0000833 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
834 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000835}
836
837/// \brief Write the LangOptions structure.
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000838void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor55abb232009-04-10 20:39:37 +0000839 RecordData Record;
840 Record.push_back(LangOpts.Trigraphs);
841 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
842 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
843 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
844 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carruthe03aa552010-04-17 20:17:31 +0000845 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor55abb232009-04-10 20:39:37 +0000846 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
847 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
848 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
849 Record.push_back(LangOpts.C99); // C99 Support
850 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +0000851 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
852 // already saved elsewhere.
Douglas Gregor55abb232009-04-10 20:39:37 +0000853 Record.push_back(LangOpts.CPlusPlus); // C++ Support
854 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +0000855 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +0000856
Douglas Gregor55abb232009-04-10 20:39:37 +0000857 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
858 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000859 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian45878032010-02-09 19:31:38 +0000860 // modern abi enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000861 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian45878032010-02-09 19:31:38 +0000862 // modern abi enabled.
Fariborz Jahanian62c56022010-04-22 21:01:59 +0000863 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump11289f42009-09-09 15:08:12 +0000864
Douglas Gregor55abb232009-04-10 20:39:37 +0000865 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +0000866 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
867 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +0000868 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +0000869 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar925152c2010-02-10 18:48:44 +0000870 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor55abb232009-04-10 20:39:37 +0000871
872 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
873 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
874 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
875
Chris Lattner258172e2009-04-27 07:35:58 +0000876 // Whether static initializers are protected by locks.
877 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +0000878 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +0000879 Record.push_back(LangOpts.Blocks); // block extension to C
880 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
881 // they are unused.
882 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
883 // (modulo the platform support).
884
Chris Lattner51924e512010-06-26 21:25:03 +0000885 Record.push_back(LangOpts.getSignedOverflowBehavior());
886 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor55abb232009-04-10 20:39:37 +0000887
888 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +0000889 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +0000890 // defined.
891 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
892 // opposed to __DYNAMIC__).
893 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
894
895 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
896 // used (instead of C99 semantics).
897 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +0000898 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
899 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000900 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
901 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +0000902 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor55abb232009-04-10 20:39:37 +0000903 Record.push_back(LangOpts.getGCMode());
904 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000905 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +0000906 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +0000907 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +0000908 Record.push_back(LangOpts.CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +0000909 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson9cedbef2009-08-22 22:30:33 +0000910 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +0000911 Record.push_back(LangOpts.SpellChecking);
Sebastian Redl539c5062010-08-18 23:57:32 +0000912 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +0000913}
914
Douglas Gregora7f71a92009-04-10 03:52:48 +0000915//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +0000916// stat cache Serialization
917//===----------------------------------------------------------------------===//
918
919namespace {
920// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000921class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +0000922public:
923 typedef const char * key_type;
924 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000925
Chris Lattner2a6fa472010-11-23 19:28:12 +0000926 typedef struct stat data_type;
927 typedef const data_type &data_type_ref;
Douglas Gregorc5046832009-04-27 18:38:38 +0000928
929 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000930 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000931 }
Mike Stump11289f42009-09-09 15:08:12 +0000932
933 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +0000934 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
935 data_type_ref Data) {
936 unsigned StrLen = strlen(path);
937 clang::io::Emit16(Out, StrLen);
Chris Lattner2a6fa472010-11-23 19:28:12 +0000938 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregorc5046832009-04-27 18:38:38 +0000939 clang::io::Emit8(Out, DataLen);
940 return std::make_pair(StrLen + 1, DataLen);
941 }
Mike Stump11289f42009-09-09 15:08:12 +0000942
Douglas Gregorc5046832009-04-27 18:38:38 +0000943 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
944 Out.write(path, KeyLen);
945 }
Mike Stump11289f42009-09-09 15:08:12 +0000946
Chris Lattner2a6fa472010-11-23 19:28:12 +0000947 void EmitData(llvm::raw_ostream &Out, key_type_ref,
Douglas Gregorc5046832009-04-27 18:38:38 +0000948 data_type_ref Data, unsigned DataLen) {
949 using namespace clang::io;
950 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +0000951
Chris Lattner2a6fa472010-11-23 19:28:12 +0000952 Emit32(Out, (uint32_t) Data.st_ino);
953 Emit32(Out, (uint32_t) Data.st_dev);
954 Emit16(Out, (uint16_t) Data.st_mode);
955 Emit64(Out, (uint64_t) Data.st_mtime);
956 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregorc5046832009-04-27 18:38:38 +0000957
958 assert(Out.tell() - Start == DataLen && "Wrong data length");
959 }
960};
961} // end anonymous namespace
962
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000963/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000964void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +0000965 // Build the on-disk hash table containing information about every
966 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000967 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +0000968 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000969 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +0000970 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000971 Stat != StatEnd; ++Stat, ++NumStatEntries) {
972 const char *Filename = Stat->first();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000973 Generator.insert(Filename, Stat->second);
974 }
Mike Stump11289f42009-09-09 15:08:12 +0000975
Douglas Gregorc5046832009-04-27 18:38:38 +0000976 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +0000977 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +0000978 uint32_t BucketOffset;
979 {
980 llvm::raw_svector_ostream Out(StatCacheData);
981 // Make sure that no bucket is at offset 0
982 clang::io::Emit32(Out, 0);
983 BucketOffset = Generator.Emit(Out);
984 }
985
986 // Create a blob abbreviation
987 using namespace llvm;
988 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000989 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +0000990 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
991 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
992 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
993 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
994
995 // Write the stat cache
996 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000997 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +0000998 Record.push_back(BucketOffset);
999 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001000 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +00001001}
1002
1003//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +00001004// Source Manager Serialization
1005//===----------------------------------------------------------------------===//
1006
1007/// \brief Create an abbreviation for the SLocEntry that refers to a
1008/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001009static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001010 using namespace llvm;
1011 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001012 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001013 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1014 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1015 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1016 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001017 // FileEntry fields.
1018 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1019 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001020 // HeaderFileInfo fields.
1021 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
1022 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
1023 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
1024 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregora7f71a92009-04-10 03:52:48 +00001025 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001026 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001027}
1028
1029/// \brief Create an abbreviation for the SLocEntry that refers to a
1030/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001031static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001032 using namespace llvm;
1033 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001034 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001035 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1036 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1037 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1038 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1039 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001040 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001041}
1042
1043/// \brief Create an abbreviation for the SLocEntry that refers to a
1044/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001045static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001046 using namespace llvm;
1047 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001048 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001049 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001050 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001051}
1052
1053/// \brief Create an abbreviation for the SLocEntry that refers to an
1054/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001055static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001056 using namespace llvm;
1057 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001058 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001059 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1060 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1061 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1062 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001063 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001064 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001065}
1066
1067/// \brief Writes the block containing the serialized form of the
1068/// source manager.
1069///
1070/// TODO: We should probably use an on-disk hash table (stored in a
1071/// blob), indexed based on the file name, so that we only create
1072/// entries for files that we actually need. In the common case (no
1073/// errors), we probably won't have to create file entries for any of
1074/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001075void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001076 const Preprocessor &PP,
1077 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001078 RecordData Record;
1079
Chris Lattner0910e3b2009-04-10 17:16:57 +00001080 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001081 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001082
1083 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001084 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1085 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1086 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1087 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001088
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001089 // Write the line table.
1090 if (SourceMgr.hasLineTable()) {
1091 LineTableInfo &LineTable = SourceMgr.getLineTable();
1092
1093 // Emit the file names
1094 Record.push_back(LineTable.getNumFilenames());
1095 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1096 // Emit the file name
1097 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001098 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001099 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1100 Record.push_back(FilenameLen);
1101 if (FilenameLen)
1102 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1103 }
Mike Stump11289f42009-09-09 15:08:12 +00001104
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001105 // Emit the line entries
1106 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1107 L != LEnd; ++L) {
1108 // Emit the file ID
1109 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +00001110
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001111 // Emit the line entries
1112 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +00001113 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001114 LEEnd = L->second.end();
1115 LE != LEEnd; ++LE) {
1116 Record.push_back(LE->FileOffset);
1117 Record.push_back(LE->LineNo);
1118 Record.push_back(LE->FilenameID);
1119 Record.push_back((unsigned)LE->FileKind);
1120 Record.push_back(LE->IncludeOffset);
1121 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001122 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001123 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001124 }
1125
Douglas Gregor258ae542009-04-27 06:38:32 +00001126 // Write out the source location entry table. We skip the first
1127 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001128 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001129 RecordData PreloadSLocs;
Sebastian Redl5c415f32010-07-22 17:01:13 +00001130 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1131 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1132 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1133 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001134 // Get this source location entry.
1135 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001136
Douglas Gregor258ae542009-04-27 06:38:32 +00001137 // Record the offset of this source-location entry.
1138 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1139
1140 // Figure out which record code to use.
1141 unsigned Code;
1142 if (SLoc->isFile()) {
1143 if (SLoc->getFile().getContentCache()->Entry)
Sebastian Redl539c5062010-08-18 23:57:32 +00001144 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001145 else
Sebastian Redl539c5062010-08-18 23:57:32 +00001146 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001147 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001148 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001149 Record.clear();
1150 Record.push_back(Code);
1151
1152 Record.push_back(SLoc->getOffset());
1153 if (SLoc->isFile()) {
1154 const SrcMgr::FileInfo &File = SLoc->getFile();
1155 Record.push_back(File.getIncludeLoc().getRawEncoding());
1156 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1157 Record.push_back(File.hasLineDirectives());
1158
1159 const SrcMgr::ContentCache *Content = File.getContentCache();
1160 if (Content->Entry) {
1161 // The source location entry is a file. The blob associated
1162 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001163
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001164 // Emit size/modification time for this file.
1165 Record.push_back(Content->Entry->getSize());
1166 Record.push_back(Content->Entry->getModificationTime());
1167
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001168 // Emit header-search information associated with this file.
1169 HeaderFileInfo HFI;
1170 HeaderSearch &HS = PP.getHeaderSearchInfo();
1171 if (Content->Entry->getUID() < HS.header_file_size())
1172 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1173 Record.push_back(HFI.isImport);
1174 Record.push_back(HFI.DirInfo);
1175 Record.push_back(HFI.NumIncludes);
1176 AddIdentifierRef(HFI.ControllingMacro, Record);
1177
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001178 // Turn the file name into an absolute path, if it isn't already.
1179 const char *Filename = Content->Entry->getName();
Michael J. Spencer740857f2010-12-21 16:45:57 +00001180 llvm::SmallString<128> FilePath(Filename);
1181 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001182 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001183
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001184 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001185 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001186
1187 // FIXME: For now, preload all file source locations, so that
1188 // we get the appropriate File entries in the reader. This is
1189 // a temporary measure.
Sebastian Redl5c415f32010-07-22 17:01:13 +00001190 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001191 } else {
1192 // The source location entry is a buffer. The blob associated
1193 // with this entry contains the contents of the buffer.
1194
1195 // We add one to the size so that we capture the trailing NULL
1196 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1197 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001198 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001199 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001200 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001201 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1202 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001203 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001204 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001205 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001206 llvm::StringRef(Buffer->getBufferStart(),
1207 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001208
1209 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl5c415f32010-07-22 17:01:13 +00001210 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001211 }
1212 } else {
1213 // The source location entry is an instantiation.
1214 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1215 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1216 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1217 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1218
1219 // Compute the token length for this macro expansion.
1220 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001221 if (I + 1 != N)
1222 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001223 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1224 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1225 }
1226 }
1227
Douglas Gregor8f45df52009-04-16 22:23:12 +00001228 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001229
1230 if (SLocEntryOffsets.empty())
1231 return;
1232
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001233 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001234 // table is used for lazily loading source-location information.
1235 using namespace llvm;
1236 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001237 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001238 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1239 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1240 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1241 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001242
Douglas Gregor258ae542009-04-27 06:38:32 +00001243 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001244 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001245 Record.push_back(SLocEntryOffsets.size());
Sebastian Redlc1d035f2010-09-22 20:19:08 +00001246 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1247 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Douglas Gregor258ae542009-04-27 06:38:32 +00001248 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001249 (const char *)data(SLocEntryOffsets),
Chris Lattner12d61d32009-04-27 19:01:47 +00001250 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001251
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001252 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001253 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001254 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001255}
1256
Douglas Gregorc5046832009-04-27 18:38:38 +00001257//===----------------------------------------------------------------------===//
1258// Preprocessor Serialization
1259//===----------------------------------------------------------------------===//
1260
Chris Lattnereeffaef2009-04-10 17:15:23 +00001261/// \brief Writes the block containing the serialized form of the
1262/// preprocessor.
1263///
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001264void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001265 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001266
Chris Lattner0af3ba12009-04-13 01:29:17 +00001267 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1268 if (PP.getCounterValue() != 0) {
1269 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001270 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001271 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001272 }
1273
1274 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001275 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001276
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001277 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001278 // FIXME: use diagnostics subsystem for localization etc.
1279 if (PP.SawDateOrTime())
1280 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001281
Douglas Gregor796d76a2010-10-20 22:00:55 +00001282
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001283 // Loop over all the macro definitions that are live at the end of the file,
1284 // emitting each to the PP section.
Douglas Gregoraae92242010-03-19 21:51:54 +00001285 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Douglas Gregor796d76a2010-10-20 22:00:55 +00001286 unsigned InclusionAbbrev = 0;
1287 if (PPRec) {
1288 using namespace llvm;
1289 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1290 Abbrev->Add(BitCodeAbbrevOp(PP_INCLUSION_DIRECTIVE));
1291 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1292 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1293 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1294 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1295 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1296 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1297 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001298 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor796d76a2010-10-20 22:00:55 +00001299 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001300
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001301 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1302 I != E; ++I) {
Chris Lattner34321bc2009-04-10 21:41:48 +00001303 // FIXME: This emits macros in hash table order, we should do it in a stable
1304 // order so that output is reproducible.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001305 MacroInfo *MI = I->second;
1306
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001307 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001308 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001309 // Also skip macros from a AST file if we're chaining.
Douglas Gregoreb114da2010-10-01 01:03:07 +00001310
1311 // FIXME: There is a (probably minor) optimization we could do here, if
1312 // the macro comes from the original PCH but the identifier comes from a
1313 // chained PCH, by storing the offset into the original PCH rather than
1314 // writing the macro definition a second time.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001315 if (MI->isBuiltinMacro() ||
Douglas Gregoreb114da2010-10-01 01:03:07 +00001316 (Chain && I->first->isFromAST() && MI->isFromAST()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001317 continue;
1318
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001319 AddIdentifierRef(I->first, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001320 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001321 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1322 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001323
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001324 unsigned Code;
1325 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001326 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001327 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001328 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001329
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001330 Record.push_back(MI->isC99Varargs());
1331 Record.push_back(MI->isGNUVarargs());
1332 Record.push_back(MI->getNumArgs());
1333 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1334 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001335 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001336 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001337
Douglas Gregoraae92242010-03-19 21:51:54 +00001338 // If we have a detailed preprocessing record, record the macro definition
1339 // ID that corresponds to this macro.
1340 if (PPRec)
1341 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001342
Douglas Gregor8f45df52009-04-16 22:23:12 +00001343 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001344 Record.clear();
1345
Chris Lattner2199f5b2009-04-10 18:08:30 +00001346 // Emit the tokens array.
1347 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1348 // Note that we know that the preprocessor does not have any annotation
1349 // tokens in it because they are created by the parser, and thus can't be
1350 // in a macro definition.
1351 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001352
Chris Lattner2199f5b2009-04-10 18:08:30 +00001353 Record.push_back(Tok.getLocation().getRawEncoding());
1354 Record.push_back(Tok.getLength());
1355
Chris Lattner2199f5b2009-04-10 18:08:30 +00001356 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1357 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001358 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump11289f42009-09-09 15:08:12 +00001359
Chris Lattner2199f5b2009-04-10 18:08:30 +00001360 // FIXME: Should translate token kind to a stable encoding.
1361 Record.push_back(Tok.getKind());
1362 // FIXME: Should translate token flags to a stable encoding.
1363 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001364
Sebastian Redl539c5062010-08-18 23:57:32 +00001365 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001366 Record.clear();
1367 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001368 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001369 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001370
Douglas Gregoraae92242010-03-19 21:51:54 +00001371 // If the preprocessor has a preprocessing record, emit it.
1372 unsigned NumPreprocessingRecords = 0;
1373 if (PPRec) {
Sebastian Redl7abd8d52010-09-27 23:20:01 +00001374 unsigned IndexBase = Chain ? PPRec->getNumPreallocatedEntities() : 0;
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001375 for (PreprocessingRecord::iterator E = PPRec->begin(Chain),
1376 EEnd = PPRec->end(Chain);
Douglas Gregoraae92242010-03-19 21:51:54 +00001377 E != EEnd; ++E) {
1378 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001379
Douglas Gregoraae92242010-03-19 21:51:54 +00001380 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1381 // Record this macro definition's location.
Sebastian Redl50e26582010-09-15 19:54:06 +00001382 MacroID ID = getMacroDefinitionID(MD);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001383
Douglas Gregor91096292010-10-02 19:29:26 +00001384 // Don't write the macro definition if it is from another AST file.
1385 if (ID < FirstMacroID)
1386 continue;
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001387
1388 // Notify the serialization listener that we're serializing this entity.
1389 if (SerializationListener)
1390 SerializationListener->SerializedPreprocessedEntity(*E,
1391 Stream.GetCurrentBitNo());
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001392
Douglas Gregor91096292010-10-02 19:29:26 +00001393 unsigned Position = ID - FirstMacroID;
1394 if (Position != MacroDefinitionOffsets.size()) {
1395 if (Position > MacroDefinitionOffsets.size())
1396 MacroDefinitionOffsets.resize(Position + 1);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001397
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001398 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
Douglas Gregoraae92242010-03-19 21:51:54 +00001399 } else
1400 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001401
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001402 Record.push_back(IndexBase + NumPreprocessingRecords++);
Douglas Gregoraae92242010-03-19 21:51:54 +00001403 Record.push_back(ID);
1404 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1405 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1406 AddIdentifierRef(MD->getName(), Record);
1407 AddSourceLocation(MD->getLocation(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +00001408 Stream.EmitRecord(PP_MACRO_DEFINITION, Record);
Douglas Gregoraae92242010-03-19 21:51:54 +00001409 continue;
1410 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001411
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001412 // Notify the serialization listener that we're serializing this entity.
1413 if (SerializationListener)
1414 SerializationListener->SerializedPreprocessedEntity(*E,
1415 Stream.GetCurrentBitNo());
1416
1417 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1418 Record.push_back(IndexBase + NumPreprocessingRecords++);
1419 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1420 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1421 AddIdentifierRef(MI->getName(), Record);
1422 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1423 Stream.EmitRecord(PP_MACRO_INSTANTIATION, Record);
1424 continue;
1425 }
1426
Douglas Gregor796d76a2010-10-20 22:00:55 +00001427 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1428 Record.push_back(PP_INCLUSION_DIRECTIVE);
1429 Record.push_back(IndexBase + NumPreprocessingRecords++);
1430 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1431 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1432 Record.push_back(ID->getFileName().size());
1433 Record.push_back(ID->wasInQuotes());
1434 Record.push_back(static_cast<unsigned>(ID->getKind()));
1435 llvm::SmallString<64> Buffer;
1436 Buffer += ID->getFileName();
1437 Buffer += ID->getFile()->getName();
1438 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1439 continue;
1440 }
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001441
1442 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
Douglas Gregoraae92242010-03-19 21:51:54 +00001443 }
1444 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001445
Douglas Gregor8f45df52009-04-16 22:23:12 +00001446 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001447
Douglas Gregoraae92242010-03-19 21:51:54 +00001448 // Write the offsets table for the preprocessing record.
1449 if (NumPreprocessingRecords > 0) {
1450 // Write the offsets table for identifier IDs.
1451 using namespace llvm;
1452 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001453 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregoraae92242010-03-19 21:51:54 +00001454 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1455 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1456 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1457 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001458
Douglas Gregoraae92242010-03-19 21:51:54 +00001459 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001460 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001461 Record.push_back(NumPreprocessingRecords);
1462 Record.push_back(MacroDefinitionOffsets.size());
1463 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001464 (const char *)data(MacroDefinitionOffsets),
Douglas Gregoraae92242010-03-19 21:51:54 +00001465 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1466 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001467}
1468
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001469void ASTWriter::WriteUserDiagnosticMappings(const Diagnostic &Diag) {
1470 RecordData Record;
1471 for (unsigned i = 0; i != diag::DIAG_UPPER_LIMIT; ++i) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001472 diag::Mapping Map = Diag.getDiagnosticMappingInfo(i,Diag.GetCurDiagState());
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001473 if (Map & 0x8) { // user mapping.
1474 Record.push_back(i);
1475 Record.push_back(Map & 0x7);
1476 }
1477 }
1478
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00001479 if (!Record.empty())
1480 Stream.EmitRecord(DIAG_USER_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001481}
1482
Douglas Gregorc5046832009-04-27 18:38:38 +00001483//===----------------------------------------------------------------------===//
1484// Type Serialization
1485//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001486
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001487/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001488void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00001489 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001490 if (Idx.getIndex() == 0) // we haven't seen this type before.
1491 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00001492
Douglas Gregor9b3932c2010-10-05 18:37:06 +00001493 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00001494
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001495 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001496 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001497 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001498 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001499 else if (TypeOffsets.size() < Index) {
1500 TypeOffsets.resize(Index + 1);
1501 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001502 }
1503
1504 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001505
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001506 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001507 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001508
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001509 if (T.hasLocalNonFastQualifiers()) {
1510 Qualifiers Qs = T.getLocalQualifiers();
1511 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001512 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001513 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00001514 } else {
1515 switch (T->getTypeClass()) {
1516 // For all of the concrete, non-dependent types, call the
1517 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001518#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001519 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001520#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001521#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001522 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001523 }
1524
1525 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001526 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001527
1528 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001529 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001530}
1531
Douglas Gregorc5046832009-04-27 18:38:38 +00001532//===----------------------------------------------------------------------===//
1533// Declaration Serialization
1534//===----------------------------------------------------------------------===//
1535
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001536/// \brief Write the block containing all of the declaration IDs
1537/// lexically declared within the given DeclContext.
1538///
1539/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1540/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001541uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001542 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001543 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001544 return 0;
1545
Douglas Gregor8f45df52009-04-16 22:23:12 +00001546 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001547 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001548 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001549 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001550 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1551 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001552 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001553
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001554 ++NumLexicalDeclContexts;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001555 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001556 reinterpret_cast<char*>(Decls.data()),
1557 Decls.size() * sizeof(KindDeclIDPair));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001558 return Offset;
1559}
1560
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001561void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001562 using namespace llvm;
1563 RecordData Record;
1564
1565 // Write the type offsets array
1566 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001567 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001568 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1569 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1570 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1571 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001572 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001573 Record.push_back(TypeOffsets.size());
1574 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001575 (const char *)data(TypeOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001576 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1577
1578 // Write the declaration offsets array
1579 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001580 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001581 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1582 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1583 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1584 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001585 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001586 Record.push_back(DeclOffsets.size());
1587 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001588 (const char *)data(DeclOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001589 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1590}
1591
Douglas Gregorc5046832009-04-27 18:38:38 +00001592//===----------------------------------------------------------------------===//
1593// Global Method Pool and Selector Serialization
1594//===----------------------------------------------------------------------===//
1595
Douglas Gregore84a9da2009-04-20 20:36:09 +00001596namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001597// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001598class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001599 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001600
1601public:
1602 typedef Selector key_type;
1603 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001604
Sebastian Redl834bb972010-08-04 17:20:04 +00001605 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00001606 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00001607 ObjCMethodList Instance, Factory;
1608 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00001609 typedef const data_type& data_type_ref;
1610
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001611 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001612
Douglas Gregorc78d3462009-04-24 21:10:55 +00001613 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00001614 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001615 }
Mike Stump11289f42009-09-09 15:08:12 +00001616
1617 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001618 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1619 data_type_ref Methods) {
1620 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1621 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00001622 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1623 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001624 Method = Method->Next)
1625 if (Method->Method)
1626 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00001627 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001628 Method = Method->Next)
1629 if (Method->Method)
1630 DataLen += 4;
1631 clang::io::Emit16(Out, DataLen);
1632 return std::make_pair(KeyLen, DataLen);
1633 }
Mike Stump11289f42009-09-09 15:08:12 +00001634
Douglas Gregor95c13f52009-04-25 17:48:32 +00001635 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001636 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001637 assert((Start >> 32) == 0 && "Selector key offset too large");
1638 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001639 unsigned N = Sel.getNumArgs();
1640 clang::io::Emit16(Out, N);
1641 if (N == 0)
1642 N = 1;
1643 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001644 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001645 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1646 }
Mike Stump11289f42009-09-09 15:08:12 +00001647
Douglas Gregorc78d3462009-04-24 21:10:55 +00001648 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001649 data_type_ref Methods, unsigned DataLen) {
1650 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00001651 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001652 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001653 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001654 Method = Method->Next)
1655 if (Method->Method)
1656 ++NumInstanceMethods;
1657
1658 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001659 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001660 Method = Method->Next)
1661 if (Method->Method)
1662 ++NumFactoryMethods;
1663
1664 clang::io::Emit16(Out, NumInstanceMethods);
1665 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00001666 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001667 Method = Method->Next)
1668 if (Method->Method)
1669 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00001670 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001671 Method = Method->Next)
1672 if (Method->Method)
1673 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001674
1675 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001676 }
1677};
1678} // end anonymous namespace
1679
Sebastian Redla19a67f2010-08-03 21:58:15 +00001680/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001681///
1682/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00001683/// in an on-disk hash table indexed by the selector. The hash table also
1684/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001685void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001686 using namespace llvm;
1687
Sebastian Redla19a67f2010-08-03 21:58:15 +00001688 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00001689 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00001690 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00001691 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001692 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001693 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001694 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001695 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00001696
Sebastian Redla19a67f2010-08-03 21:58:15 +00001697 // Create the on-disk hash table representation. We walk through every
1698 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00001699 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00001700 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00001701 I = SelectorIDs.begin(), E = SelectorIDs.end();
1702 I != E; ++I) {
1703 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001704 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001705 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00001706 I->second,
1707 ObjCMethodList(),
1708 ObjCMethodList()
1709 };
1710 if (F != SemaRef.MethodPool.end()) {
1711 Data.Instance = F->second.first;
1712 Data.Factory = F->second.second;
1713 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001714 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00001715 // changed.
1716 if (Chain && I->second < FirstSelectorID) {
1717 // Selector already exists. Did it change?
1718 bool changed = false;
1719 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
1720 M = M->Next) {
1721 if (M->Method->getPCHLevel() == 0)
1722 changed = true;
1723 }
1724 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
1725 M = M->Next) {
1726 if (M->Method->getPCHLevel() == 0)
1727 changed = true;
1728 }
1729 if (!changed)
1730 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00001731 } else if (Data.Instance.Method || Data.Factory.Method) {
1732 // A new method pool entry.
1733 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00001734 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001735 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001736 }
1737
Douglas Gregorc78d3462009-04-24 21:10:55 +00001738 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001739 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001740 uint32_t BucketOffset;
1741 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001742 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001743 llvm::raw_svector_ostream Out(MethodPool);
1744 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001745 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001746 BucketOffset = Generator.Emit(Out, Trait);
1747 }
1748
1749 // Create a blob abbreviation
1750 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001751 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001752 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001753 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001754 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1755 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1756
Douglas Gregor95c13f52009-04-25 17:48:32 +00001757 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00001758 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001759 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001760 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00001761 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001762 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00001763
1764 // Create a blob abbreviation for the selector table offsets.
1765 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001766 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001767 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor95c13f52009-04-25 17:48:32 +00001768 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1769 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1770
1771 // Write the selector offsets table.
1772 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001773 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001774 Record.push_back(SelectorOffsets.size());
1775 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001776 (const char *)data(SelectorOffsets),
Douglas Gregor95c13f52009-04-25 17:48:32 +00001777 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001778 }
1779}
1780
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001781/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001782void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001783 using namespace llvm;
1784 if (SemaRef.ReferencedSelectors.empty())
1785 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00001786
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001787 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00001788
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001789 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00001790 // very tricky to fix, and given that @selector shouldn't really appear in
1791 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001792 for (DenseMap<Selector, SourceLocation>::iterator S =
1793 SemaRef.ReferencedSelectors.begin(),
1794 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
1795 Selector Sel = (*S).first;
1796 SourceLocation Loc = (*S).second;
1797 AddSelectorRef(Sel, Record);
1798 AddSourceLocation(Loc, Record);
1799 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001800 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001801}
1802
Douglas Gregorc5046832009-04-27 18:38:38 +00001803//===----------------------------------------------------------------------===//
1804// Identifier Table Serialization
1805//===----------------------------------------------------------------------===//
1806
Douglas Gregorc78d3462009-04-24 21:10:55 +00001807namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001808class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001809 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001810 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001811
Douglas Gregor1d583f22009-04-28 21:18:29 +00001812 /// \brief Determines whether this is an "interesting" identifier
1813 /// that needs a full IdentifierInfo structure written into the hash
1814 /// table.
1815 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1816 return II->isPoisoned() ||
1817 II->isExtensionToken() ||
1818 II->hasMacroDefinition() ||
1819 II->getObjCOrBuiltinID() ||
1820 II->getFETokenInfo<void>();
1821 }
1822
Douglas Gregore84a9da2009-04-20 20:36:09 +00001823public:
1824 typedef const IdentifierInfo* key_type;
1825 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001826
Sebastian Redl539c5062010-08-18 23:57:32 +00001827 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001828 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001829
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001830 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00001831 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00001832
1833 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001834 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00001835 }
Mike Stump11289f42009-09-09 15:08:12 +00001836
1837 std::pair<unsigned,unsigned>
1838 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00001839 IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001840 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001841 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1842 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00001843 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00001844 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00001845 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00001846 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001847 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1848 DEnd = IdentifierResolver::end();
1849 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00001850 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00001851 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001852 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00001853 // We emit the key length after the data length so that every
1854 // string is preceded by a 16-bit length. This matches the PTH
1855 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00001856 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001857 return std::make_pair(KeyLen, DataLen);
1858 }
Mike Stump11289f42009-09-09 15:08:12 +00001859
1860 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001861 unsigned KeyLen) {
1862 // Record the location of the key data. This is used when generating
1863 // the mapping from persistent IDs to strings.
1864 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001865 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001866 }
Mike Stump11289f42009-09-09 15:08:12 +00001867
1868 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00001869 IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00001870 if (!isInterestingIdentifier(II)) {
1871 clang::io::Emit32(Out, ID << 1);
1872 return;
1873 }
Douglas Gregorb9256522009-04-28 21:32:13 +00001874
Douglas Gregor1d583f22009-04-28 21:18:29 +00001875 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001876 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001877 bool hasMacroDefinition =
1878 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00001879 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00001880 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001881 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1882 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1883 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00001884 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001885 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00001886 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001887
Douglas Gregorc3366a52009-04-21 23:56:24 +00001888 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00001889 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001890
Douglas Gregora868bbd2009-04-21 22:25:48 +00001891 // Emit the declaration IDs in reverse order, because the
1892 // IdentifierResolver provides the declarations as they would be
1893 // visible (e.g., the function "stat" would come before the struct
1894 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1895 // adds declarations to the end of the list (so we need to see the
1896 // struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00001897 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump11289f42009-09-09 15:08:12 +00001898 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00001899 IdentifierResolver::end());
1900 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1901 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00001902 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00001903 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001904 }
1905};
1906} // end anonymous namespace
1907
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001908/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001909///
1910/// The identifier table consists of a blob containing string data
1911/// (the actual identifiers themselves) and a separate "offsets" index
1912/// that maps identifier IDs to locations within the blob.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001913void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001914 using namespace llvm;
1915
1916 // Create and write out the blob that contains the identifier
1917 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001918 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001919 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001920 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump11289f42009-09-09 15:08:12 +00001921
Douglas Gregore6648fb2009-04-28 20:33:11 +00001922 // Look for any identifiers that were named while processing the
1923 // headers, but are otherwise not needed. We add these to the hash
1924 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001925 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00001926 // file.
1927 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1928 IDEnd = PP.getIdentifierTable().end();
1929 ID != IDEnd; ++ID)
1930 getIdentifierRef(ID->second);
1931
Sebastian Redlff4a2952010-07-23 23:49:55 +00001932 // Create the on-disk hash table representation. We only store offsets
1933 // for identifiers that appear here for the first time.
1934 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00001935 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001936 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1937 ID != IDEnd; ++ID) {
1938 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001939 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001940 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001941 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001942
Douglas Gregore84a9da2009-04-20 20:36:09 +00001943 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001944 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00001945 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001946 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001947 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001948 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001949 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001950 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001951 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001952 }
1953
1954 // Create a blob abbreviation
1955 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001956 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00001957 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001958 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00001959 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001960
1961 // Write the identifier table
1962 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001963 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001964 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001965 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001966 }
1967
1968 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00001969 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001970 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00001971 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1972 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1973 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1974
1975 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001976 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00001977 Record.push_back(IdentifierOffsets.size());
1978 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001979 (const char *)data(IdentifierOffsets),
Douglas Gregor0e149972009-04-25 19:10:14 +00001980 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001981}
1982
Douglas Gregorc5046832009-04-27 18:38:38 +00001983//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001984// DeclContext's Name Lookup Table Serialization
1985//===----------------------------------------------------------------------===//
1986
1987namespace {
1988// Trait used for the on-disk hash table used in the method pool.
1989class ASTDeclContextNameLookupTrait {
1990 ASTWriter &Writer;
1991
1992public:
1993 typedef DeclarationName key_type;
1994 typedef key_type key_type_ref;
1995
1996 typedef DeclContext::lookup_result data_type;
1997 typedef const data_type& data_type_ref;
1998
1999 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2000
2001 unsigned ComputeHash(DeclarationName Name) {
2002 llvm::FoldingSetNodeID ID;
2003 ID.AddInteger(Name.getNameKind());
2004
2005 switch (Name.getNameKind()) {
2006 case DeclarationName::Identifier:
2007 ID.AddString(Name.getAsIdentifierInfo()->getName());
2008 break;
2009 case DeclarationName::ObjCZeroArgSelector:
2010 case DeclarationName::ObjCOneArgSelector:
2011 case DeclarationName::ObjCMultiArgSelector:
2012 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2013 break;
2014 case DeclarationName::CXXConstructorName:
2015 case DeclarationName::CXXDestructorName:
2016 case DeclarationName::CXXConversionFunctionName:
2017 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
2018 break;
2019 case DeclarationName::CXXOperatorName:
2020 ID.AddInteger(Name.getCXXOverloadedOperator());
2021 break;
2022 case DeclarationName::CXXLiteralOperatorName:
2023 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2024 case DeclarationName::CXXUsingDirective:
2025 break;
2026 }
2027
2028 return ID.ComputeHash();
2029 }
2030
2031 std::pair<unsigned,unsigned>
2032 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2033 data_type_ref Lookup) {
2034 unsigned KeyLen = 1;
2035 switch (Name.getNameKind()) {
2036 case DeclarationName::Identifier:
2037 case DeclarationName::ObjCZeroArgSelector:
2038 case DeclarationName::ObjCOneArgSelector:
2039 case DeclarationName::ObjCMultiArgSelector:
2040 case DeclarationName::CXXConstructorName:
2041 case DeclarationName::CXXDestructorName:
2042 case DeclarationName::CXXConversionFunctionName:
2043 case DeclarationName::CXXLiteralOperatorName:
2044 KeyLen += 4;
2045 break;
2046 case DeclarationName::CXXOperatorName:
2047 KeyLen += 1;
2048 break;
2049 case DeclarationName::CXXUsingDirective:
2050 break;
2051 }
2052 clang::io::Emit16(Out, KeyLen);
2053
2054 // 2 bytes for num of decls and 4 for each DeclID.
2055 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2056 clang::io::Emit16(Out, DataLen);
2057
2058 return std::make_pair(KeyLen, DataLen);
2059 }
2060
2061 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2062 using namespace clang::io;
2063
2064 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2065 Emit8(Out, Name.getNameKind());
2066 switch (Name.getNameKind()) {
2067 case DeclarationName::Identifier:
2068 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2069 break;
2070 case DeclarationName::ObjCZeroArgSelector:
2071 case DeclarationName::ObjCOneArgSelector:
2072 case DeclarationName::ObjCMultiArgSelector:
2073 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2074 break;
2075 case DeclarationName::CXXConstructorName:
2076 case DeclarationName::CXXDestructorName:
2077 case DeclarationName::CXXConversionFunctionName:
2078 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2079 break;
2080 case DeclarationName::CXXOperatorName:
2081 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2082 Emit8(Out, Name.getCXXOverloadedOperator());
2083 break;
2084 case DeclarationName::CXXLiteralOperatorName:
2085 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2086 break;
2087 case DeclarationName::CXXUsingDirective:
2088 break;
2089 }
2090 }
2091
2092 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2093 data_type Lookup, unsigned DataLen) {
2094 uint64_t Start = Out.tell(); (void)Start;
2095 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2096 for (; Lookup.first != Lookup.second; ++Lookup.first)
2097 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2098
2099 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2100 }
2101};
2102} // end anonymous namespace
2103
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002104/// \brief Write the block containing all of the declaration IDs
2105/// visible from the given DeclContext.
2106///
2107/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002108/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002109uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2110 DeclContext *DC) {
2111 if (DC->getPrimaryContext() != DC)
2112 return 0;
2113
2114 // Since there is no name lookup into functions or methods, don't bother to
2115 // build a visible-declarations table for these entities.
2116 if (DC->isFunctionOrMethod())
2117 return 0;
2118
2119 // If not in C++, we perform name lookup for the translation unit via the
2120 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2121 // FIXME: In C++ we need the visible declarations in order to "see" the
2122 // friend declarations, is there a way to do this without writing the table ?
2123 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2124 return 0;
2125
2126 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00002127 if (DC->hasExternalVisibleStorage())
2128 DC->MaterializeVisibleDeclsFromExternalStorage();
2129 else
2130 DC->lookup(DeclarationName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002131
2132 // Serialize the contents of the mapping used for lookup. Note that,
2133 // although we have two very different code paths, the serialized
2134 // representation is the same for both cases: a declaration name,
2135 // followed by a size, followed by references to the visible
2136 // declarations that have that name.
2137 uint64_t Offset = Stream.GetCurrentBitNo();
2138 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2139 if (!Map || Map->empty())
2140 return 0;
2141
2142 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2143 ASTDeclContextNameLookupTrait Trait(*this);
2144
2145 // Create the on-disk hash table representation.
2146 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2147 D != DEnd; ++D) {
2148 DeclarationName Name = D->first;
2149 DeclContext::lookup_result Result = D->second.getLookupResult();
2150 Generator.insert(Name, Result, Trait);
2151 }
2152
2153 // Create the on-disk hash table in a buffer.
2154 llvm::SmallString<4096> LookupTable;
2155 uint32_t BucketOffset;
2156 {
2157 llvm::raw_svector_ostream Out(LookupTable);
2158 // Make sure that no bucket is at offset 0
2159 clang::io::Emit32(Out, 0);
2160 BucketOffset = Generator.Emit(Out, Trait);
2161 }
2162
2163 // Write the lookup table
2164 RecordData Record;
2165 Record.push_back(DECL_CONTEXT_VISIBLE);
2166 Record.push_back(BucketOffset);
2167 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2168 LookupTable.str());
2169
2170 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2171 ++NumVisibleDeclContexts;
2172 return Offset;
2173}
2174
Sebastian Redla4071b42010-08-24 00:50:09 +00002175/// \brief Write an UPDATE_VISIBLE block for the given context.
2176///
2177/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2178/// DeclContext in a dependent AST file. As such, they only exist for the TU
2179/// (in C++) and for namespaces.
2180void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00002181 // Make the context build its lookup table, but don't make it load external
2182 // decls.
2183 DC->lookup(DeclarationName());
2184
2185 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2186 if (!Map || Map->empty())
2187 return;
2188
2189 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2190 ASTDeclContextNameLookupTrait Trait(*this);
2191
2192 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002193 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2194 D != DEnd; ++D) {
2195 DeclarationName Name = D->first;
2196 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002197 // For any name that appears in this table, the results are complete, i.e.
2198 // they overwrite results from previous PCHs. Merging is always a mess.
2199 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002200 }
2201
2202 // Create the on-disk hash table in a buffer.
2203 llvm::SmallString<4096> LookupTable;
2204 uint32_t BucketOffset;
2205 {
2206 llvm::raw_svector_ostream Out(LookupTable);
2207 // Make sure that no bucket is at offset 0
2208 clang::io::Emit32(Out, 0);
2209 BucketOffset = Generator.Emit(Out, Trait);
2210 }
2211
2212 // Write the lookup table
2213 RecordData Record;
2214 Record.push_back(UPDATE_VISIBLE);
2215 Record.push_back(getDeclID(cast<Decl>(DC)));
2216 Record.push_back(BucketOffset);
2217 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2218}
2219
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002220//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00002221// General Serialization Routines
2222//===----------------------------------------------------------------------===//
2223
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002224/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002225void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00002226 Record.push_back(Attrs.size());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002227 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2228 const Attr * A = *i;
2229 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2230 AddSourceLocation(A->getLocation(), Record);
2231 Record.push_back(A->isInherited());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002232
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002233#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00002234
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002235 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002236}
2237
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002238void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002239 Record.push_back(Str.size());
2240 Record.insert(Record.end(), Str.begin(), Str.end());
2241}
2242
Douglas Gregore84a9da2009-04-20 20:36:09 +00002243/// \brief Note that the identifier II occurs at the given offset
2244/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002245void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002246 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002247 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00002248 // up earlier in the chain and thus don't need an offset.
2249 if (ID >= FirstIdentID)
2250 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002251}
2252
Douglas Gregor95c13f52009-04-25 17:48:32 +00002253/// \brief Note that the selector Sel occurs at the given offset
2254/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002255void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00002256 unsigned ID = SelectorIDs[Sel];
2257 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00002258 // Don't record offsets for selectors that are also available in a different
2259 // file.
2260 if (ID < FirstSelectorID)
2261 return;
2262 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002263}
2264
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002265ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00002266 : Stream(Stream), Chain(0), SerializationListener(0),
2267 FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00002268 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002269 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor91096292010-10-02 19:29:26 +00002270 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2271 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002272 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002273 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
2274 NextCXXBaseSpecifiersID(1)
2275{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002276}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002277
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002278void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002279 const char *isysroot) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002280 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002281 Stream.Emit((unsigned)'C', 8);
2282 Stream.Emit((unsigned)'P', 8);
2283 Stream.Emit((unsigned)'C', 8);
2284 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002285
Chris Lattner28fa4e62009-04-26 22:26:21 +00002286 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002287
Sebastian Redl143413f2010-07-12 22:02:52 +00002288 if (Chain)
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002289 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002290 else
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002291 WriteASTCore(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002292}
2293
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002294void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl143413f2010-07-12 22:02:52 +00002295 const char *isysroot) {
2296 using namespace llvm;
2297
2298 ASTContext &Context = SemaRef.Context;
2299 Preprocessor &PP = SemaRef.PP;
2300
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002301 // The translation unit is the first declaration we'll emit.
2302 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlff4a2952010-07-23 23:49:55 +00002303 ++NextDeclID;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002304 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002305
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002306 // Make sure that we emit IdentifierInfos (and any attached
2307 // declarations) for builtins.
2308 {
2309 IdentifierTable &Table = PP.getIdentifierTable();
2310 llvm::SmallVector<const char *, 32> BuiltinNames;
2311 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2312 Context.getLangOptions().NoBuiltin);
2313 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2314 getIdentifierRef(&Table.get(BuiltinNames[I]));
2315 }
2316
Chris Lattner0c797362009-09-08 18:19:27 +00002317 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002318 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002319 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002320 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00002321 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2322 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00002323 }
Douglas Gregord4df8652009-04-22 22:02:47 +00002324
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002325 // Build a record containing all of the file scoped decls in this file.
2326 RecordData UnusedFileScopedDecls;
2327 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2328 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002329
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002330 RecordData WeakUndeclaredIdentifiers;
2331 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2332 WeakUndeclaredIdentifiers.push_back(
2333 SemaRef.WeakUndeclaredIdentifiers.size());
2334 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2335 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2336 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2337 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2338 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2339 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2340 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2341 }
2342 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002343
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002344 // Build a record containing all of the locally-scoped external
2345 // declarations in this header file. Generally, this record will be
2346 // empty.
2347 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002348 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00002349 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002350 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002351 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2352 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2353 TD != TDEnd; ++TD)
2354 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2355
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002356 // Build a record containing all of the ext_vector declarations.
2357 RecordData ExtVectorDecls;
2358 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2359 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2360
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002361 // Build a record containing all of the VTable uses information.
2362 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00002363 if (!SemaRef.VTableUses.empty()) {
2364 VTableUses.push_back(SemaRef.VTableUses.size());
2365 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2366 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2367 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2368 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2369 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002370 }
2371
2372 // Build a record containing all of dynamic classes declarations.
2373 RecordData DynamicClasses;
2374 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2375 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2376
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002377 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002378 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002379 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002380 I = SemaRef.PendingInstantiations.begin(),
2381 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2382 AddDeclRef(I->first, PendingInstantiations);
2383 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002384 }
2385 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2386 "There are local ones at end of translation unit!");
2387
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002388 // Build a record containing some declaration references.
2389 RecordData SemaDeclRefs;
2390 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2391 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2392 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2393 }
2394
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002395 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002396 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002397 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002398 WriteMetadata(Context, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002399 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002400 if (StatCalls && !isysroot)
Douglas Gregor11cfd942010-07-12 23:48:14 +00002401 WriteStatCache(*StatCalls);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002402 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc277ad12009-07-18 15:33:26 +00002403 // Write the record of special types.
2404 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002405
Steve Naroffc277ad12009-07-18 15:33:26 +00002406 AddTypeRef(Context.getBuiltinVaListType(), Record);
2407 AddTypeRef(Context.getObjCIdType(), Record);
2408 AddTypeRef(Context.getObjCSelType(), Record);
2409 AddTypeRef(Context.getObjCProtoType(), Record);
2410 AddTypeRef(Context.getObjCClassType(), Record);
2411 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2412 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2413 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002414 AddTypeRef(Context.getjmp_bufType(), Record);
2415 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002416 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2417 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpd0153282009-10-20 02:12:22 +00002418 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002419 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002420 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2421 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002422 Record.push_back(Context.isInt128Installed());
Sebastian Redl539c5062010-08-18 23:57:32 +00002423 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002424
Douglas Gregor1970d882009-04-26 03:49:13 +00002425 // Keep writing types and declarations until all types and
2426 // declarations have been written.
Sebastian Redl539c5062010-08-18 23:57:32 +00002427 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002428 WriteDeclsBlockAbbrevs();
2429 while (!DeclTypesToEmit.empty()) {
2430 DeclOrType DOT = DeclTypesToEmit.front();
2431 DeclTypesToEmit.pop();
2432 if (DOT.isType())
2433 WriteType(DOT.getType());
2434 else
2435 WriteDecl(Context, DOT.getDecl());
2436 }
2437 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002438
Douglas Gregor45053152009-10-17 17:25:45 +00002439 WritePreprocessor(PP);
Sebastian Redla19a67f2010-08-03 21:58:15 +00002440 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002441 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002442 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00002443
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002444 WriteTypeDeclOffsets();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002445 WriteUserDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00002446
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002447 // Write the C++ base-specifier set offsets.
2448 if (!CXXBaseSpecifiersOffsets.empty()) {
2449 // Create a blob abbreviation for the C++ base specifiers offsets.
2450 using namespace llvm;
2451
2452 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2453 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2454 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2455 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2456 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2457
2458 // Write the selector offsets table.
2459 Record.clear();
2460 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2461 Record.push_back(CXXBaseSpecifiersOffsets.size());
2462 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
2463 (const char *)CXXBaseSpecifiersOffsets.data(),
2464 CXXBaseSpecifiersOffsets.size() * sizeof(uint32_t));
2465 }
2466
Douglas Gregord4df8652009-04-22 22:02:47 +00002467 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002468 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002469 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002470
2471 // Write the record containing tentative definitions.
2472 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002473 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002474
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002475 // Write the record containing unused file scoped decls.
2476 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002477 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002478
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002479 // Write the record containing weak undeclared identifiers.
2480 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002481 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002482 WeakUndeclaredIdentifiers);
2483
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002484 // Write the record containing locally-scoped external definitions.
2485 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002486 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002487 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002488
2489 // Write the record containing ext_vector type names.
2490 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002491 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002492
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002493 // Write the record containing VTable uses information.
2494 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002495 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002496
2497 // Write the record containing dynamic classes declarations.
2498 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002499 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002500
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002501 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002502 if (!PendingInstantiations.empty())
2503 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002504
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002505 // Write the record containing declaration references of Sema.
2506 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002507 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002508
Douglas Gregor08f01292009-04-17 22:13:46 +00002509 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002510 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002511 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002512 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002513 Record.push_back(NumLexicalDeclContexts);
2514 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00002515 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002516 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002517}
2518
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002519void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002520 const char *isysroot) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002521 using namespace llvm;
2522
2523 ASTContext &Context = SemaRef.Context;
2524 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002525
Sebastian Redl143413f2010-07-12 22:02:52 +00002526 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002527 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002528 WriteMetadata(Context, isysroot);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002529 if (StatCalls && !isysroot)
2530 WriteStatCache(*StatCalls);
2531 // FIXME: Source manager block should only write new stuff, which could be
2532 // done by tracking the largest ID in the chain
2533 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002534
2535 // The special types are in the chained PCH.
2536
2537 // We don't start with the translation unit, but with its decls that
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002538 // don't come from the chained PCH.
Sebastian Redl143413f2010-07-12 22:02:52 +00002539 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002540 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002541 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2542 E = TU->noload_decls_end();
Sebastian Redl143413f2010-07-12 22:02:52 +00002543 I != E; ++I) {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002544 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002545 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002546 else if ((*I)->isChangedSinceDeserialization())
2547 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl143413f2010-07-12 22:02:52 +00002548 }
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002549 // We also need to write a lexical updates block for the TU.
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002550 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002551 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002552 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2553 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2554 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002555 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002556 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2557 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002558 NewGlobalDecls.size() * sizeof(KindDeclIDPair));
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00002559 // And a visible updates block for the DeclContexts.
2560 Abv = new llvm::BitCodeAbbrev();
2561 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2562 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2563 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2564 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2565 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2566 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl143413f2010-07-12 22:02:52 +00002567
Sebastian Redl98912122010-07-27 23:01:28 +00002568 // Build a record containing all of the new tentative definitions in this
2569 // file, in TentativeDefinitions order.
2570 RecordData TentativeDefinitions;
2571 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2572 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2573 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2574 }
2575
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002576 // Build a record containing all of the file scoped decls in this file.
2577 RecordData UnusedFileScopedDecls;
2578 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2579 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2580 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002581 }
2582
Sebastian Redl08aca90252010-08-05 18:21:25 +00002583 // We write the entire table, overwriting the tables from the chain.
2584 RecordData WeakUndeclaredIdentifiers;
2585 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2586 WeakUndeclaredIdentifiers.push_back(
2587 SemaRef.WeakUndeclaredIdentifiers.size());
2588 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2589 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2590 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2591 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2592 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2593 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2594 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2595 }
2596 }
2597
Sebastian Redl98912122010-07-27 23:01:28 +00002598 // Build a record containing all of the locally-scoped external
2599 // declarations in this header file. Generally, this record will be
2600 // empty.
2601 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002602 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl98912122010-07-27 23:01:28 +00002603 // nondeterminstic!
2604 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2605 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2606 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2607 TD != TDEnd; ++TD) {
2608 if (TD->second->getPCHLevel() == 0)
2609 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2610 }
2611
2612 // Build a record containing all of the ext_vector declarations.
2613 RecordData ExtVectorDecls;
2614 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2615 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2616 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2617 }
2618
Sebastian Redl08aca90252010-08-05 18:21:25 +00002619 // Build a record containing all of the VTable uses information.
2620 // We write everything here, because it's too hard to determine whether
2621 // a use is new to this part.
2622 RecordData VTableUses;
2623 if (!SemaRef.VTableUses.empty()) {
2624 VTableUses.push_back(SemaRef.VTableUses.size());
2625 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2626 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2627 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2628 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2629 }
2630 }
2631
2632 // Build a record containing all of dynamic classes declarations.
2633 RecordData DynamicClasses;
2634 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2635 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
2636 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2637
2638 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002639 RecordData PendingInstantiations;
Sebastian Redl08aca90252010-08-05 18:21:25 +00002640 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002641 I = SemaRef.PendingInstantiations.begin(),
2642 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl08aca90252010-08-05 18:21:25 +00002643 if (I->first->getPCHLevel() == 0) {
Chandler Carruth54080172010-08-25 08:44:16 +00002644 AddDeclRef(I->first, PendingInstantiations);
2645 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002646 }
2647 }
2648 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2649 "There are local ones at end of translation unit!");
2650
2651 // Build a record containing some declaration references.
2652 // It's not worth the effort to avoid duplication here.
2653 RecordData SemaDeclRefs;
2654 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2655 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2656 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2657 }
2658
Sebastian Redl539c5062010-08-18 23:57:32 +00002659 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl143413f2010-07-12 22:02:52 +00002660 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00002661 for (DeclsToRewriteTy::iterator
2662 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
2663 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl143413f2010-07-12 22:02:52 +00002664 while (!DeclTypesToEmit.empty()) {
2665 DeclOrType DOT = DeclTypesToEmit.front();
2666 DeclTypesToEmit.pop();
2667 if (DOT.isType())
2668 WriteType(DOT.getType());
2669 else
2670 WriteDecl(Context, DOT.getDecl());
2671 }
2672 Stream.ExitBlock();
2673
Sebastian Redl98912122010-07-27 23:01:28 +00002674 WritePreprocessor(PP);
Sebastian Redl51c79d82010-08-04 22:21:29 +00002675 WriteSelectors(SemaRef);
2676 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlff4a2952010-07-23 23:49:55 +00002677 WriteIdentifierTable(PP);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002678 WriteTypeDeclOffsets();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002679 // FIXME: For chained PCH only write the new mappings (we currently
2680 // write all of them again).
2681 WriteUserDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl98912122010-07-27 23:01:28 +00002682
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002683 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002684 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002685 RecordData FirstLatestDeclIDs;
2686 for (FirstLatestDeclMap::iterator
2687 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
2688 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
2689 "Expected first & second to be in different PCHs");
2690 AddDeclRef(I->first, FirstLatestDeclIDs);
2691 AddDeclRef(I->second, FirstLatestDeclIDs);
2692 }
2693 if (!FirstLatestDeclIDs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002694 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002695
Sebastian Redl98912122010-07-27 23:01:28 +00002696 // Write the record containing external, unnamed definitions.
2697 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002698 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00002699
2700 // Write the record containing tentative definitions.
2701 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002702 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00002703
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002704 // Write the record containing unused file scoped decls.
2705 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002706 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002707
Sebastian Redl08aca90252010-08-05 18:21:25 +00002708 // Write the record containing weak undeclared identifiers.
2709 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002710 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl08aca90252010-08-05 18:21:25 +00002711 WeakUndeclaredIdentifiers);
2712
Sebastian Redl98912122010-07-27 23:01:28 +00002713 // Write the record containing locally-scoped external definitions.
2714 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002715 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl98912122010-07-27 23:01:28 +00002716 LocallyScopedExternalDecls);
2717
2718 // Write the record containing ext_vector type names.
2719 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002720 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002721
Sebastian Redl08aca90252010-08-05 18:21:25 +00002722 // Write the record containing VTable uses information.
2723 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002724 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002725
2726 // Write the record containing dynamic classes declarations.
2727 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002728 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002729
2730 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002731 if (!PendingInstantiations.empty())
2732 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002733
2734 // Write the record containing declaration references of Sema.
2735 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002736 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl98912122010-07-27 23:01:28 +00002737
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00002738 // Write the updates to DeclContexts.
2739 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
2740 I = UpdatedDeclContexts.begin(),
2741 E = UpdatedDeclContexts.end();
Sebastian Redla4071b42010-08-24 00:50:09 +00002742 I != E; ++I)
2743 WriteDeclContextVisibleUpdate(*I);
2744
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002745 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002746
Sebastian Redl98912122010-07-27 23:01:28 +00002747 Record.clear();
2748 Record.push_back(NumStatements);
2749 Record.push_back(NumMacros);
2750 Record.push_back(NumLexicalDeclContexts);
2751 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002752 WriteDeclReplacementsBlock();
Sebastian Redl539c5062010-08-18 23:57:32 +00002753 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl143413f2010-07-12 22:02:52 +00002754 Stream.ExitBlock();
2755}
2756
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002757void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002758 if (DeclUpdates.empty())
2759 return;
2760
2761 RecordData OffsetsRecord;
2762 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
2763 for (DeclUpdateMap::iterator
2764 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
2765 const Decl *D = I->first;
2766 UpdateRecord &URec = I->second;
2767
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00002768 if (DeclsToRewrite.count(D))
2769 continue; // The decl will be written completely,no need to store updates.
2770
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002771 uint64_t Offset = Stream.GetCurrentBitNo();
2772 Stream.EmitRecord(DECL_UPDATES, URec);
2773
2774 OffsetsRecord.push_back(GetDeclRef(D));
2775 OffsetsRecord.push_back(Offset);
2776 }
2777 Stream.ExitBlock();
2778 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
2779}
2780
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002781void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002782 if (ReplacedDecls.empty())
2783 return;
2784
2785 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002786 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002787 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
2788 Record.push_back(I->first);
2789 Record.push_back(I->second);
2790 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002791 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002792}
2793
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002794void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002795 Record.push_back(Loc.getRawEncoding());
2796}
2797
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002798void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00002799 AddSourceLocation(Range.getBegin(), Record);
2800 AddSourceLocation(Range.getEnd(), Record);
2801}
2802
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002803void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002804 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00002805 const uint64_t *Words = Value.getRawData();
2806 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002807}
2808
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002809void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00002810 Record.push_back(Value.isUnsigned());
2811 AddAPInt(Value, Record);
2812}
2813
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002814void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00002815 AddAPInt(Value.bitcastToAPInt(), Record);
2816}
2817
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002818void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002819 Record.push_back(getIdentifierRef(II));
2820}
2821
Sebastian Redl539c5062010-08-18 23:57:32 +00002822IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002823 if (II == 0)
2824 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002825
Sebastian Redl539c5062010-08-18 23:57:32 +00002826 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002827 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00002828 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002829 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002830}
2831
Sebastian Redl50e26582010-09-15 19:54:06 +00002832MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002833 if (MD == 0)
2834 return 0;
Sebastian Redl50e26582010-09-15 19:54:06 +00002835
2836 MacroID &ID = MacroDefinitions[MD];
Douglas Gregoraae92242010-03-19 21:51:54 +00002837 if (ID == 0)
Douglas Gregor91096292010-10-02 19:29:26 +00002838 ID = NextMacroID++;
Douglas Gregoraae92242010-03-19 21:51:54 +00002839 return ID;
2840}
2841
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002842void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00002843 Record.push_back(getSelectorRef(SelRef));
2844}
2845
Sebastian Redl539c5062010-08-18 23:57:32 +00002846SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00002847 if (Sel.getAsOpaquePtr() == 0) {
2848 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00002849 }
2850
Sebastian Redl539c5062010-08-18 23:57:32 +00002851 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00002852 if (SID == 0 && Chain) {
2853 // This might trigger a ReadSelector callback, which will set the ID for
2854 // this selector.
2855 Chain->LoadSelector(Sel);
2856 }
Steve Naroff2ddea052009-04-23 10:39:46 +00002857 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00002858 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00002859 }
Sebastian Redl834bb972010-08-04 17:20:04 +00002860 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00002861}
2862
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002863void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00002864 AddDeclRef(Temp->getDestructor(), Record);
2865}
2866
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002867void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
2868 CXXBaseSpecifier const *BasesEnd,
2869 RecordDataImpl &Record) {
2870 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
2871 CXXBaseSpecifiersToWrite.push_back(
2872 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
2873 Bases, BasesEnd));
2874 Record.push_back(NextCXXBaseSpecifiersID++);
2875}
2876
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002877void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002878 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002879 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002880 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00002881 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002882 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00002883 break;
2884 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002885 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00002886 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002887 case TemplateArgument::Template:
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002888 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2889 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002890 break;
John McCall0ad16662009-10-29 08:12:44 +00002891 case TemplateArgument::Null:
2892 case TemplateArgument::Integral:
2893 case TemplateArgument::Declaration:
2894 case TemplateArgument::Pack:
2895 break;
2896 }
2897}
2898
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002899void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002900 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002901 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002902
2903 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
2904 bool InfoHasSameExpr
2905 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
2906 Record.push_back(InfoHasSameExpr);
2907 if (InfoHasSameExpr)
2908 return; // Avoid storing the same expr twice.
2909 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002910 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
2911 Record);
2912}
2913
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002914void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00002915 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00002916 AddTypeRef(QualType(), Record);
2917 return;
2918 }
2919
John McCallbcd03502009-12-07 02:54:59 +00002920 AddTypeRef(TInfo->getType(), Record);
John McCall8f115c62009-10-16 21:56:05 +00002921 TypeLocWriter TLW(*this, Record);
John McCallbcd03502009-12-07 02:54:59 +00002922 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002923 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00002924}
2925
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002926void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00002927 Record.push_back(GetOrCreateTypeID(T));
2928}
2929
2930TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00002931 return MakeTypeID(T,
2932 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
2933}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002934
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002935TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00002936 return MakeTypeID(T,
2937 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002938}
2939
2940TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
2941 if (T.isNull())
2942 return TypeIdx();
2943 assert(!T.getLocalFastQualifiers());
2944
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002945 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002946 if (Idx.getIndex() == 0) {
Douglas Gregor1970d882009-04-26 03:49:13 +00002947 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00002948 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002949 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002950 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00002951 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002952 return Idx;
2953}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002954
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002955TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002956 if (T.isNull())
2957 return TypeIdx();
2958 assert(!T.getLocalFastQualifiers());
2959
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002960 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
2961 assert(I != TypeIdxs.end() && "Type not emitted!");
2962 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002963}
2964
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002965void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002966 Record.push_back(GetDeclRef(D));
2967}
2968
Sebastian Redl539c5062010-08-18 23:57:32 +00002969DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002970 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002971 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002972 }
Douglas Gregor9b3932c2010-10-05 18:37:06 +00002973 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00002974 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00002975 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002976 // We haven't seen this declaration before. Give it a new ID and
2977 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00002978 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002979 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002980 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
2981 // We don't add it to the replacement collection here, because we don't
2982 // have the offset yet.
2983 DeclTypesToEmit.push(const_cast<Decl *>(D));
2984 // Reset the flag, so that we don't add this decl multiple times.
2985 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002986 }
2987
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002988 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002989}
2990
Sebastian Redl539c5062010-08-18 23:57:32 +00002991DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00002992 if (D == 0)
2993 return 0;
2994
2995 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2996 return DeclIDs[D];
2997}
2998
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002999void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00003000 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003001 Record.push_back(Name.getNameKind());
3002 switch (Name.getNameKind()) {
3003 case DeclarationName::Identifier:
3004 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3005 break;
3006
3007 case DeclarationName::ObjCZeroArgSelector:
3008 case DeclarationName::ObjCOneArgSelector:
3009 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00003010 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003011 break;
3012
3013 case DeclarationName::CXXConstructorName:
3014 case DeclarationName::CXXDestructorName:
3015 case DeclarationName::CXXConversionFunctionName:
3016 AddTypeRef(Name.getCXXNameType(), Record);
3017 break;
3018
3019 case DeclarationName::CXXOperatorName:
3020 Record.push_back(Name.getCXXOverloadedOperator());
3021 break;
3022
Alexis Hunt3d221f22009-11-29 07:34:05 +00003023 case DeclarationName::CXXLiteralOperatorName:
3024 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3025 break;
3026
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003027 case DeclarationName::CXXUsingDirective:
3028 // No extra data to emit
3029 break;
3030 }
3031}
Chris Lattnerca025db2010-05-07 21:43:38 +00003032
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003033void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003034 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003035 switch (Name.getNameKind()) {
3036 case DeclarationName::CXXConstructorName:
3037 case DeclarationName::CXXDestructorName:
3038 case DeclarationName::CXXConversionFunctionName:
3039 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3040 break;
3041
3042 case DeclarationName::CXXOperatorName:
3043 AddSourceLocation(
3044 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3045 Record);
3046 AddSourceLocation(
3047 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3048 Record);
3049 break;
3050
3051 case DeclarationName::CXXLiteralOperatorName:
3052 AddSourceLocation(
3053 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3054 Record);
3055 break;
3056
3057 case DeclarationName::Identifier:
3058 case DeclarationName::ObjCZeroArgSelector:
3059 case DeclarationName::ObjCOneArgSelector:
3060 case DeclarationName::ObjCMultiArgSelector:
3061 case DeclarationName::CXXUsingDirective:
3062 break;
3063 }
3064}
3065
3066void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003067 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003068 AddDeclarationName(NameInfo.getName(), Record);
3069 AddSourceLocation(NameInfo.getLoc(), Record);
3070 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3071}
3072
3073void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003074 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003075 AddNestedNameSpecifier(Info.NNS, Record);
3076 AddSourceRange(Info.NNSRange, Record);
3077 Record.push_back(Info.NumTemplParamLists);
3078 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3079 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3080}
3081
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003082void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003083 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003084 // Nested name specifiers usually aren't too long. I think that 8 would
3085 // typically accomodate the vast majority.
3086 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3087
3088 // Push each of the NNS's onto a stack for serialization in reverse order.
3089 while (NNS) {
3090 NestedNames.push_back(NNS);
3091 NNS = NNS->getPrefix();
3092 }
3093
3094 Record.push_back(NestedNames.size());
3095 while(!NestedNames.empty()) {
3096 NNS = NestedNames.pop_back_val();
3097 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3098 Record.push_back(Kind);
3099 switch (Kind) {
3100 case NestedNameSpecifier::Identifier:
3101 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3102 break;
3103
3104 case NestedNameSpecifier::Namespace:
3105 AddDeclRef(NNS->getAsNamespace(), Record);
3106 break;
3107
3108 case NestedNameSpecifier::TypeSpec:
3109 case NestedNameSpecifier::TypeSpecWithTemplate:
3110 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3111 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3112 break;
3113
3114 case NestedNameSpecifier::Global:
3115 // Don't need to write an associated value.
3116 break;
3117 }
3118 }
3119}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003120
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003121void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003122 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003123 Record.push_back(Kind);
3124 switch (Kind) {
3125 case TemplateName::Template:
3126 AddDeclRef(Name.getAsTemplateDecl(), Record);
3127 break;
3128
3129 case TemplateName::OverloadedTemplate: {
3130 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3131 Record.push_back(OvT->size());
3132 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3133 I != E; ++I)
3134 AddDeclRef(*I, Record);
3135 break;
3136 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003137
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003138 case TemplateName::QualifiedTemplate: {
3139 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3140 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3141 Record.push_back(QualT->hasTemplateKeyword());
3142 AddDeclRef(QualT->getTemplateDecl(), Record);
3143 break;
3144 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003145
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003146 case TemplateName::DependentTemplate: {
3147 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3148 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3149 Record.push_back(DepT->isIdentifier());
3150 if (DepT->isIdentifier())
3151 AddIdentifierRef(DepT->getIdentifier(), Record);
3152 else
3153 Record.push_back(DepT->getOperator());
3154 break;
3155 }
3156 }
3157}
3158
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003159void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003160 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003161 Record.push_back(Arg.getKind());
3162 switch (Arg.getKind()) {
3163 case TemplateArgument::Null:
3164 break;
3165 case TemplateArgument::Type:
3166 AddTypeRef(Arg.getAsType(), Record);
3167 break;
3168 case TemplateArgument::Declaration:
3169 AddDeclRef(Arg.getAsDecl(), Record);
3170 break;
3171 case TemplateArgument::Integral:
3172 AddAPSInt(*Arg.getAsIntegral(), Record);
3173 AddTypeRef(Arg.getIntegralType(), Record);
3174 break;
3175 case TemplateArgument::Template:
3176 AddTemplateName(Arg.getAsTemplate(), Record);
3177 break;
3178 case TemplateArgument::Expression:
3179 AddStmt(Arg.getAsExpr());
3180 break;
3181 case TemplateArgument::Pack:
3182 Record.push_back(Arg.pack_size());
3183 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3184 I != E; ++I)
3185 AddTemplateArgument(*I, Record);
3186 break;
3187 }
3188}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003189
3190void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003191ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003192 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003193 assert(TemplateParams && "No TemplateParams!");
3194 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3195 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3196 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3197 Record.push_back(TemplateParams->size());
3198 for (TemplateParameterList::const_iterator
3199 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3200 P != PEnd; ++P)
3201 AddDeclRef(*P, Record);
3202}
3203
3204/// \brief Emit a template argument list.
3205void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003206ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003207 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003208 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003209 Record.push_back(TemplateArgs->size());
3210 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003211 AddTemplateArgument(TemplateArgs->get(i), Record);
3212}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003213
3214
3215void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003216ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003217 Record.push_back(Set.size());
3218 for (UnresolvedSetImpl::const_iterator
3219 I = Set.begin(), E = Set.end(); I != E; ++I) {
3220 AddDeclRef(I.getDecl(), Record);
3221 Record.push_back(I.getAccess());
3222 }
3223}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003224
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003225void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003226 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003227 Record.push_back(Base.isVirtual());
3228 Record.push_back(Base.isBaseOfClass());
3229 Record.push_back(Base.getAccessSpecifierAsWritten());
Nick Lewycky19b9f952010-07-26 16:56:01 +00003230 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003231 AddSourceRange(Base.getSourceRange(), Record);
3232}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003233
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003234void ASTWriter::FlushCXXBaseSpecifiers() {
3235 RecordData Record;
3236 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3237 Record.clear();
3238
3239 // Record the offset of this base-specifier set.
3240 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3241 if (Index == CXXBaseSpecifiersOffsets.size())
3242 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3243 else {
3244 if (Index > CXXBaseSpecifiersOffsets.size())
3245 CXXBaseSpecifiersOffsets.resize(Index + 1);
3246 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3247 }
3248
3249 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3250 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3251 Record.push_back(BEnd - B);
3252 for (; B != BEnd; ++B)
3253 AddCXXBaseSpecifier(*B, Record);
3254 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00003255
3256 // Flush any expressions that were written as part of the base specifiers.
3257 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003258 }
3259
3260 CXXBaseSpecifiersToWrite.clear();
3261}
3262
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003263void ASTWriter::AddCXXBaseOrMemberInitializers(
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003264 const CXXBaseOrMemberInitializer * const *BaseOrMembers,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003265 unsigned NumBaseOrMembers, RecordDataImpl &Record) {
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003266 Record.push_back(NumBaseOrMembers);
3267 for (unsigned i=0; i != NumBaseOrMembers; ++i) {
3268 const CXXBaseOrMemberInitializer *Init = BaseOrMembers[i];
3269
3270 Record.push_back(Init->isBaseInitializer());
3271 if (Init->isBaseInitializer()) {
3272 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3273 Record.push_back(Init->isBaseVirtual());
3274 } else {
Francois Pichetd583da02010-12-04 09:14:42 +00003275 Record.push_back(Init->isIndirectMemberInitializer());
3276 if (Init->isIndirectMemberInitializer())
3277 AddDeclRef(Init->getIndirectMember(), Record);
3278 else
3279 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003280 }
Francois Pichetd583da02010-12-04 09:14:42 +00003281
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003282 AddSourceLocation(Init->getMemberLocation(), Record);
3283 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003284 AddSourceLocation(Init->getLParenLoc(), Record);
3285 AddSourceLocation(Init->getRParenLoc(), Record);
3286 Record.push_back(Init->isWritten());
3287 if (Init->isWritten()) {
3288 Record.push_back(Init->getSourceOrder());
3289 } else {
3290 Record.push_back(Init->getNumArrayIndices());
3291 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3292 AddDeclRef(Init->getArrayIndex(i), Record);
3293 }
3294 }
3295}
3296
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003297void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3298 assert(D->DefinitionData);
3299 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3300 Record.push_back(Data.UserDeclaredConstructor);
3301 Record.push_back(Data.UserDeclaredCopyConstructor);
3302 Record.push_back(Data.UserDeclaredCopyAssignment);
3303 Record.push_back(Data.UserDeclaredDestructor);
3304 Record.push_back(Data.Aggregate);
3305 Record.push_back(Data.PlainOldData);
3306 Record.push_back(Data.Empty);
3307 Record.push_back(Data.Polymorphic);
3308 Record.push_back(Data.Abstract);
3309 Record.push_back(Data.HasTrivialConstructor);
3310 Record.push_back(Data.HasTrivialCopyConstructor);
3311 Record.push_back(Data.HasTrivialCopyAssignment);
3312 Record.push_back(Data.HasTrivialDestructor);
3313 Record.push_back(Data.ComputedVisibleConversions);
3314 Record.push_back(Data.DeclaredDefaultConstructor);
3315 Record.push_back(Data.DeclaredCopyConstructor);
3316 Record.push_back(Data.DeclaredCopyAssignment);
3317 Record.push_back(Data.DeclaredDestructor);
3318
3319 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003320 if (Data.NumBases > 0)
3321 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3322 Record);
3323
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003324 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3325 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003326 if (Data.NumVBases > 0)
3327 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3328 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003329
3330 AddUnresolvedSet(Data.Conversions, Record);
3331 AddUnresolvedSet(Data.VisibleConversions, Record);
3332 // Data.Definition is the owning decl, no need to write it.
3333 AddDeclRef(Data.FirstFriend, Record);
3334}
3335
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003336void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00003337 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003338 assert(!Chain && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00003339 assert(FirstDeclID == NextDeclID &&
3340 FirstTypeID == NextTypeID &&
3341 FirstIdentID == NextIdentID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00003342 FirstSelectorID == NextSelectorID &&
Douglas Gregor91096292010-10-02 19:29:26 +00003343 FirstMacroID == NextMacroID &&
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003344 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00003345 "Setting chain after writing has started.");
3346 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003347
3348 FirstDeclID += Chain->getTotalNumDecls();
3349 FirstTypeID += Chain->getTotalNumTypes();
3350 FirstIdentID += Chain->getTotalNumIdentifiers();
3351 FirstSelectorID += Chain->getTotalNumSelectors();
3352 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003353 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003354 NextDeclID = FirstDeclID;
3355 NextTypeID = FirstTypeID;
3356 NextIdentID = FirstIdentID;
3357 NextSelectorID = FirstSelectorID;
3358 NextMacroID = FirstMacroID;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003359 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00003360}
3361
Sebastian Redl539c5062010-08-18 23:57:32 +00003362void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00003363 IdentifierIDs[II] = ID;
3364}
3365
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003366void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003367 // Always take the highest-numbered type index. This copes with an interesting
3368 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003369 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003370 // keep the higher-numbered entry so that we can properly write it out to
3371 // the AST file.
3372 TypeIdx &StoredIdx = TypeIdxs[T];
3373 if (Idx.getIndex() >= StoredIdx.getIndex())
3374 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003375}
3376
Sebastian Redl539c5062010-08-18 23:57:32 +00003377void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003378 DeclIDs[D] = ID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003379}
Sebastian Redl834bb972010-08-04 17:20:04 +00003380
Sebastian Redl539c5062010-08-18 23:57:32 +00003381void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003382 SelectorIDs[S] = ID;
3383}
Douglas Gregor91096292010-10-02 19:29:26 +00003384
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003385void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00003386 MacroDefinition *MD) {
3387 MacroDefinitions[MD] = ID;
3388}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003389
3390void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3391 assert(D->isDefinition());
3392 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3393 // We are interested when a PCH decl is modified.
3394 if (RD->getPCHLevel() > 0) {
3395 // A forward reference was mutated into a definition. Rewrite it.
3396 // FIXME: This happens during template instantiation, should we
3397 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003398 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003399 }
3400
3401 for (CXXRecordDecl::redecl_iterator
3402 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3403 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3404 if (Redecl == RD)
3405 continue;
3406
3407 // We are interested when a PCH decl is modified.
3408 if (Redecl->getPCHLevel() > 0) {
3409 UpdateRecord &Record = DeclUpdates[Redecl];
3410 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3411 assert(Redecl->DefinitionData);
3412 assert(Redecl->DefinitionData->Definition == D);
3413 AddDeclRef(D, Record); // the DefinitionDecl
3414 }
3415 }
3416 }
3417}
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003418void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3419 // TU and namespaces are handled elsewhere.
3420 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3421 return;
3422
3423 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3424 return; // Not a source decl added to a DeclContext from PCH.
3425
3426 AddUpdatedDeclContext(DC);
3427}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003428
3429void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3430 assert(D->isImplicit());
3431 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3432 return; // Not a source member added to a class from PCH.
3433 if (!isa<CXXMethodDecl>(D))
3434 return; // We are interested in lazily declared implicit methods.
3435
3436 // A decl coming from PCH was modified.
3437 assert(RD->isDefinition());
3438 UpdateRecord &Record = DeclUpdates[RD];
3439 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3440 AddDeclRef(D, Record);
3441}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003442
3443void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3444 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00003445 // The specializations set is kept in the canonical template.
3446 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003447 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3448 return; // Not a source specialization added to a template from PCH.
3449
3450 UpdateRecord &Record = DeclUpdates[TD];
3451 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3452 AddDeclRef(D, Record);
3453}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003454
3455ASTSerializationListener::~ASTSerializationListener() { }