blob: 3f537d21ab574fb5bc3b4ecc9552edc466454aa1 [file] [log] [blame]
Sebastian Redl4ee2ad02010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregor2cf26342009-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 Redla4232eb2010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregor2cf26342009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl7faa2ec2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000015#include "ASTCommon.h"
Douglas Gregore737f502010-08-12 20:07:10 +000016#include "clang/Sema/Sema.h"
17#include "clang/Sema/IdentifierResolver.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclContextInternals.h"
John McCall2a7fb272010-08-25 05:32:35 +000021#include "clang/AST/DeclTemplate.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000022#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000023#include "clang/AST/ExprCXX.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000024#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000025#include "clang/AST/TypeLocVisitor.h"
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000026#include "clang/Serialization/ASTReader.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000027#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000028#include "clang/Lex/PreprocessingRecord.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000029#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000030#include "clang/Lex/HeaderSearch.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000031#include "clang/Basic/FileManager.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000032#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000033#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000034#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000035#include "clang/Basic/TargetInfo.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000036#include "clang/Basic/Version.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000037#include "llvm/ADT/APFloat.h"
38#include "llvm/ADT/APInt.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000039#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000040#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000041#include "llvm/Support/MemoryBuffer.h"
Douglas Gregorb64c1932009-05-12 01:31:05 +000042#include "llvm/System/Path.h"
Chris Lattner3c304bd2009-04-11 18:40:46 +000043#include <cstdio>
Douglas Gregor2cf26342009-04-09 22:27:44 +000044using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000045using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000046
Sebastian Redlade50002010-07-30 17:03:48 +000047template <typename T, typename Allocator>
48T *data(std::vector<T, Allocator> &v) {
49 return v.empty() ? 0 : &v.front();
50}
51template <typename T, typename Allocator>
52const T *data(const std::vector<T, Allocator> &v) {
53 return v.empty() ? 0 : &v.front();
54}
55
Douglas Gregor2cf26342009-04-09 22:27:44 +000056//===----------------------------------------------------------------------===//
57// Type serialization
58//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000059
Douglas Gregor2cf26342009-04-09 22:27:44 +000060namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000061 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000062 ASTWriter &Writer;
63 ASTWriter::RecordData &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000064
65 public:
66 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000067 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000068
Sebastian Redl3397c552010-08-18 23:56:27 +000069 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordData &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000070 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000071
72 void VisitArrayType(const ArrayType *T);
73 void VisitFunctionType(const FunctionType *T);
74 void VisitTagType(const TagType *T);
75
76#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
77#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000078#include "clang/AST/TypeNodes.def"
79 };
80}
81
Sebastian Redl3397c552010-08-18 23:56:27 +000082void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000083 assert(false && "Built-in types are never serialized");
84}
85
Sebastian Redl3397c552010-08-18 23:56:27 +000086void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000087 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +000088 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +000089}
90
Sebastian Redl3397c552010-08-18 23:56:27 +000091void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000092 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +000093 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +000094}
95
Sebastian Redl3397c552010-08-18 23:56:27 +000096void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +000097 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +000098 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +000099}
100
Sebastian Redl3397c552010-08-18 23:56:27 +0000101void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000102 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000103 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000104}
105
Sebastian Redl3397c552010-08-18 23:56:27 +0000106void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000107 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000108 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000109}
110
Sebastian Redl3397c552010-08-18 23:56:27 +0000111void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000112 Writer.AddTypeRef(T->getPointeeType(), Record);
113 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000114 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000115}
116
Sebastian Redl3397c552010-08-18 23:56:27 +0000117void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000118 Writer.AddTypeRef(T->getElementType(), Record);
119 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000120 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000121}
122
Sebastian Redl3397c552010-08-18 23:56:27 +0000123void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000124 VisitArrayType(T);
125 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000126 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000127}
128
Sebastian Redl3397c552010-08-18 23:56:27 +0000129void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000130 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000131 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000132}
133
Sebastian Redl3397c552010-08-18 23:56:27 +0000134void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000135 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000136 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
137 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000138 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000139 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000140}
141
Sebastian Redl3397c552010-08-18 23:56:27 +0000142void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000143 Writer.AddTypeRef(T->getElementType(), Record);
144 Record.push_back(T->getNumElements());
Chris Lattner788b0fd2010-06-23 06:00:24 +0000145 Record.push_back(T->getAltiVecSpecific());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000146 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000147}
148
Sebastian Redl3397c552010-08-18 23:56:27 +0000149void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000150 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000151 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000152}
153
Sebastian Redl3397c552010-08-18 23:56:27 +0000154void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000155 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000156 FunctionType::ExtInfo C = T->getExtInfo();
157 Record.push_back(C.getNoReturn());
Rafael Espindola425ef722010-03-30 22:15:11 +0000158 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000159 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000160 Record.push_back(C.getCC());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000161}
162
Sebastian Redl3397c552010-08-18 23:56:27 +0000163void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000164 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000165 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000166}
167
Sebastian Redl3397c552010-08-18 23:56:27 +0000168void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000169 VisitFunctionType(T);
170 Record.push_back(T->getNumArgs());
171 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
172 Writer.AddTypeRef(T->getArgType(I), Record);
173 Record.push_back(T->isVariadic());
174 Record.push_back(T->getTypeQuals());
Sebastian Redl465226e2009-05-27 22:11:52 +0000175 Record.push_back(T->hasExceptionSpec());
176 Record.push_back(T->hasAnyExceptionSpec());
177 Record.push_back(T->getNumExceptions());
178 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
179 Writer.AddTypeRef(T->getExceptionType(I), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000180 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000181}
182
Sebastian Redl3397c552010-08-18 23:56:27 +0000183void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000184 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000185 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000186}
John McCalled976492009-12-04 22:46:56 +0000187
Sebastian Redl3397c552010-08-18 23:56:27 +0000188void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000189 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000190 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
191 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000192 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000193}
194
Sebastian Redl3397c552010-08-18 23:56:27 +0000195void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000196 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000197 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000198}
199
Sebastian Redl3397c552010-08-18 23:56:27 +0000200void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000201 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000202 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000203}
204
Sebastian Redl3397c552010-08-18 23:56:27 +0000205void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson395b4752009-06-24 19:06:50 +0000206 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000207 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000208}
209
Sebastian Redl3397c552010-08-18 23:56:27 +0000210void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000211 Record.push_back(T->isDependentType());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000212 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000213 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000214 "Cannot serialize in the middle of a type definition");
215}
216
Sebastian Redl3397c552010-08-18 23:56:27 +0000217void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000218 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000219 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000220}
221
Sebastian Redl3397c552010-08-18 23:56:27 +0000222void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000223 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000224 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000225}
226
Mike Stump1eb44332009-09-09 15:08:12 +0000227void
Sebastian Redl3397c552010-08-18 23:56:27 +0000228ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000229 const SubstTemplateTypeParmType *T) {
230 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
231 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000232 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000233}
234
235void
Sebastian Redl3397c552010-08-18 23:56:27 +0000236ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000237 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000238 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000239 Writer.AddTemplateName(T->getTemplateName(), Record);
240 Record.push_back(T->getNumArgs());
241 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
242 ArgI != ArgE; ++ArgI)
243 Writer.AddTemplateArgument(*ArgI, Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000244 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
245 : T->getCanonicalTypeInternal(),
246 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000247 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000248}
249
250void
Sebastian Redl3397c552010-08-18 23:56:27 +0000251ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000252 VisitArrayType(T);
253 Writer.AddStmt(T->getSizeExpr());
254 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000255 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000256}
257
258void
Sebastian Redl3397c552010-08-18 23:56:27 +0000259ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000260 const DependentSizedExtVectorType *T) {
261 // FIXME: Serialize this type (C++ only)
262 assert(false && "Cannot serialize dependent sized extended vector types");
263}
264
265void
Sebastian Redl3397c552010-08-18 23:56:27 +0000266ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000267 Record.push_back(T->getDepth());
268 Record.push_back(T->getIndex());
269 Record.push_back(T->isParameterPack());
270 Writer.AddIdentifierRef(T->getName(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000271 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000272}
273
274void
Sebastian Redl3397c552010-08-18 23:56:27 +0000275ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000276 Record.push_back(T->getKeyword());
277 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
278 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000279 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
280 : T->getCanonicalTypeInternal(),
281 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000282 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000283}
284
285void
Sebastian Redl3397c552010-08-18 23:56:27 +0000286ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000287 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000288 Record.push_back(T->getKeyword());
289 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
290 Writer.AddIdentifierRef(T->getIdentifier(), Record);
291 Record.push_back(T->getNumArgs());
292 for (DependentTemplateSpecializationType::iterator
293 I = T->begin(), E = T->end(); I != E; ++I)
294 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000295 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000296}
297
Sebastian Redl3397c552010-08-18 23:56:27 +0000298void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000299 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000300 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
301 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000302 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000303}
304
Sebastian Redl3397c552010-08-18 23:56:27 +0000305void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCall3cb0ebd2010-03-10 03:28:59 +0000306 Writer.AddDeclRef(T->getDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000307 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000308 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000309}
310
Sebastian Redl3397c552010-08-18 23:56:27 +0000311void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000312 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000313 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000314}
315
Sebastian Redl3397c552010-08-18 23:56:27 +0000316void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000317 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000318 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000319 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000320 E = T->qual_end(); I != E; ++I)
321 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000322 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000323}
324
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000325void
Sebastian Redl3397c552010-08-18 23:56:27 +0000326ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000327 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000328 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000329}
330
John McCalla1ee0c52009-10-16 21:56:05 +0000331namespace {
332
333class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000334 ASTWriter &Writer;
335 ASTWriter::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000336
337public:
Sebastian Redla4232eb2010-08-18 23:56:21 +0000338 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordData &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000339 : Writer(Writer), Record(Record) { }
340
John McCall51bd8032009-10-18 01:05:36 +0000341#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000342#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000343 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000344#include "clang/AST/TypeLocNodes.def"
345
John McCall51bd8032009-10-18 01:05:36 +0000346 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
347 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000348};
349
350}
351
John McCall51bd8032009-10-18 01:05:36 +0000352void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
353 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000354}
John McCall51bd8032009-10-18 01:05:36 +0000355void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000356 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
357 if (TL.needsExtraLocalData()) {
358 Record.push_back(TL.getWrittenTypeSpec());
359 Record.push_back(TL.getWrittenSignSpec());
360 Record.push_back(TL.getWrittenWidthSpec());
361 Record.push_back(TL.hasModeAttr());
362 }
John McCalla1ee0c52009-10-16 21:56:05 +0000363}
John McCall51bd8032009-10-18 01:05:36 +0000364void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
365 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000366}
John McCall51bd8032009-10-18 01:05:36 +0000367void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
368 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000369}
John McCall51bd8032009-10-18 01:05:36 +0000370void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
371 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000372}
John McCall51bd8032009-10-18 01:05:36 +0000373void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
374 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000375}
John McCall51bd8032009-10-18 01:05:36 +0000376void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
377 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000378}
John McCall51bd8032009-10-18 01:05:36 +0000379void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
380 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000381}
John McCall51bd8032009-10-18 01:05:36 +0000382void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
383 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
384 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
385 Record.push_back(TL.getSizeExpr() ? 1 : 0);
386 if (TL.getSizeExpr())
387 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000388}
John McCall51bd8032009-10-18 01:05:36 +0000389void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
390 VisitArrayTypeLoc(TL);
391}
392void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
393 VisitArrayTypeLoc(TL);
394}
395void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
396 VisitArrayTypeLoc(TL);
397}
398void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
399 DependentSizedArrayTypeLoc TL) {
400 VisitArrayTypeLoc(TL);
401}
402void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
403 DependentSizedExtVectorTypeLoc TL) {
404 Writer.AddSourceLocation(TL.getNameLoc(), Record);
405}
406void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
407 Writer.AddSourceLocation(TL.getNameLoc(), Record);
408}
409void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
410 Writer.AddSourceLocation(TL.getNameLoc(), Record);
411}
412void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
413 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
414 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
415 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
416 Writer.AddDeclRef(TL.getArg(i), Record);
417}
418void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
419 VisitFunctionTypeLoc(TL);
420}
421void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
422 VisitFunctionTypeLoc(TL);
423}
John McCalled976492009-12-04 22:46:56 +0000424void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
425 Writer.AddSourceLocation(TL.getNameLoc(), Record);
426}
John McCall51bd8032009-10-18 01:05:36 +0000427void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
428 Writer.AddSourceLocation(TL.getNameLoc(), Record);
429}
430void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000431 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
432 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
433 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000434}
435void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000436 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
437 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
438 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
439 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000440}
441void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
442 Writer.AddSourceLocation(TL.getNameLoc(), Record);
443}
444void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
445 Writer.AddSourceLocation(TL.getNameLoc(), Record);
446}
447void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
448 Writer.AddSourceLocation(TL.getNameLoc(), Record);
449}
John McCall51bd8032009-10-18 01:05:36 +0000450void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
451 Writer.AddSourceLocation(TL.getNameLoc(), Record);
452}
John McCall49a832b2009-10-18 09:09:24 +0000453void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
454 SubstTemplateTypeParmTypeLoc TL) {
455 Writer.AddSourceLocation(TL.getNameLoc(), Record);
456}
John McCall51bd8032009-10-18 01:05:36 +0000457void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
458 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000459 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
460 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
461 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
462 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000463 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
464 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000465}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000466void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000467 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
468 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000469}
John McCall3cb0ebd2010-03-10 03:28:59 +0000470void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
471 Writer.AddSourceLocation(TL.getNameLoc(), Record);
472}
Douglas Gregor4714c122010-03-31 17:34:00 +0000473void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000474 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
475 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000476 Writer.AddSourceLocation(TL.getNameLoc(), Record);
477}
John McCall33500952010-06-11 00:33:02 +0000478void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
479 DependentTemplateSpecializationTypeLoc TL) {
480 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
481 Writer.AddSourceRange(TL.getQualifierRange(), Record);
482 Writer.AddSourceLocation(TL.getNameLoc(), Record);
483 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
484 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
485 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000486 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
487 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000488}
John McCall51bd8032009-10-18 01:05:36 +0000489void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
490 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000491}
492void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
493 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000494 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
495 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
496 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
497 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000498}
John McCall54e14c42009-10-22 22:37:11 +0000499void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
500 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000501}
John McCalla1ee0c52009-10-16 21:56:05 +0000502
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000503//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000504// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000505//===----------------------------------------------------------------------===//
506
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000507static void EmitBlockID(unsigned ID, const char *Name,
508 llvm::BitstreamWriter &Stream,
Sebastian Redla4232eb2010-08-18 23:56:21 +0000509 ASTWriter::RecordData &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000510 Record.clear();
511 Record.push_back(ID);
512 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
513
514 // Emit the block name if present.
515 if (Name == 0 || Name[0] == 0) return;
516 Record.clear();
517 while (*Name)
518 Record.push_back(*Name++);
519 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
520}
521
522static void EmitRecordID(unsigned ID, const char *Name,
523 llvm::BitstreamWriter &Stream,
Sebastian Redla4232eb2010-08-18 23:56:21 +0000524 ASTWriter::RecordData &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000525 Record.clear();
526 Record.push_back(ID);
527 while (*Name)
528 Record.push_back(*Name++);
529 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000530}
531
532static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Sebastian Redla4232eb2010-08-18 23:56:21 +0000533 ASTWriter::RecordData &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000534#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000535 RECORD(STMT_STOP);
536 RECORD(STMT_NULL_PTR);
537 RECORD(STMT_NULL);
538 RECORD(STMT_COMPOUND);
539 RECORD(STMT_CASE);
540 RECORD(STMT_DEFAULT);
541 RECORD(STMT_LABEL);
542 RECORD(STMT_IF);
543 RECORD(STMT_SWITCH);
544 RECORD(STMT_WHILE);
545 RECORD(STMT_DO);
546 RECORD(STMT_FOR);
547 RECORD(STMT_GOTO);
548 RECORD(STMT_INDIRECT_GOTO);
549 RECORD(STMT_CONTINUE);
550 RECORD(STMT_BREAK);
551 RECORD(STMT_RETURN);
552 RECORD(STMT_DECL);
553 RECORD(STMT_ASM);
554 RECORD(EXPR_PREDEFINED);
555 RECORD(EXPR_DECL_REF);
556 RECORD(EXPR_INTEGER_LITERAL);
557 RECORD(EXPR_FLOATING_LITERAL);
558 RECORD(EXPR_IMAGINARY_LITERAL);
559 RECORD(EXPR_STRING_LITERAL);
560 RECORD(EXPR_CHARACTER_LITERAL);
561 RECORD(EXPR_PAREN);
562 RECORD(EXPR_UNARY_OPERATOR);
563 RECORD(EXPR_SIZEOF_ALIGN_OF);
564 RECORD(EXPR_ARRAY_SUBSCRIPT);
565 RECORD(EXPR_CALL);
566 RECORD(EXPR_MEMBER);
567 RECORD(EXPR_BINARY_OPERATOR);
568 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
569 RECORD(EXPR_CONDITIONAL_OPERATOR);
570 RECORD(EXPR_IMPLICIT_CAST);
571 RECORD(EXPR_CSTYLE_CAST);
572 RECORD(EXPR_COMPOUND_LITERAL);
573 RECORD(EXPR_EXT_VECTOR_ELEMENT);
574 RECORD(EXPR_INIT_LIST);
575 RECORD(EXPR_DESIGNATED_INIT);
576 RECORD(EXPR_IMPLICIT_VALUE_INIT);
577 RECORD(EXPR_VA_ARG);
578 RECORD(EXPR_ADDR_LABEL);
579 RECORD(EXPR_STMT);
580 RECORD(EXPR_TYPES_COMPATIBLE);
581 RECORD(EXPR_CHOOSE);
582 RECORD(EXPR_GNU_NULL);
583 RECORD(EXPR_SHUFFLE_VECTOR);
584 RECORD(EXPR_BLOCK);
585 RECORD(EXPR_BLOCK_DECL_REF);
586 RECORD(EXPR_OBJC_STRING_LITERAL);
587 RECORD(EXPR_OBJC_ENCODE);
588 RECORD(EXPR_OBJC_SELECTOR_EXPR);
589 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
590 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
591 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
592 RECORD(EXPR_OBJC_KVC_REF_EXPR);
593 RECORD(EXPR_OBJC_MESSAGE_EXPR);
594 RECORD(EXPR_OBJC_SUPER_EXPR);
595 RECORD(STMT_OBJC_FOR_COLLECTION);
596 RECORD(STMT_OBJC_CATCH);
597 RECORD(STMT_OBJC_FINALLY);
598 RECORD(STMT_OBJC_AT_TRY);
599 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
600 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000601 RECORD(EXPR_CXX_OPERATOR_CALL);
602 RECORD(EXPR_CXX_CONSTRUCT);
603 RECORD(EXPR_CXX_STATIC_CAST);
604 RECORD(EXPR_CXX_DYNAMIC_CAST);
605 RECORD(EXPR_CXX_REINTERPRET_CAST);
606 RECORD(EXPR_CXX_CONST_CAST);
607 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
608 RECORD(EXPR_CXX_BOOL_LITERAL);
609 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000610#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000611}
Mike Stump1eb44332009-09-09 15:08:12 +0000612
Sebastian Redla4232eb2010-08-18 23:56:21 +0000613void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000614 RecordData Record;
615 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000616
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000617#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
618#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000619
Sebastian Redl3397c552010-08-18 23:56:27 +0000620 // AST Top-Level Block.
Sebastian Redlf29f0a22010-08-18 23:57:22 +0000621 BLOCK(AST_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000622 RECORD(ORIGINAL_FILE_NAME);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000623 RECORD(TYPE_OFFSET);
624 RECORD(DECL_OFFSET);
625 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000626 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000627 RECORD(IDENTIFIER_OFFSET);
628 RECORD(IDENTIFIER_TABLE);
629 RECORD(EXTERNAL_DEFINITIONS);
630 RECORD(SPECIAL_TYPES);
631 RECORD(STATISTICS);
632 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000633 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000634 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
635 RECORD(SELECTOR_OFFSETS);
636 RECORD(METHOD_POOL);
637 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000638 RECORD(SOURCE_LOCATION_OFFSETS);
639 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000640 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000641 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000642 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000643 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redla93e3b52010-07-08 22:01:51 +0000644 RECORD(CHAINED_METADATA);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000645 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000646
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000647 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000648 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000649 RECORD(SM_SLOC_FILE_ENTRY);
650 RECORD(SM_SLOC_BUFFER_ENTRY);
651 RECORD(SM_SLOC_BUFFER_BLOB);
652 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
653 RECORD(SM_LINE_TABLE);
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000655 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000656 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000657 RECORD(PP_MACRO_OBJECT_LIKE);
658 RECORD(PP_MACRO_FUNCTION_LIKE);
659 RECORD(PP_TOKEN);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000660 RECORD(PP_MACRO_INSTANTIATION);
661 RECORD(PP_MACRO_DEFINITION);
662
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000663 // Decls and Types block.
664 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000665 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000666 RECORD(TYPE_COMPLEX);
667 RECORD(TYPE_POINTER);
668 RECORD(TYPE_BLOCK_POINTER);
669 RECORD(TYPE_LVALUE_REFERENCE);
670 RECORD(TYPE_RVALUE_REFERENCE);
671 RECORD(TYPE_MEMBER_POINTER);
672 RECORD(TYPE_CONSTANT_ARRAY);
673 RECORD(TYPE_INCOMPLETE_ARRAY);
674 RECORD(TYPE_VARIABLE_ARRAY);
675 RECORD(TYPE_VECTOR);
676 RECORD(TYPE_EXT_VECTOR);
677 RECORD(TYPE_FUNCTION_PROTO);
678 RECORD(TYPE_FUNCTION_NO_PROTO);
679 RECORD(TYPE_TYPEDEF);
680 RECORD(TYPE_TYPEOF_EXPR);
681 RECORD(TYPE_TYPEOF);
682 RECORD(TYPE_RECORD);
683 RECORD(TYPE_ENUM);
684 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000685 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000686 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000687 RECORD(DECL_ATTR);
688 RECORD(DECL_TRANSLATION_UNIT);
689 RECORD(DECL_TYPEDEF);
690 RECORD(DECL_ENUM);
691 RECORD(DECL_RECORD);
692 RECORD(DECL_ENUM_CONSTANT);
693 RECORD(DECL_FUNCTION);
694 RECORD(DECL_OBJC_METHOD);
695 RECORD(DECL_OBJC_INTERFACE);
696 RECORD(DECL_OBJC_PROTOCOL);
697 RECORD(DECL_OBJC_IVAR);
698 RECORD(DECL_OBJC_AT_DEFS_FIELD);
699 RECORD(DECL_OBJC_CLASS);
700 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
701 RECORD(DECL_OBJC_CATEGORY);
702 RECORD(DECL_OBJC_CATEGORY_IMPL);
703 RECORD(DECL_OBJC_IMPLEMENTATION);
704 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
705 RECORD(DECL_OBJC_PROPERTY);
706 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000707 RECORD(DECL_FIELD);
708 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000709 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000710 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000711 RECORD(DECL_FILE_SCOPE_ASM);
712 RECORD(DECL_BLOCK);
713 RECORD(DECL_CONTEXT_LEXICAL);
714 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000715 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattner0558df22009-04-27 00:49:53 +0000716 AddStmtsExprs(Stream, Record);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000717#undef RECORD
718#undef BLOCK
719 Stream.ExitBlock();
720}
721
Douglas Gregore650c8c2009-07-07 00:12:59 +0000722/// \brief Adjusts the given filename to only write out the portion of the
723/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000724///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000725/// \param Filename the file name to adjust.
726///
727/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
728/// the returned filename will be adjusted by this system root.
729///
730/// \returns either the original filename (if it needs no adjustment) or the
731/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000732static const char *
Douglas Gregore650c8c2009-07-07 00:12:59 +0000733adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
734 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000735
Douglas Gregore650c8c2009-07-07 00:12:59 +0000736 if (!isysroot)
737 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000738
Douglas Gregore650c8c2009-07-07 00:12:59 +0000739 // Verify that the filename and the system root have the same prefix.
740 unsigned Pos = 0;
741 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
742 if (Filename[Pos] != isysroot[Pos])
743 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Douglas Gregore650c8c2009-07-07 00:12:59 +0000745 // We hit the end of the filename before we hit the end of the system root.
746 if (!Filename[Pos])
747 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000748
Douglas Gregore650c8c2009-07-07 00:12:59 +0000749 // If the file name has a '/' at the current position, skip over the '/'.
750 // We distinguish sysroot-based includes from absolute includes by the
751 // absence of '/' at the beginning of sysroot-based includes.
752 if (Filename[Pos] == '/')
753 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Douglas Gregore650c8c2009-07-07 00:12:59 +0000755 return Filename + Pos;
756}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000757
Sebastian Redl3397c552010-08-18 23:56:27 +0000758/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Sebastian Redla4232eb2010-08-18 23:56:21 +0000759void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000760 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000761
Douglas Gregore650c8c2009-07-07 00:12:59 +0000762 // Metadata
763 const TargetInfo &Target = Context.Target;
764 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl77f46032010-07-09 21:00:24 +0000765 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000766 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl3397c552010-08-18 23:56:27 +0000767 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
768 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregore650c8c2009-07-07 00:12:59 +0000769 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
770 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
771 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl77f46032010-07-09 21:00:24 +0000772 // Target triple or chained PCH name
773 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregore650c8c2009-07-07 00:12:59 +0000774 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Douglas Gregore650c8c2009-07-07 00:12:59 +0000776 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000777 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
778 Record.push_back(VERSION_MAJOR);
779 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000780 Record.push_back(CLANG_VERSION_MAJOR);
781 Record.push_back(CLANG_VERSION_MINOR);
782 Record.push_back(isysroot != 0);
Sebastian Redl77f46032010-07-09 21:00:24 +0000783 // FIXME: This writes the absolute path for chained headers.
784 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
785 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000786
Douglas Gregorb64c1932009-05-12 01:31:05 +0000787 // Original file name
788 SourceManager &SM = Context.getSourceManager();
789 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
790 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000791 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregorb64c1932009-05-12 01:31:05 +0000792 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
793 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
794
795 llvm::sys::Path MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000796
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000797 MainFilePath.makeAbsolute();
Douglas Gregorb64c1932009-05-12 01:31:05 +0000798
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +0000799 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000800 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000801 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000802 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000803 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000804 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000805 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000806
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000807 // Repository branch/version information.
808 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000809 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000810 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
811 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000812 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000813 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000814 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
815 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +0000816}
817
818/// \brief Write the LangOptions structure.
Sebastian Redla4232eb2010-08-18 23:56:21 +0000819void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000820 RecordData Record;
821 Record.push_back(LangOpts.Trigraphs);
822 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
823 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
824 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
825 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000826 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000827 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
828 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
829 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
830 Record.push_back(LangOpts.C99); // C99 Support
831 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
832 Record.push_back(LangOpts.CPlusPlus); // C++ Support
833 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000834 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +0000835
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000836 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
837 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000838 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000839 // modern abi enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000840 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000841 // modern abi enabled.
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +0000842 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000844 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000845 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
846 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000847 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000848 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar73482882010-02-10 18:48:44 +0000849 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000850
851 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
852 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
853 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
854
Chris Lattnerea5ce472009-04-27 07:35:58 +0000855 // Whether static initializers are protected by locks.
856 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +0000857 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000858 Record.push_back(LangOpts.Blocks); // block extension to C
859 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
860 // they are unused.
861 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
862 // (modulo the platform support).
863
Chris Lattnera4d71452010-06-26 21:25:03 +0000864 Record.push_back(LangOpts.getSignedOverflowBehavior());
865 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000866
867 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +0000868 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000869 // defined.
870 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
871 // opposed to __DYNAMIC__).
872 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
873
874 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
875 // used (instead of C99 semantics).
876 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +0000877 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
878 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +0000879 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
880 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +0000881 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000882 Record.push_back(LangOpts.getGCMode());
883 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000884 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000885 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000886 Record.push_back(LangOpts.OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +0000887 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson92f58222009-08-22 22:30:33 +0000888 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +0000889 Record.push_back(LangOpts.SpellChecking);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000890 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000891}
892
Douglas Gregor14f79002009-04-10 03:52:48 +0000893//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000894// stat cache Serialization
895//===----------------------------------------------------------------------===//
896
897namespace {
898// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +0000899class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000900public:
901 typedef const char * key_type;
902 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000903
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000904 typedef std::pair<int, struct stat> data_type;
905 typedef const data_type& data_type_ref;
906
907 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000908 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000909 }
Mike Stump1eb44332009-09-09 15:08:12 +0000910
911 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000912 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
913 data_type_ref Data) {
914 unsigned StrLen = strlen(path);
915 clang::io::Emit16(Out, StrLen);
916 unsigned DataLen = 1; // result value
917 if (Data.first == 0)
918 DataLen += 4 + 4 + 2 + 8 + 8;
919 clang::io::Emit8(Out, DataLen);
920 return std::make_pair(StrLen + 1, DataLen);
921 }
Mike Stump1eb44332009-09-09 15:08:12 +0000922
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000923 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
924 Out.write(path, KeyLen);
925 }
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000927 void EmitData(llvm::raw_ostream& Out, key_type_ref,
928 data_type_ref Data, unsigned DataLen) {
929 using namespace clang::io;
930 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000932 // Result of stat()
933 Emit8(Out, Data.first? 1 : 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000934
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000935 if (Data.first == 0) {
936 Emit32(Out, (uint32_t) Data.second.st_ino);
937 Emit32(Out, (uint32_t) Data.second.st_dev);
938 Emit16(Out, (uint16_t) Data.second.st_mode);
939 Emit64(Out, (uint64_t) Data.second.st_mtime);
940 Emit64(Out, (uint64_t) Data.second.st_size);
941 }
942
943 assert(Out.tell() - Start == DataLen && "Wrong data length");
944 }
945};
946} // end anonymous namespace
947
Sebastian Redl3397c552010-08-18 23:56:27 +0000948/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +0000949void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000950 // Build the on-disk hash table containing information about every
951 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +0000952 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000953 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000954 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000955 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000956 Stat != StatEnd; ++Stat, ++NumStatEntries) {
957 const char *Filename = Stat->first();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000958 Generator.insert(Filename, Stat->second);
959 }
Mike Stump1eb44332009-09-09 15:08:12 +0000960
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000961 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000962 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000963 uint32_t BucketOffset;
964 {
965 llvm::raw_svector_ostream Out(StatCacheData);
966 // Make sure that no bucket is at offset 0
967 clang::io::Emit32(Out, 0);
968 BucketOffset = Generator.Emit(Out);
969 }
970
971 // Create a blob abbreviation
972 using namespace llvm;
973 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000974 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000975 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
976 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
977 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
978 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
979
980 // Write the stat cache
981 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000982 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000983 Record.push_back(BucketOffset);
984 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000985 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000986}
987
988//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +0000989// Source Manager Serialization
990//===----------------------------------------------------------------------===//
991
992/// \brief Create an abbreviation for the SLocEntry that refers to a
993/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000994static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000995 using namespace llvm;
996 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000997 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +0000998 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
999 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1000 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1001 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001002 // FileEntry fields.
1003 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1004 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor12fab312010-03-16 16:35:32 +00001005 // HeaderFileInfo fields.
1006 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
1007 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
1008 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
1009 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregor14f79002009-04-10 03:52:48 +00001010 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001011 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001012}
1013
1014/// \brief Create an abbreviation for the SLocEntry that refers to a
1015/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001016static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001017 using namespace llvm;
1018 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001019 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001020 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1021 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1022 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1023 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1024 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001025 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001026}
1027
1028/// \brief Create an abbreviation for the SLocEntry that refers to a
1029/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001030static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001031 using namespace llvm;
1032 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001033 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001034 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001035 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001036}
1037
1038/// \brief Create an abbreviation for the SLocEntry that refers to an
1039/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001040static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001041 using namespace llvm;
1042 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001043 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001044 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1045 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1046 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1047 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001048 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001049 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001050}
1051
1052/// \brief Writes the block containing the serialized form of the
1053/// source manager.
1054///
1055/// TODO: We should probably use an on-disk hash table (stored in a
1056/// blob), indexed based on the file name, so that we only create
1057/// entries for files that we actually need. In the common case (no
1058/// errors), we probably won't have to create file entries for any of
1059/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001060void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001061 const Preprocessor &PP,
1062 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001063 RecordData Record;
1064
Chris Lattnerf04ad692009-04-10 17:16:57 +00001065 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001066 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001067
1068 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001069 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1070 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1071 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1072 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001073
Douglas Gregorbd945002009-04-13 16:31:14 +00001074 // Write the line table.
1075 if (SourceMgr.hasLineTable()) {
1076 LineTableInfo &LineTable = SourceMgr.getLineTable();
1077
1078 // Emit the file names
1079 Record.push_back(LineTable.getNumFilenames());
1080 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1081 // Emit the file name
1082 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001083 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +00001084 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1085 Record.push_back(FilenameLen);
1086 if (FilenameLen)
1087 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1088 }
Mike Stump1eb44332009-09-09 15:08:12 +00001089
Douglas Gregorbd945002009-04-13 16:31:14 +00001090 // Emit the line entries
1091 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1092 L != LEnd; ++L) {
1093 // Emit the file ID
1094 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +00001095
Douglas Gregorbd945002009-04-13 16:31:14 +00001096 // Emit the line entries
1097 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001098 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +00001099 LEEnd = L->second.end();
1100 LE != LEEnd; ++LE) {
1101 Record.push_back(LE->FileOffset);
1102 Record.push_back(LE->LineNo);
1103 Record.push_back(LE->FilenameID);
1104 Record.push_back((unsigned)LE->FileKind);
1105 Record.push_back(LE->IncludeOffset);
1106 }
Douglas Gregorbd945002009-04-13 16:31:14 +00001107 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001108 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001109 }
1110
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001111 // Write out the source location entry table. We skip the first
1112 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001113 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001114 RecordData PreloadSLocs;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001115 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1116 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1117 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1118 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001119 // Get this source location entry.
1120 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001121
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001122 // Record the offset of this source-location entry.
1123 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1124
1125 // Figure out which record code to use.
1126 unsigned Code;
1127 if (SLoc->isFile()) {
1128 if (SLoc->getFile().getContentCache()->Entry)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001129 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001130 else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001131 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001132 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001133 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001134 Record.clear();
1135 Record.push_back(Code);
1136
1137 Record.push_back(SLoc->getOffset());
1138 if (SLoc->isFile()) {
1139 const SrcMgr::FileInfo &File = SLoc->getFile();
1140 Record.push_back(File.getIncludeLoc().getRawEncoding());
1141 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1142 Record.push_back(File.hasLineDirectives());
1143
1144 const SrcMgr::ContentCache *Content = File.getContentCache();
1145 if (Content->Entry) {
1146 // The source location entry is a file. The blob associated
1147 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001148
Douglas Gregor2d52be52010-03-21 22:49:54 +00001149 // Emit size/modification time for this file.
1150 Record.push_back(Content->Entry->getSize());
1151 Record.push_back(Content->Entry->getModificationTime());
1152
Douglas Gregor12fab312010-03-16 16:35:32 +00001153 // Emit header-search information associated with this file.
1154 HeaderFileInfo HFI;
1155 HeaderSearch &HS = PP.getHeaderSearchInfo();
1156 if (Content->Entry->getUID() < HS.header_file_size())
1157 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1158 Record.push_back(HFI.isImport);
1159 Record.push_back(HFI.DirInfo);
1160 Record.push_back(HFI.NumIncludes);
1161 AddIdentifierRef(HFI.ControllingMacro, Record);
1162
Douglas Gregore650c8c2009-07-07 00:12:59 +00001163 // Turn the file name into an absolute path, if it isn't already.
1164 const char *Filename = Content->Entry->getName();
1165 llvm::sys::Path FilePath(Filename, strlen(Filename));
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001166 FilePath.makeAbsolute();
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001167 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001168
Douglas Gregore650c8c2009-07-07 00:12:59 +00001169 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001170 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001171
1172 // FIXME: For now, preload all file source locations, so that
1173 // we get the appropriate File entries in the reader. This is
1174 // a temporary measure.
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001175 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001176 } else {
1177 // The source location entry is a buffer. The blob associated
1178 // with this entry contains the contents of the buffer.
1179
1180 // We add one to the size so that we capture the trailing NULL
1181 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1182 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001183 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001184 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001185 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001186 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1187 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001188 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001189 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001190 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001191 llvm::StringRef(Buffer->getBufferStart(),
1192 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001193
1194 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001195 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001196 }
1197 } else {
1198 // The source location entry is an instantiation.
1199 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1200 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1201 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1202 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1203
1204 // Compute the token length for this macro expansion.
1205 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001206 if (I + 1 != N)
1207 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001208 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1209 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1210 }
1211 }
1212
Douglas Gregorc9490c02009-04-16 22:23:12 +00001213 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001214
1215 if (SLocEntryOffsets.empty())
1216 return;
1217
Sebastian Redl3397c552010-08-18 23:56:27 +00001218 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001219 // table is used for lazily loading source-location information.
1220 using namespace llvm;
1221 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001222 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001223 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1224 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1225 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1226 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001227
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001228 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001229 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001230 Record.push_back(SLocEntryOffsets.size());
Sebastian Redl8db9fae2010-09-22 20:19:08 +00001231 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1232 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001233 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001234 (const char *)data(SLocEntryOffsets),
Chris Lattner090d9b52009-04-27 19:01:47 +00001235 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001236
Sebastian Redl3397c552010-08-18 23:56:27 +00001237 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001238 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001239 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001240}
1241
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001242//===----------------------------------------------------------------------===//
1243// Preprocessor Serialization
1244//===----------------------------------------------------------------------===//
1245
Chris Lattner0b1fb982009-04-10 17:15:23 +00001246/// \brief Writes the block containing the serialized form of the
1247/// preprocessor.
1248///
Sebastian Redla4232eb2010-08-18 23:56:21 +00001249void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001250 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001251
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001252 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1253 if (PP.getCounterValue() != 0) {
1254 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001255 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001256 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001257 }
1258
1259 // Enter the preprocessor block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001260 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 2);
Mike Stump1eb44332009-09-09 15:08:12 +00001261
Sebastian Redl3397c552010-08-18 23:56:27 +00001262 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001263 // FIXME: use diagnostics subsystem for localization etc.
1264 if (PP.SawDateOrTime())
1265 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001266
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001267 // Loop over all the macro definitions that are live at the end of the file,
1268 // emitting each to the PP section.
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001269 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001270 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1271 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001272 // FIXME: This emits macros in hash table order, we should do it in a stable
1273 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001274 MacroInfo *MI = I->second;
1275
Sebastian Redl3397c552010-08-18 23:56:27 +00001276 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001277 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl3397c552010-08-18 23:56:27 +00001278 // Also skip macros from a AST file if we're chaining.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001279 if (MI->isBuiltinMacro() || (Chain && MI->isFromAST()))
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001280 continue;
1281
Chris Lattner7356a312009-04-11 21:15:38 +00001282 AddIdentifierRef(I->first, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001283 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001284 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1285 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001286
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001287 unsigned Code;
1288 if (MI->isObjectLike()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001289 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001290 } else {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001291 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001292
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001293 Record.push_back(MI->isC99Varargs());
1294 Record.push_back(MI->isGNUVarargs());
1295 Record.push_back(MI->getNumArgs());
1296 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1297 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001298 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001299 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001300
1301 // If we have a detailed preprocessing record, record the macro definition
1302 // ID that corresponds to this macro.
1303 if (PPRec)
1304 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
1305
Douglas Gregorc9490c02009-04-16 22:23:12 +00001306 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001307 Record.clear();
1308
Chris Lattnerdf961c22009-04-10 18:08:30 +00001309 // Emit the tokens array.
1310 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1311 // Note that we know that the preprocessor does not have any annotation
1312 // tokens in it because they are created by the parser, and thus can't be
1313 // in a macro definition.
1314 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001315
Chris Lattnerdf961c22009-04-10 18:08:30 +00001316 Record.push_back(Tok.getLocation().getRawEncoding());
1317 Record.push_back(Tok.getLength());
1318
Chris Lattnerdf961c22009-04-10 18:08:30 +00001319 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1320 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001321 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001322
Chris Lattnerdf961c22009-04-10 18:08:30 +00001323 // FIXME: Should translate token kind to a stable encoding.
1324 Record.push_back(Tok.getKind());
1325 // FIXME: Should translate token flags to a stable encoding.
1326 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001327
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001328 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001329 Record.clear();
1330 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001331 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001332 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001333
1334 // If the preprocessor has a preprocessing record, emit it.
1335 unsigned NumPreprocessingRecords = 0;
1336 if (PPRec) {
Sebastian Redl196f5572010-09-27 23:20:01 +00001337 unsigned IndexBase = Chain ? PPRec->getNumPreallocatedEntities() : 0;
Sebastian Redlb57a6242010-09-27 22:18:47 +00001338 for (PreprocessingRecord::iterator E = PPRec->begin(Chain),
1339 EEnd = PPRec->end(Chain);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001340 E != EEnd; ++E) {
1341 Record.clear();
1342
1343 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
Sebastian Redlb57a6242010-09-27 22:18:47 +00001344 Record.push_back(IndexBase + NumPreprocessingRecords++);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001345 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1346 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1347 AddIdentifierRef(MI->getName(), Record);
1348 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001349 Stream.EmitRecord(PP_MACRO_INSTANTIATION, Record);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001350 continue;
1351 }
1352
1353 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1354 // Record this macro definition's location.
Sebastian Redlf73c93f2010-09-15 19:54:06 +00001355 MacroID ID = getMacroDefinitionID(MD);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001356 if (ID != MacroDefinitionOffsets.size()) {
1357 if (ID > MacroDefinitionOffsets.size())
1358 MacroDefinitionOffsets.resize(ID + 1);
1359
1360 MacroDefinitionOffsets[ID] = Stream.GetCurrentBitNo();
1361 } else
1362 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
1363
Sebastian Redlb57a6242010-09-27 22:18:47 +00001364 Record.push_back(IndexBase + NumPreprocessingRecords++);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001365 Record.push_back(ID);
1366 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1367 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1368 AddIdentifierRef(MD->getName(), Record);
1369 AddSourceLocation(MD->getLocation(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001370 Stream.EmitRecord(PP_MACRO_DEFINITION, Record);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001371 continue;
1372 }
1373 }
1374 }
1375
Douglas Gregorc9490c02009-04-16 22:23:12 +00001376 Stream.ExitBlock();
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001377
1378 // Write the offsets table for the preprocessing record.
1379 if (NumPreprocessingRecords > 0) {
1380 // Write the offsets table for identifier IDs.
1381 using namespace llvm;
1382 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001383 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001384 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1385 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1386 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1387 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1388
1389 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001390 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001391 Record.push_back(NumPreprocessingRecords);
1392 Record.push_back(MacroDefinitionOffsets.size());
1393 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001394 (const char *)data(MacroDefinitionOffsets),
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001395 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1396 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001397}
1398
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001399//===----------------------------------------------------------------------===//
1400// Type Serialization
1401//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001402
Sebastian Redl3397c552010-08-18 23:56:27 +00001403/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001404void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00001405 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001406 if (Idx.getIndex() == 0) // we haven't seen this type before.
1407 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00001408
Douglas Gregor2cf26342009-04-09 22:27:44 +00001409 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001410 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00001411 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001412 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00001413 else if (TypeOffsets.size() < Index) {
1414 TypeOffsets.resize(Index + 1);
1415 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001416 }
1417
1418 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001419
Douglas Gregor2cf26342009-04-09 22:27:44 +00001420 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00001421 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001422
Douglas Gregora4923eb2009-11-16 21:35:15 +00001423 if (T.hasLocalNonFastQualifiers()) {
1424 Qualifiers Qs = T.getLocalQualifiers();
1425 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001426 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001427 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00001428 } else {
1429 switch (T->getTypeClass()) {
1430 // For all of the concrete, non-dependent types, call the
1431 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001432#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001433 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001434#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001435#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001436 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001437 }
1438
1439 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001440 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001441
1442 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001443 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001444}
1445
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001446//===----------------------------------------------------------------------===//
1447// Declaration Serialization
1448//===----------------------------------------------------------------------===//
1449
Douglas Gregor2cf26342009-04-09 22:27:44 +00001450/// \brief Write the block containing all of the declaration IDs
1451/// lexically declared within the given DeclContext.
1452///
1453/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1454/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001455uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001456 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001457 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001458 return 0;
1459
Douglas Gregorc9490c02009-04-16 22:23:12 +00001460 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001461 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001462 Record.push_back(DECL_CONTEXT_LEXICAL);
1463 llvm::SmallVector<DeclID, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001464 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1465 D != DEnd; ++D)
Sebastian Redl681d7232010-07-27 00:17:23 +00001466 Decls.push_back(GetDeclRef(*D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001467
Douglas Gregor25123082009-04-22 22:34:57 +00001468 ++NumLexicalDeclContexts;
Sebastian Redl681d7232010-07-27 00:17:23 +00001469 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001470 reinterpret_cast<char*>(Decls.data()), Decls.size() * sizeof(DeclID));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001471 return Offset;
1472}
1473
Sebastian Redla4232eb2010-08-18 23:56:21 +00001474void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00001475 using namespace llvm;
1476 RecordData Record;
1477
1478 // Write the type offsets array
1479 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001480 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001481 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1482 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1483 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1484 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001485 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00001486 Record.push_back(TypeOffsets.size());
1487 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001488 (const char *)data(TypeOffsets),
Sebastian Redl1476ed42010-07-16 16:36:56 +00001489 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1490
1491 // Write the declaration offsets array
1492 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001493 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001494 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1495 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1496 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1497 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001498 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00001499 Record.push_back(DeclOffsets.size());
1500 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001501 (const char *)data(DeclOffsets),
Sebastian Redl1476ed42010-07-16 16:36:56 +00001502 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1503}
1504
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001505//===----------------------------------------------------------------------===//
1506// Global Method Pool and Selector Serialization
1507//===----------------------------------------------------------------------===//
1508
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001509namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001510// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00001511class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00001512 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001513
1514public:
1515 typedef Selector key_type;
1516 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001517
Sebastian Redl5d050072010-08-04 17:20:04 +00001518 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001519 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00001520 ObjCMethodList Instance, Factory;
1521 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001522 typedef const data_type& data_type_ref;
1523
Sebastian Redl3397c552010-08-18 23:56:27 +00001524 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001525
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001526 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00001527 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001528 }
Mike Stump1eb44332009-09-09 15:08:12 +00001529
1530 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001531 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1532 data_type_ref Methods) {
1533 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1534 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00001535 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1536 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001537 Method = Method->Next)
1538 if (Method->Method)
1539 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00001540 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001541 Method = Method->Next)
1542 if (Method->Method)
1543 DataLen += 4;
1544 clang::io::Emit16(Out, DataLen);
1545 return std::make_pair(KeyLen, DataLen);
1546 }
Mike Stump1eb44332009-09-09 15:08:12 +00001547
Douglas Gregor83941df2009-04-25 17:48:32 +00001548 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00001549 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00001550 assert((Start >> 32) == 0 && "Selector key offset too large");
1551 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001552 unsigned N = Sel.getNumArgs();
1553 clang::io::Emit16(Out, N);
1554 if (N == 0)
1555 N = 1;
1556 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001557 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001558 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1559 }
Mike Stump1eb44332009-09-09 15:08:12 +00001560
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001561 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00001562 data_type_ref Methods, unsigned DataLen) {
1563 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00001564 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001565 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00001566 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001567 Method = Method->Next)
1568 if (Method->Method)
1569 ++NumInstanceMethods;
1570
1571 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00001572 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001573 Method = Method->Next)
1574 if (Method->Method)
1575 ++NumFactoryMethods;
1576
1577 clang::io::Emit16(Out, NumInstanceMethods);
1578 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00001579 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001580 Method = Method->Next)
1581 if (Method->Method)
1582 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00001583 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001584 Method = Method->Next)
1585 if (Method->Method)
1586 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00001587
1588 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001589 }
1590};
1591} // end anonymous namespace
1592
Sebastian Redl059612d2010-08-03 21:58:15 +00001593/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001594///
1595/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00001596/// in an on-disk hash table indexed by the selector. The hash table also
1597/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001598void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001599 using namespace llvm;
1600
Sebastian Redl059612d2010-08-03 21:58:15 +00001601 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00001602 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00001603 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00001604 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00001605 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001606 {
Sebastian Redl3397c552010-08-18 23:56:27 +00001607 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001608 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001609
Sebastian Redl059612d2010-08-03 21:58:15 +00001610 // Create the on-disk hash table representation. We walk through every
1611 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00001612 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001613 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00001614 I = SelectorIDs.begin(), E = SelectorIDs.end();
1615 I != E; ++I) {
1616 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00001617 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00001618 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00001619 I->second,
1620 ObjCMethodList(),
1621 ObjCMethodList()
1622 };
1623 if (F != SemaRef.MethodPool.end()) {
1624 Data.Instance = F->second.first;
1625 Data.Factory = F->second.second;
1626 }
Sebastian Redl3397c552010-08-18 23:56:27 +00001627 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00001628 // changed.
1629 if (Chain && I->second < FirstSelectorID) {
1630 // Selector already exists. Did it change?
1631 bool changed = false;
1632 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
1633 M = M->Next) {
1634 if (M->Method->getPCHLevel() == 0)
1635 changed = true;
1636 }
1637 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
1638 M = M->Next) {
1639 if (M->Method->getPCHLevel() == 0)
1640 changed = true;
1641 }
1642 if (!changed)
1643 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00001644 } else if (Data.Instance.Method || Data.Factory.Method) {
1645 // A new method pool entry.
1646 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00001647 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001648 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001649 }
1650
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001651 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001652 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001653 uint32_t BucketOffset;
1654 {
Sebastian Redl3397c552010-08-18 23:56:27 +00001655 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001656 llvm::raw_svector_ostream Out(MethodPool);
1657 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001658 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001659 BucketOffset = Generator.Emit(Out, Trait);
1660 }
1661
1662 // Create a blob abbreviation
1663 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001664 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001665 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00001666 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001667 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1668 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1669
Douglas Gregor83941df2009-04-25 17:48:32 +00001670 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001671 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001672 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001673 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00001674 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001675 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00001676
1677 // Create a blob abbreviation for the selector table offsets.
1678 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001679 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor83941df2009-04-25 17:48:32 +00001680 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1681 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1682 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1683
1684 // Write the selector offsets table.
1685 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001686 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00001687 Record.push_back(SelectorOffsets.size());
1688 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001689 (const char *)data(SelectorOffsets),
Douglas Gregor83941df2009-04-25 17:48:32 +00001690 SelectorOffsets.size() * 4);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001691 }
1692}
1693
Sebastian Redl3397c552010-08-18 23:56:27 +00001694/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001695void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00001696 using namespace llvm;
1697 if (SemaRef.ReferencedSelectors.empty())
1698 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00001699
Fariborz Jahanian32019832010-07-23 19:11:11 +00001700 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00001701
Sebastian Redl3397c552010-08-18 23:56:27 +00001702 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00001703 // very tricky to fix, and given that @selector shouldn't really appear in
1704 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00001705 for (DenseMap<Selector, SourceLocation>::iterator S =
1706 SemaRef.ReferencedSelectors.begin(),
1707 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
1708 Selector Sel = (*S).first;
1709 SourceLocation Loc = (*S).second;
1710 AddSelectorRef(Sel, Record);
1711 AddSourceLocation(Loc, Record);
1712 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001713 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00001714}
1715
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001716//===----------------------------------------------------------------------===//
1717// Identifier Table Serialization
1718//===----------------------------------------------------------------------===//
1719
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001720namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00001721class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00001722 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00001723 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001724
Douglas Gregora92193e2009-04-28 21:18:29 +00001725 /// \brief Determines whether this is an "interesting" identifier
1726 /// that needs a full IdentifierInfo structure written into the hash
1727 /// table.
1728 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1729 return II->isPoisoned() ||
1730 II->isExtensionToken() ||
1731 II->hasMacroDefinition() ||
1732 II->getObjCOrBuiltinID() ||
1733 II->getFETokenInfo<void>();
1734 }
1735
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001736public:
1737 typedef const IdentifierInfo* key_type;
1738 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001739
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001740 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001741 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001742
Sebastian Redl3397c552010-08-18 23:56:27 +00001743 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00001744 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001745
1746 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001747 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001748 }
Mike Stump1eb44332009-09-09 15:08:12 +00001749
1750 std::pair<unsigned,unsigned>
1751 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001752 IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00001753 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00001754 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1755 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00001756 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00001757 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00001758 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00001759 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00001760 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1761 DEnd = IdentifierResolver::end();
1762 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001763 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00001764 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001765 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00001766 // We emit the key length after the data length so that every
1767 // string is preceded by a 16-bit length. This matches the PTH
1768 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00001769 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001770 return std::make_pair(KeyLen, DataLen);
1771 }
Mike Stump1eb44332009-09-09 15:08:12 +00001772
1773 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001774 unsigned KeyLen) {
1775 // Record the location of the key data. This is used when generating
1776 // the mapping from persistent IDs to strings.
1777 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00001778 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001779 }
Mike Stump1eb44332009-09-09 15:08:12 +00001780
1781 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001782 IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00001783 if (!isInterestingIdentifier(II)) {
1784 clang::io::Emit32(Out, ID << 1);
1785 return;
1786 }
Douglas Gregor5998da52009-04-28 21:32:13 +00001787
Douglas Gregora92193e2009-04-28 21:18:29 +00001788 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001789 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001790 bool hasMacroDefinition =
1791 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00001792 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00001793 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001794 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1795 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1796 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00001797 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001798 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00001799 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001800
Douglas Gregor37e26842009-04-21 23:56:24 +00001801 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00001802 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00001803
Douglas Gregor668c1a42009-04-21 22:25:48 +00001804 // Emit the declaration IDs in reverse order, because the
1805 // IdentifierResolver provides the declarations as they would be
1806 // visible (e.g., the function "stat" would come before the struct
1807 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1808 // adds declarations to the end of the list (so we need to see the
1809 // struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00001810 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump1eb44332009-09-09 15:08:12 +00001811 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00001812 IdentifierResolver::end());
1813 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1814 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001815 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00001816 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001817 }
1818};
1819} // end anonymous namespace
1820
Sebastian Redl3397c552010-08-18 23:56:27 +00001821/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001822///
1823/// The identifier table consists of a blob containing string data
1824/// (the actual identifiers themselves) and a separate "offsets" index
1825/// that maps identifier IDs to locations within the blob.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001826void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001827 using namespace llvm;
1828
1829 // Create and write out the blob that contains the identifier
1830 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001831 {
Sebastian Redl3397c552010-08-18 23:56:27 +00001832 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001833 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump1eb44332009-09-09 15:08:12 +00001834
Douglas Gregor92b059e2009-04-28 20:33:11 +00001835 // Look for any identifiers that were named while processing the
1836 // headers, but are otherwise not needed. We add these to the hash
1837 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00001838 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00001839 // file.
1840 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1841 IDEnd = PP.getIdentifierTable().end();
1842 ID != IDEnd; ++ID)
1843 getIdentifierRef(ID->second);
1844
Sebastian Redlf2f0f032010-07-23 23:49:55 +00001845 // Create the on-disk hash table representation. We only store offsets
1846 // for identifiers that appear here for the first time.
1847 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001848 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00001849 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1850 ID != IDEnd; ++ID) {
1851 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001852 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001853 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001854 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001855
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001856 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001857 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00001858 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001859 {
Sebastian Redl3397c552010-08-18 23:56:27 +00001860 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001861 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001862 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001863 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001864 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001865 }
1866
1867 // Create a blob abbreviation
1868 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001869 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001870 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001871 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00001872 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001873
1874 // Write the identifier table
1875 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001876 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001877 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001878 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001879 }
1880
1881 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001882 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001883 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001884 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1885 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1886 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1887
1888 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001889 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001890 Record.push_back(IdentifierOffsets.size());
1891 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001892 (const char *)data(IdentifierOffsets),
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001893 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregorafaf3082009-04-11 00:14:32 +00001894}
1895
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001896//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001897// DeclContext's Name Lookup Table Serialization
1898//===----------------------------------------------------------------------===//
1899
1900namespace {
1901// Trait used for the on-disk hash table used in the method pool.
1902class ASTDeclContextNameLookupTrait {
1903 ASTWriter &Writer;
1904
1905public:
1906 typedef DeclarationName key_type;
1907 typedef key_type key_type_ref;
1908
1909 typedef DeclContext::lookup_result data_type;
1910 typedef const data_type& data_type_ref;
1911
1912 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
1913
1914 unsigned ComputeHash(DeclarationName Name) {
1915 llvm::FoldingSetNodeID ID;
1916 ID.AddInteger(Name.getNameKind());
1917
1918 switch (Name.getNameKind()) {
1919 case DeclarationName::Identifier:
1920 ID.AddString(Name.getAsIdentifierInfo()->getName());
1921 break;
1922 case DeclarationName::ObjCZeroArgSelector:
1923 case DeclarationName::ObjCOneArgSelector:
1924 case DeclarationName::ObjCMultiArgSelector:
1925 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
1926 break;
1927 case DeclarationName::CXXConstructorName:
1928 case DeclarationName::CXXDestructorName:
1929 case DeclarationName::CXXConversionFunctionName:
1930 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
1931 break;
1932 case DeclarationName::CXXOperatorName:
1933 ID.AddInteger(Name.getCXXOverloadedOperator());
1934 break;
1935 case DeclarationName::CXXLiteralOperatorName:
1936 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
1937 case DeclarationName::CXXUsingDirective:
1938 break;
1939 }
1940
1941 return ID.ComputeHash();
1942 }
1943
1944 std::pair<unsigned,unsigned>
1945 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
1946 data_type_ref Lookup) {
1947 unsigned KeyLen = 1;
1948 switch (Name.getNameKind()) {
1949 case DeclarationName::Identifier:
1950 case DeclarationName::ObjCZeroArgSelector:
1951 case DeclarationName::ObjCOneArgSelector:
1952 case DeclarationName::ObjCMultiArgSelector:
1953 case DeclarationName::CXXConstructorName:
1954 case DeclarationName::CXXDestructorName:
1955 case DeclarationName::CXXConversionFunctionName:
1956 case DeclarationName::CXXLiteralOperatorName:
1957 KeyLen += 4;
1958 break;
1959 case DeclarationName::CXXOperatorName:
1960 KeyLen += 1;
1961 break;
1962 case DeclarationName::CXXUsingDirective:
1963 break;
1964 }
1965 clang::io::Emit16(Out, KeyLen);
1966
1967 // 2 bytes for num of decls and 4 for each DeclID.
1968 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
1969 clang::io::Emit16(Out, DataLen);
1970
1971 return std::make_pair(KeyLen, DataLen);
1972 }
1973
1974 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
1975 using namespace clang::io;
1976
1977 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
1978 Emit8(Out, Name.getNameKind());
1979 switch (Name.getNameKind()) {
1980 case DeclarationName::Identifier:
1981 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
1982 break;
1983 case DeclarationName::ObjCZeroArgSelector:
1984 case DeclarationName::ObjCOneArgSelector:
1985 case DeclarationName::ObjCMultiArgSelector:
1986 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
1987 break;
1988 case DeclarationName::CXXConstructorName:
1989 case DeclarationName::CXXDestructorName:
1990 case DeclarationName::CXXConversionFunctionName:
1991 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
1992 break;
1993 case DeclarationName::CXXOperatorName:
1994 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
1995 Emit8(Out, Name.getCXXOverloadedOperator());
1996 break;
1997 case DeclarationName::CXXLiteralOperatorName:
1998 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
1999 break;
2000 case DeclarationName::CXXUsingDirective:
2001 break;
2002 }
2003 }
2004
2005 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2006 data_type Lookup, unsigned DataLen) {
2007 uint64_t Start = Out.tell(); (void)Start;
2008 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2009 for (; Lookup.first != Lookup.second; ++Lookup.first)
2010 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2011
2012 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2013 }
2014};
2015} // end anonymous namespace
2016
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002017/// \brief Write the block containing all of the declaration IDs
2018/// visible from the given DeclContext.
2019///
2020/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002021/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002022uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2023 DeclContext *DC) {
2024 if (DC->getPrimaryContext() != DC)
2025 return 0;
2026
2027 // Since there is no name lookup into functions or methods, don't bother to
2028 // build a visible-declarations table for these entities.
2029 if (DC->isFunctionOrMethod())
2030 return 0;
2031
2032 // If not in C++, we perform name lookup for the translation unit via the
2033 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2034 // FIXME: In C++ we need the visible declarations in order to "see" the
2035 // friend declarations, is there a way to do this without writing the table ?
2036 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2037 return 0;
2038
2039 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00002040 if (DC->hasExternalVisibleStorage())
2041 DC->MaterializeVisibleDeclsFromExternalStorage();
2042 else
2043 DC->lookup(DeclarationName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002044
2045 // Serialize the contents of the mapping used for lookup. Note that,
2046 // although we have two very different code paths, the serialized
2047 // representation is the same for both cases: a declaration name,
2048 // followed by a size, followed by references to the visible
2049 // declarations that have that name.
2050 uint64_t Offset = Stream.GetCurrentBitNo();
2051 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2052 if (!Map || Map->empty())
2053 return 0;
2054
2055 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2056 ASTDeclContextNameLookupTrait Trait(*this);
2057
2058 // Create the on-disk hash table representation.
2059 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2060 D != DEnd; ++D) {
2061 DeclarationName Name = D->first;
2062 DeclContext::lookup_result Result = D->second.getLookupResult();
2063 Generator.insert(Name, Result, Trait);
2064 }
2065
2066 // Create the on-disk hash table in a buffer.
2067 llvm::SmallString<4096> LookupTable;
2068 uint32_t BucketOffset;
2069 {
2070 llvm::raw_svector_ostream Out(LookupTable);
2071 // Make sure that no bucket is at offset 0
2072 clang::io::Emit32(Out, 0);
2073 BucketOffset = Generator.Emit(Out, Trait);
2074 }
2075
2076 // Write the lookup table
2077 RecordData Record;
2078 Record.push_back(DECL_CONTEXT_VISIBLE);
2079 Record.push_back(BucketOffset);
2080 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2081 LookupTable.str());
2082
2083 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2084 ++NumVisibleDeclContexts;
2085 return Offset;
2086}
2087
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002088/// \brief Write an UPDATE_VISIBLE block for the given context.
2089///
2090/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2091/// DeclContext in a dependent AST file. As such, they only exist for the TU
2092/// (in C++) and for namespaces.
2093void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
2094 assert((DC->isTranslationUnit() || DC->isNamespace()) &&
2095 "Only TU and namespaces should have visible decl updates.");
2096
2097 // Make the context build its lookup table, but don't make it load external
2098 // decls.
2099 DC->lookup(DeclarationName());
2100
2101 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2102 if (!Map || Map->empty())
2103 return;
2104
2105 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2106 ASTDeclContextNameLookupTrait Trait(*this);
2107
2108 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002109 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2110 D != DEnd; ++D) {
2111 DeclarationName Name = D->first;
2112 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002113 // For any name that appears in this table, the results are complete, i.e.
2114 // they overwrite results from previous PCHs. Merging is always a mess.
2115 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002116 }
2117
2118 // Create the on-disk hash table in a buffer.
2119 llvm::SmallString<4096> LookupTable;
2120 uint32_t BucketOffset;
2121 {
2122 llvm::raw_svector_ostream Out(LookupTable);
2123 // Make sure that no bucket is at offset 0
2124 clang::io::Emit32(Out, 0);
2125 BucketOffset = Generator.Emit(Out, Trait);
2126 }
2127
2128 // Write the lookup table
2129 RecordData Record;
2130 Record.push_back(UPDATE_VISIBLE);
2131 Record.push_back(getDeclID(cast<Decl>(DC)));
2132 Record.push_back(BucketOffset);
2133 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2134}
2135
Sebastian Redl4153a062010-08-24 22:50:24 +00002136/// \brief Write ADDITIONAL_TEMPLATE_SPECIALIZATIONS blocks for all templates
2137/// that have new specializations in the current AST file.
2138void ASTWriter::WriteAdditionalTemplateSpecializations() {
2139 RecordData Record;
2140 for (AdditionalTemplateSpecializationsMap::iterator
2141 I = AdditionalTemplateSpecializations.begin(),
2142 E = AdditionalTemplateSpecializations.end();
2143 I != E; ++I) {
2144 Record.clear();
2145 Record.push_back(I->first);
2146 Record.insert(Record.end(), I->second.begin(), I->second.end());
2147 Stream.EmitRecord(ADDITIONAL_TEMPLATE_SPECIALIZATIONS, Record);
2148 }
2149}
2150
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002151//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002152// General Serialization Routines
2153//===----------------------------------------------------------------------===//
2154
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002155/// \brief Write a record containing the given attributes.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002156void ASTWriter::WriteAttributeRecord(const AttrVec &Attrs) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002157 RecordData Record;
Sean Huntcf807c42010-08-18 23:23:40 +00002158 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2159 const Attr * A = *i;
2160 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2161 AddSourceLocation(A->getLocation(), Record);
2162 Record.push_back(A->isInherited());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002163
Sean Huntcf807c42010-08-18 23:23:40 +00002164#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00002165
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002166 }
2167
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002168 Stream.EmitRecord(DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002169}
2170
Benjamin Kramer1d6107c2010-09-02 15:06:24 +00002171void ASTWriter::AddString(llvm::StringRef Str, RecordData &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002172 Record.push_back(Str.size());
2173 Record.insert(Record.end(), Str.begin(), Str.end());
2174}
2175
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002176/// \brief Note that the identifier II occurs at the given offset
2177/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002178void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002179 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00002180 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002181 // up earlier in the chain and thus don't need an offset.
2182 if (ID >= FirstIdentID)
2183 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002184}
2185
Douglas Gregor83941df2009-04-25 17:48:32 +00002186/// \brief Note that the selector Sel occurs at the given offset
2187/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002188void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00002189 unsigned ID = SelectorIDs[Sel];
2190 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00002191 // Don't record offsets for selectors that are also available in a different
2192 // file.
2193 if (ID < FirstSelectorID)
2194 return;
2195 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00002196}
2197
Sebastian Redla4232eb2010-08-18 23:56:21 +00002198ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Sebastian Redle58aa892010-08-04 18:21:41 +00002199 : Stream(Stream), Chain(0), FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002200 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redle58aa892010-08-04 18:21:41 +00002201 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
2202 NextSelectorID(FirstSelectorID), CollectedStmts(&StmtsToEmit),
2203 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2204 NumVisibleDeclContexts(0) {
Sebastian Redl30c514c2010-07-14 23:45:08 +00002205}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002206
Sebastian Redla4232eb2010-08-18 23:56:21 +00002207void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002208 const char *isysroot) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002209 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002210 Stream.Emit((unsigned)'C', 8);
2211 Stream.Emit((unsigned)'P', 8);
2212 Stream.Emit((unsigned)'C', 8);
2213 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002214
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002215 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002216
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002217 if (Chain)
Sebastian Redla4232eb2010-08-18 23:56:21 +00002218 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002219 else
Sebastian Redla4232eb2010-08-18 23:56:21 +00002220 WriteASTCore(SemaRef, StatCalls, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002221}
2222
Sebastian Redla4232eb2010-08-18 23:56:21 +00002223void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002224 const char *isysroot) {
2225 using namespace llvm;
2226
2227 ASTContext &Context = SemaRef.Context;
2228 Preprocessor &PP = SemaRef.PP;
2229
Douglas Gregor2cf26342009-04-09 22:27:44 +00002230 // The translation unit is the first declaration we'll emit.
2231 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002232 ++NextDeclID;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002233 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002234
Douglas Gregor2deaea32009-04-22 18:49:13 +00002235 // Make sure that we emit IdentifierInfos (and any attached
2236 // declarations) for builtins.
2237 {
2238 IdentifierTable &Table = PP.getIdentifierTable();
2239 llvm::SmallVector<const char *, 32> BuiltinNames;
2240 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2241 Context.getLangOptions().NoBuiltin);
2242 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2243 getIdentifierRef(&Table.get(BuiltinNames[I]));
2244 }
2245
Chris Lattner63d65f82009-09-08 18:19:27 +00002246 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002247 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002248 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002249 RecordData TentativeDefinitions;
Sebastian Redle9d12b62010-01-31 22:27:38 +00002250 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2251 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner63d65f82009-09-08 18:19:27 +00002252 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002253
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002254 // Build a record containing all of the file scoped decls in this file.
2255 RecordData UnusedFileScopedDecls;
2256 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2257 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00002258
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002259 RecordData WeakUndeclaredIdentifiers;
2260 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2261 WeakUndeclaredIdentifiers.push_back(
2262 SemaRef.WeakUndeclaredIdentifiers.size());
2263 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2264 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2265 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2266 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2267 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2268 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2269 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2270 }
2271 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002272
Douglas Gregor14c22f22009-04-22 22:18:58 +00002273 // Build a record containing all of the locally-scoped external
2274 // declarations in this header file. Generally, this record will be
2275 // empty.
2276 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002277 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00002278 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002279 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002280 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2281 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2282 TD != TDEnd; ++TD)
2283 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2284
Douglas Gregorb81c1702009-04-27 20:06:05 +00002285 // Build a record containing all of the ext_vector declarations.
2286 RecordData ExtVectorDecls;
2287 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2288 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2289
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002290 // Build a record containing all of the VTable uses information.
2291 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00002292 if (!SemaRef.VTableUses.empty()) {
2293 VTableUses.push_back(SemaRef.VTableUses.size());
2294 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2295 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2296 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2297 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2298 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002299 }
2300
2301 // Build a record containing all of dynamic classes declarations.
2302 RecordData DynamicClasses;
2303 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2304 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2305
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002306 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002307 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002308 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00002309 I = SemaRef.PendingInstantiations.begin(),
2310 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2311 AddDeclRef(I->first, PendingInstantiations);
2312 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002313 }
2314 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2315 "There are local ones at end of translation unit!");
2316
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002317 // Build a record containing some declaration references.
2318 RecordData SemaDeclRefs;
2319 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2320 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2321 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2322 }
2323
Sebastian Redl3397c552010-08-18 23:56:27 +00002324 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002325 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002326 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl30c514c2010-07-14 23:45:08 +00002327 WriteMetadata(Context, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002328 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00002329 if (StatCalls && !isysroot)
Douglas Gregordd41ed52010-07-12 23:48:14 +00002330 WriteStatCache(*StatCalls);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002331 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002332 // Write the record of special types.
2333 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00002334
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002335 AddTypeRef(Context.getBuiltinVaListType(), Record);
2336 AddTypeRef(Context.getObjCIdType(), Record);
2337 AddTypeRef(Context.getObjCSelType(), Record);
2338 AddTypeRef(Context.getObjCProtoType(), Record);
2339 AddTypeRef(Context.getObjCClassType(), Record);
2340 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2341 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2342 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002343 AddTypeRef(Context.getjmp_bufType(), Record);
2344 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002345 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2346 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpadaaad32009-10-20 02:12:22 +00002347 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002348 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002349 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2350 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002351 Record.push_back(Context.isInt128Installed());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002352 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002353
Douglas Gregor366809a2009-04-26 03:49:13 +00002354 // Keep writing types and declarations until all types and
2355 // declarations have been written.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002356 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002357 WriteDeclsBlockAbbrevs();
2358 while (!DeclTypesToEmit.empty()) {
2359 DeclOrType DOT = DeclTypesToEmit.front();
2360 DeclTypesToEmit.pop();
2361 if (DOT.isType())
2362 WriteType(DOT.getType());
2363 else
2364 WriteDecl(Context, DOT.getDecl());
2365 }
2366 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002367
Douglas Gregor813a97b2009-10-17 17:25:45 +00002368 WritePreprocessor(PP);
Sebastian Redl059612d2010-08-03 21:58:15 +00002369 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002370 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002371 WriteIdentifierTable(PP);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002372
Sebastian Redl1476ed42010-07-16 16:36:56 +00002373 WriteTypeDeclOffsets();
Douglas Gregorad1de002009-04-18 05:55:16 +00002374
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002375 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002376 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002377 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002378
2379 // Write the record containing tentative definitions.
2380 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002381 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002382
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002383 // Write the record containing unused file scoped decls.
2384 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002385 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002386
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002387 // Write the record containing weak undeclared identifiers.
2388 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002389 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002390 WeakUndeclaredIdentifiers);
2391
Douglas Gregor14c22f22009-04-22 22:18:58 +00002392 // Write the record containing locally-scoped external definitions.
2393 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002394 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002395 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002396
2397 // Write the record containing ext_vector type names.
2398 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002399 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002400
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002401 // Write the record containing VTable uses information.
2402 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002403 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002404
2405 // Write the record containing dynamic classes declarations.
2406 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002407 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002408
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002409 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002410 if (!PendingInstantiations.empty())
2411 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002412
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002413 // Write the record containing declaration references of Sema.
2414 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002415 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002416
Douglas Gregor3e1af842009-04-17 22:13:46 +00002417 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002418 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002419 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002420 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002421 Record.push_back(NumLexicalDeclContexts);
2422 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002423 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002424 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002425}
2426
Sebastian Redla4232eb2010-08-18 23:56:21 +00002427void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002428 const char *isysroot) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002429 using namespace llvm;
2430
Sebastian Redlffaab3e2010-07-30 00:29:29 +00002431 FirstDeclID += Chain->getTotalNumDecls();
2432 FirstTypeID += Chain->getTotalNumTypes();
2433 FirstIdentID += Chain->getTotalNumIdentifiers();
Sebastian Redle58aa892010-08-04 18:21:41 +00002434 FirstSelectorID += Chain->getTotalNumSelectors();
Sebastian Redlffaab3e2010-07-30 00:29:29 +00002435 NextDeclID = FirstDeclID;
2436 NextTypeID = FirstTypeID;
2437 NextIdentID = FirstIdentID;
Sebastian Redle58aa892010-08-04 18:21:41 +00002438 NextSelectorID = FirstSelectorID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00002439
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002440 ASTContext &Context = SemaRef.Context;
2441 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1476ed42010-07-16 16:36:56 +00002442
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002443 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002444 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl30c514c2010-07-14 23:45:08 +00002445 WriteMetadata(Context, isysroot);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002446 if (StatCalls && !isysroot)
2447 WriteStatCache(*StatCalls);
2448 // FIXME: Source manager block should only write new stuff, which could be
2449 // done by tracking the largest ID in the chain
2450 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002451
2452 // The special types are in the chained PCH.
2453
2454 // We don't start with the translation unit, but with its decls that
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002455 // don't come from the chained PCH.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002456 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002457 llvm::SmallVector<DeclID, 64> NewGlobalDecls;
Sebastian Redl681d7232010-07-27 00:17:23 +00002458 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2459 E = TU->noload_decls_end();
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002460 I != E; ++I) {
Sebastian Redld692af72010-07-27 18:24:41 +00002461 if ((*I)->getPCHLevel() == 0)
2462 NewGlobalDecls.push_back(GetDeclRef(*I));
Sebastian Redl0b17c612010-08-13 00:28:03 +00002463 else if ((*I)->isChangedSinceDeserialization())
2464 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002465 }
Sebastian Redl681d7232010-07-27 00:17:23 +00002466 // We also need to write a lexical updates block for the TU.
Sebastian Redld692af72010-07-27 18:24:41 +00002467 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002468 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redld692af72010-07-27 18:24:41 +00002469 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2470 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2471 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002472 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redld692af72010-07-27 18:24:41 +00002473 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2474 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002475 NewGlobalDecls.size() * sizeof(DeclID));
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002476 // And in C++, a visible updates block for the TU.
2477 if (Context.getLangOptions().CPlusPlus) {
2478 Abv = new llvm::BitCodeAbbrev();
2479 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2480 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2481 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2482 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2483 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2484 WriteDeclContextVisibleUpdate(TU);
2485 }
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002486
Sebastian Redl083abdf2010-07-27 23:01:28 +00002487 // Build a record containing all of the new tentative definitions in this
2488 // file, in TentativeDefinitions order.
2489 RecordData TentativeDefinitions;
2490 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2491 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2492 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2493 }
2494
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002495 // Build a record containing all of the file scoped decls in this file.
2496 RecordData UnusedFileScopedDecls;
2497 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2498 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2499 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002500 }
2501
Sebastian Redl40566802010-08-05 18:21:25 +00002502 // We write the entire table, overwriting the tables from the chain.
2503 RecordData WeakUndeclaredIdentifiers;
2504 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2505 WeakUndeclaredIdentifiers.push_back(
2506 SemaRef.WeakUndeclaredIdentifiers.size());
2507 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2508 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2509 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2510 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2511 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2512 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2513 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2514 }
2515 }
2516
Sebastian Redl083abdf2010-07-27 23:01:28 +00002517 // Build a record containing all of the locally-scoped external
2518 // declarations in this header file. Generally, this record will be
2519 // empty.
2520 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002521 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl083abdf2010-07-27 23:01:28 +00002522 // nondeterminstic!
2523 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2524 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2525 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2526 TD != TDEnd; ++TD) {
2527 if (TD->second->getPCHLevel() == 0)
2528 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2529 }
2530
2531 // Build a record containing all of the ext_vector declarations.
2532 RecordData ExtVectorDecls;
2533 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2534 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2535 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2536 }
2537
Sebastian Redl40566802010-08-05 18:21:25 +00002538 // Build a record containing all of the VTable uses information.
2539 // We write everything here, because it's too hard to determine whether
2540 // a use is new to this part.
2541 RecordData VTableUses;
2542 if (!SemaRef.VTableUses.empty()) {
2543 VTableUses.push_back(SemaRef.VTableUses.size());
2544 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2545 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2546 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2547 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2548 }
2549 }
2550
2551 // Build a record containing all of dynamic classes declarations.
2552 RecordData DynamicClasses;
2553 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2554 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
2555 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2556
2557 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002558 RecordData PendingInstantiations;
Sebastian Redl40566802010-08-05 18:21:25 +00002559 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00002560 I = SemaRef.PendingInstantiations.begin(),
2561 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl40566802010-08-05 18:21:25 +00002562 if (I->first->getPCHLevel() == 0) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002563 AddDeclRef(I->first, PendingInstantiations);
2564 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl40566802010-08-05 18:21:25 +00002565 }
2566 }
2567 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2568 "There are local ones at end of translation unit!");
2569
2570 // Build a record containing some declaration references.
2571 // It's not worth the effort to avoid duplication here.
2572 RecordData SemaDeclRefs;
2573 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2574 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2575 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2576 }
2577
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002578 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002579 WriteDeclsBlockAbbrevs();
2580 while (!DeclTypesToEmit.empty()) {
2581 DeclOrType DOT = DeclTypesToEmit.front();
2582 DeclTypesToEmit.pop();
2583 if (DOT.isType())
2584 WriteType(DOT.getType());
2585 else
2586 WriteDecl(Context, DOT.getDecl());
2587 }
2588 Stream.ExitBlock();
2589
Sebastian Redl083abdf2010-07-27 23:01:28 +00002590 WritePreprocessor(PP);
Sebastian Redla68340f2010-08-04 22:21:29 +00002591 WriteSelectors(SemaRef);
2592 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002593 WriteIdentifierTable(PP);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002594 WriteTypeDeclOffsets();
Sebastian Redl083abdf2010-07-27 23:01:28 +00002595
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002596 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl3397c552010-08-18 23:56:27 +00002597 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002598 RecordData FirstLatestDeclIDs;
2599 for (FirstLatestDeclMap::iterator
2600 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
2601 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
2602 "Expected first & second to be in different PCHs");
2603 AddDeclRef(I->first, FirstLatestDeclIDs);
2604 AddDeclRef(I->second, FirstLatestDeclIDs);
2605 }
2606 if (!FirstLatestDeclIDs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002607 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002608
Sebastian Redl083abdf2010-07-27 23:01:28 +00002609 // Write the record containing external, unnamed definitions.
2610 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002611 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002612
2613 // Write the record containing tentative definitions.
2614 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002615 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002616
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002617 // Write the record containing unused file scoped decls.
2618 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002619 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002620
Sebastian Redl40566802010-08-05 18:21:25 +00002621 // Write the record containing weak undeclared identifiers.
2622 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002623 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl40566802010-08-05 18:21:25 +00002624 WeakUndeclaredIdentifiers);
2625
Sebastian Redl083abdf2010-07-27 23:01:28 +00002626 // Write the record containing locally-scoped external definitions.
2627 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002628 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl083abdf2010-07-27 23:01:28 +00002629 LocallyScopedExternalDecls);
2630
2631 // Write the record containing ext_vector type names.
2632 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002633 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002634
Sebastian Redl40566802010-08-05 18:21:25 +00002635 // Write the record containing VTable uses information.
2636 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002637 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl40566802010-08-05 18:21:25 +00002638
2639 // Write the record containing dynamic classes declarations.
2640 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002641 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl40566802010-08-05 18:21:25 +00002642
2643 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002644 if (!PendingInstantiations.empty())
2645 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl40566802010-08-05 18:21:25 +00002646
2647 // Write the record containing declaration references of Sema.
2648 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002649 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002650
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002651 // Write the updates to C++ namespaces.
2652 for (llvm::SmallPtrSet<const NamespaceDecl *, 16>::iterator
2653 I = UpdatedNamespaces.begin(),
2654 E = UpdatedNamespaces.end();
2655 I != E; ++I)
2656 WriteDeclContextVisibleUpdate(*I);
2657
Sebastian Redl4153a062010-08-24 22:50:24 +00002658 // Write the updates to C++ template specialization lists.
2659 if (!AdditionalTemplateSpecializations.empty())
2660 WriteAdditionalTemplateSpecializations();
2661
Sebastian Redl083abdf2010-07-27 23:01:28 +00002662 Record.clear();
2663 Record.push_back(NumStatements);
2664 Record.push_back(NumMacros);
2665 Record.push_back(NumLexicalDeclContexts);
2666 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl0b17c612010-08-13 00:28:03 +00002667 WriteDeclUpdateBlock();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002668 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002669 Stream.ExitBlock();
2670}
2671
Sebastian Redla4232eb2010-08-18 23:56:21 +00002672void ASTWriter::WriteDeclUpdateBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00002673 if (ReplacedDecls.empty())
2674 return;
2675
2676 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002677 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00002678 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
2679 Record.push_back(I->first);
2680 Record.push_back(I->second);
2681 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002682 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00002683}
2684
Sebastian Redla4232eb2010-08-18 23:56:21 +00002685void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002686 Record.push_back(Loc.getRawEncoding());
2687}
2688
Sebastian Redla4232eb2010-08-18 23:56:21 +00002689void ASTWriter::AddSourceRange(SourceRange Range, RecordData &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002690 AddSourceLocation(Range.getBegin(), Record);
2691 AddSourceLocation(Range.getEnd(), Record);
2692}
2693
Sebastian Redla4232eb2010-08-18 23:56:21 +00002694void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002695 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00002696 const uint64_t *Words = Value.getRawData();
2697 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002698}
2699
Sebastian Redla4232eb2010-08-18 23:56:21 +00002700void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002701 Record.push_back(Value.isUnsigned());
2702 AddAPInt(Value, Record);
2703}
2704
Sebastian Redla4232eb2010-08-18 23:56:21 +00002705void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00002706 AddAPInt(Value.bitcastToAPInt(), Record);
2707}
2708
Sebastian Redla4232eb2010-08-18 23:56:21 +00002709void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002710 Record.push_back(getIdentifierRef(II));
2711}
2712
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002713IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002714 if (II == 0)
2715 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002716
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002717 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00002718 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002719 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00002720 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002721}
2722
Sebastian Redlf73c93f2010-09-15 19:54:06 +00002723MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002724 if (MD == 0)
2725 return 0;
Sebastian Redlf73c93f2010-09-15 19:54:06 +00002726
2727 MacroID &ID = MacroDefinitions[MD];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002728 if (ID == 0)
2729 ID = MacroDefinitions.size();
2730 return ID;
2731}
2732
Sebastian Redla4232eb2010-08-18 23:56:21 +00002733void ASTWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00002734 Record.push_back(getSelectorRef(SelRef));
2735}
2736
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002737SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00002738 if (Sel.getAsOpaquePtr() == 0) {
2739 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002740 }
2741
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002742 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00002743 if (SID == 0 && Chain) {
2744 // This might trigger a ReadSelector callback, which will set the ID for
2745 // this selector.
2746 Chain->LoadSelector(Sel);
2747 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002748 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00002749 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002750 }
Sebastian Redl5d050072010-08-04 17:20:04 +00002751 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002752}
2753
Sebastian Redla4232eb2010-08-18 23:56:21 +00002754void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordData &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00002755 AddDeclRef(Temp->getDestructor(), Record);
2756}
2757
Sebastian Redla4232eb2010-08-18 23:56:21 +00002758void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002759 const TemplateArgumentLocInfo &Arg,
2760 RecordData &Record) {
2761 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00002762 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002763 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00002764 break;
2765 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002766 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00002767 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00002768 case TemplateArgument::Template:
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002769 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2770 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00002771 break;
John McCall833ca992009-10-29 08:12:44 +00002772 case TemplateArgument::Null:
2773 case TemplateArgument::Integral:
2774 case TemplateArgument::Declaration:
2775 case TemplateArgument::Pack:
2776 break;
2777 }
2778}
2779
Sebastian Redla4232eb2010-08-18 23:56:21 +00002780void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002781 RecordData &Record) {
2782 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002783
2784 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
2785 bool InfoHasSameExpr
2786 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
2787 Record.push_back(InfoHasSameExpr);
2788 if (InfoHasSameExpr)
2789 return; // Avoid storing the same expr twice.
2790 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002791 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
2792 Record);
2793}
2794
Sebastian Redla4232eb2010-08-18 23:56:21 +00002795void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
John McCalla93c9342009-12-07 02:54:59 +00002796 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00002797 AddTypeRef(QualType(), Record);
2798 return;
2799 }
2800
John McCalla93c9342009-12-07 02:54:59 +00002801 AddTypeRef(TInfo->getType(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +00002802 TypeLocWriter TLW(*this, Record);
John McCalla93c9342009-12-07 02:54:59 +00002803 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002804 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00002805}
2806
Sebastian Redla4232eb2010-08-18 23:56:21 +00002807void ASTWriter::AddTypeRef(QualType T, RecordData &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00002808 Record.push_back(GetOrCreateTypeID(T));
2809}
2810
2811TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00002812 return MakeTypeID(T,
2813 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
2814}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002815
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002816TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00002817 return MakeTypeID(T,
2818 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00002819}
2820
2821TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
2822 if (T.isNull())
2823 return TypeIdx();
2824 assert(!T.getLocalFastQualifiers());
2825
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002826 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002827 if (Idx.getIndex() == 0) {
Douglas Gregor366809a2009-04-26 03:49:13 +00002828 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00002829 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002830 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002831 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00002832 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00002833 return Idx;
2834}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002835
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002836TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00002837 if (T.isNull())
2838 return TypeIdx();
2839 assert(!T.getLocalFastQualifiers());
2840
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002841 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
2842 assert(I != TypeIdxs.end() && "Type not emitted!");
2843 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002844}
2845
Sebastian Redla4232eb2010-08-18 23:56:21 +00002846void ASTWriter::AddDeclRef(const Decl *D, RecordData &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00002847 Record.push_back(GetDeclRef(D));
2848}
2849
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002850DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002851 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00002852 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002853 }
2854
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002855 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00002856 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002857 // We haven't seen this declaration before. Give it a new ID and
2858 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002859 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002860 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redl0b17c612010-08-13 00:28:03 +00002861 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
2862 // We don't add it to the replacement collection here, because we don't
2863 // have the offset yet.
2864 DeclTypesToEmit.push(const_cast<Decl *>(D));
2865 // Reset the flag, so that we don't add this decl multiple times.
2866 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002867 }
2868
Sebastian Redl681d7232010-07-27 00:17:23 +00002869 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002870}
2871
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002872DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002873 if (D == 0)
2874 return 0;
2875
2876 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2877 return DeclIDs[D];
2878}
2879
Sebastian Redla4232eb2010-08-18 23:56:21 +00002880void ASTWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00002881 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002882 Record.push_back(Name.getNameKind());
2883 switch (Name.getNameKind()) {
2884 case DeclarationName::Identifier:
2885 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2886 break;
2887
2888 case DeclarationName::ObjCZeroArgSelector:
2889 case DeclarationName::ObjCOneArgSelector:
2890 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002891 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002892 break;
2893
2894 case DeclarationName::CXXConstructorName:
2895 case DeclarationName::CXXDestructorName:
2896 case DeclarationName::CXXConversionFunctionName:
2897 AddTypeRef(Name.getCXXNameType(), Record);
2898 break;
2899
2900 case DeclarationName::CXXOperatorName:
2901 Record.push_back(Name.getCXXOverloadedOperator());
2902 break;
2903
Sean Hunt3e518bd2009-11-29 07:34:05 +00002904 case DeclarationName::CXXLiteralOperatorName:
2905 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2906 break;
2907
Douglas Gregor2cf26342009-04-09 22:27:44 +00002908 case DeclarationName::CXXUsingDirective:
2909 // No extra data to emit
2910 break;
2911 }
2912}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002913
Sebastian Redla4232eb2010-08-18 23:56:21 +00002914void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002915 RecordData &Record) {
2916 // Nested name specifiers usually aren't too long. I think that 8 would
2917 // typically accomodate the vast majority.
2918 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
2919
2920 // Push each of the NNS's onto a stack for serialization in reverse order.
2921 while (NNS) {
2922 NestedNames.push_back(NNS);
2923 NNS = NNS->getPrefix();
2924 }
2925
2926 Record.push_back(NestedNames.size());
2927 while(!NestedNames.empty()) {
2928 NNS = NestedNames.pop_back_val();
2929 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
2930 Record.push_back(Kind);
2931 switch (Kind) {
2932 case NestedNameSpecifier::Identifier:
2933 AddIdentifierRef(NNS->getAsIdentifier(), Record);
2934 break;
2935
2936 case NestedNameSpecifier::Namespace:
2937 AddDeclRef(NNS->getAsNamespace(), Record);
2938 break;
2939
2940 case NestedNameSpecifier::TypeSpec:
2941 case NestedNameSpecifier::TypeSpecWithTemplate:
2942 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
2943 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
2944 break;
2945
2946 case NestedNameSpecifier::Global:
2947 // Don't need to write an associated value.
2948 break;
2949 }
2950 }
2951}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00002952
Sebastian Redla4232eb2010-08-18 23:56:21 +00002953void ASTWriter::AddTemplateName(TemplateName Name, RecordData &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00002954 TemplateName::NameKind Kind = Name.getKind();
2955 Record.push_back(Kind);
2956 switch (Kind) {
2957 case TemplateName::Template:
2958 AddDeclRef(Name.getAsTemplateDecl(), Record);
2959 break;
2960
2961 case TemplateName::OverloadedTemplate: {
2962 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
2963 Record.push_back(OvT->size());
2964 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
2965 I != E; ++I)
2966 AddDeclRef(*I, Record);
2967 break;
2968 }
2969
2970 case TemplateName::QualifiedTemplate: {
2971 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
2972 AddNestedNameSpecifier(QualT->getQualifier(), Record);
2973 Record.push_back(QualT->hasTemplateKeyword());
2974 AddDeclRef(QualT->getTemplateDecl(), Record);
2975 break;
2976 }
2977
2978 case TemplateName::DependentTemplate: {
2979 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
2980 AddNestedNameSpecifier(DepT->getQualifier(), Record);
2981 Record.push_back(DepT->isIdentifier());
2982 if (DepT->isIdentifier())
2983 AddIdentifierRef(DepT->getIdentifier(), Record);
2984 else
2985 Record.push_back(DepT->getOperator());
2986 break;
2987 }
2988 }
2989}
2990
Sebastian Redla4232eb2010-08-18 23:56:21 +00002991void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00002992 RecordData &Record) {
2993 Record.push_back(Arg.getKind());
2994 switch (Arg.getKind()) {
2995 case TemplateArgument::Null:
2996 break;
2997 case TemplateArgument::Type:
2998 AddTypeRef(Arg.getAsType(), Record);
2999 break;
3000 case TemplateArgument::Declaration:
3001 AddDeclRef(Arg.getAsDecl(), Record);
3002 break;
3003 case TemplateArgument::Integral:
3004 AddAPSInt(*Arg.getAsIntegral(), Record);
3005 AddTypeRef(Arg.getIntegralType(), Record);
3006 break;
3007 case TemplateArgument::Template:
3008 AddTemplateName(Arg.getAsTemplate(), Record);
3009 break;
3010 case TemplateArgument::Expression:
3011 AddStmt(Arg.getAsExpr());
3012 break;
3013 case TemplateArgument::Pack:
3014 Record.push_back(Arg.pack_size());
3015 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3016 I != E; ++I)
3017 AddTemplateArgument(*I, Record);
3018 break;
3019 }
3020}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003021
3022void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003023ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003024 RecordData &Record) {
3025 assert(TemplateParams && "No TemplateParams!");
3026 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3027 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3028 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3029 Record.push_back(TemplateParams->size());
3030 for (TemplateParameterList::const_iterator
3031 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3032 P != PEnd; ++P)
3033 AddDeclRef(*P, Record);
3034}
3035
3036/// \brief Emit a template argument list.
3037void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003038ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003039 RecordData &Record) {
3040 assert(TemplateArgs && "No TemplateArgs!");
3041 Record.push_back(TemplateArgs->flat_size());
3042 for (int i=0, e = TemplateArgs->flat_size(); i != e; ++i)
3043 AddTemplateArgument(TemplateArgs->get(i), Record);
3044}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003045
3046
3047void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003048ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordData &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003049 Record.push_back(Set.size());
3050 for (UnresolvedSetImpl::const_iterator
3051 I = Set.begin(), E = Set.end(); I != E; ++I) {
3052 AddDeclRef(I.getDecl(), Record);
3053 Record.push_back(I.getAccess());
3054 }
3055}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003056
Sebastian Redla4232eb2010-08-18 23:56:21 +00003057void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003058 RecordData &Record) {
3059 Record.push_back(Base.isVirtual());
3060 Record.push_back(Base.isBaseOfClass());
3061 Record.push_back(Base.getAccessSpecifierAsWritten());
Nick Lewycky56062202010-07-26 16:56:01 +00003062 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003063 AddSourceRange(Base.getSourceRange(), Record);
3064}
Sebastian Redl30c514c2010-07-14 23:45:08 +00003065
Sebastian Redla4232eb2010-08-18 23:56:21 +00003066void ASTWriter::AddCXXBaseOrMemberInitializers(
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003067 const CXXBaseOrMemberInitializer * const *BaseOrMembers,
3068 unsigned NumBaseOrMembers, RecordData &Record) {
3069 Record.push_back(NumBaseOrMembers);
3070 for (unsigned i=0; i != NumBaseOrMembers; ++i) {
3071 const CXXBaseOrMemberInitializer *Init = BaseOrMembers[i];
3072
3073 Record.push_back(Init->isBaseInitializer());
3074 if (Init->isBaseInitializer()) {
3075 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3076 Record.push_back(Init->isBaseVirtual());
3077 } else {
3078 AddDeclRef(Init->getMember(), Record);
3079 }
3080 AddSourceLocation(Init->getMemberLocation(), Record);
3081 AddStmt(Init->getInit());
3082 AddDeclRef(Init->getAnonUnionMember(), Record);
3083 AddSourceLocation(Init->getLParenLoc(), Record);
3084 AddSourceLocation(Init->getRParenLoc(), Record);
3085 Record.push_back(Init->isWritten());
3086 if (Init->isWritten()) {
3087 Record.push_back(Init->getSourceOrder());
3088 } else {
3089 Record.push_back(Init->getNumArrayIndices());
3090 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3091 AddDeclRef(Init->getArrayIndex(i), Record);
3092 }
3093 }
3094}
3095
Sebastian Redlc43b54c2010-08-18 23:56:43 +00003096void ASTWriter::SetReader(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003097 assert(Reader && "Cannot remove chain");
3098 assert(FirstDeclID == NextDeclID &&
3099 FirstTypeID == NextTypeID &&
3100 FirstIdentID == NextIdentID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00003101 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003102 "Setting chain after writing has started.");
3103 Chain = Reader;
3104}
3105
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003106void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003107 IdentifierIDs[II] = ID;
3108}
3109
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003110void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00003111 TypeIdxs[T] = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003112}
3113
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003114void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1476ed42010-07-16 16:36:56 +00003115 DeclIDs[D] = ID;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003116}
Sebastian Redl5d050072010-08-04 17:20:04 +00003117
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003118void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003119 SelectorIDs[S] = ID;
3120}