blob: be181f44069b9c6ba50c177125e842318e7ea567 [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"
Douglas Gregor0b748912009-04-14 21:18:50 +000021#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000023#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000024#include "clang/AST/TypeLocVisitor.h"
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000025#include "clang/Serialization/ASTReader.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000026#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000027#include "clang/Lex/PreprocessingRecord.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000028#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000029#include "clang/Lex/HeaderSearch.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000030#include "clang/Basic/FileManager.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000031#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000032#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000033#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000034#include "clang/Basic/TargetInfo.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000035#include "clang/Basic/Version.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000036#include "llvm/ADT/APFloat.h"
37#include "llvm/ADT/APInt.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000038#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000039#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000040#include "llvm/Support/MemoryBuffer.h"
Douglas Gregorb64c1932009-05-12 01:31:05 +000041#include "llvm/System/Path.h"
Chris Lattner3c304bd2009-04-11 18:40:46 +000042#include <cstdio>
Douglas Gregor2cf26342009-04-09 22:27:44 +000043using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000044using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000045
Sebastian Redlade50002010-07-30 17:03:48 +000046template <typename T, typename Allocator>
47T *data(std::vector<T, Allocator> &v) {
48 return v.empty() ? 0 : &v.front();
49}
50template <typename T, typename Allocator>
51const T *data(const std::vector<T, Allocator> &v) {
52 return v.empty() ? 0 : &v.front();
53}
54
Douglas Gregor2cf26342009-04-09 22:27:44 +000055//===----------------------------------------------------------------------===//
56// Type serialization
57//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000058
Douglas Gregor2cf26342009-04-09 22:27:44 +000059namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000060 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000061 ASTWriter &Writer;
62 ASTWriter::RecordData &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000063
64 public:
65 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000066 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000067
Sebastian Redl3397c552010-08-18 23:56:27 +000068 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordData &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000069 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000070
71 void VisitArrayType(const ArrayType *T);
72 void VisitFunctionType(const FunctionType *T);
73 void VisitTagType(const TagType *T);
74
75#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
76#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000077#include "clang/AST/TypeNodes.def"
78 };
79}
80
Sebastian Redl3397c552010-08-18 23:56:27 +000081void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000082 assert(false && "Built-in types are never serialized");
83}
84
Sebastian Redl3397c552010-08-18 23:56:27 +000085void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000086 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +000087 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +000088}
89
Sebastian Redl3397c552010-08-18 23:56:27 +000090void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000091 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +000092 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +000093}
94
Sebastian Redl3397c552010-08-18 23:56:27 +000095void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +000096 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +000097 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +000098}
99
Sebastian Redl3397c552010-08-18 23:56:27 +0000100void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000101 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000102 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000103}
104
Sebastian Redl3397c552010-08-18 23:56:27 +0000105void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000106 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000107 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000108}
109
Sebastian Redl3397c552010-08-18 23:56:27 +0000110void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000111 Writer.AddTypeRef(T->getPointeeType(), Record);
112 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000113 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000114}
115
Sebastian Redl3397c552010-08-18 23:56:27 +0000116void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000117 Writer.AddTypeRef(T->getElementType(), Record);
118 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000119 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000120}
121
Sebastian Redl3397c552010-08-18 23:56:27 +0000122void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000123 VisitArrayType(T);
124 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000125 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000126}
127
Sebastian Redl3397c552010-08-18 23:56:27 +0000128void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000129 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000130 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000131}
132
Sebastian Redl3397c552010-08-18 23:56:27 +0000133void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000134 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000135 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
136 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000137 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000138 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000139}
140
Sebastian Redl3397c552010-08-18 23:56:27 +0000141void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000142 Writer.AddTypeRef(T->getElementType(), Record);
143 Record.push_back(T->getNumElements());
Chris Lattner788b0fd2010-06-23 06:00:24 +0000144 Record.push_back(T->getAltiVecSpecific());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000145 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000146}
147
Sebastian Redl3397c552010-08-18 23:56:27 +0000148void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000149 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000150 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000151}
152
Sebastian Redl3397c552010-08-18 23:56:27 +0000153void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000154 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000155 FunctionType::ExtInfo C = T->getExtInfo();
156 Record.push_back(C.getNoReturn());
Rafael Espindola425ef722010-03-30 22:15:11 +0000157 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000158 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000159 Record.push_back(C.getCC());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000160}
161
Sebastian Redl3397c552010-08-18 23:56:27 +0000162void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000163 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000164 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000165}
166
Sebastian Redl3397c552010-08-18 23:56:27 +0000167void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000168 VisitFunctionType(T);
169 Record.push_back(T->getNumArgs());
170 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
171 Writer.AddTypeRef(T->getArgType(I), Record);
172 Record.push_back(T->isVariadic());
173 Record.push_back(T->getTypeQuals());
Sebastian Redl465226e2009-05-27 22:11:52 +0000174 Record.push_back(T->hasExceptionSpec());
175 Record.push_back(T->hasAnyExceptionSpec());
176 Record.push_back(T->getNumExceptions());
177 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
178 Writer.AddTypeRef(T->getExceptionType(I), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000179 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000180}
181
Sebastian Redl3397c552010-08-18 23:56:27 +0000182void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000183 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000184 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000185}
John McCalled976492009-12-04 22:46:56 +0000186
Sebastian Redl3397c552010-08-18 23:56:27 +0000187void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000188 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000189 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
190 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000191 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000192}
193
Sebastian Redl3397c552010-08-18 23:56:27 +0000194void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000195 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000196 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000197}
198
Sebastian Redl3397c552010-08-18 23:56:27 +0000199void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000200 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000201 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000202}
203
Sebastian Redl3397c552010-08-18 23:56:27 +0000204void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson395b4752009-06-24 19:06:50 +0000205 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000206 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000207}
208
Sebastian Redl3397c552010-08-18 23:56:27 +0000209void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000210 Record.push_back(T->isDependentType());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000211 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000212 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000213 "Cannot serialize in the middle of a type definition");
214}
215
Sebastian Redl3397c552010-08-18 23:56:27 +0000216void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000217 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000218 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000219}
220
Sebastian Redl3397c552010-08-18 23:56:27 +0000221void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000222 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000223 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000224}
225
Mike Stump1eb44332009-09-09 15:08:12 +0000226void
Sebastian Redl3397c552010-08-18 23:56:27 +0000227ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000228 const SubstTemplateTypeParmType *T) {
229 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
230 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000231 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000232}
233
234void
Sebastian Redl3397c552010-08-18 23:56:27 +0000235ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000236 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000237 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000238 Writer.AddTemplateName(T->getTemplateName(), Record);
239 Record.push_back(T->getNumArgs());
240 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
241 ArgI != ArgE; ++ArgI)
242 Writer.AddTemplateArgument(*ArgI, Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000243 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
244 : T->getCanonicalTypeInternal(),
245 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000246 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000247}
248
249void
Sebastian Redl3397c552010-08-18 23:56:27 +0000250ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000251 VisitArrayType(T);
252 Writer.AddStmt(T->getSizeExpr());
253 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000254 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000255}
256
257void
Sebastian Redl3397c552010-08-18 23:56:27 +0000258ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000259 const DependentSizedExtVectorType *T) {
260 // FIXME: Serialize this type (C++ only)
261 assert(false && "Cannot serialize dependent sized extended vector types");
262}
263
264void
Sebastian Redl3397c552010-08-18 23:56:27 +0000265ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000266 Record.push_back(T->getDepth());
267 Record.push_back(T->getIndex());
268 Record.push_back(T->isParameterPack());
269 Writer.AddIdentifierRef(T->getName(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000270 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000271}
272
273void
Sebastian Redl3397c552010-08-18 23:56:27 +0000274ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000275 Record.push_back(T->getKeyword());
276 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
277 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000278 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
279 : T->getCanonicalTypeInternal(),
280 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000281 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000282}
283
284void
Sebastian Redl3397c552010-08-18 23:56:27 +0000285ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000286 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000287 Record.push_back(T->getKeyword());
288 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
289 Writer.AddIdentifierRef(T->getIdentifier(), Record);
290 Record.push_back(T->getNumArgs());
291 for (DependentTemplateSpecializationType::iterator
292 I = T->begin(), E = T->end(); I != E; ++I)
293 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000294 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000295}
296
Sebastian Redl3397c552010-08-18 23:56:27 +0000297void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000298 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000299 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
300 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000301 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000302}
303
Sebastian Redl3397c552010-08-18 23:56:27 +0000304void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCall3cb0ebd2010-03-10 03:28:59 +0000305 Writer.AddDeclRef(T->getDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000306 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000307 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000308}
309
Sebastian Redl3397c552010-08-18 23:56:27 +0000310void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000311 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000312 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000313}
314
Sebastian Redl3397c552010-08-18 23:56:27 +0000315void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000316 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000317 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000318 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000319 E = T->qual_end(); I != E; ++I)
320 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000321 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000322}
323
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000324void
Sebastian Redl3397c552010-08-18 23:56:27 +0000325ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000326 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000327 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000328}
329
John McCalla1ee0c52009-10-16 21:56:05 +0000330namespace {
331
332class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000333 ASTWriter &Writer;
334 ASTWriter::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000335
336public:
Sebastian Redla4232eb2010-08-18 23:56:21 +0000337 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordData &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000338 : Writer(Writer), Record(Record) { }
339
John McCall51bd8032009-10-18 01:05:36 +0000340#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000341#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000342 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000343#include "clang/AST/TypeLocNodes.def"
344
John McCall51bd8032009-10-18 01:05:36 +0000345 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
346 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000347};
348
349}
350
John McCall51bd8032009-10-18 01:05:36 +0000351void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
352 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000353}
John McCall51bd8032009-10-18 01:05:36 +0000354void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000355 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
356 if (TL.needsExtraLocalData()) {
357 Record.push_back(TL.getWrittenTypeSpec());
358 Record.push_back(TL.getWrittenSignSpec());
359 Record.push_back(TL.getWrittenWidthSpec());
360 Record.push_back(TL.hasModeAttr());
361 }
John McCalla1ee0c52009-10-16 21:56:05 +0000362}
John McCall51bd8032009-10-18 01:05:36 +0000363void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
364 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000365}
John McCall51bd8032009-10-18 01:05:36 +0000366void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
367 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000368}
John McCall51bd8032009-10-18 01:05:36 +0000369void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
370 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000371}
John McCall51bd8032009-10-18 01:05:36 +0000372void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
373 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000374}
John McCall51bd8032009-10-18 01:05:36 +0000375void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
376 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000377}
John McCall51bd8032009-10-18 01:05:36 +0000378void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
379 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000380}
John McCall51bd8032009-10-18 01:05:36 +0000381void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
382 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
383 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
384 Record.push_back(TL.getSizeExpr() ? 1 : 0);
385 if (TL.getSizeExpr())
386 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000387}
John McCall51bd8032009-10-18 01:05:36 +0000388void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
389 VisitArrayTypeLoc(TL);
390}
391void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
392 VisitArrayTypeLoc(TL);
393}
394void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
395 VisitArrayTypeLoc(TL);
396}
397void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
398 DependentSizedArrayTypeLoc TL) {
399 VisitArrayTypeLoc(TL);
400}
401void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
402 DependentSizedExtVectorTypeLoc TL) {
403 Writer.AddSourceLocation(TL.getNameLoc(), Record);
404}
405void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
406 Writer.AddSourceLocation(TL.getNameLoc(), Record);
407}
408void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
409 Writer.AddSourceLocation(TL.getNameLoc(), Record);
410}
411void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
412 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
413 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
414 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
415 Writer.AddDeclRef(TL.getArg(i), Record);
416}
417void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
418 VisitFunctionTypeLoc(TL);
419}
420void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
421 VisitFunctionTypeLoc(TL);
422}
John McCalled976492009-12-04 22:46:56 +0000423void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
424 Writer.AddSourceLocation(TL.getNameLoc(), Record);
425}
John McCall51bd8032009-10-18 01:05:36 +0000426void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
427 Writer.AddSourceLocation(TL.getNameLoc(), Record);
428}
429void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000430 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
431 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
432 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000433}
434void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000435 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
436 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
437 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
438 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000439}
440void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
441 Writer.AddSourceLocation(TL.getNameLoc(), Record);
442}
443void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
444 Writer.AddSourceLocation(TL.getNameLoc(), Record);
445}
446void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
447 Writer.AddSourceLocation(TL.getNameLoc(), Record);
448}
John McCall51bd8032009-10-18 01:05:36 +0000449void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
450 Writer.AddSourceLocation(TL.getNameLoc(), Record);
451}
John McCall49a832b2009-10-18 09:09:24 +0000452void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
453 SubstTemplateTypeParmTypeLoc TL) {
454 Writer.AddSourceLocation(TL.getNameLoc(), Record);
455}
John McCall51bd8032009-10-18 01:05:36 +0000456void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
457 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000458 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
459 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
460 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
461 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000462 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
463 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000464}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000465void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000466 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
467 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000468}
John McCall3cb0ebd2010-03-10 03:28:59 +0000469void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
470 Writer.AddSourceLocation(TL.getNameLoc(), Record);
471}
Douglas Gregor4714c122010-03-31 17:34:00 +0000472void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000473 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
474 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000475 Writer.AddSourceLocation(TL.getNameLoc(), Record);
476}
John McCall33500952010-06-11 00:33:02 +0000477void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
478 DependentTemplateSpecializationTypeLoc TL) {
479 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
480 Writer.AddSourceRange(TL.getQualifierRange(), Record);
481 Writer.AddSourceLocation(TL.getNameLoc(), Record);
482 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
483 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
484 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000485 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
486 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000487}
John McCall51bd8032009-10-18 01:05:36 +0000488void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
489 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000490}
491void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
492 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000493 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
494 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
495 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
496 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000497}
John McCall54e14c42009-10-22 22:37:11 +0000498void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
499 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000500}
John McCalla1ee0c52009-10-16 21:56:05 +0000501
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000502//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000503// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000504//===----------------------------------------------------------------------===//
505
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000506static void EmitBlockID(unsigned ID, const char *Name,
507 llvm::BitstreamWriter &Stream,
Sebastian Redla4232eb2010-08-18 23:56:21 +0000508 ASTWriter::RecordData &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000509 Record.clear();
510 Record.push_back(ID);
511 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
512
513 // Emit the block name if present.
514 if (Name == 0 || Name[0] == 0) return;
515 Record.clear();
516 while (*Name)
517 Record.push_back(*Name++);
518 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
519}
520
521static void EmitRecordID(unsigned ID, const char *Name,
522 llvm::BitstreamWriter &Stream,
Sebastian Redla4232eb2010-08-18 23:56:21 +0000523 ASTWriter::RecordData &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000524 Record.clear();
525 Record.push_back(ID);
526 while (*Name)
527 Record.push_back(*Name++);
528 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000529}
530
531static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Sebastian Redla4232eb2010-08-18 23:56:21 +0000532 ASTWriter::RecordData &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000533#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000534 RECORD(STMT_STOP);
535 RECORD(STMT_NULL_PTR);
536 RECORD(STMT_NULL);
537 RECORD(STMT_COMPOUND);
538 RECORD(STMT_CASE);
539 RECORD(STMT_DEFAULT);
540 RECORD(STMT_LABEL);
541 RECORD(STMT_IF);
542 RECORD(STMT_SWITCH);
543 RECORD(STMT_WHILE);
544 RECORD(STMT_DO);
545 RECORD(STMT_FOR);
546 RECORD(STMT_GOTO);
547 RECORD(STMT_INDIRECT_GOTO);
548 RECORD(STMT_CONTINUE);
549 RECORD(STMT_BREAK);
550 RECORD(STMT_RETURN);
551 RECORD(STMT_DECL);
552 RECORD(STMT_ASM);
553 RECORD(EXPR_PREDEFINED);
554 RECORD(EXPR_DECL_REF);
555 RECORD(EXPR_INTEGER_LITERAL);
556 RECORD(EXPR_FLOATING_LITERAL);
557 RECORD(EXPR_IMAGINARY_LITERAL);
558 RECORD(EXPR_STRING_LITERAL);
559 RECORD(EXPR_CHARACTER_LITERAL);
560 RECORD(EXPR_PAREN);
561 RECORD(EXPR_UNARY_OPERATOR);
562 RECORD(EXPR_SIZEOF_ALIGN_OF);
563 RECORD(EXPR_ARRAY_SUBSCRIPT);
564 RECORD(EXPR_CALL);
565 RECORD(EXPR_MEMBER);
566 RECORD(EXPR_BINARY_OPERATOR);
567 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
568 RECORD(EXPR_CONDITIONAL_OPERATOR);
569 RECORD(EXPR_IMPLICIT_CAST);
570 RECORD(EXPR_CSTYLE_CAST);
571 RECORD(EXPR_COMPOUND_LITERAL);
572 RECORD(EXPR_EXT_VECTOR_ELEMENT);
573 RECORD(EXPR_INIT_LIST);
574 RECORD(EXPR_DESIGNATED_INIT);
575 RECORD(EXPR_IMPLICIT_VALUE_INIT);
576 RECORD(EXPR_VA_ARG);
577 RECORD(EXPR_ADDR_LABEL);
578 RECORD(EXPR_STMT);
579 RECORD(EXPR_TYPES_COMPATIBLE);
580 RECORD(EXPR_CHOOSE);
581 RECORD(EXPR_GNU_NULL);
582 RECORD(EXPR_SHUFFLE_VECTOR);
583 RECORD(EXPR_BLOCK);
584 RECORD(EXPR_BLOCK_DECL_REF);
585 RECORD(EXPR_OBJC_STRING_LITERAL);
586 RECORD(EXPR_OBJC_ENCODE);
587 RECORD(EXPR_OBJC_SELECTOR_EXPR);
588 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
589 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
590 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
591 RECORD(EXPR_OBJC_KVC_REF_EXPR);
592 RECORD(EXPR_OBJC_MESSAGE_EXPR);
593 RECORD(EXPR_OBJC_SUPER_EXPR);
594 RECORD(STMT_OBJC_FOR_COLLECTION);
595 RECORD(STMT_OBJC_CATCH);
596 RECORD(STMT_OBJC_FINALLY);
597 RECORD(STMT_OBJC_AT_TRY);
598 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
599 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000600 RECORD(EXPR_CXX_OPERATOR_CALL);
601 RECORD(EXPR_CXX_CONSTRUCT);
602 RECORD(EXPR_CXX_STATIC_CAST);
603 RECORD(EXPR_CXX_DYNAMIC_CAST);
604 RECORD(EXPR_CXX_REINTERPRET_CAST);
605 RECORD(EXPR_CXX_CONST_CAST);
606 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
607 RECORD(EXPR_CXX_BOOL_LITERAL);
608 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000609#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000610}
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Sebastian Redla4232eb2010-08-18 23:56:21 +0000612void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000613 RecordData Record;
614 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000616#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
617#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Sebastian Redl3397c552010-08-18 23:56:27 +0000619 // AST Top-Level Block.
Sebastian Redlf29f0a22010-08-18 23:57:22 +0000620 BLOCK(AST_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000621 RECORD(ORIGINAL_FILE_NAME);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000622 RECORD(TYPE_OFFSET);
623 RECORD(DECL_OFFSET);
624 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000625 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000626 RECORD(IDENTIFIER_OFFSET);
627 RECORD(IDENTIFIER_TABLE);
628 RECORD(EXTERNAL_DEFINITIONS);
629 RECORD(SPECIAL_TYPES);
630 RECORD(STATISTICS);
631 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000632 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000633 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
634 RECORD(SELECTOR_OFFSETS);
635 RECORD(METHOD_POOL);
636 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000637 RECORD(SOURCE_LOCATION_OFFSETS);
638 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000639 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000640 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000641 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000642 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redla93e3b52010-07-08 22:01:51 +0000643 RECORD(CHAINED_METADATA);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000644 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000645
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000646 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000647 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000648 RECORD(SM_SLOC_FILE_ENTRY);
649 RECORD(SM_SLOC_BUFFER_ENTRY);
650 RECORD(SM_SLOC_BUFFER_BLOB);
651 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
652 RECORD(SM_LINE_TABLE);
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000654 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000655 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000656 RECORD(PP_MACRO_OBJECT_LIKE);
657 RECORD(PP_MACRO_FUNCTION_LIKE);
658 RECORD(PP_TOKEN);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000659 RECORD(PP_MACRO_INSTANTIATION);
660 RECORD(PP_MACRO_DEFINITION);
661
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000662 // Decls and Types block.
663 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000664 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000665 RECORD(TYPE_COMPLEX);
666 RECORD(TYPE_POINTER);
667 RECORD(TYPE_BLOCK_POINTER);
668 RECORD(TYPE_LVALUE_REFERENCE);
669 RECORD(TYPE_RVALUE_REFERENCE);
670 RECORD(TYPE_MEMBER_POINTER);
671 RECORD(TYPE_CONSTANT_ARRAY);
672 RECORD(TYPE_INCOMPLETE_ARRAY);
673 RECORD(TYPE_VARIABLE_ARRAY);
674 RECORD(TYPE_VECTOR);
675 RECORD(TYPE_EXT_VECTOR);
676 RECORD(TYPE_FUNCTION_PROTO);
677 RECORD(TYPE_FUNCTION_NO_PROTO);
678 RECORD(TYPE_TYPEDEF);
679 RECORD(TYPE_TYPEOF_EXPR);
680 RECORD(TYPE_TYPEOF);
681 RECORD(TYPE_RECORD);
682 RECORD(TYPE_ENUM);
683 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000684 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000685 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000686 RECORD(DECL_ATTR);
687 RECORD(DECL_TRANSLATION_UNIT);
688 RECORD(DECL_TYPEDEF);
689 RECORD(DECL_ENUM);
690 RECORD(DECL_RECORD);
691 RECORD(DECL_ENUM_CONSTANT);
692 RECORD(DECL_FUNCTION);
693 RECORD(DECL_OBJC_METHOD);
694 RECORD(DECL_OBJC_INTERFACE);
695 RECORD(DECL_OBJC_PROTOCOL);
696 RECORD(DECL_OBJC_IVAR);
697 RECORD(DECL_OBJC_AT_DEFS_FIELD);
698 RECORD(DECL_OBJC_CLASS);
699 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
700 RECORD(DECL_OBJC_CATEGORY);
701 RECORD(DECL_OBJC_CATEGORY_IMPL);
702 RECORD(DECL_OBJC_IMPLEMENTATION);
703 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
704 RECORD(DECL_OBJC_PROPERTY);
705 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000706 RECORD(DECL_FIELD);
707 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000708 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000709 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000710 RECORD(DECL_FILE_SCOPE_ASM);
711 RECORD(DECL_BLOCK);
712 RECORD(DECL_CONTEXT_LEXICAL);
713 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000714 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattner0558df22009-04-27 00:49:53 +0000715 AddStmtsExprs(Stream, Record);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000716#undef RECORD
717#undef BLOCK
718 Stream.ExitBlock();
719}
720
Douglas Gregore650c8c2009-07-07 00:12:59 +0000721/// \brief Adjusts the given filename to only write out the portion of the
722/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000723///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000724/// \param Filename the file name to adjust.
725///
726/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
727/// the returned filename will be adjusted by this system root.
728///
729/// \returns either the original filename (if it needs no adjustment) or the
730/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000731static const char *
Douglas Gregore650c8c2009-07-07 00:12:59 +0000732adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
733 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000734
Douglas Gregore650c8c2009-07-07 00:12:59 +0000735 if (!isysroot)
736 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Douglas Gregore650c8c2009-07-07 00:12:59 +0000738 // Verify that the filename and the system root have the same prefix.
739 unsigned Pos = 0;
740 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
741 if (Filename[Pos] != isysroot[Pos])
742 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000743
Douglas Gregore650c8c2009-07-07 00:12:59 +0000744 // We hit the end of the filename before we hit the end of the system root.
745 if (!Filename[Pos])
746 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000747
Douglas Gregore650c8c2009-07-07 00:12:59 +0000748 // If the file name has a '/' at the current position, skip over the '/'.
749 // We distinguish sysroot-based includes from absolute includes by the
750 // absence of '/' at the beginning of sysroot-based includes.
751 if (Filename[Pos] == '/')
752 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000753
Douglas Gregore650c8c2009-07-07 00:12:59 +0000754 return Filename + Pos;
755}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000756
Sebastian Redl3397c552010-08-18 23:56:27 +0000757/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Sebastian Redla4232eb2010-08-18 23:56:21 +0000758void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000759 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000760
Douglas Gregore650c8c2009-07-07 00:12:59 +0000761 // Metadata
762 const TargetInfo &Target = Context.Target;
763 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl77f46032010-07-09 21:00:24 +0000764 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000765 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl3397c552010-08-18 23:56:27 +0000766 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
767 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregore650c8c2009-07-07 00:12:59 +0000768 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
769 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
770 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl77f46032010-07-09 21:00:24 +0000771 // Target triple or chained PCH name
772 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregore650c8c2009-07-07 00:12:59 +0000773 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000774
Douglas Gregore650c8c2009-07-07 00:12:59 +0000775 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000776 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
777 Record.push_back(VERSION_MAJOR);
778 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000779 Record.push_back(CLANG_VERSION_MAJOR);
780 Record.push_back(CLANG_VERSION_MINOR);
781 Record.push_back(isysroot != 0);
Sebastian Redl77f46032010-07-09 21:00:24 +0000782 // FIXME: This writes the absolute path for chained headers.
783 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
784 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000785
Douglas Gregorb64c1932009-05-12 01:31:05 +0000786 // Original file name
787 SourceManager &SM = Context.getSourceManager();
788 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
789 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000790 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregorb64c1932009-05-12 01:31:05 +0000791 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
792 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
793
794 llvm::sys::Path MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000795
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000796 MainFilePath.makeAbsolute();
Douglas Gregorb64c1932009-05-12 01:31:05 +0000797
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +0000798 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000799 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000800 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000801 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000802 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000803 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000804 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000805
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000806 // Repository branch/version information.
807 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000808 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000809 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
810 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000811 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000812 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000813 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
814 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +0000815}
816
817/// \brief Write the LangOptions structure.
Sebastian Redla4232eb2010-08-18 23:56:21 +0000818void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000819 RecordData Record;
820 Record.push_back(LangOpts.Trigraphs);
821 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
822 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
823 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
824 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000825 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000826 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
827 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
828 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
829 Record.push_back(LangOpts.C99); // C99 Support
830 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
831 Record.push_back(LangOpts.CPlusPlus); // C++ Support
832 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000833 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +0000834
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000835 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
836 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000837 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000838 // modern abi enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000839 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000840 // modern abi enabled.
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +0000841 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump1eb44332009-09-09 15:08:12 +0000842
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000843 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000844 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
845 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000846 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000847 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar73482882010-02-10 18:48:44 +0000848 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000849
850 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
851 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
852 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
853
Chris Lattnerea5ce472009-04-27 07:35:58 +0000854 // Whether static initializers are protected by locks.
855 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +0000856 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000857 Record.push_back(LangOpts.Blocks); // block extension to C
858 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
859 // they are unused.
860 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
861 // (modulo the platform support).
862
Chris Lattnera4d71452010-06-26 21:25:03 +0000863 Record.push_back(LangOpts.getSignedOverflowBehavior());
864 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000865
866 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +0000867 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000868 // defined.
869 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
870 // opposed to __DYNAMIC__).
871 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
872
873 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
874 // used (instead of C99 semantics).
875 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +0000876 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
877 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +0000878 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
879 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +0000880 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000881 Record.push_back(LangOpts.getGCMode());
882 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000883 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000884 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000885 Record.push_back(LangOpts.OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +0000886 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson92f58222009-08-22 22:30:33 +0000887 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +0000888 Record.push_back(LangOpts.SpellChecking);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000889 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000890}
891
Douglas Gregor14f79002009-04-10 03:52:48 +0000892//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000893// stat cache Serialization
894//===----------------------------------------------------------------------===//
895
896namespace {
897// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +0000898class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000899public:
900 typedef const char * key_type;
901 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000902
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000903 typedef std::pair<int, struct stat> data_type;
904 typedef const data_type& data_type_ref;
905
906 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000907 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000908 }
Mike Stump1eb44332009-09-09 15:08:12 +0000909
910 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000911 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
912 data_type_ref Data) {
913 unsigned StrLen = strlen(path);
914 clang::io::Emit16(Out, StrLen);
915 unsigned DataLen = 1; // result value
916 if (Data.first == 0)
917 DataLen += 4 + 4 + 2 + 8 + 8;
918 clang::io::Emit8(Out, DataLen);
919 return std::make_pair(StrLen + 1, DataLen);
920 }
Mike Stump1eb44332009-09-09 15:08:12 +0000921
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000922 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
923 Out.write(path, KeyLen);
924 }
Mike Stump1eb44332009-09-09 15:08:12 +0000925
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000926 void EmitData(llvm::raw_ostream& Out, key_type_ref,
927 data_type_ref Data, unsigned DataLen) {
928 using namespace clang::io;
929 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +0000930
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000931 // Result of stat()
932 Emit8(Out, Data.first? 1 : 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000934 if (Data.first == 0) {
935 Emit32(Out, (uint32_t) Data.second.st_ino);
936 Emit32(Out, (uint32_t) Data.second.st_dev);
937 Emit16(Out, (uint16_t) Data.second.st_mode);
938 Emit64(Out, (uint64_t) Data.second.st_mtime);
939 Emit64(Out, (uint64_t) Data.second.st_size);
940 }
941
942 assert(Out.tell() - Start == DataLen && "Wrong data length");
943 }
944};
945} // end anonymous namespace
946
Sebastian Redl3397c552010-08-18 23:56:27 +0000947/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +0000948void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000949 // Build the on-disk hash table containing information about every
950 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +0000951 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000952 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000953 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000954 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000955 Stat != StatEnd; ++Stat, ++NumStatEntries) {
956 const char *Filename = Stat->first();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000957 Generator.insert(Filename, Stat->second);
958 }
Mike Stump1eb44332009-09-09 15:08:12 +0000959
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000960 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000961 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000962 uint32_t BucketOffset;
963 {
964 llvm::raw_svector_ostream Out(StatCacheData);
965 // Make sure that no bucket is at offset 0
966 clang::io::Emit32(Out, 0);
967 BucketOffset = Generator.Emit(Out);
968 }
969
970 // Create a blob abbreviation
971 using namespace llvm;
972 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000973 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000974 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
975 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
976 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
977 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
978
979 // Write the stat cache
980 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000981 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000982 Record.push_back(BucketOffset);
983 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000984 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000985}
986
987//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +0000988// Source Manager Serialization
989//===----------------------------------------------------------------------===//
990
991/// \brief Create an abbreviation for the SLocEntry that refers to a
992/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000993static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000994 using namespace llvm;
995 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000996 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +0000997 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
998 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
999 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1000 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001001 // FileEntry fields.
1002 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1003 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor12fab312010-03-16 16:35:32 +00001004 // HeaderFileInfo fields.
1005 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
1006 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
1007 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
1008 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregor14f79002009-04-10 03:52:48 +00001009 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001010 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001011}
1012
1013/// \brief Create an abbreviation for the SLocEntry that refers to a
1014/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001015static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001016 using namespace llvm;
1017 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001018 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001019 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1020 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1021 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1022 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1023 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001024 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001025}
1026
1027/// \brief Create an abbreviation for the SLocEntry that refers to a
1028/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001029static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001030 using namespace llvm;
1031 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001032 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001033 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001034 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001035}
1036
1037/// \brief Create an abbreviation for the SLocEntry that refers to an
1038/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001039static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001040 using namespace llvm;
1041 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001042 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001043 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1044 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1045 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1046 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001047 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001048 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001049}
1050
1051/// \brief Writes the block containing the serialized form of the
1052/// source manager.
1053///
1054/// TODO: We should probably use an on-disk hash table (stored in a
1055/// blob), indexed based on the file name, so that we only create
1056/// entries for files that we actually need. In the common case (no
1057/// errors), we probably won't have to create file entries for any of
1058/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001059void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001060 const Preprocessor &PP,
1061 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001062 RecordData Record;
1063
Chris Lattnerf04ad692009-04-10 17:16:57 +00001064 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001065 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001066
1067 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001068 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1069 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1070 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1071 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001072
Douglas Gregorbd945002009-04-13 16:31:14 +00001073 // Write the line table.
1074 if (SourceMgr.hasLineTable()) {
1075 LineTableInfo &LineTable = SourceMgr.getLineTable();
1076
1077 // Emit the file names
1078 Record.push_back(LineTable.getNumFilenames());
1079 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1080 // Emit the file name
1081 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001082 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +00001083 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1084 Record.push_back(FilenameLen);
1085 if (FilenameLen)
1086 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1087 }
Mike Stump1eb44332009-09-09 15:08:12 +00001088
Douglas Gregorbd945002009-04-13 16:31:14 +00001089 // Emit the line entries
1090 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1091 L != LEnd; ++L) {
1092 // Emit the file ID
1093 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Douglas Gregorbd945002009-04-13 16:31:14 +00001095 // Emit the line entries
1096 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001097 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +00001098 LEEnd = L->second.end();
1099 LE != LEEnd; ++LE) {
1100 Record.push_back(LE->FileOffset);
1101 Record.push_back(LE->LineNo);
1102 Record.push_back(LE->FilenameID);
1103 Record.push_back((unsigned)LE->FileKind);
1104 Record.push_back(LE->IncludeOffset);
1105 }
Douglas Gregorbd945002009-04-13 16:31:14 +00001106 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001107 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001108 }
1109
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001110 // Write out the source location entry table. We skip the first
1111 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001112 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001113 RecordData PreloadSLocs;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001114 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1115 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1116 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1117 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001118 // Get this source location entry.
1119 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001120
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001121 // Record the offset of this source-location entry.
1122 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1123
1124 // Figure out which record code to use.
1125 unsigned Code;
1126 if (SLoc->isFile()) {
1127 if (SLoc->getFile().getContentCache()->Entry)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001128 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001129 else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001130 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001131 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001132 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001133 Record.clear();
1134 Record.push_back(Code);
1135
1136 Record.push_back(SLoc->getOffset());
1137 if (SLoc->isFile()) {
1138 const SrcMgr::FileInfo &File = SLoc->getFile();
1139 Record.push_back(File.getIncludeLoc().getRawEncoding());
1140 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1141 Record.push_back(File.hasLineDirectives());
1142
1143 const SrcMgr::ContentCache *Content = File.getContentCache();
1144 if (Content->Entry) {
1145 // The source location entry is a file. The blob associated
1146 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001147
Douglas Gregor2d52be52010-03-21 22:49:54 +00001148 // Emit size/modification time for this file.
1149 Record.push_back(Content->Entry->getSize());
1150 Record.push_back(Content->Entry->getModificationTime());
1151
Douglas Gregor12fab312010-03-16 16:35:32 +00001152 // Emit header-search information associated with this file.
1153 HeaderFileInfo HFI;
1154 HeaderSearch &HS = PP.getHeaderSearchInfo();
1155 if (Content->Entry->getUID() < HS.header_file_size())
1156 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1157 Record.push_back(HFI.isImport);
1158 Record.push_back(HFI.DirInfo);
1159 Record.push_back(HFI.NumIncludes);
1160 AddIdentifierRef(HFI.ControllingMacro, Record);
1161
Douglas Gregore650c8c2009-07-07 00:12:59 +00001162 // Turn the file name into an absolute path, if it isn't already.
1163 const char *Filename = Content->Entry->getName();
1164 llvm::sys::Path FilePath(Filename, strlen(Filename));
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001165 FilePath.makeAbsolute();
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001166 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001167
Douglas Gregore650c8c2009-07-07 00:12:59 +00001168 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001169 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001170
1171 // FIXME: For now, preload all file source locations, so that
1172 // we get the appropriate File entries in the reader. This is
1173 // a temporary measure.
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001174 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001175 } else {
1176 // The source location entry is a buffer. The blob associated
1177 // with this entry contains the contents of the buffer.
1178
1179 // We add one to the size so that we capture the trailing NULL
1180 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1181 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001182 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001183 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001184 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001185 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1186 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001187 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001188 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001189 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001190 llvm::StringRef(Buffer->getBufferStart(),
1191 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001192
1193 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001194 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001195 }
1196 } else {
1197 // The source location entry is an instantiation.
1198 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1199 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1200 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1201 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1202
1203 // Compute the token length for this macro expansion.
1204 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001205 if (I + 1 != N)
1206 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001207 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1208 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1209 }
1210 }
1211
Douglas Gregorc9490c02009-04-16 22:23:12 +00001212 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001213
1214 if (SLocEntryOffsets.empty())
1215 return;
1216
Sebastian Redl3397c552010-08-18 23:56:27 +00001217 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001218 // table is used for lazily loading source-location information.
1219 using namespace llvm;
1220 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001221 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001222 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1223 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1224 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1225 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001226
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001227 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001228 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001229 Record.push_back(SLocEntryOffsets.size());
1230 Record.push_back(SourceMgr.getNextOffset());
1231 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001232 (const char *)data(SLocEntryOffsets),
Chris Lattner090d9b52009-04-27 19:01:47 +00001233 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001234
Sebastian Redl3397c552010-08-18 23:56:27 +00001235 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001236 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001237 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001238}
1239
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001240//===----------------------------------------------------------------------===//
1241// Preprocessor Serialization
1242//===----------------------------------------------------------------------===//
1243
Chris Lattner0b1fb982009-04-10 17:15:23 +00001244/// \brief Writes the block containing the serialized form of the
1245/// preprocessor.
1246///
Sebastian Redla4232eb2010-08-18 23:56:21 +00001247void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001248 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001249
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001250 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1251 if (PP.getCounterValue() != 0) {
1252 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001253 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001254 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001255 }
1256
1257 // Enter the preprocessor block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001258 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 2);
Mike Stump1eb44332009-09-09 15:08:12 +00001259
Sebastian Redl3397c552010-08-18 23:56:27 +00001260 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001261 // FIXME: use diagnostics subsystem for localization etc.
1262 if (PP.SawDateOrTime())
1263 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001264
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001265 // Loop over all the macro definitions that are live at the end of the file,
1266 // emitting each to the PP section.
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001267 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001268 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1269 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001270 // FIXME: This emits macros in hash table order, we should do it in a stable
1271 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001272 MacroInfo *MI = I->second;
1273
Sebastian Redl3397c552010-08-18 23:56:27 +00001274 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001275 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl3397c552010-08-18 23:56:27 +00001276 // Also skip macros from a AST file if we're chaining.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001277 if (MI->isBuiltinMacro() || (Chain && MI->isFromAST()))
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001278 continue;
1279
Chris Lattner7356a312009-04-11 21:15:38 +00001280 AddIdentifierRef(I->first, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001281 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001282 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1283 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001284
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001285 unsigned Code;
1286 if (MI->isObjectLike()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001287 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001288 } else {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001289 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001290
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001291 Record.push_back(MI->isC99Varargs());
1292 Record.push_back(MI->isGNUVarargs());
1293 Record.push_back(MI->getNumArgs());
1294 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1295 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001296 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001297 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001298
1299 // If we have a detailed preprocessing record, record the macro definition
1300 // ID that corresponds to this macro.
1301 if (PPRec)
1302 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
1303
Douglas Gregorc9490c02009-04-16 22:23:12 +00001304 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001305 Record.clear();
1306
Chris Lattnerdf961c22009-04-10 18:08:30 +00001307 // Emit the tokens array.
1308 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1309 // Note that we know that the preprocessor does not have any annotation
1310 // tokens in it because they are created by the parser, and thus can't be
1311 // in a macro definition.
1312 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001313
Chris Lattnerdf961c22009-04-10 18:08:30 +00001314 Record.push_back(Tok.getLocation().getRawEncoding());
1315 Record.push_back(Tok.getLength());
1316
Chris Lattnerdf961c22009-04-10 18:08:30 +00001317 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1318 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001319 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001320
Chris Lattnerdf961c22009-04-10 18:08:30 +00001321 // FIXME: Should translate token kind to a stable encoding.
1322 Record.push_back(Tok.getKind());
1323 // FIXME: Should translate token flags to a stable encoding.
1324 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001325
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001326 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001327 Record.clear();
1328 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001329 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001330 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001331
1332 // If the preprocessor has a preprocessing record, emit it.
1333 unsigned NumPreprocessingRecords = 0;
1334 if (PPRec) {
1335 for (PreprocessingRecord::iterator E = PPRec->begin(), EEnd = PPRec->end();
1336 E != EEnd; ++E) {
1337 Record.clear();
1338
1339 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1340 Record.push_back(NumPreprocessingRecords++);
1341 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1342 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1343 AddIdentifierRef(MI->getName(), Record);
1344 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001345 Stream.EmitRecord(PP_MACRO_INSTANTIATION, Record);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001346 continue;
1347 }
1348
1349 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1350 // Record this macro definition's location.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001351 IdentID ID = getMacroDefinitionID(MD);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001352 if (ID != MacroDefinitionOffsets.size()) {
1353 if (ID > MacroDefinitionOffsets.size())
1354 MacroDefinitionOffsets.resize(ID + 1);
1355
1356 MacroDefinitionOffsets[ID] = Stream.GetCurrentBitNo();
1357 } else
1358 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
1359
1360 Record.push_back(NumPreprocessingRecords++);
1361 Record.push_back(ID);
1362 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1363 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1364 AddIdentifierRef(MD->getName(), Record);
1365 AddSourceLocation(MD->getLocation(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001366 Stream.EmitRecord(PP_MACRO_DEFINITION, Record);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001367 continue;
1368 }
1369 }
1370 }
1371
Douglas Gregorc9490c02009-04-16 22:23:12 +00001372 Stream.ExitBlock();
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001373
1374 // Write the offsets table for the preprocessing record.
1375 if (NumPreprocessingRecords > 0) {
1376 // Write the offsets table for identifier IDs.
1377 using namespace llvm;
1378 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001379 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001380 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1381 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1382 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1383 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1384
1385 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001386 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001387 Record.push_back(NumPreprocessingRecords);
1388 Record.push_back(MacroDefinitionOffsets.size());
1389 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001390 (const char *)data(MacroDefinitionOffsets),
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001391 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1392 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001393}
1394
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001395//===----------------------------------------------------------------------===//
1396// Type Serialization
1397//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001398
Sebastian Redl3397c552010-08-18 23:56:27 +00001399/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001400void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00001401 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001402 if (Idx.getIndex() == 0) // we haven't seen this type before.
1403 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00001404
Douglas Gregor2cf26342009-04-09 22:27:44 +00001405 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001406 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00001407 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001408 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00001409 else if (TypeOffsets.size() < Index) {
1410 TypeOffsets.resize(Index + 1);
1411 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001412 }
1413
1414 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001415
Douglas Gregor2cf26342009-04-09 22:27:44 +00001416 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00001417 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001418
Douglas Gregora4923eb2009-11-16 21:35:15 +00001419 if (T.hasLocalNonFastQualifiers()) {
1420 Qualifiers Qs = T.getLocalQualifiers();
1421 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001422 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001423 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00001424 } else {
1425 switch (T->getTypeClass()) {
1426 // For all of the concrete, non-dependent types, call the
1427 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001428#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001429 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001430#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001431#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001432 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001433 }
1434
1435 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001436 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001437
1438 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001439 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001440}
1441
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001442//===----------------------------------------------------------------------===//
1443// Declaration Serialization
1444//===----------------------------------------------------------------------===//
1445
Douglas Gregor2cf26342009-04-09 22:27:44 +00001446/// \brief Write the block containing all of the declaration IDs
1447/// lexically declared within the given DeclContext.
1448///
1449/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1450/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001451uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001452 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001453 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001454 return 0;
1455
Douglas Gregorc9490c02009-04-16 22:23:12 +00001456 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001457 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001458 Record.push_back(DECL_CONTEXT_LEXICAL);
1459 llvm::SmallVector<DeclID, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001460 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1461 D != DEnd; ++D)
Sebastian Redl681d7232010-07-27 00:17:23 +00001462 Decls.push_back(GetDeclRef(*D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001463
Douglas Gregor25123082009-04-22 22:34:57 +00001464 ++NumLexicalDeclContexts;
Sebastian Redl681d7232010-07-27 00:17:23 +00001465 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001466 reinterpret_cast<char*>(Decls.data()), Decls.size() * sizeof(DeclID));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001467 return Offset;
1468}
1469
Sebastian Redla4232eb2010-08-18 23:56:21 +00001470void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00001471 using namespace llvm;
1472 RecordData Record;
1473
1474 // Write the type offsets array
1475 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001476 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001477 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1478 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1479 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1480 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001481 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00001482 Record.push_back(TypeOffsets.size());
1483 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001484 (const char *)data(TypeOffsets),
Sebastian Redl1476ed42010-07-16 16:36:56 +00001485 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1486
1487 // Write the declaration offsets array
1488 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001489 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001490 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1491 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1492 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1493 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001494 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00001495 Record.push_back(DeclOffsets.size());
1496 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001497 (const char *)data(DeclOffsets),
Sebastian Redl1476ed42010-07-16 16:36:56 +00001498 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1499}
1500
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001501//===----------------------------------------------------------------------===//
1502// Global Method Pool and Selector Serialization
1503//===----------------------------------------------------------------------===//
1504
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001505namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001506// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00001507class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00001508 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001509
1510public:
1511 typedef Selector key_type;
1512 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001513
Sebastian Redl5d050072010-08-04 17:20:04 +00001514 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001515 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00001516 ObjCMethodList Instance, Factory;
1517 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001518 typedef const data_type& data_type_ref;
1519
Sebastian Redl3397c552010-08-18 23:56:27 +00001520 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001521
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001522 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00001523 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001524 }
Mike Stump1eb44332009-09-09 15:08:12 +00001525
1526 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001527 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1528 data_type_ref Methods) {
1529 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1530 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00001531 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1532 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001533 Method = Method->Next)
1534 if (Method->Method)
1535 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00001536 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001537 Method = Method->Next)
1538 if (Method->Method)
1539 DataLen += 4;
1540 clang::io::Emit16(Out, DataLen);
1541 return std::make_pair(KeyLen, DataLen);
1542 }
Mike Stump1eb44332009-09-09 15:08:12 +00001543
Douglas Gregor83941df2009-04-25 17:48:32 +00001544 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00001545 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00001546 assert((Start >> 32) == 0 && "Selector key offset too large");
1547 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001548 unsigned N = Sel.getNumArgs();
1549 clang::io::Emit16(Out, N);
1550 if (N == 0)
1551 N = 1;
1552 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001553 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001554 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1555 }
Mike Stump1eb44332009-09-09 15:08:12 +00001556
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001557 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00001558 data_type_ref Methods, unsigned DataLen) {
1559 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00001560 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001561 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00001562 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001563 Method = Method->Next)
1564 if (Method->Method)
1565 ++NumInstanceMethods;
1566
1567 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00001568 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001569 Method = Method->Next)
1570 if (Method->Method)
1571 ++NumFactoryMethods;
1572
1573 clang::io::Emit16(Out, NumInstanceMethods);
1574 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00001575 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001576 Method = Method->Next)
1577 if (Method->Method)
1578 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00001579 for (const ObjCMethodList *Method = &Methods.Factory; 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));
Douglas Gregora67e58c2009-04-24 21:49:02 +00001583
1584 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001585 }
1586};
1587} // end anonymous namespace
1588
Sebastian Redl059612d2010-08-03 21:58:15 +00001589/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001590///
1591/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00001592/// in an on-disk hash table indexed by the selector. The hash table also
1593/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001594void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001595 using namespace llvm;
1596
Sebastian Redl059612d2010-08-03 21:58:15 +00001597 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00001598 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00001599 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00001600 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00001601 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001602 {
Sebastian Redl3397c552010-08-18 23:56:27 +00001603 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001604 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001605
Sebastian Redl059612d2010-08-03 21:58:15 +00001606 // Create the on-disk hash table representation. We walk through every
1607 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00001608 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001609 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00001610 I = SelectorIDs.begin(), E = SelectorIDs.end();
1611 I != E; ++I) {
1612 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00001613 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00001614 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00001615 I->second,
1616 ObjCMethodList(),
1617 ObjCMethodList()
1618 };
1619 if (F != SemaRef.MethodPool.end()) {
1620 Data.Instance = F->second.first;
1621 Data.Factory = F->second.second;
1622 }
Sebastian Redl3397c552010-08-18 23:56:27 +00001623 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00001624 // changed.
1625 if (Chain && I->second < FirstSelectorID) {
1626 // Selector already exists. Did it change?
1627 bool changed = false;
1628 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
1629 M = M->Next) {
1630 if (M->Method->getPCHLevel() == 0)
1631 changed = true;
1632 }
1633 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
1634 M = M->Next) {
1635 if (M->Method->getPCHLevel() == 0)
1636 changed = true;
1637 }
1638 if (!changed)
1639 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00001640 } else if (Data.Instance.Method || Data.Factory.Method) {
1641 // A new method pool entry.
1642 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00001643 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001644 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001645 }
1646
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001647 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001648 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001649 uint32_t BucketOffset;
1650 {
Sebastian Redl3397c552010-08-18 23:56:27 +00001651 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001652 llvm::raw_svector_ostream Out(MethodPool);
1653 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001654 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001655 BucketOffset = Generator.Emit(Out, Trait);
1656 }
1657
1658 // Create a blob abbreviation
1659 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001660 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001661 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00001662 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001663 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1664 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1665
Douglas Gregor83941df2009-04-25 17:48:32 +00001666 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001667 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001668 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001669 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00001670 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001671 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00001672
1673 // Create a blob abbreviation for the selector table offsets.
1674 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001675 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor83941df2009-04-25 17:48:32 +00001676 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1677 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1678 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1679
1680 // Write the selector offsets table.
1681 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001682 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00001683 Record.push_back(SelectorOffsets.size());
1684 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001685 (const char *)data(SelectorOffsets),
Douglas Gregor83941df2009-04-25 17:48:32 +00001686 SelectorOffsets.size() * 4);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001687 }
1688}
1689
Sebastian Redl3397c552010-08-18 23:56:27 +00001690/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001691void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00001692 using namespace llvm;
1693 if (SemaRef.ReferencedSelectors.empty())
1694 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00001695
Fariborz Jahanian32019832010-07-23 19:11:11 +00001696 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00001697
Sebastian Redl3397c552010-08-18 23:56:27 +00001698 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00001699 // very tricky to fix, and given that @selector shouldn't really appear in
1700 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00001701 for (DenseMap<Selector, SourceLocation>::iterator S =
1702 SemaRef.ReferencedSelectors.begin(),
1703 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
1704 Selector Sel = (*S).first;
1705 SourceLocation Loc = (*S).second;
1706 AddSelectorRef(Sel, Record);
1707 AddSourceLocation(Loc, Record);
1708 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001709 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00001710}
1711
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001712//===----------------------------------------------------------------------===//
1713// Identifier Table Serialization
1714//===----------------------------------------------------------------------===//
1715
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001716namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00001717class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00001718 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00001719 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001720
Douglas Gregora92193e2009-04-28 21:18:29 +00001721 /// \brief Determines whether this is an "interesting" identifier
1722 /// that needs a full IdentifierInfo structure written into the hash
1723 /// table.
1724 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1725 return II->isPoisoned() ||
1726 II->isExtensionToken() ||
1727 II->hasMacroDefinition() ||
1728 II->getObjCOrBuiltinID() ||
1729 II->getFETokenInfo<void>();
1730 }
1731
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001732public:
1733 typedef const IdentifierInfo* key_type;
1734 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001735
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001736 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001737 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001738
Sebastian Redl3397c552010-08-18 23:56:27 +00001739 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00001740 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001741
1742 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001743 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001744 }
Mike Stump1eb44332009-09-09 15:08:12 +00001745
1746 std::pair<unsigned,unsigned>
1747 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001748 IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00001749 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00001750 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1751 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00001752 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00001753 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00001754 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00001755 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00001756 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1757 DEnd = IdentifierResolver::end();
1758 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001759 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00001760 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001761 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00001762 // We emit the key length after the data length so that every
1763 // string is preceded by a 16-bit length. This matches the PTH
1764 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00001765 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001766 return std::make_pair(KeyLen, DataLen);
1767 }
Mike Stump1eb44332009-09-09 15:08:12 +00001768
1769 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001770 unsigned KeyLen) {
1771 // Record the location of the key data. This is used when generating
1772 // the mapping from persistent IDs to strings.
1773 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00001774 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001775 }
Mike Stump1eb44332009-09-09 15:08:12 +00001776
1777 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001778 IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00001779 if (!isInterestingIdentifier(II)) {
1780 clang::io::Emit32(Out, ID << 1);
1781 return;
1782 }
Douglas Gregor5998da52009-04-28 21:32:13 +00001783
Douglas Gregora92193e2009-04-28 21:18:29 +00001784 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001785 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001786 bool hasMacroDefinition =
1787 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00001788 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00001789 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001790 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1791 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1792 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00001793 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001794 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00001795 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001796
Douglas Gregor37e26842009-04-21 23:56:24 +00001797 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00001798 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00001799
Douglas Gregor668c1a42009-04-21 22:25:48 +00001800 // Emit the declaration IDs in reverse order, because the
1801 // IdentifierResolver provides the declarations as they would be
1802 // visible (e.g., the function "stat" would come before the struct
1803 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1804 // adds declarations to the end of the list (so we need to see the
1805 // struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00001806 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump1eb44332009-09-09 15:08:12 +00001807 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00001808 IdentifierResolver::end());
1809 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1810 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001811 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00001812 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001813 }
1814};
1815} // end anonymous namespace
1816
Sebastian Redl3397c552010-08-18 23:56:27 +00001817/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001818///
1819/// The identifier table consists of a blob containing string data
1820/// (the actual identifiers themselves) and a separate "offsets" index
1821/// that maps identifier IDs to locations within the blob.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001822void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001823 using namespace llvm;
1824
1825 // Create and write out the blob that contains the identifier
1826 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001827 {
Sebastian Redl3397c552010-08-18 23:56:27 +00001828 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001829 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump1eb44332009-09-09 15:08:12 +00001830
Douglas Gregor92b059e2009-04-28 20:33:11 +00001831 // Look for any identifiers that were named while processing the
1832 // headers, but are otherwise not needed. We add these to the hash
1833 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00001834 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00001835 // file.
1836 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1837 IDEnd = PP.getIdentifierTable().end();
1838 ID != IDEnd; ++ID)
1839 getIdentifierRef(ID->second);
1840
Sebastian Redlf2f0f032010-07-23 23:49:55 +00001841 // Create the on-disk hash table representation. We only store offsets
1842 // for identifiers that appear here for the first time.
1843 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001844 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00001845 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1846 ID != IDEnd; ++ID) {
1847 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001848 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001849 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001850 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001851
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001852 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001853 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00001854 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001855 {
Sebastian Redl3397c552010-08-18 23:56:27 +00001856 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001857 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001858 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001859 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001860 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001861 }
1862
1863 // Create a blob abbreviation
1864 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001865 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001866 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001867 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00001868 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001869
1870 // Write the identifier table
1871 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001872 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001873 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001874 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001875 }
1876
1877 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001878 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001879 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001880 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1881 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1882 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1883
1884 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001885 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001886 Record.push_back(IdentifierOffsets.size());
1887 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001888 (const char *)data(IdentifierOffsets),
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001889 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregorafaf3082009-04-11 00:14:32 +00001890}
1891
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001892//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001893// DeclContext's Name Lookup Table Serialization
1894//===----------------------------------------------------------------------===//
1895
1896namespace {
1897// Trait used for the on-disk hash table used in the method pool.
1898class ASTDeclContextNameLookupTrait {
1899 ASTWriter &Writer;
1900
1901public:
1902 typedef DeclarationName key_type;
1903 typedef key_type key_type_ref;
1904
1905 typedef DeclContext::lookup_result data_type;
1906 typedef const data_type& data_type_ref;
1907
1908 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
1909
1910 unsigned ComputeHash(DeclarationName Name) {
1911 llvm::FoldingSetNodeID ID;
1912 ID.AddInteger(Name.getNameKind());
1913
1914 switch (Name.getNameKind()) {
1915 case DeclarationName::Identifier:
1916 ID.AddString(Name.getAsIdentifierInfo()->getName());
1917 break;
1918 case DeclarationName::ObjCZeroArgSelector:
1919 case DeclarationName::ObjCOneArgSelector:
1920 case DeclarationName::ObjCMultiArgSelector:
1921 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
1922 break;
1923 case DeclarationName::CXXConstructorName:
1924 case DeclarationName::CXXDestructorName:
1925 case DeclarationName::CXXConversionFunctionName:
1926 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
1927 break;
1928 case DeclarationName::CXXOperatorName:
1929 ID.AddInteger(Name.getCXXOverloadedOperator());
1930 break;
1931 case DeclarationName::CXXLiteralOperatorName:
1932 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
1933 case DeclarationName::CXXUsingDirective:
1934 break;
1935 }
1936
1937 return ID.ComputeHash();
1938 }
1939
1940 std::pair<unsigned,unsigned>
1941 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
1942 data_type_ref Lookup) {
1943 unsigned KeyLen = 1;
1944 switch (Name.getNameKind()) {
1945 case DeclarationName::Identifier:
1946 case DeclarationName::ObjCZeroArgSelector:
1947 case DeclarationName::ObjCOneArgSelector:
1948 case DeclarationName::ObjCMultiArgSelector:
1949 case DeclarationName::CXXConstructorName:
1950 case DeclarationName::CXXDestructorName:
1951 case DeclarationName::CXXConversionFunctionName:
1952 case DeclarationName::CXXLiteralOperatorName:
1953 KeyLen += 4;
1954 break;
1955 case DeclarationName::CXXOperatorName:
1956 KeyLen += 1;
1957 break;
1958 case DeclarationName::CXXUsingDirective:
1959 break;
1960 }
1961 clang::io::Emit16(Out, KeyLen);
1962
1963 // 2 bytes for num of decls and 4 for each DeclID.
1964 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
1965 clang::io::Emit16(Out, DataLen);
1966
1967 return std::make_pair(KeyLen, DataLen);
1968 }
1969
1970 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
1971 using namespace clang::io;
1972
1973 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
1974 Emit8(Out, Name.getNameKind());
1975 switch (Name.getNameKind()) {
1976 case DeclarationName::Identifier:
1977 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
1978 break;
1979 case DeclarationName::ObjCZeroArgSelector:
1980 case DeclarationName::ObjCOneArgSelector:
1981 case DeclarationName::ObjCMultiArgSelector:
1982 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
1983 break;
1984 case DeclarationName::CXXConstructorName:
1985 case DeclarationName::CXXDestructorName:
1986 case DeclarationName::CXXConversionFunctionName:
1987 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
1988 break;
1989 case DeclarationName::CXXOperatorName:
1990 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
1991 Emit8(Out, Name.getCXXOverloadedOperator());
1992 break;
1993 case DeclarationName::CXXLiteralOperatorName:
1994 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
1995 break;
1996 case DeclarationName::CXXUsingDirective:
1997 break;
1998 }
1999 }
2000
2001 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2002 data_type Lookup, unsigned DataLen) {
2003 uint64_t Start = Out.tell(); (void)Start;
2004 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2005 for (; Lookup.first != Lookup.second; ++Lookup.first)
2006 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2007
2008 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2009 }
2010};
2011} // end anonymous namespace
2012
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002013/// \brief Write the block containing all of the declaration IDs
2014/// visible from the given DeclContext.
2015///
2016/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002017/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002018uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2019 DeclContext *DC) {
2020 if (DC->getPrimaryContext() != DC)
2021 return 0;
2022
2023 // Since there is no name lookup into functions or methods, don't bother to
2024 // build a visible-declarations table for these entities.
2025 if (DC->isFunctionOrMethod())
2026 return 0;
2027
2028 // If not in C++, we perform name lookup for the translation unit via the
2029 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2030 // FIXME: In C++ we need the visible declarations in order to "see" the
2031 // friend declarations, is there a way to do this without writing the table ?
2032 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2033 return 0;
2034
2035 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00002036 if (DC->hasExternalVisibleStorage())
2037 DC->MaterializeVisibleDeclsFromExternalStorage();
2038 else
2039 DC->lookup(DeclarationName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002040
2041 // Serialize the contents of the mapping used for lookup. Note that,
2042 // although we have two very different code paths, the serialized
2043 // representation is the same for both cases: a declaration name,
2044 // followed by a size, followed by references to the visible
2045 // declarations that have that name.
2046 uint64_t Offset = Stream.GetCurrentBitNo();
2047 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2048 if (!Map || Map->empty())
2049 return 0;
2050
2051 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2052 ASTDeclContextNameLookupTrait Trait(*this);
2053
2054 // Create the on-disk hash table representation.
2055 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2056 D != DEnd; ++D) {
2057 DeclarationName Name = D->first;
2058 DeclContext::lookup_result Result = D->second.getLookupResult();
2059 Generator.insert(Name, Result, Trait);
2060 }
2061
2062 // Create the on-disk hash table in a buffer.
2063 llvm::SmallString<4096> LookupTable;
2064 uint32_t BucketOffset;
2065 {
2066 llvm::raw_svector_ostream Out(LookupTable);
2067 // Make sure that no bucket is at offset 0
2068 clang::io::Emit32(Out, 0);
2069 BucketOffset = Generator.Emit(Out, Trait);
2070 }
2071
2072 // Write the lookup table
2073 RecordData Record;
2074 Record.push_back(DECL_CONTEXT_VISIBLE);
2075 Record.push_back(BucketOffset);
2076 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2077 LookupTable.str());
2078
2079 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2080 ++NumVisibleDeclContexts;
2081 return Offset;
2082}
2083
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002084/// \brief Write an UPDATE_VISIBLE block for the given context.
2085///
2086/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2087/// DeclContext in a dependent AST file. As such, they only exist for the TU
2088/// (in C++) and for namespaces.
2089void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
2090 assert((DC->isTranslationUnit() || DC->isNamespace()) &&
2091 "Only TU and namespaces should have visible decl updates.");
2092
2093 // Make the context build its lookup table, but don't make it load external
2094 // decls.
2095 DC->lookup(DeclarationName());
2096
2097 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2098 if (!Map || Map->empty())
2099 return;
2100
2101 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2102 ASTDeclContextNameLookupTrait Trait(*this);
2103
2104 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002105 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2106 D != DEnd; ++D) {
2107 DeclarationName Name = D->first;
2108 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002109 // For any name that appears in this table, the results are complete, i.e.
2110 // they overwrite results from previous PCHs. Merging is always a mess.
2111 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002112 }
2113
2114 // Create the on-disk hash table in a buffer.
2115 llvm::SmallString<4096> LookupTable;
2116 uint32_t BucketOffset;
2117 {
2118 llvm::raw_svector_ostream Out(LookupTable);
2119 // Make sure that no bucket is at offset 0
2120 clang::io::Emit32(Out, 0);
2121 BucketOffset = Generator.Emit(Out, Trait);
2122 }
2123
2124 // Write the lookup table
2125 RecordData Record;
2126 Record.push_back(UPDATE_VISIBLE);
2127 Record.push_back(getDeclID(cast<Decl>(DC)));
2128 Record.push_back(BucketOffset);
2129 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2130}
2131
Sebastian Redl4153a062010-08-24 22:50:24 +00002132/// \brief Write ADDITIONAL_TEMPLATE_SPECIALIZATIONS blocks for all templates
2133/// that have new specializations in the current AST file.
2134void ASTWriter::WriteAdditionalTemplateSpecializations() {
2135 RecordData Record;
2136 for (AdditionalTemplateSpecializationsMap::iterator
2137 I = AdditionalTemplateSpecializations.begin(),
2138 E = AdditionalTemplateSpecializations.end();
2139 I != E; ++I) {
2140 Record.clear();
2141 Record.push_back(I->first);
2142 Record.insert(Record.end(), I->second.begin(), I->second.end());
2143 Stream.EmitRecord(ADDITIONAL_TEMPLATE_SPECIALIZATIONS, Record);
2144 }
2145}
2146
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002147//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002148// General Serialization Routines
2149//===----------------------------------------------------------------------===//
2150
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002151/// \brief Write a record containing the given attributes.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002152void ASTWriter::WriteAttributeRecord(const AttrVec &Attrs) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002153 RecordData Record;
Sean Huntcf807c42010-08-18 23:23:40 +00002154 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2155 const Attr * A = *i;
2156 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2157 AddSourceLocation(A->getLocation(), Record);
2158 Record.push_back(A->isInherited());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002159
Sean Huntcf807c42010-08-18 23:23:40 +00002160#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00002161
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002162 }
2163
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002164 Stream.EmitRecord(DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002165}
2166
Sebastian Redla4232eb2010-08-18 23:56:21 +00002167void ASTWriter::AddString(const std::string &Str, RecordData &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002168 Record.push_back(Str.size());
2169 Record.insert(Record.end(), Str.begin(), Str.end());
2170}
2171
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002172/// \brief Note that the identifier II occurs at the given offset
2173/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002174void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002175 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00002176 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002177 // up earlier in the chain and thus don't need an offset.
2178 if (ID >= FirstIdentID)
2179 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002180}
2181
Douglas Gregor83941df2009-04-25 17:48:32 +00002182/// \brief Note that the selector Sel occurs at the given offset
2183/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002184void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00002185 unsigned ID = SelectorIDs[Sel];
2186 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00002187 // Don't record offsets for selectors that are also available in a different
2188 // file.
2189 if (ID < FirstSelectorID)
2190 return;
2191 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00002192}
2193
Sebastian Redla4232eb2010-08-18 23:56:21 +00002194ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Sebastian Redle58aa892010-08-04 18:21:41 +00002195 : Stream(Stream), Chain(0), FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002196 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redle58aa892010-08-04 18:21:41 +00002197 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
2198 NextSelectorID(FirstSelectorID), CollectedStmts(&StmtsToEmit),
2199 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2200 NumVisibleDeclContexts(0) {
Sebastian Redl30c514c2010-07-14 23:45:08 +00002201}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002202
Sebastian Redla4232eb2010-08-18 23:56:21 +00002203void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002204 const char *isysroot) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002205 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002206 Stream.Emit((unsigned)'C', 8);
2207 Stream.Emit((unsigned)'P', 8);
2208 Stream.Emit((unsigned)'C', 8);
2209 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002210
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002211 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002212
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002213 if (Chain)
Sebastian Redla4232eb2010-08-18 23:56:21 +00002214 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002215 else
Sebastian Redla4232eb2010-08-18 23:56:21 +00002216 WriteASTCore(SemaRef, StatCalls, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002217}
2218
Sebastian Redla4232eb2010-08-18 23:56:21 +00002219void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002220 const char *isysroot) {
2221 using namespace llvm;
2222
2223 ASTContext &Context = SemaRef.Context;
2224 Preprocessor &PP = SemaRef.PP;
2225
Douglas Gregor2cf26342009-04-09 22:27:44 +00002226 // The translation unit is the first declaration we'll emit.
2227 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002228 ++NextDeclID;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002229 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002230
Douglas Gregor2deaea32009-04-22 18:49:13 +00002231 // Make sure that we emit IdentifierInfos (and any attached
2232 // declarations) for builtins.
2233 {
2234 IdentifierTable &Table = PP.getIdentifierTable();
2235 llvm::SmallVector<const char *, 32> BuiltinNames;
2236 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2237 Context.getLangOptions().NoBuiltin);
2238 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2239 getIdentifierRef(&Table.get(BuiltinNames[I]));
2240 }
2241
Chris Lattner63d65f82009-09-08 18:19:27 +00002242 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002243 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002244 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002245 RecordData TentativeDefinitions;
Sebastian Redle9d12b62010-01-31 22:27:38 +00002246 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2247 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner63d65f82009-09-08 18:19:27 +00002248 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002249
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002250 // Build a record containing all of the file scoped decls in this file.
2251 RecordData UnusedFileScopedDecls;
2252 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2253 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00002254
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002255 RecordData WeakUndeclaredIdentifiers;
2256 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2257 WeakUndeclaredIdentifiers.push_back(
2258 SemaRef.WeakUndeclaredIdentifiers.size());
2259 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2260 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2261 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2262 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2263 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2264 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2265 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2266 }
2267 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002268
Douglas Gregor14c22f22009-04-22 22:18:58 +00002269 // Build a record containing all of the locally-scoped external
2270 // declarations in this header file. Generally, this record will be
2271 // empty.
2272 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002273 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00002274 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002275 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002276 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2277 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2278 TD != TDEnd; ++TD)
2279 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2280
Douglas Gregorb81c1702009-04-27 20:06:05 +00002281 // Build a record containing all of the ext_vector declarations.
2282 RecordData ExtVectorDecls;
2283 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2284 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2285
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002286 // Build a record containing all of the VTable uses information.
2287 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00002288 if (!SemaRef.VTableUses.empty()) {
2289 VTableUses.push_back(SemaRef.VTableUses.size());
2290 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2291 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2292 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2293 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2294 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002295 }
2296
2297 // Build a record containing all of dynamic classes declarations.
2298 RecordData DynamicClasses;
2299 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2300 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2301
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002302 // Build a record containing all of pending implicit instantiations.
2303 RecordData PendingImplicitInstantiations;
2304 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
2305 I = SemaRef.PendingImplicitInstantiations.begin(),
2306 N = SemaRef.PendingImplicitInstantiations.end(); I != N; ++I) {
2307 AddDeclRef(I->first, PendingImplicitInstantiations);
2308 AddSourceLocation(I->second, PendingImplicitInstantiations);
2309 }
2310 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2311 "There are local ones at end of translation unit!");
2312
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002313 // Build a record containing some declaration references.
2314 RecordData SemaDeclRefs;
2315 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2316 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2317 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2318 }
2319
Sebastian Redl3397c552010-08-18 23:56:27 +00002320 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002321 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002322 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl30c514c2010-07-14 23:45:08 +00002323 WriteMetadata(Context, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002324 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00002325 if (StatCalls && !isysroot)
Douglas Gregordd41ed52010-07-12 23:48:14 +00002326 WriteStatCache(*StatCalls);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002327 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002328 // Write the record of special types.
2329 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00002330
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002331 AddTypeRef(Context.getBuiltinVaListType(), Record);
2332 AddTypeRef(Context.getObjCIdType(), Record);
2333 AddTypeRef(Context.getObjCSelType(), Record);
2334 AddTypeRef(Context.getObjCProtoType(), Record);
2335 AddTypeRef(Context.getObjCClassType(), Record);
2336 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2337 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2338 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002339 AddTypeRef(Context.getjmp_bufType(), Record);
2340 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002341 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2342 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpadaaad32009-10-20 02:12:22 +00002343 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002344 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002345 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2346 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002347 Record.push_back(Context.isInt128Installed());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002348 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002349
Douglas Gregor366809a2009-04-26 03:49:13 +00002350 // Keep writing types and declarations until all types and
2351 // declarations have been written.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002352 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002353 WriteDeclsBlockAbbrevs();
2354 while (!DeclTypesToEmit.empty()) {
2355 DeclOrType DOT = DeclTypesToEmit.front();
2356 DeclTypesToEmit.pop();
2357 if (DOT.isType())
2358 WriteType(DOT.getType());
2359 else
2360 WriteDecl(Context, DOT.getDecl());
2361 }
2362 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002363
Douglas Gregor813a97b2009-10-17 17:25:45 +00002364 WritePreprocessor(PP);
Sebastian Redl059612d2010-08-03 21:58:15 +00002365 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002366 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002367 WriteIdentifierTable(PP);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002368
Sebastian Redl1476ed42010-07-16 16:36:56 +00002369 WriteTypeDeclOffsets();
Douglas Gregorad1de002009-04-18 05:55:16 +00002370
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002371 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002372 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002373 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002374
2375 // Write the record containing tentative definitions.
2376 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002377 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002378
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002379 // Write the record containing unused file scoped decls.
2380 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002381 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002382
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002383 // Write the record containing weak undeclared identifiers.
2384 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002385 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002386 WeakUndeclaredIdentifiers);
2387
Douglas Gregor14c22f22009-04-22 22:18:58 +00002388 // Write the record containing locally-scoped external definitions.
2389 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002390 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002391 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002392
2393 // Write the record containing ext_vector type names.
2394 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002395 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002396
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002397 // Write the record containing VTable uses information.
2398 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002399 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002400
2401 // Write the record containing dynamic classes declarations.
2402 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002403 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002404
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002405 // Write the record containing pending implicit instantiations.
2406 if (!PendingImplicitInstantiations.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002407 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS,
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002408 PendingImplicitInstantiations);
2409
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002410 // Write the record containing declaration references of Sema.
2411 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002412 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002413
Douglas Gregor3e1af842009-04-17 22:13:46 +00002414 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002415 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002416 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002417 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002418 Record.push_back(NumLexicalDeclContexts);
2419 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002420 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002421 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002422}
2423
Sebastian Redla4232eb2010-08-18 23:56:21 +00002424void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002425 const char *isysroot) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002426 using namespace llvm;
2427
Sebastian Redlffaab3e2010-07-30 00:29:29 +00002428 FirstDeclID += Chain->getTotalNumDecls();
2429 FirstTypeID += Chain->getTotalNumTypes();
2430 FirstIdentID += Chain->getTotalNumIdentifiers();
Sebastian Redle58aa892010-08-04 18:21:41 +00002431 FirstSelectorID += Chain->getTotalNumSelectors();
Sebastian Redlffaab3e2010-07-30 00:29:29 +00002432 NextDeclID = FirstDeclID;
2433 NextTypeID = FirstTypeID;
2434 NextIdentID = FirstIdentID;
Sebastian Redle58aa892010-08-04 18:21:41 +00002435 NextSelectorID = FirstSelectorID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00002436
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002437 ASTContext &Context = SemaRef.Context;
2438 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1476ed42010-07-16 16:36:56 +00002439
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002440 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002441 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl30c514c2010-07-14 23:45:08 +00002442 WriteMetadata(Context, isysroot);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002443 if (StatCalls && !isysroot)
2444 WriteStatCache(*StatCalls);
2445 // FIXME: Source manager block should only write new stuff, which could be
2446 // done by tracking the largest ID in the chain
2447 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002448
2449 // The special types are in the chained PCH.
2450
2451 // We don't start with the translation unit, but with its decls that
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002452 // don't come from the chained PCH.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002453 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002454 llvm::SmallVector<DeclID, 64> NewGlobalDecls;
Sebastian Redl681d7232010-07-27 00:17:23 +00002455 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2456 E = TU->noload_decls_end();
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002457 I != E; ++I) {
Sebastian Redld692af72010-07-27 18:24:41 +00002458 if ((*I)->getPCHLevel() == 0)
2459 NewGlobalDecls.push_back(GetDeclRef(*I));
Sebastian Redl0b17c612010-08-13 00:28:03 +00002460 else if ((*I)->isChangedSinceDeserialization())
2461 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002462 }
Sebastian Redl681d7232010-07-27 00:17:23 +00002463 // We also need to write a lexical updates block for the TU.
Sebastian Redld692af72010-07-27 18:24:41 +00002464 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002465 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redld692af72010-07-27 18:24:41 +00002466 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2467 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2468 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002469 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redld692af72010-07-27 18:24:41 +00002470 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2471 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002472 NewGlobalDecls.size() * sizeof(DeclID));
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002473 // And in C++, a visible updates block for the TU.
2474 if (Context.getLangOptions().CPlusPlus) {
2475 Abv = new llvm::BitCodeAbbrev();
2476 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2477 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2478 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2479 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2480 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2481 WriteDeclContextVisibleUpdate(TU);
2482 }
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002483
Sebastian Redl083abdf2010-07-27 23:01:28 +00002484 // Build a record containing all of the new tentative definitions in this
2485 // file, in TentativeDefinitions order.
2486 RecordData TentativeDefinitions;
2487 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2488 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2489 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2490 }
2491
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002492 // Build a record containing all of the file scoped decls in this file.
2493 RecordData UnusedFileScopedDecls;
2494 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2495 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2496 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002497 }
2498
Sebastian Redl40566802010-08-05 18:21:25 +00002499 // We write the entire table, overwriting the tables from the chain.
2500 RecordData WeakUndeclaredIdentifiers;
2501 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2502 WeakUndeclaredIdentifiers.push_back(
2503 SemaRef.WeakUndeclaredIdentifiers.size());
2504 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2505 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2506 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2507 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2508 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2509 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2510 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2511 }
2512 }
2513
Sebastian Redl083abdf2010-07-27 23:01:28 +00002514 // Build a record containing all of the locally-scoped external
2515 // declarations in this header file. Generally, this record will be
2516 // empty.
2517 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002518 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl083abdf2010-07-27 23:01:28 +00002519 // nondeterminstic!
2520 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2521 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2522 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2523 TD != TDEnd; ++TD) {
2524 if (TD->second->getPCHLevel() == 0)
2525 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2526 }
2527
2528 // Build a record containing all of the ext_vector declarations.
2529 RecordData ExtVectorDecls;
2530 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2531 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2532 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2533 }
2534
Sebastian Redl40566802010-08-05 18:21:25 +00002535 // Build a record containing all of the VTable uses information.
2536 // We write everything here, because it's too hard to determine whether
2537 // a use is new to this part.
2538 RecordData VTableUses;
2539 if (!SemaRef.VTableUses.empty()) {
2540 VTableUses.push_back(SemaRef.VTableUses.size());
2541 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2542 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2543 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2544 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2545 }
2546 }
2547
2548 // Build a record containing all of dynamic classes declarations.
2549 RecordData DynamicClasses;
2550 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2551 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
2552 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2553
2554 // Build a record containing all of pending implicit instantiations.
2555 RecordData PendingImplicitInstantiations;
2556 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
2557 I = SemaRef.PendingImplicitInstantiations.begin(),
2558 N = SemaRef.PendingImplicitInstantiations.end(); I != N; ++I) {
2559 if (I->first->getPCHLevel() == 0) {
2560 AddDeclRef(I->first, PendingImplicitInstantiations);
2561 AddSourceLocation(I->second, PendingImplicitInstantiations);
2562 }
2563 }
2564 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2565 "There are local ones at end of translation unit!");
2566
2567 // Build a record containing some declaration references.
2568 // It's not worth the effort to avoid duplication here.
2569 RecordData SemaDeclRefs;
2570 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2571 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2572 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2573 }
2574
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002575 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002576 WriteDeclsBlockAbbrevs();
2577 while (!DeclTypesToEmit.empty()) {
2578 DeclOrType DOT = DeclTypesToEmit.front();
2579 DeclTypesToEmit.pop();
2580 if (DOT.isType())
2581 WriteType(DOT.getType());
2582 else
2583 WriteDecl(Context, DOT.getDecl());
2584 }
2585 Stream.ExitBlock();
2586
Sebastian Redl083abdf2010-07-27 23:01:28 +00002587 WritePreprocessor(PP);
Sebastian Redla68340f2010-08-04 22:21:29 +00002588 WriteSelectors(SemaRef);
2589 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002590 WriteIdentifierTable(PP);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002591 WriteTypeDeclOffsets();
Sebastian Redl083abdf2010-07-27 23:01:28 +00002592
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002593 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl3397c552010-08-18 23:56:27 +00002594 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002595 RecordData FirstLatestDeclIDs;
2596 for (FirstLatestDeclMap::iterator
2597 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
2598 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
2599 "Expected first & second to be in different PCHs");
2600 AddDeclRef(I->first, FirstLatestDeclIDs);
2601 AddDeclRef(I->second, FirstLatestDeclIDs);
2602 }
2603 if (!FirstLatestDeclIDs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002604 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002605
Sebastian Redl083abdf2010-07-27 23:01:28 +00002606 // Write the record containing external, unnamed definitions.
2607 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002608 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002609
2610 // Write the record containing tentative definitions.
2611 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002612 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002613
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002614 // Write the record containing unused file scoped decls.
2615 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002616 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002617
Sebastian Redl40566802010-08-05 18:21:25 +00002618 // Write the record containing weak undeclared identifiers.
2619 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002620 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl40566802010-08-05 18:21:25 +00002621 WeakUndeclaredIdentifiers);
2622
Sebastian Redl083abdf2010-07-27 23:01:28 +00002623 // Write the record containing locally-scoped external definitions.
2624 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002625 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl083abdf2010-07-27 23:01:28 +00002626 LocallyScopedExternalDecls);
2627
2628 // Write the record containing ext_vector type names.
2629 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002630 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002631
Sebastian Redl40566802010-08-05 18:21:25 +00002632 // Write the record containing VTable uses information.
2633 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002634 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl40566802010-08-05 18:21:25 +00002635
2636 // Write the record containing dynamic classes declarations.
2637 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002638 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl40566802010-08-05 18:21:25 +00002639
2640 // Write the record containing pending implicit instantiations.
2641 if (!PendingImplicitInstantiations.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002642 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS,
Sebastian Redl40566802010-08-05 18:21:25 +00002643 PendingImplicitInstantiations);
2644
2645 // Write the record containing declaration references of Sema.
2646 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002647 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002648
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002649 // Write the updates to C++ namespaces.
2650 for (llvm::SmallPtrSet<const NamespaceDecl *, 16>::iterator
2651 I = UpdatedNamespaces.begin(),
2652 E = UpdatedNamespaces.end();
2653 I != E; ++I)
2654 WriteDeclContextVisibleUpdate(*I);
2655
Sebastian Redl4153a062010-08-24 22:50:24 +00002656 // Write the updates to C++ template specialization lists.
2657 if (!AdditionalTemplateSpecializations.empty())
2658 WriteAdditionalTemplateSpecializations();
2659
Sebastian Redl083abdf2010-07-27 23:01:28 +00002660 Record.clear();
2661 Record.push_back(NumStatements);
2662 Record.push_back(NumMacros);
2663 Record.push_back(NumLexicalDeclContexts);
2664 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl0b17c612010-08-13 00:28:03 +00002665 WriteDeclUpdateBlock();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002666 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002667 Stream.ExitBlock();
2668}
2669
Sebastian Redla4232eb2010-08-18 23:56:21 +00002670void ASTWriter::WriteDeclUpdateBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00002671 if (ReplacedDecls.empty())
2672 return;
2673
2674 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002675 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00002676 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
2677 Record.push_back(I->first);
2678 Record.push_back(I->second);
2679 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002680 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00002681}
2682
Sebastian Redla4232eb2010-08-18 23:56:21 +00002683void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002684 Record.push_back(Loc.getRawEncoding());
2685}
2686
Sebastian Redla4232eb2010-08-18 23:56:21 +00002687void ASTWriter::AddSourceRange(SourceRange Range, RecordData &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002688 AddSourceLocation(Range.getBegin(), Record);
2689 AddSourceLocation(Range.getEnd(), Record);
2690}
2691
Sebastian Redla4232eb2010-08-18 23:56:21 +00002692void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002693 Record.push_back(Value.getBitWidth());
2694 unsigned N = Value.getNumWords();
2695 const uint64_t* Words = Value.getRawData();
2696 for (unsigned I = 0; I != N; ++I)
2697 Record.push_back(Words[I]);
2698}
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 Redl8538e8d2010-08-18 23:57:32 +00002723IdentID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002724 if (MD == 0)
2725 return 0;
2726
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002727 IdentID &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}