blob: e95e422fbd57cd81a90618ce2ff9f1f59dc11b8d [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"
Douglas Gregor89d99802010-11-30 06:16:57 +000015#include "clang/Serialization/ASTSerializationListener.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000016#include "ASTCommon.h"
Douglas Gregore737f502010-08-12 20:07:10 +000017#include "clang/Sema/Sema.h"
18#include "clang/Sema/IdentifierResolver.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclContextInternals.h"
John McCall2a7fb272010-08-25 05:32:35 +000022#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000023#include "clang/AST/DeclFriend.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000024#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000025#include "clang/AST/ExprCXX.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000026#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000027#include "clang/AST/TypeLocVisitor.h"
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000028#include "clang/Serialization/ASTReader.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000029#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000030#include "clang/Lex/PreprocessingRecord.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000031#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000032#include "clang/Lex/HeaderSearch.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000033#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000034#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000035#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000036#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000037#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000038#include "clang/Basic/TargetInfo.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000039#include "clang/Basic/Version.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000040#include "llvm/ADT/APFloat.h"
41#include "llvm/ADT/APInt.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000042#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000043#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000044#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000045#include "llvm/Support/Path.h"
Chris Lattner3c304bd2009-04-11 18:40:46 +000046#include <cstdio>
Douglas Gregor2cf26342009-04-09 22:27:44 +000047using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000048using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000049
Sebastian Redlade50002010-07-30 17:03:48 +000050template <typename T, typename Allocator>
51T *data(std::vector<T, Allocator> &v) {
52 return v.empty() ? 0 : &v.front();
53}
54template <typename T, typename Allocator>
55const T *data(const std::vector<T, Allocator> &v) {
56 return v.empty() ? 0 : &v.front();
57}
58
Douglas Gregor2cf26342009-04-09 22:27:44 +000059//===----------------------------------------------------------------------===//
60// Type serialization
61//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000062
Douglas Gregor2cf26342009-04-09 22:27:44 +000063namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000064 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000065 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000066 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000067
68 public:
69 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000070 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000071
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000072 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000073 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000074
75 void VisitArrayType(const ArrayType *T);
76 void VisitFunctionType(const FunctionType *T);
77 void VisitTagType(const TagType *T);
78
79#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
80#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000081#include "clang/AST/TypeNodes.def"
82 };
83}
84
Sebastian Redl3397c552010-08-18 23:56:27 +000085void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000086 assert(false && "Built-in types are never serialized");
87}
88
Sebastian Redl3397c552010-08-18 23:56:27 +000089void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000090 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +000091 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +000092}
93
Sebastian Redl3397c552010-08-18 23:56:27 +000094void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000095 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +000096 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +000097}
98
Sebastian Redl3397c552010-08-18 23:56:27 +000099void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000100 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000101 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000102}
103
Sebastian Redl3397c552010-08-18 23:56:27 +0000104void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000105 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000106 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000107}
108
Sebastian Redl3397c552010-08-18 23:56:27 +0000109void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000110 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000111 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000112}
113
Sebastian Redl3397c552010-08-18 23:56:27 +0000114void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000115 Writer.AddTypeRef(T->getPointeeType(), Record);
116 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000117 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000118}
119
Sebastian Redl3397c552010-08-18 23:56:27 +0000120void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000121 Writer.AddTypeRef(T->getElementType(), Record);
122 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000123 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000124}
125
Sebastian Redl3397c552010-08-18 23:56:27 +0000126void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000127 VisitArrayType(T);
128 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000129 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000130}
131
Sebastian Redl3397c552010-08-18 23:56:27 +0000132void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000133 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000134 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000135}
136
Sebastian Redl3397c552010-08-18 23:56:27 +0000137void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000138 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000139 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
140 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000141 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000142 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000143}
144
Sebastian Redl3397c552010-08-18 23:56:27 +0000145void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000146 Writer.AddTypeRef(T->getElementType(), Record);
147 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000148 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000149 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000150}
151
Sebastian Redl3397c552010-08-18 23:56:27 +0000152void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000153 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000154 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000155}
156
Sebastian Redl3397c552010-08-18 23:56:27 +0000157void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000158 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000159 FunctionType::ExtInfo C = T->getExtInfo();
160 Record.push_back(C.getNoReturn());
Rafael Espindola425ef722010-03-30 22:15:11 +0000161 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000162 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000163 Record.push_back(C.getCC());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000164}
165
Sebastian Redl3397c552010-08-18 23:56:27 +0000166void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000167 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000168 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000169}
170
Sebastian Redl3397c552010-08-18 23:56:27 +0000171void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000172 VisitFunctionType(T);
173 Record.push_back(T->getNumArgs());
174 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
175 Writer.AddTypeRef(T->getArgType(I), Record);
176 Record.push_back(T->isVariadic());
177 Record.push_back(T->getTypeQuals());
Sebastian Redl465226e2009-05-27 22:11:52 +0000178 Record.push_back(T->hasExceptionSpec());
179 Record.push_back(T->hasAnyExceptionSpec());
180 Record.push_back(T->getNumExceptions());
181 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
182 Writer.AddTypeRef(T->getExceptionType(I), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000183 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000184}
185
Sebastian Redl3397c552010-08-18 23:56:27 +0000186void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000187 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000188 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000189}
John McCalled976492009-12-04 22:46:56 +0000190
Sebastian Redl3397c552010-08-18 23:56:27 +0000191void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000192 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000193 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
194 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000195 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000196}
197
Sebastian Redl3397c552010-08-18 23:56:27 +0000198void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000199 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000200 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000201}
202
Sebastian Redl3397c552010-08-18 23:56:27 +0000203void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000204 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000205 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000206}
207
Sebastian Redl3397c552010-08-18 23:56:27 +0000208void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson395b4752009-06-24 19:06:50 +0000209 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000210 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000211}
212
Sebastian Redl3397c552010-08-18 23:56:27 +0000213void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000214 Record.push_back(T->isDependentType());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000215 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000216 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000217 "Cannot serialize in the middle of a type definition");
218}
219
Sebastian Redl3397c552010-08-18 23:56:27 +0000220void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000221 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000222 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000223}
224
Sebastian Redl3397c552010-08-18 23:56:27 +0000225void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000226 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000227 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000228}
229
Mike Stump1eb44332009-09-09 15:08:12 +0000230void
Sebastian Redl3397c552010-08-18 23:56:27 +0000231ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000232 const SubstTemplateTypeParmType *T) {
233 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
234 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000235 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000236}
237
238void
Sebastian Redl3397c552010-08-18 23:56:27 +0000239ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000240 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000241 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000242 Writer.AddTemplateName(T->getTemplateName(), Record);
243 Record.push_back(T->getNumArgs());
244 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
245 ArgI != ArgE; ++ArgI)
246 Writer.AddTemplateArgument(*ArgI, Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000247 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
248 : T->getCanonicalTypeInternal(),
249 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000250 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000251}
252
253void
Sebastian Redl3397c552010-08-18 23:56:27 +0000254ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000255 VisitArrayType(T);
256 Writer.AddStmt(T->getSizeExpr());
257 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000258 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000259}
260
261void
Sebastian Redl3397c552010-08-18 23:56:27 +0000262ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000263 const DependentSizedExtVectorType *T) {
264 // FIXME: Serialize this type (C++ only)
265 assert(false && "Cannot serialize dependent sized extended vector types");
266}
267
268void
Sebastian Redl3397c552010-08-18 23:56:27 +0000269ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000270 Record.push_back(T->getDepth());
271 Record.push_back(T->getIndex());
272 Record.push_back(T->isParameterPack());
273 Writer.AddIdentifierRef(T->getName(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000274 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000275}
276
277void
Sebastian Redl3397c552010-08-18 23:56:27 +0000278ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000279 Record.push_back(T->getKeyword());
280 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
281 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000282 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
283 : T->getCanonicalTypeInternal(),
284 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000285 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000286}
287
288void
Sebastian Redl3397c552010-08-18 23:56:27 +0000289ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000290 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000291 Record.push_back(T->getKeyword());
292 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
293 Writer.AddIdentifierRef(T->getIdentifier(), Record);
294 Record.push_back(T->getNumArgs());
295 for (DependentTemplateSpecializationType::iterator
296 I = T->begin(), E = T->end(); I != E; ++I)
297 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000298 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000299}
300
Sebastian Redl3397c552010-08-18 23:56:27 +0000301void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000302 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000303 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
304 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000305 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000306}
307
Sebastian Redl3397c552010-08-18 23:56:27 +0000308void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCall3cb0ebd2010-03-10 03:28:59 +0000309 Writer.AddDeclRef(T->getDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000310 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000311 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000312}
313
Sebastian Redl3397c552010-08-18 23:56:27 +0000314void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000315 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000316 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000317}
318
Sebastian Redl3397c552010-08-18 23:56:27 +0000319void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000320 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000321 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000322 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000323 E = T->qual_end(); I != E; ++I)
324 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000325 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000326}
327
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000328void
Sebastian Redl3397c552010-08-18 23:56:27 +0000329ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000330 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000331 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000332}
333
John McCalla1ee0c52009-10-16 21:56:05 +0000334namespace {
335
336class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000337 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000338 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000339
340public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000341 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000342 : Writer(Writer), Record(Record) { }
343
John McCall51bd8032009-10-18 01:05:36 +0000344#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000345#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000346 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000347#include "clang/AST/TypeLocNodes.def"
348
John McCall51bd8032009-10-18 01:05:36 +0000349 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
350 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000351};
352
353}
354
John McCall51bd8032009-10-18 01:05:36 +0000355void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
356 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000357}
John McCall51bd8032009-10-18 01:05:36 +0000358void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000359 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
360 if (TL.needsExtraLocalData()) {
361 Record.push_back(TL.getWrittenTypeSpec());
362 Record.push_back(TL.getWrittenSignSpec());
363 Record.push_back(TL.getWrittenWidthSpec());
364 Record.push_back(TL.hasModeAttr());
365 }
John McCalla1ee0c52009-10-16 21:56:05 +0000366}
John McCall51bd8032009-10-18 01:05:36 +0000367void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
368 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000369}
John McCall51bd8032009-10-18 01:05:36 +0000370void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
371 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000372}
John McCall51bd8032009-10-18 01:05:36 +0000373void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
374 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000375}
John McCall51bd8032009-10-18 01:05:36 +0000376void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
377 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000378}
John McCall51bd8032009-10-18 01:05:36 +0000379void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
380 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000381}
John McCall51bd8032009-10-18 01:05:36 +0000382void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
383 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000384}
John McCall51bd8032009-10-18 01:05:36 +0000385void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
386 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
387 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
388 Record.push_back(TL.getSizeExpr() ? 1 : 0);
389 if (TL.getSizeExpr())
390 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000391}
John McCall51bd8032009-10-18 01:05:36 +0000392void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
393 VisitArrayTypeLoc(TL);
394}
395void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
396 VisitArrayTypeLoc(TL);
397}
398void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
399 VisitArrayTypeLoc(TL);
400}
401void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
402 DependentSizedArrayTypeLoc TL) {
403 VisitArrayTypeLoc(TL);
404}
405void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
406 DependentSizedExtVectorTypeLoc TL) {
407 Writer.AddSourceLocation(TL.getNameLoc(), Record);
408}
409void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
410 Writer.AddSourceLocation(TL.getNameLoc(), Record);
411}
412void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
413 Writer.AddSourceLocation(TL.getNameLoc(), Record);
414}
415void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
416 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
417 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Douglas Gregordab60ad2010-10-01 18:44:50 +0000418 Record.push_back(TL.getTrailingReturn());
John McCall51bd8032009-10-18 01:05:36 +0000419 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
420 Writer.AddDeclRef(TL.getArg(i), Record);
421}
422void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
423 VisitFunctionTypeLoc(TL);
424}
425void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
426 VisitFunctionTypeLoc(TL);
427}
John McCalled976492009-12-04 22:46:56 +0000428void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
429 Writer.AddSourceLocation(TL.getNameLoc(), Record);
430}
John McCall51bd8032009-10-18 01:05:36 +0000431void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
432 Writer.AddSourceLocation(TL.getNameLoc(), Record);
433}
434void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc 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);
John McCall51bd8032009-10-18 01:05:36 +0000438}
439void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000440 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
441 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
442 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
443 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000444}
445void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
446 Writer.AddSourceLocation(TL.getNameLoc(), Record);
447}
448void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
449 Writer.AddSourceLocation(TL.getNameLoc(), Record);
450}
451void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
452 Writer.AddSourceLocation(TL.getNameLoc(), Record);
453}
John McCall51bd8032009-10-18 01:05:36 +0000454void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
455 Writer.AddSourceLocation(TL.getNameLoc(), Record);
456}
John McCall49a832b2009-10-18 09:09:24 +0000457void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
458 SubstTemplateTypeParmTypeLoc TL) {
459 Writer.AddSourceLocation(TL.getNameLoc(), Record);
460}
John McCall51bd8032009-10-18 01:05:36 +0000461void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
462 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000463 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
464 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
465 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
466 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000467 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
468 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000469}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000470void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000471 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
472 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000473}
John McCall3cb0ebd2010-03-10 03:28:59 +0000474void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
475 Writer.AddSourceLocation(TL.getNameLoc(), Record);
476}
Douglas Gregor4714c122010-03-31 17:34:00 +0000477void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000478 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
479 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000480 Writer.AddSourceLocation(TL.getNameLoc(), Record);
481}
John McCall33500952010-06-11 00:33:02 +0000482void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
483 DependentTemplateSpecializationTypeLoc TL) {
484 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
485 Writer.AddSourceRange(TL.getQualifierRange(), Record);
486 Writer.AddSourceLocation(TL.getNameLoc(), Record);
487 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
488 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
489 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000490 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
491 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000492}
John McCall51bd8032009-10-18 01:05:36 +0000493void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
494 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000495}
496void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
497 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000498 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
499 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
500 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
501 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000502}
John McCall54e14c42009-10-22 22:37:11 +0000503void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
504 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000505}
John McCalla1ee0c52009-10-16 21:56:05 +0000506
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000507//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000508// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000509//===----------------------------------------------------------------------===//
510
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000511static void EmitBlockID(unsigned ID, const char *Name,
512 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000513 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000514 Record.clear();
515 Record.push_back(ID);
516 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
517
518 // Emit the block name if present.
519 if (Name == 0 || Name[0] == 0) return;
520 Record.clear();
521 while (*Name)
522 Record.push_back(*Name++);
523 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
524}
525
526static void EmitRecordID(unsigned ID, const char *Name,
527 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000528 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000529 Record.clear();
530 Record.push_back(ID);
531 while (*Name)
532 Record.push_back(*Name++);
533 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000534}
535
536static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000537 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000538#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000539 RECORD(STMT_STOP);
540 RECORD(STMT_NULL_PTR);
541 RECORD(STMT_NULL);
542 RECORD(STMT_COMPOUND);
543 RECORD(STMT_CASE);
544 RECORD(STMT_DEFAULT);
545 RECORD(STMT_LABEL);
546 RECORD(STMT_IF);
547 RECORD(STMT_SWITCH);
548 RECORD(STMT_WHILE);
549 RECORD(STMT_DO);
550 RECORD(STMT_FOR);
551 RECORD(STMT_GOTO);
552 RECORD(STMT_INDIRECT_GOTO);
553 RECORD(STMT_CONTINUE);
554 RECORD(STMT_BREAK);
555 RECORD(STMT_RETURN);
556 RECORD(STMT_DECL);
557 RECORD(STMT_ASM);
558 RECORD(EXPR_PREDEFINED);
559 RECORD(EXPR_DECL_REF);
560 RECORD(EXPR_INTEGER_LITERAL);
561 RECORD(EXPR_FLOATING_LITERAL);
562 RECORD(EXPR_IMAGINARY_LITERAL);
563 RECORD(EXPR_STRING_LITERAL);
564 RECORD(EXPR_CHARACTER_LITERAL);
565 RECORD(EXPR_PAREN);
566 RECORD(EXPR_UNARY_OPERATOR);
567 RECORD(EXPR_SIZEOF_ALIGN_OF);
568 RECORD(EXPR_ARRAY_SUBSCRIPT);
569 RECORD(EXPR_CALL);
570 RECORD(EXPR_MEMBER);
571 RECORD(EXPR_BINARY_OPERATOR);
572 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
573 RECORD(EXPR_CONDITIONAL_OPERATOR);
574 RECORD(EXPR_IMPLICIT_CAST);
575 RECORD(EXPR_CSTYLE_CAST);
576 RECORD(EXPR_COMPOUND_LITERAL);
577 RECORD(EXPR_EXT_VECTOR_ELEMENT);
578 RECORD(EXPR_INIT_LIST);
579 RECORD(EXPR_DESIGNATED_INIT);
580 RECORD(EXPR_IMPLICIT_VALUE_INIT);
581 RECORD(EXPR_VA_ARG);
582 RECORD(EXPR_ADDR_LABEL);
583 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000584 RECORD(EXPR_CHOOSE);
585 RECORD(EXPR_GNU_NULL);
586 RECORD(EXPR_SHUFFLE_VECTOR);
587 RECORD(EXPR_BLOCK);
588 RECORD(EXPR_BLOCK_DECL_REF);
589 RECORD(EXPR_OBJC_STRING_LITERAL);
590 RECORD(EXPR_OBJC_ENCODE);
591 RECORD(EXPR_OBJC_SELECTOR_EXPR);
592 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
593 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
594 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
595 RECORD(EXPR_OBJC_KVC_REF_EXPR);
596 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000597 RECORD(STMT_OBJC_FOR_COLLECTION);
598 RECORD(STMT_OBJC_CATCH);
599 RECORD(STMT_OBJC_FINALLY);
600 RECORD(STMT_OBJC_AT_TRY);
601 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
602 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000603 RECORD(EXPR_CXX_OPERATOR_CALL);
604 RECORD(EXPR_CXX_CONSTRUCT);
605 RECORD(EXPR_CXX_STATIC_CAST);
606 RECORD(EXPR_CXX_DYNAMIC_CAST);
607 RECORD(EXPR_CXX_REINTERPRET_CAST);
608 RECORD(EXPR_CXX_CONST_CAST);
609 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
610 RECORD(EXPR_CXX_BOOL_LITERAL);
611 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000612#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000613}
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Sebastian Redla4232eb2010-08-18 23:56:21 +0000615void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000616 RecordData Record;
617 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000619#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
620#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000621
Sebastian Redl3397c552010-08-18 23:56:27 +0000622 // AST Top-Level Block.
Sebastian Redlf29f0a22010-08-18 23:57:22 +0000623 BLOCK(AST_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000624 RECORD(ORIGINAL_FILE_NAME);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000625 RECORD(TYPE_OFFSET);
626 RECORD(DECL_OFFSET);
627 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000628 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000629 RECORD(IDENTIFIER_OFFSET);
630 RECORD(IDENTIFIER_TABLE);
631 RECORD(EXTERNAL_DEFINITIONS);
632 RECORD(SPECIAL_TYPES);
633 RECORD(STATISTICS);
634 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000635 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000636 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
637 RECORD(SELECTOR_OFFSETS);
638 RECORD(METHOD_POOL);
639 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000640 RECORD(SOURCE_LOCATION_OFFSETS);
641 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000642 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000643 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000644 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000645 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redla93e3b52010-07-08 22:01:51 +0000646 RECORD(CHAINED_METADATA);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000647 RECORD(REFERENCED_SELECTOR_POOL);
Michael J. Spencer20249a12010-10-21 03:16:25 +0000648
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000649 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000650 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000651 RECORD(SM_SLOC_FILE_ENTRY);
652 RECORD(SM_SLOC_BUFFER_ENTRY);
653 RECORD(SM_SLOC_BUFFER_BLOB);
654 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
655 RECORD(SM_LINE_TABLE);
Mike Stump1eb44332009-09-09 15:08:12 +0000656
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000657 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000658 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000659 RECORD(PP_MACRO_OBJECT_LIKE);
660 RECORD(PP_MACRO_FUNCTION_LIKE);
661 RECORD(PP_TOKEN);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000662 RECORD(PP_MACRO_INSTANTIATION);
663 RECORD(PP_MACRO_DEFINITION);
Michael J. Spencer20249a12010-10-21 03:16:25 +0000664
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000665 // Decls and Types block.
666 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000667 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000668 RECORD(TYPE_COMPLEX);
669 RECORD(TYPE_POINTER);
670 RECORD(TYPE_BLOCK_POINTER);
671 RECORD(TYPE_LVALUE_REFERENCE);
672 RECORD(TYPE_RVALUE_REFERENCE);
673 RECORD(TYPE_MEMBER_POINTER);
674 RECORD(TYPE_CONSTANT_ARRAY);
675 RECORD(TYPE_INCOMPLETE_ARRAY);
676 RECORD(TYPE_VARIABLE_ARRAY);
677 RECORD(TYPE_VECTOR);
678 RECORD(TYPE_EXT_VECTOR);
679 RECORD(TYPE_FUNCTION_PROTO);
680 RECORD(TYPE_FUNCTION_NO_PROTO);
681 RECORD(TYPE_TYPEDEF);
682 RECORD(TYPE_TYPEOF_EXPR);
683 RECORD(TYPE_TYPEOF);
684 RECORD(TYPE_RECORD);
685 RECORD(TYPE_ENUM);
686 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000687 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000688 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000689 RECORD(DECL_TRANSLATION_UNIT);
690 RECORD(DECL_TYPEDEF);
691 RECORD(DECL_ENUM);
692 RECORD(DECL_RECORD);
693 RECORD(DECL_ENUM_CONSTANT);
694 RECORD(DECL_FUNCTION);
695 RECORD(DECL_OBJC_METHOD);
696 RECORD(DECL_OBJC_INTERFACE);
697 RECORD(DECL_OBJC_PROTOCOL);
698 RECORD(DECL_OBJC_IVAR);
699 RECORD(DECL_OBJC_AT_DEFS_FIELD);
700 RECORD(DECL_OBJC_CLASS);
701 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
702 RECORD(DECL_OBJC_CATEGORY);
703 RECORD(DECL_OBJC_CATEGORY_IMPL);
704 RECORD(DECL_OBJC_IMPLEMENTATION);
705 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
706 RECORD(DECL_OBJC_PROPERTY);
707 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000708 RECORD(DECL_FIELD);
709 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000710 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000711 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000712 RECORD(DECL_FILE_SCOPE_ASM);
713 RECORD(DECL_BLOCK);
714 RECORD(DECL_CONTEXT_LEXICAL);
715 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000716 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattner0558df22009-04-27 00:49:53 +0000717 AddStmtsExprs(Stream, Record);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000718#undef RECORD
719#undef BLOCK
720 Stream.ExitBlock();
721}
722
Douglas Gregore650c8c2009-07-07 00:12:59 +0000723/// \brief Adjusts the given filename to only write out the portion of the
724/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000725///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000726/// \param Filename the file name to adjust.
727///
728/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
729/// the returned filename will be adjusted by this system root.
730///
731/// \returns either the original filename (if it needs no adjustment) or the
732/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000733static const char *
Douglas Gregore650c8c2009-07-07 00:12:59 +0000734adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
735 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000736
Douglas Gregore650c8c2009-07-07 00:12:59 +0000737 if (!isysroot)
738 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Douglas Gregore650c8c2009-07-07 00:12:59 +0000740 // Verify that the filename and the system root have the same prefix.
741 unsigned Pos = 0;
742 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
743 if (Filename[Pos] != isysroot[Pos])
744 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Douglas Gregore650c8c2009-07-07 00:12:59 +0000746 // We hit the end of the filename before we hit the end of the system root.
747 if (!Filename[Pos])
748 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Douglas Gregore650c8c2009-07-07 00:12:59 +0000750 // If the file name has a '/' at the current position, skip over the '/'.
751 // We distinguish sysroot-based includes from absolute includes by the
752 // absence of '/' at the beginning of sysroot-based includes.
753 if (Filename[Pos] == '/')
754 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000755
Douglas Gregore650c8c2009-07-07 00:12:59 +0000756 return Filename + Pos;
757}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000758
Sebastian Redl3397c552010-08-18 23:56:27 +0000759/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Sebastian Redla4232eb2010-08-18 23:56:21 +0000760void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000761 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000762
Douglas Gregore650c8c2009-07-07 00:12:59 +0000763 // Metadata
764 const TargetInfo &Target = Context.Target;
765 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl77f46032010-07-09 21:00:24 +0000766 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000767 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl3397c552010-08-18 23:56:27 +0000768 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
769 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregore650c8c2009-07-07 00:12:59 +0000770 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
771 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
772 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl77f46032010-07-09 21:00:24 +0000773 // Target triple or chained PCH name
774 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregore650c8c2009-07-07 00:12:59 +0000775 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Douglas Gregore650c8c2009-07-07 00:12:59 +0000777 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000778 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
779 Record.push_back(VERSION_MAJOR);
780 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000781 Record.push_back(CLANG_VERSION_MAJOR);
782 Record.push_back(CLANG_VERSION_MINOR);
783 Record.push_back(isysroot != 0);
Sebastian Redl77f46032010-07-09 21:00:24 +0000784 // FIXME: This writes the absolute path for chained headers.
785 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
786 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000787
Douglas Gregorb64c1932009-05-12 01:31:05 +0000788 // Original file name
789 SourceManager &SM = Context.getSourceManager();
790 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
791 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000792 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregorb64c1932009-05-12 01:31:05 +0000793 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
794 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
795
796 llvm::sys::Path MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000797
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000798 MainFilePath.makeAbsolute();
Douglas Gregorb64c1932009-05-12 01:31:05 +0000799
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +0000800 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000801 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000802 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000803 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000804 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000805 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000806 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000807
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000808 // Repository branch/version information.
809 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000810 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000811 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
812 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000813 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000814 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000815 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
816 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +0000817}
818
819/// \brief Write the LangOptions structure.
Sebastian Redla4232eb2010-08-18 23:56:21 +0000820void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000821 RecordData Record;
822 Record.push_back(LangOpts.Trigraphs);
823 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
824 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
825 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
826 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000827 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000828 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
829 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
830 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
831 Record.push_back(LangOpts.C99); // C99 Support
832 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencerdae4ac42010-10-21 05:21:48 +0000833 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
834 // already saved elsewhere.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000835 Record.push_back(LangOpts.CPlusPlus); // C++ Support
836 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000837 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000839 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
840 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000841 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000842 // modern abi enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000843 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000844 // modern abi enabled.
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +0000845 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump1eb44332009-09-09 15:08:12 +0000846
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000847 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000848 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
849 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000850 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000851 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar73482882010-02-10 18:48:44 +0000852 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000853
854 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
855 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
856 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
857
Chris Lattnerea5ce472009-04-27 07:35:58 +0000858 // Whether static initializers are protected by locks.
859 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +0000860 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000861 Record.push_back(LangOpts.Blocks); // block extension to C
862 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
863 // they are unused.
864 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
865 // (modulo the platform support).
866
Chris Lattnera4d71452010-06-26 21:25:03 +0000867 Record.push_back(LangOpts.getSignedOverflowBehavior());
868 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000869
870 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +0000871 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000872 // defined.
873 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
874 // opposed to __DYNAMIC__).
875 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
876
877 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
878 // used (instead of C99 semantics).
879 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +0000880 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
881 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +0000882 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
883 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +0000884 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000885 Record.push_back(LangOpts.getGCMode());
886 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000887 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000888 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000889 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne08a53262010-12-01 19:14:57 +0000890 Record.push_back(LangOpts.CUDA);
Mike Stump9c276ae2009-12-12 01:27:46 +0000891 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson92f58222009-08-22 22:30:33 +0000892 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +0000893 Record.push_back(LangOpts.SpellChecking);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000894 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000895}
896
Douglas Gregor14f79002009-04-10 03:52:48 +0000897//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000898// stat cache Serialization
899//===----------------------------------------------------------------------===//
900
901namespace {
902// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +0000903class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000904public:
905 typedef const char * key_type;
906 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000907
Chris Lattner74e976b2010-11-23 19:28:12 +0000908 typedef struct stat data_type;
909 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000910
911 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000912 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000913 }
Mike Stump1eb44332009-09-09 15:08:12 +0000914
915 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000916 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
917 data_type_ref Data) {
918 unsigned StrLen = strlen(path);
919 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +0000920 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000921 clang::io::Emit8(Out, DataLen);
922 return std::make_pair(StrLen + 1, DataLen);
923 }
Mike Stump1eb44332009-09-09 15:08:12 +0000924
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000925 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
926 Out.write(path, KeyLen);
927 }
Mike Stump1eb44332009-09-09 15:08:12 +0000928
Chris Lattner74e976b2010-11-23 19:28:12 +0000929 void EmitData(llvm::raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000930 data_type_ref Data, unsigned DataLen) {
931 using namespace clang::io;
932 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Chris Lattner74e976b2010-11-23 19:28:12 +0000934 Emit32(Out, (uint32_t) Data.st_ino);
935 Emit32(Out, (uint32_t) Data.st_dev);
936 Emit16(Out, (uint16_t) Data.st_mode);
937 Emit64(Out, (uint64_t) Data.st_mtime);
938 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000939
940 assert(Out.tell() - Start == DataLen && "Wrong data length");
941 }
942};
943} // end anonymous namespace
944
Sebastian Redl3397c552010-08-18 23:56:27 +0000945/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +0000946void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000947 // Build the on-disk hash table containing information about every
948 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +0000949 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000950 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000951 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000952 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000953 Stat != StatEnd; ++Stat, ++NumStatEntries) {
954 const char *Filename = Stat->first();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000955 Generator.insert(Filename, Stat->second);
956 }
Mike Stump1eb44332009-09-09 15:08:12 +0000957
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000958 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000959 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000960 uint32_t BucketOffset;
961 {
962 llvm::raw_svector_ostream Out(StatCacheData);
963 // Make sure that no bucket is at offset 0
964 clang::io::Emit32(Out, 0);
965 BucketOffset = Generator.Emit(Out);
966 }
967
968 // Create a blob abbreviation
969 using namespace llvm;
970 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000971 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000972 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
973 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
974 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
975 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
976
977 // Write the stat cache
978 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000979 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000980 Record.push_back(BucketOffset);
981 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000982 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000983}
984
985//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +0000986// Source Manager Serialization
987//===----------------------------------------------------------------------===//
988
989/// \brief Create an abbreviation for the SLocEntry that refers to a
990/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000991static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000992 using namespace llvm;
993 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000994 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +0000995 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
996 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
997 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
998 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +0000999 // FileEntry fields.
1000 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1001 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor12fab312010-03-16 16:35:32 +00001002 // HeaderFileInfo fields.
1003 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
1004 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
1005 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
1006 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregor14f79002009-04-10 03:52:48 +00001007 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001008 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001009}
1010
1011/// \brief Create an abbreviation for the SLocEntry that refers to a
1012/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001013static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001014 using namespace llvm;
1015 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001016 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001017 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1018 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1019 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1020 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1021 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001022 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001023}
1024
1025/// \brief Create an abbreviation for the SLocEntry that refers to a
1026/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001027static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001028 using namespace llvm;
1029 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001030 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001031 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001032 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001033}
1034
1035/// \brief Create an abbreviation for the SLocEntry that refers to an
1036/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001037static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001038 using namespace llvm;
1039 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001040 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001041 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1042 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1043 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1044 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001045 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001046 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001047}
1048
1049/// \brief Writes the block containing the serialized form of the
1050/// source manager.
1051///
1052/// TODO: We should probably use an on-disk hash table (stored in a
1053/// blob), indexed based on the file name, so that we only create
1054/// entries for files that we actually need. In the common case (no
1055/// errors), we probably won't have to create file entries for any of
1056/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001057void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001058 const Preprocessor &PP,
1059 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001060 RecordData Record;
1061
Chris Lattnerf04ad692009-04-10 17:16:57 +00001062 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001063 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001064
1065 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001066 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1067 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1068 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1069 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001070
Douglas Gregorbd945002009-04-13 16:31:14 +00001071 // Write the line table.
1072 if (SourceMgr.hasLineTable()) {
1073 LineTableInfo &LineTable = SourceMgr.getLineTable();
1074
1075 // Emit the file names
1076 Record.push_back(LineTable.getNumFilenames());
1077 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1078 // Emit the file name
1079 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001080 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +00001081 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1082 Record.push_back(FilenameLen);
1083 if (FilenameLen)
1084 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1085 }
Mike Stump1eb44332009-09-09 15:08:12 +00001086
Douglas Gregorbd945002009-04-13 16:31:14 +00001087 // Emit the line entries
1088 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1089 L != LEnd; ++L) {
1090 // Emit the file ID
1091 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +00001092
Douglas Gregorbd945002009-04-13 16:31:14 +00001093 // Emit the line entries
1094 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001095 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +00001096 LEEnd = L->second.end();
1097 LE != LEEnd; ++LE) {
1098 Record.push_back(LE->FileOffset);
1099 Record.push_back(LE->LineNo);
1100 Record.push_back(LE->FilenameID);
1101 Record.push_back((unsigned)LE->FileKind);
1102 Record.push_back(LE->IncludeOffset);
1103 }
Douglas Gregorbd945002009-04-13 16:31:14 +00001104 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001105 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001106 }
1107
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001108 // Write out the source location entry table. We skip the first
1109 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001110 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001111 RecordData PreloadSLocs;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001112 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1113 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1114 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1115 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001116 // Get this source location entry.
1117 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001118
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001119 // Record the offset of this source-location entry.
1120 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1121
1122 // Figure out which record code to use.
1123 unsigned Code;
1124 if (SLoc->isFile()) {
1125 if (SLoc->getFile().getContentCache()->Entry)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001126 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001127 else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001128 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001129 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001130 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001131 Record.clear();
1132 Record.push_back(Code);
1133
1134 Record.push_back(SLoc->getOffset());
1135 if (SLoc->isFile()) {
1136 const SrcMgr::FileInfo &File = SLoc->getFile();
1137 Record.push_back(File.getIncludeLoc().getRawEncoding());
1138 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1139 Record.push_back(File.hasLineDirectives());
1140
1141 const SrcMgr::ContentCache *Content = File.getContentCache();
1142 if (Content->Entry) {
1143 // The source location entry is a file. The blob associated
1144 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001145
Douglas Gregor2d52be52010-03-21 22:49:54 +00001146 // Emit size/modification time for this file.
1147 Record.push_back(Content->Entry->getSize());
1148 Record.push_back(Content->Entry->getModificationTime());
1149
Douglas Gregor12fab312010-03-16 16:35:32 +00001150 // Emit header-search information associated with this file.
1151 HeaderFileInfo HFI;
1152 HeaderSearch &HS = PP.getHeaderSearchInfo();
1153 if (Content->Entry->getUID() < HS.header_file_size())
1154 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1155 Record.push_back(HFI.isImport);
1156 Record.push_back(HFI.DirInfo);
1157 Record.push_back(HFI.NumIncludes);
1158 AddIdentifierRef(HFI.ControllingMacro, Record);
1159
Douglas Gregore650c8c2009-07-07 00:12:59 +00001160 // Turn the file name into an absolute path, if it isn't already.
1161 const char *Filename = Content->Entry->getName();
1162 llvm::sys::Path FilePath(Filename, strlen(Filename));
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001163 FilePath.makeAbsolute();
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001164 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001165
Douglas Gregore650c8c2009-07-07 00:12:59 +00001166 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001167 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001168
1169 // FIXME: For now, preload all file source locations, so that
1170 // we get the appropriate File entries in the reader. This is
1171 // a temporary measure.
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001172 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001173 } else {
1174 // The source location entry is a buffer. The blob associated
1175 // with this entry contains the contents of the buffer.
1176
1177 // We add one to the size so that we capture the trailing NULL
1178 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1179 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001180 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001181 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001182 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001183 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1184 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001185 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001186 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001187 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001188 llvm::StringRef(Buffer->getBufferStart(),
1189 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001190
1191 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001192 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001193 }
1194 } else {
1195 // The source location entry is an instantiation.
1196 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1197 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1198 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1199 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1200
1201 // Compute the token length for this macro expansion.
1202 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001203 if (I + 1 != N)
1204 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001205 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1206 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1207 }
1208 }
1209
Douglas Gregorc9490c02009-04-16 22:23:12 +00001210 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001211
1212 if (SLocEntryOffsets.empty())
1213 return;
1214
Sebastian Redl3397c552010-08-18 23:56:27 +00001215 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001216 // table is used for lazily loading source-location information.
1217 using namespace llvm;
1218 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001219 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001220 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1221 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1222 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1223 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001224
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001225 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001226 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001227 Record.push_back(SLocEntryOffsets.size());
Sebastian Redl8db9fae2010-09-22 20:19:08 +00001228 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1229 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001230 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001231 (const char *)data(SLocEntryOffsets),
Chris Lattner090d9b52009-04-27 19:01:47 +00001232 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001233
Sebastian Redl3397c552010-08-18 23:56:27 +00001234 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001235 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001236 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001237}
1238
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001239//===----------------------------------------------------------------------===//
1240// Preprocessor Serialization
1241//===----------------------------------------------------------------------===//
1242
Chris Lattner0b1fb982009-04-10 17:15:23 +00001243/// \brief Writes the block containing the serialized form of the
1244/// preprocessor.
1245///
Sebastian Redla4232eb2010-08-18 23:56:21 +00001246void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001247 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001248
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001249 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1250 if (PP.getCounterValue() != 0) {
1251 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001252 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001253 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001254 }
1255
1256 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001257 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001258
Sebastian Redl3397c552010-08-18 23:56:27 +00001259 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001260 // FIXME: use diagnostics subsystem for localization etc.
1261 if (PP.SawDateOrTime())
1262 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001263
Douglas Gregorecdcb882010-10-20 22:00:55 +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();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001268 unsigned InclusionAbbrev = 0;
1269 if (PPRec) {
1270 using namespace llvm;
1271 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1272 Abbrev->Add(BitCodeAbbrevOp(PP_INCLUSION_DIRECTIVE));
1273 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1274 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1275 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1276 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1277 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1278 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1279 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Michael J. Spencer20249a12010-10-21 03:16:25 +00001280 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorecdcb882010-10-20 22:00:55 +00001281 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001282
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001283 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1284 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001285 // FIXME: This emits macros in hash table order, we should do it in a stable
1286 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001287 MacroInfo *MI = I->second;
1288
Sebastian Redl3397c552010-08-18 23:56:27 +00001289 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001290 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl3397c552010-08-18 23:56:27 +00001291 // Also skip macros from a AST file if we're chaining.
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001292
1293 // FIXME: There is a (probably minor) optimization we could do here, if
1294 // the macro comes from the original PCH but the identifier comes from a
1295 // chained PCH, by storing the offset into the original PCH rather than
1296 // writing the macro definition a second time.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001297 if (MI->isBuiltinMacro() ||
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001298 (Chain && I->first->isFromAST() && MI->isFromAST()))
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001299 continue;
1300
Chris Lattner7356a312009-04-11 21:15:38 +00001301 AddIdentifierRef(I->first, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001302 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001303 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1304 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001305
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001306 unsigned Code;
1307 if (MI->isObjectLike()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001308 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001309 } else {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001310 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001311
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001312 Record.push_back(MI->isC99Varargs());
1313 Record.push_back(MI->isGNUVarargs());
1314 Record.push_back(MI->getNumArgs());
1315 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1316 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001317 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001318 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001319
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001320 // If we have a detailed preprocessing record, record the macro definition
1321 // ID that corresponds to this macro.
1322 if (PPRec)
1323 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer20249a12010-10-21 03:16:25 +00001324
Douglas Gregorc9490c02009-04-16 22:23:12 +00001325 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001326 Record.clear();
1327
Chris Lattnerdf961c22009-04-10 18:08:30 +00001328 // Emit the tokens array.
1329 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1330 // Note that we know that the preprocessor does not have any annotation
1331 // tokens in it because they are created by the parser, and thus can't be
1332 // in a macro definition.
1333 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001334
Chris Lattnerdf961c22009-04-10 18:08:30 +00001335 Record.push_back(Tok.getLocation().getRawEncoding());
1336 Record.push_back(Tok.getLength());
1337
Chris Lattnerdf961c22009-04-10 18:08:30 +00001338 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1339 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001340 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001341
Chris Lattnerdf961c22009-04-10 18:08:30 +00001342 // FIXME: Should translate token kind to a stable encoding.
1343 Record.push_back(Tok.getKind());
1344 // FIXME: Should translate token flags to a stable encoding.
1345 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001346
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001347 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001348 Record.clear();
1349 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001350 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001351 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001352
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001353 // If the preprocessor has a preprocessing record, emit it.
1354 unsigned NumPreprocessingRecords = 0;
1355 if (PPRec) {
Sebastian Redl196f5572010-09-27 23:20:01 +00001356 unsigned IndexBase = Chain ? PPRec->getNumPreallocatedEntities() : 0;
Sebastian Redlb57a6242010-09-27 22:18:47 +00001357 for (PreprocessingRecord::iterator E = PPRec->begin(Chain),
1358 EEnd = PPRec->end(Chain);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001359 E != EEnd; ++E) {
1360 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001361
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001362 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1363 // Record this macro definition's location.
Sebastian Redlf73c93f2010-09-15 19:54:06 +00001364 MacroID ID = getMacroDefinitionID(MD);
Douglas Gregor89d99802010-11-30 06:16:57 +00001365
Douglas Gregor77424bc2010-10-02 19:29:26 +00001366 // Don't write the macro definition if it is from another AST file.
1367 if (ID < FirstMacroID)
1368 continue;
Douglas Gregor89d99802010-11-30 06:16:57 +00001369
1370 // Notify the serialization listener that we're serializing this entity.
1371 if (SerializationListener)
1372 SerializationListener->SerializedPreprocessedEntity(*E,
1373 Stream.GetCurrentBitNo());
Michael J. Spencer20249a12010-10-21 03:16:25 +00001374
Douglas Gregor77424bc2010-10-02 19:29:26 +00001375 unsigned Position = ID - FirstMacroID;
1376 if (Position != MacroDefinitionOffsets.size()) {
1377 if (Position > MacroDefinitionOffsets.size())
1378 MacroDefinitionOffsets.resize(Position + 1);
Douglas Gregor89d99802010-11-30 06:16:57 +00001379
Michael J. Spencer20249a12010-10-21 03:16:25 +00001380 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001381 } else
1382 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor89d99802010-11-30 06:16:57 +00001383
Sebastian Redlb57a6242010-09-27 22:18:47 +00001384 Record.push_back(IndexBase + NumPreprocessingRecords++);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001385 Record.push_back(ID);
1386 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1387 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1388 AddIdentifierRef(MD->getName(), Record);
1389 AddSourceLocation(MD->getLocation(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001390 Stream.EmitRecord(PP_MACRO_DEFINITION, Record);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001391 continue;
1392 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001393
Douglas Gregor89d99802010-11-30 06:16:57 +00001394 // Notify the serialization listener that we're serializing this entity.
1395 if (SerializationListener)
1396 SerializationListener->SerializedPreprocessedEntity(*E,
1397 Stream.GetCurrentBitNo());
1398
1399 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1400 Record.push_back(IndexBase + NumPreprocessingRecords++);
1401 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1402 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1403 AddIdentifierRef(MI->getName(), Record);
1404 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1405 Stream.EmitRecord(PP_MACRO_INSTANTIATION, Record);
1406 continue;
1407 }
1408
Douglas Gregorecdcb882010-10-20 22:00:55 +00001409 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1410 Record.push_back(PP_INCLUSION_DIRECTIVE);
1411 Record.push_back(IndexBase + NumPreprocessingRecords++);
1412 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1413 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1414 Record.push_back(ID->getFileName().size());
1415 Record.push_back(ID->wasInQuotes());
1416 Record.push_back(static_cast<unsigned>(ID->getKind()));
1417 llvm::SmallString<64> Buffer;
1418 Buffer += ID->getFileName();
1419 Buffer += ID->getFile()->getName();
1420 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1421 continue;
1422 }
Douglas Gregor89d99802010-11-30 06:16:57 +00001423
1424 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001425 }
1426 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001427
Douglas Gregorc9490c02009-04-16 22:23:12 +00001428 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001429
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001430 // Write the offsets table for the preprocessing record.
1431 if (NumPreprocessingRecords > 0) {
1432 // Write the offsets table for identifier IDs.
1433 using namespace llvm;
1434 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001435 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001436 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1437 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1438 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1439 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001440
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001441 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001442 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001443 Record.push_back(NumPreprocessingRecords);
1444 Record.push_back(MacroDefinitionOffsets.size());
1445 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001446 (const char *)data(MacroDefinitionOffsets),
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001447 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1448 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001449}
1450
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001451void ASTWriter::WriteUserDiagnosticMappings(const Diagnostic &Diag) {
1452 RecordData Record;
1453 for (unsigned i = 0; i != diag::DIAG_UPPER_LIMIT; ++i) {
1454 diag::Mapping Map = Diag.getDiagnosticMappingInfo(i);
1455 if (Map & 0x8) { // user mapping.
1456 Record.push_back(i);
1457 Record.push_back(Map & 0x7);
1458 }
1459 }
1460
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00001461 if (!Record.empty())
1462 Stream.EmitRecord(DIAG_USER_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001463}
1464
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001465//===----------------------------------------------------------------------===//
1466// Type Serialization
1467//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001468
Sebastian Redl3397c552010-08-18 23:56:27 +00001469/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001470void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00001471 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001472 if (Idx.getIndex() == 0) // we haven't seen this type before.
1473 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00001474
Douglas Gregor97475832010-10-05 18:37:06 +00001475 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00001476
Douglas Gregor2cf26342009-04-09 22:27:44 +00001477 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001478 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00001479 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001480 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00001481 else if (TypeOffsets.size() < Index) {
1482 TypeOffsets.resize(Index + 1);
1483 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001484 }
1485
1486 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001487
Douglas Gregor2cf26342009-04-09 22:27:44 +00001488 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00001489 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001490
Douglas Gregora4923eb2009-11-16 21:35:15 +00001491 if (T.hasLocalNonFastQualifiers()) {
1492 Qualifiers Qs = T.getLocalQualifiers();
1493 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001494 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001495 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00001496 } else {
1497 switch (T->getTypeClass()) {
1498 // For all of the concrete, non-dependent types, call the
1499 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001500#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001501 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001502#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001503#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001504 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001505 }
1506
1507 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001508 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001509
1510 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001511 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001512}
1513
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001514//===----------------------------------------------------------------------===//
1515// Declaration Serialization
1516//===----------------------------------------------------------------------===//
1517
Douglas Gregor2cf26342009-04-09 22:27:44 +00001518/// \brief Write the block containing all of the declaration IDs
1519/// lexically declared within the given DeclContext.
1520///
1521/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1522/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001523uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001524 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001525 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001526 return 0;
1527
Douglas Gregorc9490c02009-04-16 22:23:12 +00001528 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001529 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001530 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001531 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001532 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1533 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001534 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001535
Douglas Gregor25123082009-04-22 22:34:57 +00001536 ++NumLexicalDeclContexts;
Sebastian Redl681d7232010-07-27 00:17:23 +00001537 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001538 reinterpret_cast<char*>(Decls.data()),
1539 Decls.size() * sizeof(KindDeclIDPair));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001540 return Offset;
1541}
1542
Sebastian Redla4232eb2010-08-18 23:56:21 +00001543void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00001544 using namespace llvm;
1545 RecordData Record;
1546
1547 // Write the type offsets array
1548 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001549 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001550 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1551 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1552 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1553 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001554 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00001555 Record.push_back(TypeOffsets.size());
1556 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001557 (const char *)data(TypeOffsets),
Sebastian Redl1476ed42010-07-16 16:36:56 +00001558 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1559
1560 // Write the declaration offsets array
1561 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001562 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001563 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1564 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1565 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1566 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001567 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00001568 Record.push_back(DeclOffsets.size());
1569 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001570 (const char *)data(DeclOffsets),
Sebastian Redl1476ed42010-07-16 16:36:56 +00001571 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1572}
1573
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001574//===----------------------------------------------------------------------===//
1575// Global Method Pool and Selector Serialization
1576//===----------------------------------------------------------------------===//
1577
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001578namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001579// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00001580class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00001581 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001582
1583public:
1584 typedef Selector key_type;
1585 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001586
Sebastian Redl5d050072010-08-04 17:20:04 +00001587 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001588 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00001589 ObjCMethodList Instance, Factory;
1590 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001591 typedef const data_type& data_type_ref;
1592
Sebastian Redl3397c552010-08-18 23:56:27 +00001593 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001594
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001595 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00001596 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001597 }
Mike Stump1eb44332009-09-09 15:08:12 +00001598
1599 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001600 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1601 data_type_ref Methods) {
1602 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1603 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00001604 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1605 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001606 Method = Method->Next)
1607 if (Method->Method)
1608 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00001609 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001610 Method = Method->Next)
1611 if (Method->Method)
1612 DataLen += 4;
1613 clang::io::Emit16(Out, DataLen);
1614 return std::make_pair(KeyLen, DataLen);
1615 }
Mike Stump1eb44332009-09-09 15:08:12 +00001616
Douglas Gregor83941df2009-04-25 17:48:32 +00001617 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00001618 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00001619 assert((Start >> 32) == 0 && "Selector key offset too large");
1620 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001621 unsigned N = Sel.getNumArgs();
1622 clang::io::Emit16(Out, N);
1623 if (N == 0)
1624 N = 1;
1625 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001626 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001627 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1628 }
Mike Stump1eb44332009-09-09 15:08:12 +00001629
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001630 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00001631 data_type_ref Methods, unsigned DataLen) {
1632 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00001633 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001634 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00001635 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001636 Method = Method->Next)
1637 if (Method->Method)
1638 ++NumInstanceMethods;
1639
1640 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00001641 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001642 Method = Method->Next)
1643 if (Method->Method)
1644 ++NumFactoryMethods;
1645
1646 clang::io::Emit16(Out, NumInstanceMethods);
1647 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00001648 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001649 Method = Method->Next)
1650 if (Method->Method)
1651 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00001652 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001653 Method = Method->Next)
1654 if (Method->Method)
1655 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00001656
1657 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001658 }
1659};
1660} // end anonymous namespace
1661
Sebastian Redl059612d2010-08-03 21:58:15 +00001662/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001663///
1664/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00001665/// in an on-disk hash table indexed by the selector. The hash table also
1666/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001667void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001668 using namespace llvm;
1669
Sebastian Redl059612d2010-08-03 21:58:15 +00001670 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00001671 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00001672 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00001673 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00001674 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001675 {
Sebastian Redl3397c552010-08-18 23:56:27 +00001676 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001677 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001678
Sebastian Redl059612d2010-08-03 21:58:15 +00001679 // Create the on-disk hash table representation. We walk through every
1680 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00001681 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001682 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00001683 I = SelectorIDs.begin(), E = SelectorIDs.end();
1684 I != E; ++I) {
1685 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00001686 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00001687 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00001688 I->second,
1689 ObjCMethodList(),
1690 ObjCMethodList()
1691 };
1692 if (F != SemaRef.MethodPool.end()) {
1693 Data.Instance = F->second.first;
1694 Data.Factory = F->second.second;
1695 }
Sebastian Redl3397c552010-08-18 23:56:27 +00001696 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00001697 // changed.
1698 if (Chain && I->second < FirstSelectorID) {
1699 // Selector already exists. Did it change?
1700 bool changed = false;
1701 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
1702 M = M->Next) {
1703 if (M->Method->getPCHLevel() == 0)
1704 changed = true;
1705 }
1706 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
1707 M = M->Next) {
1708 if (M->Method->getPCHLevel() == 0)
1709 changed = true;
1710 }
1711 if (!changed)
1712 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00001713 } else if (Data.Instance.Method || Data.Factory.Method) {
1714 // A new method pool entry.
1715 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00001716 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001717 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001718 }
1719
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001720 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001721 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001722 uint32_t BucketOffset;
1723 {
Sebastian Redl3397c552010-08-18 23:56:27 +00001724 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001725 llvm::raw_svector_ostream Out(MethodPool);
1726 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001727 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001728 BucketOffset = Generator.Emit(Out, Trait);
1729 }
1730
1731 // Create a blob abbreviation
1732 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001733 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001734 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00001735 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001736 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1737 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1738
Douglas Gregor83941df2009-04-25 17:48:32 +00001739 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001740 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001741 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001742 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00001743 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001744 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00001745
1746 // Create a blob abbreviation for the selector table offsets.
1747 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001748 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00001749 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor83941df2009-04-25 17:48:32 +00001750 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1751 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1752
1753 // Write the selector offsets table.
1754 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001755 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00001756 Record.push_back(SelectorOffsets.size());
1757 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001758 (const char *)data(SelectorOffsets),
Douglas Gregor83941df2009-04-25 17:48:32 +00001759 SelectorOffsets.size() * 4);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001760 }
1761}
1762
Sebastian Redl3397c552010-08-18 23:56:27 +00001763/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001764void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00001765 using namespace llvm;
1766 if (SemaRef.ReferencedSelectors.empty())
1767 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00001768
Fariborz Jahanian32019832010-07-23 19:11:11 +00001769 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00001770
Sebastian Redl3397c552010-08-18 23:56:27 +00001771 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00001772 // very tricky to fix, and given that @selector shouldn't really appear in
1773 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00001774 for (DenseMap<Selector, SourceLocation>::iterator S =
1775 SemaRef.ReferencedSelectors.begin(),
1776 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
1777 Selector Sel = (*S).first;
1778 SourceLocation Loc = (*S).second;
1779 AddSelectorRef(Sel, Record);
1780 AddSourceLocation(Loc, Record);
1781 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001782 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00001783}
1784
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001785//===----------------------------------------------------------------------===//
1786// Identifier Table Serialization
1787//===----------------------------------------------------------------------===//
1788
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001789namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00001790class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00001791 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00001792 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001793
Douglas Gregora92193e2009-04-28 21:18:29 +00001794 /// \brief Determines whether this is an "interesting" identifier
1795 /// that needs a full IdentifierInfo structure written into the hash
1796 /// table.
1797 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1798 return II->isPoisoned() ||
1799 II->isExtensionToken() ||
1800 II->hasMacroDefinition() ||
1801 II->getObjCOrBuiltinID() ||
1802 II->getFETokenInfo<void>();
1803 }
1804
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001805public:
1806 typedef const IdentifierInfo* key_type;
1807 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001808
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001809 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001810 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001811
Sebastian Redl3397c552010-08-18 23:56:27 +00001812 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00001813 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001814
1815 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001816 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001817 }
Mike Stump1eb44332009-09-09 15:08:12 +00001818
1819 std::pair<unsigned,unsigned>
1820 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001821 IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00001822 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00001823 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1824 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00001825 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00001826 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00001827 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00001828 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00001829 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1830 DEnd = IdentifierResolver::end();
1831 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001832 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00001833 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001834 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00001835 // We emit the key length after the data length so that every
1836 // string is preceded by a 16-bit length. This matches the PTH
1837 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00001838 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001839 return std::make_pair(KeyLen, DataLen);
1840 }
Mike Stump1eb44332009-09-09 15:08:12 +00001841
1842 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001843 unsigned KeyLen) {
1844 // Record the location of the key data. This is used when generating
1845 // the mapping from persistent IDs to strings.
1846 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00001847 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001848 }
Mike Stump1eb44332009-09-09 15:08:12 +00001849
1850 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001851 IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00001852 if (!isInterestingIdentifier(II)) {
1853 clang::io::Emit32(Out, ID << 1);
1854 return;
1855 }
Douglas Gregor5998da52009-04-28 21:32:13 +00001856
Douglas Gregora92193e2009-04-28 21:18:29 +00001857 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001858 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001859 bool hasMacroDefinition =
1860 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00001861 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00001862 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001863 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1864 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1865 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00001866 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001867 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00001868 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001869
Douglas Gregor37e26842009-04-21 23:56:24 +00001870 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00001871 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00001872
Douglas Gregor668c1a42009-04-21 22:25:48 +00001873 // Emit the declaration IDs in reverse order, because the
1874 // IdentifierResolver provides the declarations as they would be
1875 // visible (e.g., the function "stat" would come before the struct
1876 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1877 // adds declarations to the end of the list (so we need to see the
1878 // struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00001879 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump1eb44332009-09-09 15:08:12 +00001880 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00001881 IdentifierResolver::end());
1882 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1883 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001884 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00001885 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001886 }
1887};
1888} // end anonymous namespace
1889
Sebastian Redl3397c552010-08-18 23:56:27 +00001890/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001891///
1892/// The identifier table consists of a blob containing string data
1893/// (the actual identifiers themselves) and a separate "offsets" index
1894/// that maps identifier IDs to locations within the blob.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001895void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001896 using namespace llvm;
1897
1898 // Create and write out the blob that contains the identifier
1899 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001900 {
Sebastian Redl3397c552010-08-18 23:56:27 +00001901 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001902 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump1eb44332009-09-09 15:08:12 +00001903
Douglas Gregor92b059e2009-04-28 20:33:11 +00001904 // Look for any identifiers that were named while processing the
1905 // headers, but are otherwise not needed. We add these to the hash
1906 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00001907 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00001908 // file.
1909 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1910 IDEnd = PP.getIdentifierTable().end();
1911 ID != IDEnd; ++ID)
1912 getIdentifierRef(ID->second);
1913
Sebastian Redlf2f0f032010-07-23 23:49:55 +00001914 // Create the on-disk hash table representation. We only store offsets
1915 // for identifiers that appear here for the first time.
1916 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001917 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00001918 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1919 ID != IDEnd; ++ID) {
1920 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001921 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001922 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001923 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001924
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001925 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001926 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00001927 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001928 {
Sebastian Redl3397c552010-08-18 23:56:27 +00001929 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001930 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001931 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001932 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001933 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001934 }
1935
1936 // Create a blob abbreviation
1937 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001938 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001939 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001940 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00001941 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001942
1943 // Write the identifier table
1944 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001945 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001946 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001947 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001948 }
1949
1950 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001951 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001952 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001953 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1954 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1955 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1956
1957 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001958 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001959 Record.push_back(IdentifierOffsets.size());
1960 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001961 (const char *)data(IdentifierOffsets),
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001962 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregorafaf3082009-04-11 00:14:32 +00001963}
1964
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001965//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001966// DeclContext's Name Lookup Table Serialization
1967//===----------------------------------------------------------------------===//
1968
1969namespace {
1970// Trait used for the on-disk hash table used in the method pool.
1971class ASTDeclContextNameLookupTrait {
1972 ASTWriter &Writer;
1973
1974public:
1975 typedef DeclarationName key_type;
1976 typedef key_type key_type_ref;
1977
1978 typedef DeclContext::lookup_result data_type;
1979 typedef const data_type& data_type_ref;
1980
1981 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
1982
1983 unsigned ComputeHash(DeclarationName Name) {
1984 llvm::FoldingSetNodeID ID;
1985 ID.AddInteger(Name.getNameKind());
1986
1987 switch (Name.getNameKind()) {
1988 case DeclarationName::Identifier:
1989 ID.AddString(Name.getAsIdentifierInfo()->getName());
1990 break;
1991 case DeclarationName::ObjCZeroArgSelector:
1992 case DeclarationName::ObjCOneArgSelector:
1993 case DeclarationName::ObjCMultiArgSelector:
1994 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
1995 break;
1996 case DeclarationName::CXXConstructorName:
1997 case DeclarationName::CXXDestructorName:
1998 case DeclarationName::CXXConversionFunctionName:
1999 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
2000 break;
2001 case DeclarationName::CXXOperatorName:
2002 ID.AddInteger(Name.getCXXOverloadedOperator());
2003 break;
2004 case DeclarationName::CXXLiteralOperatorName:
2005 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2006 case DeclarationName::CXXUsingDirective:
2007 break;
2008 }
2009
2010 return ID.ComputeHash();
2011 }
2012
2013 std::pair<unsigned,unsigned>
2014 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2015 data_type_ref Lookup) {
2016 unsigned KeyLen = 1;
2017 switch (Name.getNameKind()) {
2018 case DeclarationName::Identifier:
2019 case DeclarationName::ObjCZeroArgSelector:
2020 case DeclarationName::ObjCOneArgSelector:
2021 case DeclarationName::ObjCMultiArgSelector:
2022 case DeclarationName::CXXConstructorName:
2023 case DeclarationName::CXXDestructorName:
2024 case DeclarationName::CXXConversionFunctionName:
2025 case DeclarationName::CXXLiteralOperatorName:
2026 KeyLen += 4;
2027 break;
2028 case DeclarationName::CXXOperatorName:
2029 KeyLen += 1;
2030 break;
2031 case DeclarationName::CXXUsingDirective:
2032 break;
2033 }
2034 clang::io::Emit16(Out, KeyLen);
2035
2036 // 2 bytes for num of decls and 4 for each DeclID.
2037 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2038 clang::io::Emit16(Out, DataLen);
2039
2040 return std::make_pair(KeyLen, DataLen);
2041 }
2042
2043 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2044 using namespace clang::io;
2045
2046 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2047 Emit8(Out, Name.getNameKind());
2048 switch (Name.getNameKind()) {
2049 case DeclarationName::Identifier:
2050 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2051 break;
2052 case DeclarationName::ObjCZeroArgSelector:
2053 case DeclarationName::ObjCOneArgSelector:
2054 case DeclarationName::ObjCMultiArgSelector:
2055 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2056 break;
2057 case DeclarationName::CXXConstructorName:
2058 case DeclarationName::CXXDestructorName:
2059 case DeclarationName::CXXConversionFunctionName:
2060 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2061 break;
2062 case DeclarationName::CXXOperatorName:
2063 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2064 Emit8(Out, Name.getCXXOverloadedOperator());
2065 break;
2066 case DeclarationName::CXXLiteralOperatorName:
2067 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2068 break;
2069 case DeclarationName::CXXUsingDirective:
2070 break;
2071 }
2072 }
2073
2074 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2075 data_type Lookup, unsigned DataLen) {
2076 uint64_t Start = Out.tell(); (void)Start;
2077 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2078 for (; Lookup.first != Lookup.second; ++Lookup.first)
2079 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2080
2081 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2082 }
2083};
2084} // end anonymous namespace
2085
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002086/// \brief Write the block containing all of the declaration IDs
2087/// visible from the given DeclContext.
2088///
2089/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002090/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002091uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2092 DeclContext *DC) {
2093 if (DC->getPrimaryContext() != DC)
2094 return 0;
2095
2096 // Since there is no name lookup into functions or methods, don't bother to
2097 // build a visible-declarations table for these entities.
2098 if (DC->isFunctionOrMethod())
2099 return 0;
2100
2101 // If not in C++, we perform name lookup for the translation unit via the
2102 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2103 // FIXME: In C++ we need the visible declarations in order to "see" the
2104 // friend declarations, is there a way to do this without writing the table ?
2105 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2106 return 0;
2107
2108 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00002109 if (DC->hasExternalVisibleStorage())
2110 DC->MaterializeVisibleDeclsFromExternalStorage();
2111 else
2112 DC->lookup(DeclarationName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002113
2114 // Serialize the contents of the mapping used for lookup. Note that,
2115 // although we have two very different code paths, the serialized
2116 // representation is the same for both cases: a declaration name,
2117 // followed by a size, followed by references to the visible
2118 // declarations that have that name.
2119 uint64_t Offset = Stream.GetCurrentBitNo();
2120 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2121 if (!Map || Map->empty())
2122 return 0;
2123
2124 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2125 ASTDeclContextNameLookupTrait Trait(*this);
2126
2127 // Create the on-disk hash table representation.
2128 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2129 D != DEnd; ++D) {
2130 DeclarationName Name = D->first;
2131 DeclContext::lookup_result Result = D->second.getLookupResult();
2132 Generator.insert(Name, Result, Trait);
2133 }
2134
2135 // Create the on-disk hash table in a buffer.
2136 llvm::SmallString<4096> LookupTable;
2137 uint32_t BucketOffset;
2138 {
2139 llvm::raw_svector_ostream Out(LookupTable);
2140 // Make sure that no bucket is at offset 0
2141 clang::io::Emit32(Out, 0);
2142 BucketOffset = Generator.Emit(Out, Trait);
2143 }
2144
2145 // Write the lookup table
2146 RecordData Record;
2147 Record.push_back(DECL_CONTEXT_VISIBLE);
2148 Record.push_back(BucketOffset);
2149 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2150 LookupTable.str());
2151
2152 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2153 ++NumVisibleDeclContexts;
2154 return Offset;
2155}
2156
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002157/// \brief Write an UPDATE_VISIBLE block for the given context.
2158///
2159/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2160/// DeclContext in a dependent AST file. As such, they only exist for the TU
2161/// (in C++) and for namespaces.
2162void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002163 // Make the context build its lookup table, but don't make it load external
2164 // decls.
2165 DC->lookup(DeclarationName());
2166
2167 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2168 if (!Map || Map->empty())
2169 return;
2170
2171 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2172 ASTDeclContextNameLookupTrait Trait(*this);
2173
2174 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002175 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2176 D != DEnd; ++D) {
2177 DeclarationName Name = D->first;
2178 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002179 // For any name that appears in this table, the results are complete, i.e.
2180 // they overwrite results from previous PCHs. Merging is always a mess.
2181 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002182 }
2183
2184 // Create the on-disk hash table in a buffer.
2185 llvm::SmallString<4096> LookupTable;
2186 uint32_t BucketOffset;
2187 {
2188 llvm::raw_svector_ostream Out(LookupTable);
2189 // Make sure that no bucket is at offset 0
2190 clang::io::Emit32(Out, 0);
2191 BucketOffset = Generator.Emit(Out, Trait);
2192 }
2193
2194 // Write the lookup table
2195 RecordData Record;
2196 Record.push_back(UPDATE_VISIBLE);
2197 Record.push_back(getDeclID(cast<Decl>(DC)));
2198 Record.push_back(BucketOffset);
2199 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2200}
2201
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002202//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002203// General Serialization Routines
2204//===----------------------------------------------------------------------===//
2205
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002206/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002207void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00002208 Record.push_back(Attrs.size());
Sean Huntcf807c42010-08-18 23:23:40 +00002209 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2210 const Attr * A = *i;
2211 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2212 AddSourceLocation(A->getLocation(), Record);
2213 Record.push_back(A->isInherited());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002214
Sean Huntcf807c42010-08-18 23:23:40 +00002215#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00002216
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002217 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002218}
2219
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002220void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002221 Record.push_back(Str.size());
2222 Record.insert(Record.end(), Str.begin(), Str.end());
2223}
2224
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002225/// \brief Note that the identifier II occurs at the given offset
2226/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002227void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002228 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00002229 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002230 // up earlier in the chain and thus don't need an offset.
2231 if (ID >= FirstIdentID)
2232 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002233}
2234
Douglas Gregor83941df2009-04-25 17:48:32 +00002235/// \brief Note that the selector Sel occurs at the given offset
2236/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002237void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00002238 unsigned ID = SelectorIDs[Sel];
2239 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00002240 // Don't record offsets for selectors that are also available in a different
2241 // file.
2242 if (ID < FirstSelectorID)
2243 return;
2244 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00002245}
2246
Sebastian Redla4232eb2010-08-18 23:56:21 +00002247ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregor89d99802010-11-30 06:16:57 +00002248 : Stream(Stream), Chain(0), SerializationListener(0),
2249 FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002250 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redle58aa892010-08-04 18:21:41 +00002251 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor77424bc2010-10-02 19:29:26 +00002252 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2253 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00002254 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor7c789c12010-10-29 22:39:52 +00002255 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
2256 NextCXXBaseSpecifiersID(1)
2257{
Sebastian Redl30c514c2010-07-14 23:45:08 +00002258}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002259
Sebastian Redla4232eb2010-08-18 23:56:21 +00002260void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002261 const char *isysroot) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002262 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002263 Stream.Emit((unsigned)'C', 8);
2264 Stream.Emit((unsigned)'P', 8);
2265 Stream.Emit((unsigned)'C', 8);
2266 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002267
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002268 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002269
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002270 if (Chain)
Sebastian Redla4232eb2010-08-18 23:56:21 +00002271 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002272 else
Sebastian Redla4232eb2010-08-18 23:56:21 +00002273 WriteASTCore(SemaRef, StatCalls, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002274}
2275
Sebastian Redla4232eb2010-08-18 23:56:21 +00002276void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002277 const char *isysroot) {
2278 using namespace llvm;
2279
2280 ASTContext &Context = SemaRef.Context;
2281 Preprocessor &PP = SemaRef.PP;
2282
Douglas Gregor2cf26342009-04-09 22:27:44 +00002283 // The translation unit is the first declaration we'll emit.
2284 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002285 ++NextDeclID;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002286 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002287
Douglas Gregor2deaea32009-04-22 18:49:13 +00002288 // Make sure that we emit IdentifierInfos (and any attached
2289 // declarations) for builtins.
2290 {
2291 IdentifierTable &Table = PP.getIdentifierTable();
2292 llvm::SmallVector<const char *, 32> BuiltinNames;
2293 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2294 Context.getLangOptions().NoBuiltin);
2295 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2296 getIdentifierRef(&Table.get(BuiltinNames[I]));
2297 }
2298
Chris Lattner63d65f82009-09-08 18:19:27 +00002299 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002300 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002301 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002302 RecordData TentativeDefinitions;
Sebastian Redle9d12b62010-01-31 22:27:38 +00002303 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2304 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner63d65f82009-09-08 18:19:27 +00002305 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002306
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002307 // Build a record containing all of the file scoped decls in this file.
2308 RecordData UnusedFileScopedDecls;
2309 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2310 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00002311
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002312 RecordData WeakUndeclaredIdentifiers;
2313 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2314 WeakUndeclaredIdentifiers.push_back(
2315 SemaRef.WeakUndeclaredIdentifiers.size());
2316 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2317 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2318 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2319 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2320 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2321 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2322 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2323 }
2324 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002325
Douglas Gregor14c22f22009-04-22 22:18:58 +00002326 // Build a record containing all of the locally-scoped external
2327 // declarations in this header file. Generally, this record will be
2328 // empty.
2329 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002330 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00002331 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002332 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002333 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2334 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2335 TD != TDEnd; ++TD)
2336 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2337
Douglas Gregorb81c1702009-04-27 20:06:05 +00002338 // Build a record containing all of the ext_vector declarations.
2339 RecordData ExtVectorDecls;
2340 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2341 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2342
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002343 // Build a record containing all of the VTable uses information.
2344 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00002345 if (!SemaRef.VTableUses.empty()) {
2346 VTableUses.push_back(SemaRef.VTableUses.size());
2347 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2348 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2349 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2350 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2351 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002352 }
2353
2354 // Build a record containing all of dynamic classes declarations.
2355 RecordData DynamicClasses;
2356 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2357 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2358
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002359 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002360 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002361 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00002362 I = SemaRef.PendingInstantiations.begin(),
2363 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2364 AddDeclRef(I->first, PendingInstantiations);
2365 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002366 }
2367 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2368 "There are local ones at end of translation unit!");
2369
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002370 // Build a record containing some declaration references.
2371 RecordData SemaDeclRefs;
2372 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2373 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2374 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2375 }
2376
Sebastian Redl3397c552010-08-18 23:56:27 +00002377 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002378 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002379 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl30c514c2010-07-14 23:45:08 +00002380 WriteMetadata(Context, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002381 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00002382 if (StatCalls && !isysroot)
Douglas Gregordd41ed52010-07-12 23:48:14 +00002383 WriteStatCache(*StatCalls);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002384 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002385 // Write the record of special types.
2386 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00002387
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002388 AddTypeRef(Context.getBuiltinVaListType(), Record);
2389 AddTypeRef(Context.getObjCIdType(), Record);
2390 AddTypeRef(Context.getObjCSelType(), Record);
2391 AddTypeRef(Context.getObjCProtoType(), Record);
2392 AddTypeRef(Context.getObjCClassType(), Record);
2393 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2394 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2395 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002396 AddTypeRef(Context.getjmp_bufType(), Record);
2397 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002398 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2399 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpadaaad32009-10-20 02:12:22 +00002400 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002401 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002402 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2403 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002404 Record.push_back(Context.isInt128Installed());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002405 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002406
Douglas Gregor366809a2009-04-26 03:49:13 +00002407 // Keep writing types and declarations until all types and
2408 // declarations have been written.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002409 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002410 WriteDeclsBlockAbbrevs();
2411 while (!DeclTypesToEmit.empty()) {
2412 DeclOrType DOT = DeclTypesToEmit.front();
2413 DeclTypesToEmit.pop();
2414 if (DOT.isType())
2415 WriteType(DOT.getType());
2416 else
2417 WriteDecl(Context, DOT.getDecl());
2418 }
2419 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002420
Douglas Gregor813a97b2009-10-17 17:25:45 +00002421 WritePreprocessor(PP);
Sebastian Redl059612d2010-08-03 21:58:15 +00002422 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002423 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002424 WriteIdentifierTable(PP);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002425
Sebastian Redl1476ed42010-07-16 16:36:56 +00002426 WriteTypeDeclOffsets();
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002427 WriteUserDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00002428
Douglas Gregor7c789c12010-10-29 22:39:52 +00002429 // Write the C++ base-specifier set offsets.
2430 if (!CXXBaseSpecifiersOffsets.empty()) {
2431 // Create a blob abbreviation for the C++ base specifiers offsets.
2432 using namespace llvm;
2433
2434 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2435 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2436 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2437 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2438 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2439
2440 // Write the selector offsets table.
2441 Record.clear();
2442 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2443 Record.push_back(CXXBaseSpecifiersOffsets.size());
2444 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
2445 (const char *)CXXBaseSpecifiersOffsets.data(),
2446 CXXBaseSpecifiersOffsets.size() * sizeof(uint32_t));
2447 }
2448
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002449 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002450 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002451 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002452
2453 // Write the record containing tentative definitions.
2454 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002455 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002456
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002457 // Write the record containing unused file scoped decls.
2458 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002459 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002460
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002461 // Write the record containing weak undeclared identifiers.
2462 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002463 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002464 WeakUndeclaredIdentifiers);
2465
Douglas Gregor14c22f22009-04-22 22:18:58 +00002466 // Write the record containing locally-scoped external definitions.
2467 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002468 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002469 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002470
2471 // Write the record containing ext_vector type names.
2472 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002473 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002474
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002475 // Write the record containing VTable uses information.
2476 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002477 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002478
2479 // Write the record containing dynamic classes declarations.
2480 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002481 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002482
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002483 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002484 if (!PendingInstantiations.empty())
2485 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002486
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002487 // Write the record containing declaration references of Sema.
2488 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002489 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002490
Douglas Gregor3e1af842009-04-17 22:13:46 +00002491 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002492 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002493 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002494 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002495 Record.push_back(NumLexicalDeclContexts);
2496 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002497 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002498 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002499}
2500
Sebastian Redla4232eb2010-08-18 23:56:21 +00002501void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002502 const char *isysroot) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002503 using namespace llvm;
2504
2505 ASTContext &Context = SemaRef.Context;
2506 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1476ed42010-07-16 16:36:56 +00002507
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002508 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002509 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl30c514c2010-07-14 23:45:08 +00002510 WriteMetadata(Context, isysroot);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002511 if (StatCalls && !isysroot)
2512 WriteStatCache(*StatCalls);
2513 // FIXME: Source manager block should only write new stuff, which could be
2514 // done by tracking the largest ID in the chain
2515 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002516
2517 // The special types are in the chained PCH.
2518
2519 // We don't start with the translation unit, but with its decls that
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002520 // don't come from the chained PCH.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002521 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002522 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl681d7232010-07-27 00:17:23 +00002523 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2524 E = TU->noload_decls_end();
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002525 I != E; ++I) {
Sebastian Redld692af72010-07-27 18:24:41 +00002526 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002527 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redl0b17c612010-08-13 00:28:03 +00002528 else if ((*I)->isChangedSinceDeserialization())
2529 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002530 }
Sebastian Redl681d7232010-07-27 00:17:23 +00002531 // We also need to write a lexical updates block for the TU.
Sebastian Redld692af72010-07-27 18:24:41 +00002532 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002533 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redld692af72010-07-27 18:24:41 +00002534 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2535 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2536 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002537 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redld692af72010-07-27 18:24:41 +00002538 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2539 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002540 NewGlobalDecls.size() * sizeof(KindDeclIDPair));
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00002541 // And a visible updates block for the DeclContexts.
2542 Abv = new llvm::BitCodeAbbrev();
2543 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2544 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2545 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2546 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2547 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2548 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002549
Sebastian Redl083abdf2010-07-27 23:01:28 +00002550 // Build a record containing all of the new tentative definitions in this
2551 // file, in TentativeDefinitions order.
2552 RecordData TentativeDefinitions;
2553 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2554 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2555 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2556 }
2557
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002558 // Build a record containing all of the file scoped decls in this file.
2559 RecordData UnusedFileScopedDecls;
2560 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2561 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2562 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002563 }
2564
Sebastian Redl40566802010-08-05 18:21:25 +00002565 // We write the entire table, overwriting the tables from the chain.
2566 RecordData WeakUndeclaredIdentifiers;
2567 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2568 WeakUndeclaredIdentifiers.push_back(
2569 SemaRef.WeakUndeclaredIdentifiers.size());
2570 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2571 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2572 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2573 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2574 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2575 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2576 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2577 }
2578 }
2579
Sebastian Redl083abdf2010-07-27 23:01:28 +00002580 // Build a record containing all of the locally-scoped external
2581 // declarations in this header file. Generally, this record will be
2582 // empty.
2583 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002584 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl083abdf2010-07-27 23:01:28 +00002585 // nondeterminstic!
2586 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2587 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2588 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2589 TD != TDEnd; ++TD) {
2590 if (TD->second->getPCHLevel() == 0)
2591 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2592 }
2593
2594 // Build a record containing all of the ext_vector declarations.
2595 RecordData ExtVectorDecls;
2596 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2597 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2598 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2599 }
2600
Sebastian Redl40566802010-08-05 18:21:25 +00002601 // Build a record containing all of the VTable uses information.
2602 // We write everything here, because it's too hard to determine whether
2603 // a use is new to this part.
2604 RecordData VTableUses;
2605 if (!SemaRef.VTableUses.empty()) {
2606 VTableUses.push_back(SemaRef.VTableUses.size());
2607 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2608 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2609 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2610 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2611 }
2612 }
2613
2614 // Build a record containing all of dynamic classes declarations.
2615 RecordData DynamicClasses;
2616 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2617 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
2618 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2619
2620 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002621 RecordData PendingInstantiations;
Sebastian Redl40566802010-08-05 18:21:25 +00002622 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00002623 I = SemaRef.PendingInstantiations.begin(),
2624 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl40566802010-08-05 18:21:25 +00002625 if (I->first->getPCHLevel() == 0) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002626 AddDeclRef(I->first, PendingInstantiations);
2627 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl40566802010-08-05 18:21:25 +00002628 }
2629 }
2630 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2631 "There are local ones at end of translation unit!");
2632
2633 // Build a record containing some declaration references.
2634 // It's not worth the effort to avoid duplication here.
2635 RecordData SemaDeclRefs;
2636 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2637 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2638 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2639 }
2640
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002641 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002642 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00002643 for (DeclsToRewriteTy::iterator
2644 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
2645 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002646 while (!DeclTypesToEmit.empty()) {
2647 DeclOrType DOT = DeclTypesToEmit.front();
2648 DeclTypesToEmit.pop();
2649 if (DOT.isType())
2650 WriteType(DOT.getType());
2651 else
2652 WriteDecl(Context, DOT.getDecl());
2653 }
2654 Stream.ExitBlock();
2655
Sebastian Redl083abdf2010-07-27 23:01:28 +00002656 WritePreprocessor(PP);
Sebastian Redla68340f2010-08-04 22:21:29 +00002657 WriteSelectors(SemaRef);
2658 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002659 WriteIdentifierTable(PP);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002660 WriteTypeDeclOffsets();
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002661 // FIXME: For chained PCH only write the new mappings (we currently
2662 // write all of them again).
2663 WriteUserDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl083abdf2010-07-27 23:01:28 +00002664
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002665 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl3397c552010-08-18 23:56:27 +00002666 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002667 RecordData FirstLatestDeclIDs;
2668 for (FirstLatestDeclMap::iterator
2669 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
2670 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
2671 "Expected first & second to be in different PCHs");
2672 AddDeclRef(I->first, FirstLatestDeclIDs);
2673 AddDeclRef(I->second, FirstLatestDeclIDs);
2674 }
2675 if (!FirstLatestDeclIDs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002676 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002677
Sebastian Redl083abdf2010-07-27 23:01:28 +00002678 // Write the record containing external, unnamed definitions.
2679 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002680 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002681
2682 // Write the record containing tentative definitions.
2683 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002684 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002685
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002686 // Write the record containing unused file scoped decls.
2687 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002688 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002689
Sebastian Redl40566802010-08-05 18:21:25 +00002690 // Write the record containing weak undeclared identifiers.
2691 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002692 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl40566802010-08-05 18:21:25 +00002693 WeakUndeclaredIdentifiers);
2694
Sebastian Redl083abdf2010-07-27 23:01:28 +00002695 // Write the record containing locally-scoped external definitions.
2696 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002697 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl083abdf2010-07-27 23:01:28 +00002698 LocallyScopedExternalDecls);
2699
2700 // Write the record containing ext_vector type names.
2701 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002702 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002703
Sebastian Redl40566802010-08-05 18:21:25 +00002704 // Write the record containing VTable uses information.
2705 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002706 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl40566802010-08-05 18:21:25 +00002707
2708 // Write the record containing dynamic classes declarations.
2709 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002710 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl40566802010-08-05 18:21:25 +00002711
2712 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002713 if (!PendingInstantiations.empty())
2714 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl40566802010-08-05 18:21:25 +00002715
2716 // Write the record containing declaration references of Sema.
2717 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002718 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002719
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00002720 // Write the updates to DeclContexts.
2721 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
2722 I = UpdatedDeclContexts.begin(),
2723 E = UpdatedDeclContexts.end();
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002724 I != E; ++I)
2725 WriteDeclContextVisibleUpdate(*I);
2726
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00002727 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002728
Sebastian Redl083abdf2010-07-27 23:01:28 +00002729 Record.clear();
2730 Record.push_back(NumStatements);
2731 Record.push_back(NumMacros);
2732 Record.push_back(NumLexicalDeclContexts);
2733 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00002734 WriteDeclReplacementsBlock();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002735 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002736 Stream.ExitBlock();
2737}
2738
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00002739void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002740 if (DeclUpdates.empty())
2741 return;
2742
2743 RecordData OffsetsRecord;
2744 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
2745 for (DeclUpdateMap::iterator
2746 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
2747 const Decl *D = I->first;
2748 UpdateRecord &URec = I->second;
2749
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00002750 if (DeclsToRewrite.count(D))
2751 continue; // The decl will be written completely,no need to store updates.
2752
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002753 uint64_t Offset = Stream.GetCurrentBitNo();
2754 Stream.EmitRecord(DECL_UPDATES, URec);
2755
2756 OffsetsRecord.push_back(GetDeclRef(D));
2757 OffsetsRecord.push_back(Offset);
2758 }
2759 Stream.ExitBlock();
2760 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
2761}
2762
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00002763void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00002764 if (ReplacedDecls.empty())
2765 return;
2766
2767 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002768 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00002769 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
2770 Record.push_back(I->first);
2771 Record.push_back(I->second);
2772 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002773 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00002774}
2775
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002776void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002777 Record.push_back(Loc.getRawEncoding());
2778}
2779
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002780void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002781 AddSourceLocation(Range.getBegin(), Record);
2782 AddSourceLocation(Range.getEnd(), Record);
2783}
2784
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002785void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002786 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00002787 const uint64_t *Words = Value.getRawData();
2788 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002789}
2790
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002791void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002792 Record.push_back(Value.isUnsigned());
2793 AddAPInt(Value, Record);
2794}
2795
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002796void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00002797 AddAPInt(Value.bitcastToAPInt(), Record);
2798}
2799
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002800void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002801 Record.push_back(getIdentifierRef(II));
2802}
2803
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002804IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002805 if (II == 0)
2806 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002807
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002808 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00002809 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002810 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00002811 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002812}
2813
Sebastian Redlf73c93f2010-09-15 19:54:06 +00002814MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002815 if (MD == 0)
2816 return 0;
Sebastian Redlf73c93f2010-09-15 19:54:06 +00002817
2818 MacroID &ID = MacroDefinitions[MD];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002819 if (ID == 0)
Douglas Gregor77424bc2010-10-02 19:29:26 +00002820 ID = NextMacroID++;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002821 return ID;
2822}
2823
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002824void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00002825 Record.push_back(getSelectorRef(SelRef));
2826}
2827
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002828SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00002829 if (Sel.getAsOpaquePtr() == 0) {
2830 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002831 }
2832
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002833 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00002834 if (SID == 0 && Chain) {
2835 // This might trigger a ReadSelector callback, which will set the ID for
2836 // this selector.
2837 Chain->LoadSelector(Sel);
2838 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002839 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00002840 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002841 }
Sebastian Redl5d050072010-08-04 17:20:04 +00002842 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002843}
2844
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002845void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00002846 AddDeclRef(Temp->getDestructor(), Record);
2847}
2848
Douglas Gregor7c789c12010-10-29 22:39:52 +00002849void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
2850 CXXBaseSpecifier const *BasesEnd,
2851 RecordDataImpl &Record) {
2852 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
2853 CXXBaseSpecifiersToWrite.push_back(
2854 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
2855 Bases, BasesEnd));
2856 Record.push_back(NextCXXBaseSpecifiersID++);
2857}
2858
Sebastian Redla4232eb2010-08-18 23:56:21 +00002859void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002860 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002861 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002862 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00002863 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002864 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00002865 break;
2866 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002867 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00002868 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00002869 case TemplateArgument::Template:
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002870 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2871 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00002872 break;
John McCall833ca992009-10-29 08:12:44 +00002873 case TemplateArgument::Null:
2874 case TemplateArgument::Integral:
2875 case TemplateArgument::Declaration:
2876 case TemplateArgument::Pack:
2877 break;
2878 }
2879}
2880
Sebastian Redla4232eb2010-08-18 23:56:21 +00002881void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002882 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002883 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002884
2885 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
2886 bool InfoHasSameExpr
2887 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
2888 Record.push_back(InfoHasSameExpr);
2889 if (InfoHasSameExpr)
2890 return; // Avoid storing the same expr twice.
2891 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002892 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
2893 Record);
2894}
2895
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002896void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00002897 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00002898 AddTypeRef(QualType(), Record);
2899 return;
2900 }
2901
John McCalla93c9342009-12-07 02:54:59 +00002902 AddTypeRef(TInfo->getType(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +00002903 TypeLocWriter TLW(*this, Record);
John McCalla93c9342009-12-07 02:54:59 +00002904 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002905 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00002906}
2907
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002908void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00002909 Record.push_back(GetOrCreateTypeID(T));
2910}
2911
2912TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00002913 return MakeTypeID(T,
2914 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
2915}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002916
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002917TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00002918 return MakeTypeID(T,
2919 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00002920}
2921
2922TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
2923 if (T.isNull())
2924 return TypeIdx();
2925 assert(!T.getLocalFastQualifiers());
2926
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002927 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002928 if (Idx.getIndex() == 0) {
Douglas Gregor366809a2009-04-26 03:49:13 +00002929 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00002930 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002931 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002932 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00002933 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00002934 return Idx;
2935}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002936
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002937TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00002938 if (T.isNull())
2939 return TypeIdx();
2940 assert(!T.getLocalFastQualifiers());
2941
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002942 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
2943 assert(I != TypeIdxs.end() && "Type not emitted!");
2944 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002945}
2946
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002947void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00002948 Record.push_back(GetDeclRef(D));
2949}
2950
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002951DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002952 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00002953 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002954 }
Douglas Gregor97475832010-10-05 18:37:06 +00002955 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002956 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00002957 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002958 // We haven't seen this declaration before. Give it a new ID and
2959 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002960 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002961 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redl0b17c612010-08-13 00:28:03 +00002962 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
2963 // We don't add it to the replacement collection here, because we don't
2964 // have the offset yet.
2965 DeclTypesToEmit.push(const_cast<Decl *>(D));
2966 // Reset the flag, so that we don't add this decl multiple times.
2967 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002968 }
2969
Sebastian Redl681d7232010-07-27 00:17:23 +00002970 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002971}
2972
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002973DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002974 if (D == 0)
2975 return 0;
2976
2977 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2978 return DeclIDs[D];
2979}
2980
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002981void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00002982 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002983 Record.push_back(Name.getNameKind());
2984 switch (Name.getNameKind()) {
2985 case DeclarationName::Identifier:
2986 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2987 break;
2988
2989 case DeclarationName::ObjCZeroArgSelector:
2990 case DeclarationName::ObjCOneArgSelector:
2991 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002992 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002993 break;
2994
2995 case DeclarationName::CXXConstructorName:
2996 case DeclarationName::CXXDestructorName:
2997 case DeclarationName::CXXConversionFunctionName:
2998 AddTypeRef(Name.getCXXNameType(), Record);
2999 break;
3000
3001 case DeclarationName::CXXOperatorName:
3002 Record.push_back(Name.getCXXOverloadedOperator());
3003 break;
3004
Sean Hunt3e518bd2009-11-29 07:34:05 +00003005 case DeclarationName::CXXLiteralOperatorName:
3006 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3007 break;
3008
Douglas Gregor2cf26342009-04-09 22:27:44 +00003009 case DeclarationName::CXXUsingDirective:
3010 // No extra data to emit
3011 break;
3012 }
3013}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003014
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003015void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003016 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003017 switch (Name.getNameKind()) {
3018 case DeclarationName::CXXConstructorName:
3019 case DeclarationName::CXXDestructorName:
3020 case DeclarationName::CXXConversionFunctionName:
3021 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3022 break;
3023
3024 case DeclarationName::CXXOperatorName:
3025 AddSourceLocation(
3026 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3027 Record);
3028 AddSourceLocation(
3029 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3030 Record);
3031 break;
3032
3033 case DeclarationName::CXXLiteralOperatorName:
3034 AddSourceLocation(
3035 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3036 Record);
3037 break;
3038
3039 case DeclarationName::Identifier:
3040 case DeclarationName::ObjCZeroArgSelector:
3041 case DeclarationName::ObjCOneArgSelector:
3042 case DeclarationName::ObjCMultiArgSelector:
3043 case DeclarationName::CXXUsingDirective:
3044 break;
3045 }
3046}
3047
3048void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003049 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003050 AddDeclarationName(NameInfo.getName(), Record);
3051 AddSourceLocation(NameInfo.getLoc(), Record);
3052 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3053}
3054
3055void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003056 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003057 AddNestedNameSpecifier(Info.NNS, Record);
3058 AddSourceRange(Info.NNSRange, Record);
3059 Record.push_back(Info.NumTemplParamLists);
3060 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3061 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3062}
3063
Sebastian Redla4232eb2010-08-18 23:56:21 +00003064void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003065 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003066 // Nested name specifiers usually aren't too long. I think that 8 would
3067 // typically accomodate the vast majority.
3068 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3069
3070 // Push each of the NNS's onto a stack for serialization in reverse order.
3071 while (NNS) {
3072 NestedNames.push_back(NNS);
3073 NNS = NNS->getPrefix();
3074 }
3075
3076 Record.push_back(NestedNames.size());
3077 while(!NestedNames.empty()) {
3078 NNS = NestedNames.pop_back_val();
3079 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3080 Record.push_back(Kind);
3081 switch (Kind) {
3082 case NestedNameSpecifier::Identifier:
3083 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3084 break;
3085
3086 case NestedNameSpecifier::Namespace:
3087 AddDeclRef(NNS->getAsNamespace(), Record);
3088 break;
3089
3090 case NestedNameSpecifier::TypeSpec:
3091 case NestedNameSpecifier::TypeSpecWithTemplate:
3092 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3093 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3094 break;
3095
3096 case NestedNameSpecifier::Global:
3097 // Don't need to write an associated value.
3098 break;
3099 }
3100 }
3101}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003102
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003103void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00003104 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003105 Record.push_back(Kind);
3106 switch (Kind) {
3107 case TemplateName::Template:
3108 AddDeclRef(Name.getAsTemplateDecl(), Record);
3109 break;
3110
3111 case TemplateName::OverloadedTemplate: {
3112 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3113 Record.push_back(OvT->size());
3114 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3115 I != E; ++I)
3116 AddDeclRef(*I, Record);
3117 break;
3118 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003119
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003120 case TemplateName::QualifiedTemplate: {
3121 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3122 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3123 Record.push_back(QualT->hasTemplateKeyword());
3124 AddDeclRef(QualT->getTemplateDecl(), Record);
3125 break;
3126 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003127
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003128 case TemplateName::DependentTemplate: {
3129 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3130 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3131 Record.push_back(DepT->isIdentifier());
3132 if (DepT->isIdentifier())
3133 AddIdentifierRef(DepT->getIdentifier(), Record);
3134 else
3135 Record.push_back(DepT->getOperator());
3136 break;
3137 }
3138 }
3139}
3140
Michael J. Spencer20249a12010-10-21 03:16:25 +00003141void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003142 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003143 Record.push_back(Arg.getKind());
3144 switch (Arg.getKind()) {
3145 case TemplateArgument::Null:
3146 break;
3147 case TemplateArgument::Type:
3148 AddTypeRef(Arg.getAsType(), Record);
3149 break;
3150 case TemplateArgument::Declaration:
3151 AddDeclRef(Arg.getAsDecl(), Record);
3152 break;
3153 case TemplateArgument::Integral:
3154 AddAPSInt(*Arg.getAsIntegral(), Record);
3155 AddTypeRef(Arg.getIntegralType(), Record);
3156 break;
3157 case TemplateArgument::Template:
3158 AddTemplateName(Arg.getAsTemplate(), Record);
3159 break;
3160 case TemplateArgument::Expression:
3161 AddStmt(Arg.getAsExpr());
3162 break;
3163 case TemplateArgument::Pack:
3164 Record.push_back(Arg.pack_size());
3165 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3166 I != E; ++I)
3167 AddTemplateArgument(*I, Record);
3168 break;
3169 }
3170}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003171
3172void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003173ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003174 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003175 assert(TemplateParams && "No TemplateParams!");
3176 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3177 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3178 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3179 Record.push_back(TemplateParams->size());
3180 for (TemplateParameterList::const_iterator
3181 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3182 P != PEnd; ++P)
3183 AddDeclRef(*P, Record);
3184}
3185
3186/// \brief Emit a template argument list.
3187void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003188ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003189 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003190 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00003191 Record.push_back(TemplateArgs->size());
3192 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003193 AddTemplateArgument(TemplateArgs->get(i), Record);
3194}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003195
3196
3197void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003198ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003199 Record.push_back(Set.size());
3200 for (UnresolvedSetImpl::const_iterator
3201 I = Set.begin(), E = Set.end(); I != E; ++I) {
3202 AddDeclRef(I.getDecl(), Record);
3203 Record.push_back(I.getAccess());
3204 }
3205}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003206
Sebastian Redla4232eb2010-08-18 23:56:21 +00003207void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003208 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003209 Record.push_back(Base.isVirtual());
3210 Record.push_back(Base.isBaseOfClass());
3211 Record.push_back(Base.getAccessSpecifierAsWritten());
Nick Lewycky56062202010-07-26 16:56:01 +00003212 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003213 AddSourceRange(Base.getSourceRange(), Record);
3214}
Sebastian Redl30c514c2010-07-14 23:45:08 +00003215
Douglas Gregor7c789c12010-10-29 22:39:52 +00003216void ASTWriter::FlushCXXBaseSpecifiers() {
3217 RecordData Record;
3218 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3219 Record.clear();
3220
3221 // Record the offset of this base-specifier set.
3222 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3223 if (Index == CXXBaseSpecifiersOffsets.size())
3224 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3225 else {
3226 if (Index > CXXBaseSpecifiersOffsets.size())
3227 CXXBaseSpecifiersOffsets.resize(Index + 1);
3228 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3229 }
3230
3231 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3232 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3233 Record.push_back(BEnd - B);
3234 for (; B != BEnd; ++B)
3235 AddCXXBaseSpecifier(*B, Record);
3236 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00003237
3238 // Flush any expressions that were written as part of the base specifiers.
3239 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003240 }
3241
3242 CXXBaseSpecifiersToWrite.clear();
3243}
3244
Sebastian Redla4232eb2010-08-18 23:56:21 +00003245void ASTWriter::AddCXXBaseOrMemberInitializers(
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003246 const CXXBaseOrMemberInitializer * const *BaseOrMembers,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003247 unsigned NumBaseOrMembers, RecordDataImpl &Record) {
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003248 Record.push_back(NumBaseOrMembers);
3249 for (unsigned i=0; i != NumBaseOrMembers; ++i) {
3250 const CXXBaseOrMemberInitializer *Init = BaseOrMembers[i];
3251
3252 Record.push_back(Init->isBaseInitializer());
3253 if (Init->isBaseInitializer()) {
3254 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3255 Record.push_back(Init->isBaseVirtual());
3256 } else {
Francois Pichet00eb3f92010-12-04 09:14:42 +00003257 Record.push_back(Init->isIndirectMemberInitializer());
3258 if (Init->isIndirectMemberInitializer())
3259 AddDeclRef(Init->getIndirectMember(), Record);
3260 else
3261 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003262 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00003263
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003264 AddSourceLocation(Init->getMemberLocation(), Record);
3265 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003266 AddSourceLocation(Init->getLParenLoc(), Record);
3267 AddSourceLocation(Init->getRParenLoc(), Record);
3268 Record.push_back(Init->isWritten());
3269 if (Init->isWritten()) {
3270 Record.push_back(Init->getSourceOrder());
3271 } else {
3272 Record.push_back(Init->getNumArrayIndices());
3273 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3274 AddDeclRef(Init->getArrayIndex(i), Record);
3275 }
3276 }
3277}
3278
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003279void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3280 assert(D->DefinitionData);
3281 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3282 Record.push_back(Data.UserDeclaredConstructor);
3283 Record.push_back(Data.UserDeclaredCopyConstructor);
3284 Record.push_back(Data.UserDeclaredCopyAssignment);
3285 Record.push_back(Data.UserDeclaredDestructor);
3286 Record.push_back(Data.Aggregate);
3287 Record.push_back(Data.PlainOldData);
3288 Record.push_back(Data.Empty);
3289 Record.push_back(Data.Polymorphic);
3290 Record.push_back(Data.Abstract);
3291 Record.push_back(Data.HasTrivialConstructor);
3292 Record.push_back(Data.HasTrivialCopyConstructor);
3293 Record.push_back(Data.HasTrivialCopyAssignment);
3294 Record.push_back(Data.HasTrivialDestructor);
3295 Record.push_back(Data.ComputedVisibleConversions);
3296 Record.push_back(Data.DeclaredDefaultConstructor);
3297 Record.push_back(Data.DeclaredCopyConstructor);
3298 Record.push_back(Data.DeclaredCopyAssignment);
3299 Record.push_back(Data.DeclaredDestructor);
3300
3301 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003302 if (Data.NumBases > 0)
3303 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3304 Record);
3305
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003306 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3307 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003308 if (Data.NumVBases > 0)
3309 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3310 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003311
3312 AddUnresolvedSet(Data.Conversions, Record);
3313 AddUnresolvedSet(Data.VisibleConversions, Record);
3314 // Data.Definition is the owning decl, no need to write it.
3315 AddDeclRef(Data.FirstFriend, Record);
3316}
3317
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003318void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003319 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003320 assert(!Chain && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003321 assert(FirstDeclID == NextDeclID &&
3322 FirstTypeID == NextTypeID &&
3323 FirstIdentID == NextIdentID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00003324 FirstSelectorID == NextSelectorID &&
Douglas Gregor77424bc2010-10-02 19:29:26 +00003325 FirstMacroID == NextMacroID &&
Douglas Gregor7c789c12010-10-29 22:39:52 +00003326 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003327 "Setting chain after writing has started.");
3328 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003329
3330 FirstDeclID += Chain->getTotalNumDecls();
3331 FirstTypeID += Chain->getTotalNumTypes();
3332 FirstIdentID += Chain->getTotalNumIdentifiers();
3333 FirstSelectorID += Chain->getTotalNumSelectors();
3334 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003335 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003336 NextDeclID = FirstDeclID;
3337 NextTypeID = FirstTypeID;
3338 NextIdentID = FirstIdentID;
3339 NextSelectorID = FirstSelectorID;
3340 NextMacroID = FirstMacroID;
Douglas Gregor7c789c12010-10-29 22:39:52 +00003341 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003342}
3343
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003344void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003345 IdentifierIDs[II] = ID;
3346}
3347
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003348void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00003349 // Always take the highest-numbered type index. This copes with an interesting
3350 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00003351 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00003352 // keep the higher-numbered entry so that we can properly write it out to
3353 // the AST file.
3354 TypeIdx &StoredIdx = TypeIdxs[T];
3355 if (Idx.getIndex() >= StoredIdx.getIndex())
3356 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003357}
3358
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003359void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1476ed42010-07-16 16:36:56 +00003360 DeclIDs[D] = ID;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003361}
Sebastian Redl5d050072010-08-04 17:20:04 +00003362
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003363void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003364 SelectorIDs[S] = ID;
3365}
Douglas Gregor77424bc2010-10-02 19:29:26 +00003366
Michael J. Spencer20249a12010-10-21 03:16:25 +00003367void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00003368 MacroDefinition *MD) {
3369 MacroDefinitions[MD] = ID;
3370}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003371
3372void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3373 assert(D->isDefinition());
3374 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3375 // We are interested when a PCH decl is modified.
3376 if (RD->getPCHLevel() > 0) {
3377 // A forward reference was mutated into a definition. Rewrite it.
3378 // FIXME: This happens during template instantiation, should we
3379 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00003380 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003381 }
3382
3383 for (CXXRecordDecl::redecl_iterator
3384 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3385 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3386 if (Redecl == RD)
3387 continue;
3388
3389 // We are interested when a PCH decl is modified.
3390 if (Redecl->getPCHLevel() > 0) {
3391 UpdateRecord &Record = DeclUpdates[Redecl];
3392 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3393 assert(Redecl->DefinitionData);
3394 assert(Redecl->DefinitionData->Definition == D);
3395 AddDeclRef(D, Record); // the DefinitionDecl
3396 }
3397 }
3398 }
3399}
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00003400void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3401 // TU and namespaces are handled elsewhere.
3402 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3403 return;
3404
3405 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3406 return; // Not a source decl added to a DeclContext from PCH.
3407
3408 AddUpdatedDeclContext(DC);
3409}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00003410
3411void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3412 assert(D->isImplicit());
3413 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3414 return; // Not a source member added to a class from PCH.
3415 if (!isa<CXXMethodDecl>(D))
3416 return; // We are interested in lazily declared implicit methods.
3417
3418 // A decl coming from PCH was modified.
3419 assert(RD->isDefinition());
3420 UpdateRecord &Record = DeclUpdates[RD];
3421 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3422 AddDeclRef(D, Record);
3423}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00003424
3425void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3426 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00003427 // The specializations set is kept in the canonical template.
3428 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00003429 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3430 return; // Not a source specialization added to a template from PCH.
3431
3432 UpdateRecord &Record = DeclUpdates[TD];
3433 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3434 AddDeclRef(D, Record);
3435}
Douglas Gregor89d99802010-11-30 06:16:57 +00003436
3437ASTSerializationListener::~ASTSerializationListener() { }