blob: 7b405e9c8b31adf4f1ccc09e60551e72f5bcd0f6 [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
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000301void ASTTypeWriter::VisitParenType(const ParenType *T) {
302 Writer.AddTypeRef(T->getInnerType(), Record);
303 Code = TYPE_PAREN;
304}
305
Sebastian Redl3397c552010-08-18 23:56:27 +0000306void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000307 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000308 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
309 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000310 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000311}
312
Sebastian Redl3397c552010-08-18 23:56:27 +0000313void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCall3cb0ebd2010-03-10 03:28:59 +0000314 Writer.AddDeclRef(T->getDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000315 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000316 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000317}
318
Sebastian Redl3397c552010-08-18 23:56:27 +0000319void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000320 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000321 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000322}
323
Sebastian Redl3397c552010-08-18 23:56:27 +0000324void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000325 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000326 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000327 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000328 E = T->qual_end(); I != E; ++I)
329 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000330 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000331}
332
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000333void
Sebastian Redl3397c552010-08-18 23:56:27 +0000334ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000335 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000336 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000337}
338
John McCalla1ee0c52009-10-16 21:56:05 +0000339namespace {
340
341class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000342 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000343 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000344
345public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000346 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000347 : Writer(Writer), Record(Record) { }
348
John McCall51bd8032009-10-18 01:05:36 +0000349#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000350#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000351 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000352#include "clang/AST/TypeLocNodes.def"
353
John McCall51bd8032009-10-18 01:05:36 +0000354 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
355 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000356};
357
358}
359
John McCall51bd8032009-10-18 01:05:36 +0000360void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
361 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000362}
John McCall51bd8032009-10-18 01:05:36 +0000363void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000364 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
365 if (TL.needsExtraLocalData()) {
366 Record.push_back(TL.getWrittenTypeSpec());
367 Record.push_back(TL.getWrittenSignSpec());
368 Record.push_back(TL.getWrittenWidthSpec());
369 Record.push_back(TL.hasModeAttr());
370 }
John McCalla1ee0c52009-10-16 21:56:05 +0000371}
John McCall51bd8032009-10-18 01:05:36 +0000372void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
373 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000374}
John McCall51bd8032009-10-18 01:05:36 +0000375void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
376 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000377}
John McCall51bd8032009-10-18 01:05:36 +0000378void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
379 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000380}
John McCall51bd8032009-10-18 01:05:36 +0000381void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
382 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000383}
John McCall51bd8032009-10-18 01:05:36 +0000384void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
385 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000386}
John McCall51bd8032009-10-18 01:05:36 +0000387void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
388 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000389}
John McCall51bd8032009-10-18 01:05:36 +0000390void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
391 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
392 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
393 Record.push_back(TL.getSizeExpr() ? 1 : 0);
394 if (TL.getSizeExpr())
395 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000396}
John McCall51bd8032009-10-18 01:05:36 +0000397void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
398 VisitArrayTypeLoc(TL);
399}
400void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
401 VisitArrayTypeLoc(TL);
402}
403void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
404 VisitArrayTypeLoc(TL);
405}
406void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
407 DependentSizedArrayTypeLoc TL) {
408 VisitArrayTypeLoc(TL);
409}
410void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
411 DependentSizedExtVectorTypeLoc TL) {
412 Writer.AddSourceLocation(TL.getNameLoc(), Record);
413}
414void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
415 Writer.AddSourceLocation(TL.getNameLoc(), Record);
416}
417void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
418 Writer.AddSourceLocation(TL.getNameLoc(), Record);
419}
420void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
421 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
422 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Douglas Gregordab60ad2010-10-01 18:44:50 +0000423 Record.push_back(TL.getTrailingReturn());
John McCall51bd8032009-10-18 01:05:36 +0000424 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
425 Writer.AddDeclRef(TL.getArg(i), Record);
426}
427void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
428 VisitFunctionTypeLoc(TL);
429}
430void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
431 VisitFunctionTypeLoc(TL);
432}
John McCalled976492009-12-04 22:46:56 +0000433void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
434 Writer.AddSourceLocation(TL.getNameLoc(), Record);
435}
John McCall51bd8032009-10-18 01:05:36 +0000436void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
437 Writer.AddSourceLocation(TL.getNameLoc(), Record);
438}
439void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc 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);
John McCall51bd8032009-10-18 01:05:36 +0000443}
444void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000445 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
446 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
447 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
448 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000449}
450void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
451 Writer.AddSourceLocation(TL.getNameLoc(), Record);
452}
453void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
454 Writer.AddSourceLocation(TL.getNameLoc(), Record);
455}
456void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
457 Writer.AddSourceLocation(TL.getNameLoc(), Record);
458}
John McCall51bd8032009-10-18 01:05:36 +0000459void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
460 Writer.AddSourceLocation(TL.getNameLoc(), Record);
461}
John McCall49a832b2009-10-18 09:09:24 +0000462void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
463 SubstTemplateTypeParmTypeLoc TL) {
464 Writer.AddSourceLocation(TL.getNameLoc(), Record);
465}
John McCall51bd8032009-10-18 01:05:36 +0000466void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
467 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000468 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
469 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
470 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
471 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000472 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
473 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000474}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000475void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
476 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
477 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
478}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000479void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000480 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
481 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000482}
John McCall3cb0ebd2010-03-10 03:28:59 +0000483void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
484 Writer.AddSourceLocation(TL.getNameLoc(), Record);
485}
Douglas Gregor4714c122010-03-31 17:34:00 +0000486void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000487 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
488 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000489 Writer.AddSourceLocation(TL.getNameLoc(), Record);
490}
John McCall33500952010-06-11 00:33:02 +0000491void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
492 DependentTemplateSpecializationTypeLoc TL) {
493 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
494 Writer.AddSourceRange(TL.getQualifierRange(), Record);
495 Writer.AddSourceLocation(TL.getNameLoc(), Record);
496 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
497 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
498 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000499 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
500 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000501}
John McCall51bd8032009-10-18 01:05:36 +0000502void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
503 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000504}
505void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
506 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000507 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
508 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
509 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
510 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000511}
John McCall54e14c42009-10-22 22:37:11 +0000512void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
513 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000514}
John McCalla1ee0c52009-10-16 21:56:05 +0000515
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000516//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000517// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000518//===----------------------------------------------------------------------===//
519
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000520static void EmitBlockID(unsigned ID, const char *Name,
521 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000522 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000523 Record.clear();
524 Record.push_back(ID);
525 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
526
527 // Emit the block name if present.
528 if (Name == 0 || Name[0] == 0) return;
529 Record.clear();
530 while (*Name)
531 Record.push_back(*Name++);
532 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
533}
534
535static void EmitRecordID(unsigned ID, const char *Name,
536 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000537 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000538 Record.clear();
539 Record.push_back(ID);
540 while (*Name)
541 Record.push_back(*Name++);
542 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000543}
544
545static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000546 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000547#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000548 RECORD(STMT_STOP);
549 RECORD(STMT_NULL_PTR);
550 RECORD(STMT_NULL);
551 RECORD(STMT_COMPOUND);
552 RECORD(STMT_CASE);
553 RECORD(STMT_DEFAULT);
554 RECORD(STMT_LABEL);
555 RECORD(STMT_IF);
556 RECORD(STMT_SWITCH);
557 RECORD(STMT_WHILE);
558 RECORD(STMT_DO);
559 RECORD(STMT_FOR);
560 RECORD(STMT_GOTO);
561 RECORD(STMT_INDIRECT_GOTO);
562 RECORD(STMT_CONTINUE);
563 RECORD(STMT_BREAK);
564 RECORD(STMT_RETURN);
565 RECORD(STMT_DECL);
566 RECORD(STMT_ASM);
567 RECORD(EXPR_PREDEFINED);
568 RECORD(EXPR_DECL_REF);
569 RECORD(EXPR_INTEGER_LITERAL);
570 RECORD(EXPR_FLOATING_LITERAL);
571 RECORD(EXPR_IMAGINARY_LITERAL);
572 RECORD(EXPR_STRING_LITERAL);
573 RECORD(EXPR_CHARACTER_LITERAL);
574 RECORD(EXPR_PAREN);
575 RECORD(EXPR_UNARY_OPERATOR);
576 RECORD(EXPR_SIZEOF_ALIGN_OF);
577 RECORD(EXPR_ARRAY_SUBSCRIPT);
578 RECORD(EXPR_CALL);
579 RECORD(EXPR_MEMBER);
580 RECORD(EXPR_BINARY_OPERATOR);
581 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
582 RECORD(EXPR_CONDITIONAL_OPERATOR);
583 RECORD(EXPR_IMPLICIT_CAST);
584 RECORD(EXPR_CSTYLE_CAST);
585 RECORD(EXPR_COMPOUND_LITERAL);
586 RECORD(EXPR_EXT_VECTOR_ELEMENT);
587 RECORD(EXPR_INIT_LIST);
588 RECORD(EXPR_DESIGNATED_INIT);
589 RECORD(EXPR_IMPLICIT_VALUE_INIT);
590 RECORD(EXPR_VA_ARG);
591 RECORD(EXPR_ADDR_LABEL);
592 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000593 RECORD(EXPR_CHOOSE);
594 RECORD(EXPR_GNU_NULL);
595 RECORD(EXPR_SHUFFLE_VECTOR);
596 RECORD(EXPR_BLOCK);
597 RECORD(EXPR_BLOCK_DECL_REF);
598 RECORD(EXPR_OBJC_STRING_LITERAL);
599 RECORD(EXPR_OBJC_ENCODE);
600 RECORD(EXPR_OBJC_SELECTOR_EXPR);
601 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
602 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
603 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
604 RECORD(EXPR_OBJC_KVC_REF_EXPR);
605 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000606 RECORD(STMT_OBJC_FOR_COLLECTION);
607 RECORD(STMT_OBJC_CATCH);
608 RECORD(STMT_OBJC_FINALLY);
609 RECORD(STMT_OBJC_AT_TRY);
610 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
611 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000612 RECORD(EXPR_CXX_OPERATOR_CALL);
613 RECORD(EXPR_CXX_CONSTRUCT);
614 RECORD(EXPR_CXX_STATIC_CAST);
615 RECORD(EXPR_CXX_DYNAMIC_CAST);
616 RECORD(EXPR_CXX_REINTERPRET_CAST);
617 RECORD(EXPR_CXX_CONST_CAST);
618 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
619 RECORD(EXPR_CXX_BOOL_LITERAL);
620 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000621#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000622}
Mike Stump1eb44332009-09-09 15:08:12 +0000623
Sebastian Redla4232eb2010-08-18 23:56:21 +0000624void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000625 RecordData Record;
626 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000627
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000628#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
629#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000630
Sebastian Redl3397c552010-08-18 23:56:27 +0000631 // AST Top-Level Block.
Sebastian Redlf29f0a22010-08-18 23:57:22 +0000632 BLOCK(AST_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000633 RECORD(ORIGINAL_FILE_NAME);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000634 RECORD(TYPE_OFFSET);
635 RECORD(DECL_OFFSET);
636 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000637 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000638 RECORD(IDENTIFIER_OFFSET);
639 RECORD(IDENTIFIER_TABLE);
640 RECORD(EXTERNAL_DEFINITIONS);
641 RECORD(SPECIAL_TYPES);
642 RECORD(STATISTICS);
643 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000644 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000645 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
646 RECORD(SELECTOR_OFFSETS);
647 RECORD(METHOD_POOL);
648 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000649 RECORD(SOURCE_LOCATION_OFFSETS);
650 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000651 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000652 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000653 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000654 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redla93e3b52010-07-08 22:01:51 +0000655 RECORD(CHAINED_METADATA);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000656 RECORD(REFERENCED_SELECTOR_POOL);
Michael J. Spencer20249a12010-10-21 03:16:25 +0000657
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000658 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000659 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000660 RECORD(SM_SLOC_FILE_ENTRY);
661 RECORD(SM_SLOC_BUFFER_ENTRY);
662 RECORD(SM_SLOC_BUFFER_BLOB);
663 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
664 RECORD(SM_LINE_TABLE);
Mike Stump1eb44332009-09-09 15:08:12 +0000665
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000666 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000667 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000668 RECORD(PP_MACRO_OBJECT_LIKE);
669 RECORD(PP_MACRO_FUNCTION_LIKE);
670 RECORD(PP_TOKEN);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000671 RECORD(PP_MACRO_INSTANTIATION);
672 RECORD(PP_MACRO_DEFINITION);
Michael J. Spencer20249a12010-10-21 03:16:25 +0000673
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000674 // Decls and Types block.
675 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000676 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000677 RECORD(TYPE_COMPLEX);
678 RECORD(TYPE_POINTER);
679 RECORD(TYPE_BLOCK_POINTER);
680 RECORD(TYPE_LVALUE_REFERENCE);
681 RECORD(TYPE_RVALUE_REFERENCE);
682 RECORD(TYPE_MEMBER_POINTER);
683 RECORD(TYPE_CONSTANT_ARRAY);
684 RECORD(TYPE_INCOMPLETE_ARRAY);
685 RECORD(TYPE_VARIABLE_ARRAY);
686 RECORD(TYPE_VECTOR);
687 RECORD(TYPE_EXT_VECTOR);
688 RECORD(TYPE_FUNCTION_PROTO);
689 RECORD(TYPE_FUNCTION_NO_PROTO);
690 RECORD(TYPE_TYPEDEF);
691 RECORD(TYPE_TYPEOF_EXPR);
692 RECORD(TYPE_TYPEOF);
693 RECORD(TYPE_RECORD);
694 RECORD(TYPE_ENUM);
695 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000696 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000697 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000698 RECORD(DECL_TRANSLATION_UNIT);
699 RECORD(DECL_TYPEDEF);
700 RECORD(DECL_ENUM);
701 RECORD(DECL_RECORD);
702 RECORD(DECL_ENUM_CONSTANT);
703 RECORD(DECL_FUNCTION);
704 RECORD(DECL_OBJC_METHOD);
705 RECORD(DECL_OBJC_INTERFACE);
706 RECORD(DECL_OBJC_PROTOCOL);
707 RECORD(DECL_OBJC_IVAR);
708 RECORD(DECL_OBJC_AT_DEFS_FIELD);
709 RECORD(DECL_OBJC_CLASS);
710 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
711 RECORD(DECL_OBJC_CATEGORY);
712 RECORD(DECL_OBJC_CATEGORY_IMPL);
713 RECORD(DECL_OBJC_IMPLEMENTATION);
714 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
715 RECORD(DECL_OBJC_PROPERTY);
716 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000717 RECORD(DECL_FIELD);
718 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000719 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000720 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000721 RECORD(DECL_FILE_SCOPE_ASM);
722 RECORD(DECL_BLOCK);
723 RECORD(DECL_CONTEXT_LEXICAL);
724 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000725 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattner0558df22009-04-27 00:49:53 +0000726 AddStmtsExprs(Stream, Record);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000727#undef RECORD
728#undef BLOCK
729 Stream.ExitBlock();
730}
731
Douglas Gregore650c8c2009-07-07 00:12:59 +0000732/// \brief Adjusts the given filename to only write out the portion of the
733/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000734///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000735/// \param Filename the file name to adjust.
736///
737/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
738/// the returned filename will be adjusted by this system root.
739///
740/// \returns either the original filename (if it needs no adjustment) or the
741/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000742static const char *
Douglas Gregore650c8c2009-07-07 00:12:59 +0000743adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
744 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000745
Douglas Gregore650c8c2009-07-07 00:12:59 +0000746 if (!isysroot)
747 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000748
Douglas Gregore650c8c2009-07-07 00:12:59 +0000749 // Verify that the filename and the system root have the same prefix.
750 unsigned Pos = 0;
751 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
752 if (Filename[Pos] != isysroot[Pos])
753 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Douglas Gregore650c8c2009-07-07 00:12:59 +0000755 // We hit the end of the filename before we hit the end of the system root.
756 if (!Filename[Pos])
757 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Douglas Gregore650c8c2009-07-07 00:12:59 +0000759 // If the file name has a '/' at the current position, skip over the '/'.
760 // We distinguish sysroot-based includes from absolute includes by the
761 // absence of '/' at the beginning of sysroot-based includes.
762 if (Filename[Pos] == '/')
763 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Douglas Gregore650c8c2009-07-07 00:12:59 +0000765 return Filename + Pos;
766}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000767
Sebastian Redl3397c552010-08-18 23:56:27 +0000768/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Sebastian Redla4232eb2010-08-18 23:56:21 +0000769void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000770 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000771
Douglas Gregore650c8c2009-07-07 00:12:59 +0000772 // Metadata
773 const TargetInfo &Target = Context.Target;
774 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl77f46032010-07-09 21:00:24 +0000775 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000776 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl3397c552010-08-18 23:56:27 +0000777 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
778 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregore650c8c2009-07-07 00:12:59 +0000779 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
780 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
781 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl77f46032010-07-09 21:00:24 +0000782 // Target triple or chained PCH name
783 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregore650c8c2009-07-07 00:12:59 +0000784 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000785
Douglas Gregore650c8c2009-07-07 00:12:59 +0000786 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000787 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
788 Record.push_back(VERSION_MAJOR);
789 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000790 Record.push_back(CLANG_VERSION_MAJOR);
791 Record.push_back(CLANG_VERSION_MINOR);
792 Record.push_back(isysroot != 0);
Sebastian Redl77f46032010-07-09 21:00:24 +0000793 // FIXME: This writes the absolute path for chained headers.
794 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
795 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000796
Douglas Gregorb64c1932009-05-12 01:31:05 +0000797 // Original file name
798 SourceManager &SM = Context.getSourceManager();
799 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
800 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000801 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregorb64c1932009-05-12 01:31:05 +0000802 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
803 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
804
805 llvm::sys::Path MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000807 MainFilePath.makeAbsolute();
Douglas Gregorb64c1932009-05-12 01:31:05 +0000808
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +0000809 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000810 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000811 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000812 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000813 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000814 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000815 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000816
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000817 // Repository branch/version information.
818 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000819 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000820 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
821 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000822 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000823 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000824 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
825 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +0000826}
827
828/// \brief Write the LangOptions structure.
Sebastian Redla4232eb2010-08-18 23:56:21 +0000829void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000830 RecordData Record;
831 Record.push_back(LangOpts.Trigraphs);
832 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
833 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
834 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
835 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000836 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000837 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
838 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
839 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
840 Record.push_back(LangOpts.C99); // C99 Support
841 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencerdae4ac42010-10-21 05:21:48 +0000842 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
843 // already saved elsewhere.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000844 Record.push_back(LangOpts.CPlusPlus); // C++ Support
845 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000846 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000848 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
849 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000850 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000851 // modern abi enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000852 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000853 // modern abi enabled.
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +0000854 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump1eb44332009-09-09 15:08:12 +0000855
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000856 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000857 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
858 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000859 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000860 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar73482882010-02-10 18:48:44 +0000861 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000862
863 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
864 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
865 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
866
Chris Lattnerea5ce472009-04-27 07:35:58 +0000867 // Whether static initializers are protected by locks.
868 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +0000869 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000870 Record.push_back(LangOpts.Blocks); // block extension to C
871 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
872 // they are unused.
873 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
874 // (modulo the platform support).
875
Chris Lattnera4d71452010-06-26 21:25:03 +0000876 Record.push_back(LangOpts.getSignedOverflowBehavior());
877 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000878
879 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +0000880 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000881 // defined.
882 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
883 // opposed to __DYNAMIC__).
884 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
885
886 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
887 // used (instead of C99 semantics).
888 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +0000889 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
890 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +0000891 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
892 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +0000893 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000894 Record.push_back(LangOpts.getGCMode());
895 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000896 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000897 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000898 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne08a53262010-12-01 19:14:57 +0000899 Record.push_back(LangOpts.CUDA);
Mike Stump9c276ae2009-12-12 01:27:46 +0000900 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson92f58222009-08-22 22:30:33 +0000901 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +0000902 Record.push_back(LangOpts.SpellChecking);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000903 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000904}
905
Douglas Gregor14f79002009-04-10 03:52:48 +0000906//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000907// stat cache Serialization
908//===----------------------------------------------------------------------===//
909
910namespace {
911// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +0000912class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000913public:
914 typedef const char * key_type;
915 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Chris Lattner74e976b2010-11-23 19:28:12 +0000917 typedef struct stat data_type;
918 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000919
920 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000921 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000922 }
Mike Stump1eb44332009-09-09 15:08:12 +0000923
924 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000925 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
926 data_type_ref Data) {
927 unsigned StrLen = strlen(path);
928 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +0000929 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000930 clang::io::Emit8(Out, DataLen);
931 return std::make_pair(StrLen + 1, DataLen);
932 }
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000934 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
935 Out.write(path, KeyLen);
936 }
Mike Stump1eb44332009-09-09 15:08:12 +0000937
Chris Lattner74e976b2010-11-23 19:28:12 +0000938 void EmitData(llvm::raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000939 data_type_ref Data, unsigned DataLen) {
940 using namespace clang::io;
941 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +0000942
Chris Lattner74e976b2010-11-23 19:28:12 +0000943 Emit32(Out, (uint32_t) Data.st_ino);
944 Emit32(Out, (uint32_t) Data.st_dev);
945 Emit16(Out, (uint16_t) Data.st_mode);
946 Emit64(Out, (uint64_t) Data.st_mtime);
947 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000948
949 assert(Out.tell() - Start == DataLen && "Wrong data length");
950 }
951};
952} // end anonymous namespace
953
Sebastian Redl3397c552010-08-18 23:56:27 +0000954/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +0000955void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000956 // Build the on-disk hash table containing information about every
957 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +0000958 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000959 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000960 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000961 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000962 Stat != StatEnd; ++Stat, ++NumStatEntries) {
963 const char *Filename = Stat->first();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000964 Generator.insert(Filename, Stat->second);
965 }
Mike Stump1eb44332009-09-09 15:08:12 +0000966
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000967 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000968 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000969 uint32_t BucketOffset;
970 {
971 llvm::raw_svector_ostream Out(StatCacheData);
972 // Make sure that no bucket is at offset 0
973 clang::io::Emit32(Out, 0);
974 BucketOffset = Generator.Emit(Out);
975 }
976
977 // Create a blob abbreviation
978 using namespace llvm;
979 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000980 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000981 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
982 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
983 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
984 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
985
986 // Write the stat cache
987 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000988 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000989 Record.push_back(BucketOffset);
990 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000991 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000992}
993
994//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +0000995// Source Manager Serialization
996//===----------------------------------------------------------------------===//
997
998/// \brief Create an abbreviation for the SLocEntry that refers to a
999/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001000static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001001 using namespace llvm;
1002 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001003 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001004 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1005 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1006 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1007 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001008 // FileEntry fields.
1009 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1010 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor12fab312010-03-16 16:35:32 +00001011 // HeaderFileInfo fields.
1012 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
1013 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
1014 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
1015 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregor14f79002009-04-10 03:52:48 +00001016 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001017 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001018}
1019
1020/// \brief Create an abbreviation for the SLocEntry that refers to a
1021/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001022static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001023 using namespace llvm;
1024 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001025 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001026 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1027 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1028 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1029 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1030 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001031 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001032}
1033
1034/// \brief Create an abbreviation for the SLocEntry that refers to a
1035/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001036static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001037 using namespace llvm;
1038 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001039 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001040 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001041 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001042}
1043
1044/// \brief Create an abbreviation for the SLocEntry that refers to an
1045/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001046static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001047 using namespace llvm;
1048 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001049 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001050 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1051 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1052 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1053 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001054 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001055 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001056}
1057
1058/// \brief Writes the block containing the serialized form of the
1059/// source manager.
1060///
1061/// TODO: We should probably use an on-disk hash table (stored in a
1062/// blob), indexed based on the file name, so that we only create
1063/// entries for files that we actually need. In the common case (no
1064/// errors), we probably won't have to create file entries for any of
1065/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001066void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001067 const Preprocessor &PP,
1068 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001069 RecordData Record;
1070
Chris Lattnerf04ad692009-04-10 17:16:57 +00001071 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001072 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001073
1074 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001075 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1076 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1077 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1078 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001079
Douglas Gregorbd945002009-04-13 16:31:14 +00001080 // Write the line table.
1081 if (SourceMgr.hasLineTable()) {
1082 LineTableInfo &LineTable = SourceMgr.getLineTable();
1083
1084 // Emit the file names
1085 Record.push_back(LineTable.getNumFilenames());
1086 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1087 // Emit the file name
1088 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001089 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +00001090 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1091 Record.push_back(FilenameLen);
1092 if (FilenameLen)
1093 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1094 }
Mike Stump1eb44332009-09-09 15:08:12 +00001095
Douglas Gregorbd945002009-04-13 16:31:14 +00001096 // Emit the line entries
1097 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1098 L != LEnd; ++L) {
1099 // Emit the file ID
1100 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +00001101
Douglas Gregorbd945002009-04-13 16:31:14 +00001102 // Emit the line entries
1103 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001104 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +00001105 LEEnd = L->second.end();
1106 LE != LEEnd; ++LE) {
1107 Record.push_back(LE->FileOffset);
1108 Record.push_back(LE->LineNo);
1109 Record.push_back(LE->FilenameID);
1110 Record.push_back((unsigned)LE->FileKind);
1111 Record.push_back(LE->IncludeOffset);
1112 }
Douglas Gregorbd945002009-04-13 16:31:14 +00001113 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001114 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001115 }
1116
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001117 // Write out the source location entry table. We skip the first
1118 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001119 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001120 RecordData PreloadSLocs;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001121 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1122 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1123 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1124 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001125 // Get this source location entry.
1126 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001127
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001128 // Record the offset of this source-location entry.
1129 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1130
1131 // Figure out which record code to use.
1132 unsigned Code;
1133 if (SLoc->isFile()) {
1134 if (SLoc->getFile().getContentCache()->Entry)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001135 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001136 else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001137 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001138 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001139 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001140 Record.clear();
1141 Record.push_back(Code);
1142
1143 Record.push_back(SLoc->getOffset());
1144 if (SLoc->isFile()) {
1145 const SrcMgr::FileInfo &File = SLoc->getFile();
1146 Record.push_back(File.getIncludeLoc().getRawEncoding());
1147 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1148 Record.push_back(File.hasLineDirectives());
1149
1150 const SrcMgr::ContentCache *Content = File.getContentCache();
1151 if (Content->Entry) {
1152 // The source location entry is a file. The blob associated
1153 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001154
Douglas Gregor2d52be52010-03-21 22:49:54 +00001155 // Emit size/modification time for this file.
1156 Record.push_back(Content->Entry->getSize());
1157 Record.push_back(Content->Entry->getModificationTime());
1158
Douglas Gregor12fab312010-03-16 16:35:32 +00001159 // Emit header-search information associated with this file.
1160 HeaderFileInfo HFI;
1161 HeaderSearch &HS = PP.getHeaderSearchInfo();
1162 if (Content->Entry->getUID() < HS.header_file_size())
1163 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1164 Record.push_back(HFI.isImport);
1165 Record.push_back(HFI.DirInfo);
1166 Record.push_back(HFI.NumIncludes);
1167 AddIdentifierRef(HFI.ControllingMacro, Record);
1168
Douglas Gregore650c8c2009-07-07 00:12:59 +00001169 // Turn the file name into an absolute path, if it isn't already.
1170 const char *Filename = Content->Entry->getName();
1171 llvm::sys::Path FilePath(Filename, strlen(Filename));
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001172 FilePath.makeAbsolute();
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001173 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001174
Douglas Gregore650c8c2009-07-07 00:12:59 +00001175 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001176 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001177
1178 // FIXME: For now, preload all file source locations, so that
1179 // we get the appropriate File entries in the reader. This is
1180 // a temporary measure.
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001181 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001182 } else {
1183 // The source location entry is a buffer. The blob associated
1184 // with this entry contains the contents of the buffer.
1185
1186 // We add one to the size so that we capture the trailing NULL
1187 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1188 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001189 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001190 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001191 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001192 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1193 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001194 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001195 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001196 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001197 llvm::StringRef(Buffer->getBufferStart(),
1198 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001199
1200 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001201 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001202 }
1203 } else {
1204 // The source location entry is an instantiation.
1205 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1206 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1207 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1208 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1209
1210 // Compute the token length for this macro expansion.
1211 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001212 if (I + 1 != N)
1213 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001214 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1215 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1216 }
1217 }
1218
Douglas Gregorc9490c02009-04-16 22:23:12 +00001219 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001220
1221 if (SLocEntryOffsets.empty())
1222 return;
1223
Sebastian Redl3397c552010-08-18 23:56:27 +00001224 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001225 // table is used for lazily loading source-location information.
1226 using namespace llvm;
1227 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001228 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001229 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1230 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1231 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1232 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001233
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001234 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001235 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001236 Record.push_back(SLocEntryOffsets.size());
Sebastian Redl8db9fae2010-09-22 20:19:08 +00001237 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1238 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001239 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001240 (const char *)data(SLocEntryOffsets),
Chris Lattner090d9b52009-04-27 19:01:47 +00001241 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001242
Sebastian Redl3397c552010-08-18 23:56:27 +00001243 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001244 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001245 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001246}
1247
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001248//===----------------------------------------------------------------------===//
1249// Preprocessor Serialization
1250//===----------------------------------------------------------------------===//
1251
Chris Lattner0b1fb982009-04-10 17:15:23 +00001252/// \brief Writes the block containing the serialized form of the
1253/// preprocessor.
1254///
Sebastian Redla4232eb2010-08-18 23:56:21 +00001255void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001256 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001257
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001258 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1259 if (PP.getCounterValue() != 0) {
1260 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001261 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001262 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001263 }
1264
1265 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001266 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001267
Sebastian Redl3397c552010-08-18 23:56:27 +00001268 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001269 // FIXME: use diagnostics subsystem for localization etc.
1270 if (PP.SawDateOrTime())
1271 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001272
Douglas Gregorecdcb882010-10-20 22:00:55 +00001273
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001274 // Loop over all the macro definitions that are live at the end of the file,
1275 // emitting each to the PP section.
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001276 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001277 unsigned InclusionAbbrev = 0;
1278 if (PPRec) {
1279 using namespace llvm;
1280 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1281 Abbrev->Add(BitCodeAbbrevOp(PP_INCLUSION_DIRECTIVE));
1282 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1283 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1284 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1285 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1286 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1287 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1288 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Michael J. Spencer20249a12010-10-21 03:16:25 +00001289 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorecdcb882010-10-20 22:00:55 +00001290 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001291
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001292 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1293 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001294 // FIXME: This emits macros in hash table order, we should do it in a stable
1295 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001296 MacroInfo *MI = I->second;
1297
Sebastian Redl3397c552010-08-18 23:56:27 +00001298 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001299 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl3397c552010-08-18 23:56:27 +00001300 // Also skip macros from a AST file if we're chaining.
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001301
1302 // FIXME: There is a (probably minor) optimization we could do here, if
1303 // the macro comes from the original PCH but the identifier comes from a
1304 // chained PCH, by storing the offset into the original PCH rather than
1305 // writing the macro definition a second time.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001306 if (MI->isBuiltinMacro() ||
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001307 (Chain && I->first->isFromAST() && MI->isFromAST()))
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001308 continue;
1309
Chris Lattner7356a312009-04-11 21:15:38 +00001310 AddIdentifierRef(I->first, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001311 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001312 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1313 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001314
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001315 unsigned Code;
1316 if (MI->isObjectLike()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001317 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001318 } else {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001319 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001320
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001321 Record.push_back(MI->isC99Varargs());
1322 Record.push_back(MI->isGNUVarargs());
1323 Record.push_back(MI->getNumArgs());
1324 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1325 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001326 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001327 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001328
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001329 // If we have a detailed preprocessing record, record the macro definition
1330 // ID that corresponds to this macro.
1331 if (PPRec)
1332 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer20249a12010-10-21 03:16:25 +00001333
Douglas Gregorc9490c02009-04-16 22:23:12 +00001334 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001335 Record.clear();
1336
Chris Lattnerdf961c22009-04-10 18:08:30 +00001337 // Emit the tokens array.
1338 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1339 // Note that we know that the preprocessor does not have any annotation
1340 // tokens in it because they are created by the parser, and thus can't be
1341 // in a macro definition.
1342 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001343
Chris Lattnerdf961c22009-04-10 18:08:30 +00001344 Record.push_back(Tok.getLocation().getRawEncoding());
1345 Record.push_back(Tok.getLength());
1346
Chris Lattnerdf961c22009-04-10 18:08:30 +00001347 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1348 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001349 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001350
Chris Lattnerdf961c22009-04-10 18:08:30 +00001351 // FIXME: Should translate token kind to a stable encoding.
1352 Record.push_back(Tok.getKind());
1353 // FIXME: Should translate token flags to a stable encoding.
1354 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001355
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001356 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001357 Record.clear();
1358 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001359 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001360 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001361
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001362 // If the preprocessor has a preprocessing record, emit it.
1363 unsigned NumPreprocessingRecords = 0;
1364 if (PPRec) {
Sebastian Redl196f5572010-09-27 23:20:01 +00001365 unsigned IndexBase = Chain ? PPRec->getNumPreallocatedEntities() : 0;
Sebastian Redlb57a6242010-09-27 22:18:47 +00001366 for (PreprocessingRecord::iterator E = PPRec->begin(Chain),
1367 EEnd = PPRec->end(Chain);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001368 E != EEnd; ++E) {
1369 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001370
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001371 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1372 // Record this macro definition's location.
Sebastian Redlf73c93f2010-09-15 19:54:06 +00001373 MacroID ID = getMacroDefinitionID(MD);
Douglas Gregor89d99802010-11-30 06:16:57 +00001374
Douglas Gregor77424bc2010-10-02 19:29:26 +00001375 // Don't write the macro definition if it is from another AST file.
1376 if (ID < FirstMacroID)
1377 continue;
Douglas Gregor89d99802010-11-30 06:16:57 +00001378
1379 // Notify the serialization listener that we're serializing this entity.
1380 if (SerializationListener)
1381 SerializationListener->SerializedPreprocessedEntity(*E,
1382 Stream.GetCurrentBitNo());
Michael J. Spencer20249a12010-10-21 03:16:25 +00001383
Douglas Gregor77424bc2010-10-02 19:29:26 +00001384 unsigned Position = ID - FirstMacroID;
1385 if (Position != MacroDefinitionOffsets.size()) {
1386 if (Position > MacroDefinitionOffsets.size())
1387 MacroDefinitionOffsets.resize(Position + 1);
Douglas Gregor89d99802010-11-30 06:16:57 +00001388
Michael J. Spencer20249a12010-10-21 03:16:25 +00001389 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001390 } else
1391 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor89d99802010-11-30 06:16:57 +00001392
Sebastian Redlb57a6242010-09-27 22:18:47 +00001393 Record.push_back(IndexBase + NumPreprocessingRecords++);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001394 Record.push_back(ID);
1395 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1396 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1397 AddIdentifierRef(MD->getName(), Record);
1398 AddSourceLocation(MD->getLocation(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001399 Stream.EmitRecord(PP_MACRO_DEFINITION, Record);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001400 continue;
1401 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001402
Douglas Gregor89d99802010-11-30 06:16:57 +00001403 // Notify the serialization listener that we're serializing this entity.
1404 if (SerializationListener)
1405 SerializationListener->SerializedPreprocessedEntity(*E,
1406 Stream.GetCurrentBitNo());
1407
1408 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1409 Record.push_back(IndexBase + NumPreprocessingRecords++);
1410 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1411 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1412 AddIdentifierRef(MI->getName(), Record);
1413 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1414 Stream.EmitRecord(PP_MACRO_INSTANTIATION, Record);
1415 continue;
1416 }
1417
Douglas Gregorecdcb882010-10-20 22:00:55 +00001418 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1419 Record.push_back(PP_INCLUSION_DIRECTIVE);
1420 Record.push_back(IndexBase + NumPreprocessingRecords++);
1421 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1422 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1423 Record.push_back(ID->getFileName().size());
1424 Record.push_back(ID->wasInQuotes());
1425 Record.push_back(static_cast<unsigned>(ID->getKind()));
1426 llvm::SmallString<64> Buffer;
1427 Buffer += ID->getFileName();
1428 Buffer += ID->getFile()->getName();
1429 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1430 continue;
1431 }
Douglas Gregor89d99802010-11-30 06:16:57 +00001432
1433 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001434 }
1435 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001436
Douglas Gregorc9490c02009-04-16 22:23:12 +00001437 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001438
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001439 // Write the offsets table for the preprocessing record.
1440 if (NumPreprocessingRecords > 0) {
1441 // Write the offsets table for identifier IDs.
1442 using namespace llvm;
1443 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001444 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001445 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1446 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1447 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1448 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001449
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001450 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001451 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001452 Record.push_back(NumPreprocessingRecords);
1453 Record.push_back(MacroDefinitionOffsets.size());
1454 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001455 (const char *)data(MacroDefinitionOffsets),
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001456 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1457 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001458}
1459
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001460void ASTWriter::WriteUserDiagnosticMappings(const Diagnostic &Diag) {
1461 RecordData Record;
1462 for (unsigned i = 0; i != diag::DIAG_UPPER_LIMIT; ++i) {
1463 diag::Mapping Map = Diag.getDiagnosticMappingInfo(i);
1464 if (Map & 0x8) { // user mapping.
1465 Record.push_back(i);
1466 Record.push_back(Map & 0x7);
1467 }
1468 }
1469
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00001470 if (!Record.empty())
1471 Stream.EmitRecord(DIAG_USER_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001472}
1473
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001474//===----------------------------------------------------------------------===//
1475// Type Serialization
1476//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001477
Sebastian Redl3397c552010-08-18 23:56:27 +00001478/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001479void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00001480 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001481 if (Idx.getIndex() == 0) // we haven't seen this type before.
1482 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00001483
Douglas Gregor97475832010-10-05 18:37:06 +00001484 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00001485
Douglas Gregor2cf26342009-04-09 22:27:44 +00001486 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001487 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00001488 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001489 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00001490 else if (TypeOffsets.size() < Index) {
1491 TypeOffsets.resize(Index + 1);
1492 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001493 }
1494
1495 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001496
Douglas Gregor2cf26342009-04-09 22:27:44 +00001497 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00001498 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001499
Douglas Gregora4923eb2009-11-16 21:35:15 +00001500 if (T.hasLocalNonFastQualifiers()) {
1501 Qualifiers Qs = T.getLocalQualifiers();
1502 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001503 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001504 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00001505 } else {
1506 switch (T->getTypeClass()) {
1507 // For all of the concrete, non-dependent types, call the
1508 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001509#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001510 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001511#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001512#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001513 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001514 }
1515
1516 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001517 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001518
1519 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001520 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001521}
1522
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001523//===----------------------------------------------------------------------===//
1524// Declaration Serialization
1525//===----------------------------------------------------------------------===//
1526
Douglas Gregor2cf26342009-04-09 22:27:44 +00001527/// \brief Write the block containing all of the declaration IDs
1528/// lexically declared within the given DeclContext.
1529///
1530/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1531/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001532uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001533 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001534 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001535 return 0;
1536
Douglas Gregorc9490c02009-04-16 22:23:12 +00001537 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001538 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001539 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001540 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001541 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1542 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001543 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001544
Douglas Gregor25123082009-04-22 22:34:57 +00001545 ++NumLexicalDeclContexts;
Sebastian Redl681d7232010-07-27 00:17:23 +00001546 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001547 reinterpret_cast<char*>(Decls.data()),
1548 Decls.size() * sizeof(KindDeclIDPair));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001549 return Offset;
1550}
1551
Sebastian Redla4232eb2010-08-18 23:56:21 +00001552void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00001553 using namespace llvm;
1554 RecordData Record;
1555
1556 // Write the type offsets array
1557 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001558 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001559 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1560 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1561 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1562 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001563 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00001564 Record.push_back(TypeOffsets.size());
1565 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001566 (const char *)data(TypeOffsets),
Sebastian Redl1476ed42010-07-16 16:36:56 +00001567 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1568
1569 // Write the declaration offsets array
1570 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001571 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001572 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1573 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1574 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1575 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001576 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00001577 Record.push_back(DeclOffsets.size());
1578 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001579 (const char *)data(DeclOffsets),
Sebastian Redl1476ed42010-07-16 16:36:56 +00001580 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1581}
1582
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001583//===----------------------------------------------------------------------===//
1584// Global Method Pool and Selector Serialization
1585//===----------------------------------------------------------------------===//
1586
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001587namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001588// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00001589class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00001590 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001591
1592public:
1593 typedef Selector key_type;
1594 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001595
Sebastian Redl5d050072010-08-04 17:20:04 +00001596 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001597 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00001598 ObjCMethodList Instance, Factory;
1599 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001600 typedef const data_type& data_type_ref;
1601
Sebastian Redl3397c552010-08-18 23:56:27 +00001602 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001603
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001604 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00001605 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001606 }
Mike Stump1eb44332009-09-09 15:08:12 +00001607
1608 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001609 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1610 data_type_ref Methods) {
1611 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1612 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00001613 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1614 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001615 Method = Method->Next)
1616 if (Method->Method)
1617 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00001618 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001619 Method = Method->Next)
1620 if (Method->Method)
1621 DataLen += 4;
1622 clang::io::Emit16(Out, DataLen);
1623 return std::make_pair(KeyLen, DataLen);
1624 }
Mike Stump1eb44332009-09-09 15:08:12 +00001625
Douglas Gregor83941df2009-04-25 17:48:32 +00001626 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00001627 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00001628 assert((Start >> 32) == 0 && "Selector key offset too large");
1629 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001630 unsigned N = Sel.getNumArgs();
1631 clang::io::Emit16(Out, N);
1632 if (N == 0)
1633 N = 1;
1634 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001635 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001636 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1637 }
Mike Stump1eb44332009-09-09 15:08:12 +00001638
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001639 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00001640 data_type_ref Methods, unsigned DataLen) {
1641 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00001642 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001643 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00001644 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001645 Method = Method->Next)
1646 if (Method->Method)
1647 ++NumInstanceMethods;
1648
1649 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00001650 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001651 Method = Method->Next)
1652 if (Method->Method)
1653 ++NumFactoryMethods;
1654
1655 clang::io::Emit16(Out, NumInstanceMethods);
1656 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00001657 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001658 Method = Method->Next)
1659 if (Method->Method)
1660 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00001661 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001662 Method = Method->Next)
1663 if (Method->Method)
1664 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00001665
1666 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001667 }
1668};
1669} // end anonymous namespace
1670
Sebastian Redl059612d2010-08-03 21:58:15 +00001671/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001672///
1673/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00001674/// in an on-disk hash table indexed by the selector. The hash table also
1675/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001676void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001677 using namespace llvm;
1678
Sebastian Redl059612d2010-08-03 21:58:15 +00001679 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00001680 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00001681 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00001682 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00001683 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001684 {
Sebastian Redl3397c552010-08-18 23:56:27 +00001685 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001686 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001687
Sebastian Redl059612d2010-08-03 21:58:15 +00001688 // Create the on-disk hash table representation. We walk through every
1689 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00001690 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001691 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00001692 I = SelectorIDs.begin(), E = SelectorIDs.end();
1693 I != E; ++I) {
1694 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00001695 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00001696 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00001697 I->second,
1698 ObjCMethodList(),
1699 ObjCMethodList()
1700 };
1701 if (F != SemaRef.MethodPool.end()) {
1702 Data.Instance = F->second.first;
1703 Data.Factory = F->second.second;
1704 }
Sebastian Redl3397c552010-08-18 23:56:27 +00001705 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00001706 // changed.
1707 if (Chain && I->second < FirstSelectorID) {
1708 // Selector already exists. Did it change?
1709 bool changed = false;
1710 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
1711 M = M->Next) {
1712 if (M->Method->getPCHLevel() == 0)
1713 changed = true;
1714 }
1715 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
1716 M = M->Next) {
1717 if (M->Method->getPCHLevel() == 0)
1718 changed = true;
1719 }
1720 if (!changed)
1721 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00001722 } else if (Data.Instance.Method || Data.Factory.Method) {
1723 // A new method pool entry.
1724 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00001725 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001726 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001727 }
1728
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001729 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001730 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001731 uint32_t BucketOffset;
1732 {
Sebastian Redl3397c552010-08-18 23:56:27 +00001733 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001734 llvm::raw_svector_ostream Out(MethodPool);
1735 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001736 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001737 BucketOffset = Generator.Emit(Out, Trait);
1738 }
1739
1740 // Create a blob abbreviation
1741 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001742 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001743 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00001744 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001745 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1746 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1747
Douglas Gregor83941df2009-04-25 17:48:32 +00001748 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001749 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001750 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001751 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00001752 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001753 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00001754
1755 // Create a blob abbreviation for the selector table offsets.
1756 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001757 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00001758 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor83941df2009-04-25 17:48:32 +00001759 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1760 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1761
1762 // Write the selector offsets table.
1763 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001764 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00001765 Record.push_back(SelectorOffsets.size());
1766 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001767 (const char *)data(SelectorOffsets),
Douglas Gregor83941df2009-04-25 17:48:32 +00001768 SelectorOffsets.size() * 4);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001769 }
1770}
1771
Sebastian Redl3397c552010-08-18 23:56:27 +00001772/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001773void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00001774 using namespace llvm;
1775 if (SemaRef.ReferencedSelectors.empty())
1776 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00001777
Fariborz Jahanian32019832010-07-23 19:11:11 +00001778 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00001779
Sebastian Redl3397c552010-08-18 23:56:27 +00001780 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00001781 // very tricky to fix, and given that @selector shouldn't really appear in
1782 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00001783 for (DenseMap<Selector, SourceLocation>::iterator S =
1784 SemaRef.ReferencedSelectors.begin(),
1785 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
1786 Selector Sel = (*S).first;
1787 SourceLocation Loc = (*S).second;
1788 AddSelectorRef(Sel, Record);
1789 AddSourceLocation(Loc, Record);
1790 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001791 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00001792}
1793
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001794//===----------------------------------------------------------------------===//
1795// Identifier Table Serialization
1796//===----------------------------------------------------------------------===//
1797
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001798namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00001799class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00001800 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00001801 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001802
Douglas Gregora92193e2009-04-28 21:18:29 +00001803 /// \brief Determines whether this is an "interesting" identifier
1804 /// that needs a full IdentifierInfo structure written into the hash
1805 /// table.
1806 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1807 return II->isPoisoned() ||
1808 II->isExtensionToken() ||
1809 II->hasMacroDefinition() ||
1810 II->getObjCOrBuiltinID() ||
1811 II->getFETokenInfo<void>();
1812 }
1813
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001814public:
1815 typedef const IdentifierInfo* key_type;
1816 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001817
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001818 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001819 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001820
Sebastian Redl3397c552010-08-18 23:56:27 +00001821 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00001822 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001823
1824 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001825 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001826 }
Mike Stump1eb44332009-09-09 15:08:12 +00001827
1828 std::pair<unsigned,unsigned>
1829 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001830 IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00001831 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00001832 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1833 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00001834 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00001835 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00001836 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00001837 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00001838 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1839 DEnd = IdentifierResolver::end();
1840 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001841 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00001842 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001843 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00001844 // We emit the key length after the data length so that every
1845 // string is preceded by a 16-bit length. This matches the PTH
1846 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00001847 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001848 return std::make_pair(KeyLen, DataLen);
1849 }
Mike Stump1eb44332009-09-09 15:08:12 +00001850
1851 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001852 unsigned KeyLen) {
1853 // Record the location of the key data. This is used when generating
1854 // the mapping from persistent IDs to strings.
1855 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00001856 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001857 }
Mike Stump1eb44332009-09-09 15:08:12 +00001858
1859 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001860 IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00001861 if (!isInterestingIdentifier(II)) {
1862 clang::io::Emit32(Out, ID << 1);
1863 return;
1864 }
Douglas Gregor5998da52009-04-28 21:32:13 +00001865
Douglas Gregora92193e2009-04-28 21:18:29 +00001866 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001867 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001868 bool hasMacroDefinition =
1869 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00001870 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00001871 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001872 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1873 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1874 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00001875 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001876 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00001877 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001878
Douglas Gregor37e26842009-04-21 23:56:24 +00001879 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00001880 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00001881
Douglas Gregor668c1a42009-04-21 22:25:48 +00001882 // Emit the declaration IDs in reverse order, because the
1883 // IdentifierResolver provides the declarations as they would be
1884 // visible (e.g., the function "stat" would come before the struct
1885 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1886 // adds declarations to the end of the list (so we need to see the
1887 // struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00001888 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump1eb44332009-09-09 15:08:12 +00001889 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00001890 IdentifierResolver::end());
1891 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1892 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001893 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00001894 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001895 }
1896};
1897} // end anonymous namespace
1898
Sebastian Redl3397c552010-08-18 23:56:27 +00001899/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001900///
1901/// The identifier table consists of a blob containing string data
1902/// (the actual identifiers themselves) and a separate "offsets" index
1903/// that maps identifier IDs to locations within the blob.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001904void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001905 using namespace llvm;
1906
1907 // Create and write out the blob that contains the identifier
1908 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001909 {
Sebastian Redl3397c552010-08-18 23:56:27 +00001910 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001911 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump1eb44332009-09-09 15:08:12 +00001912
Douglas Gregor92b059e2009-04-28 20:33:11 +00001913 // Look for any identifiers that were named while processing the
1914 // headers, but are otherwise not needed. We add these to the hash
1915 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00001916 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00001917 // file.
1918 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1919 IDEnd = PP.getIdentifierTable().end();
1920 ID != IDEnd; ++ID)
1921 getIdentifierRef(ID->second);
1922
Sebastian Redlf2f0f032010-07-23 23:49:55 +00001923 // Create the on-disk hash table representation. We only store offsets
1924 // for identifiers that appear here for the first time.
1925 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001926 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00001927 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1928 ID != IDEnd; ++ID) {
1929 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001930 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001931 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001932 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001933
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001934 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001935 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00001936 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001937 {
Sebastian Redl3397c552010-08-18 23:56:27 +00001938 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001939 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001940 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001941 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001942 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001943 }
1944
1945 // Create a blob abbreviation
1946 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001947 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001948 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001949 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00001950 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001951
1952 // Write the identifier table
1953 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001954 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001955 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001956 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001957 }
1958
1959 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001960 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001961 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001962 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1963 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1964 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1965
1966 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001967 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001968 Record.push_back(IdentifierOffsets.size());
1969 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001970 (const char *)data(IdentifierOffsets),
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001971 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregorafaf3082009-04-11 00:14:32 +00001972}
1973
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001974//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001975// DeclContext's Name Lookup Table Serialization
1976//===----------------------------------------------------------------------===//
1977
1978namespace {
1979// Trait used for the on-disk hash table used in the method pool.
1980class ASTDeclContextNameLookupTrait {
1981 ASTWriter &Writer;
1982
1983public:
1984 typedef DeclarationName key_type;
1985 typedef key_type key_type_ref;
1986
1987 typedef DeclContext::lookup_result data_type;
1988 typedef const data_type& data_type_ref;
1989
1990 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
1991
1992 unsigned ComputeHash(DeclarationName Name) {
1993 llvm::FoldingSetNodeID ID;
1994 ID.AddInteger(Name.getNameKind());
1995
1996 switch (Name.getNameKind()) {
1997 case DeclarationName::Identifier:
1998 ID.AddString(Name.getAsIdentifierInfo()->getName());
1999 break;
2000 case DeclarationName::ObjCZeroArgSelector:
2001 case DeclarationName::ObjCOneArgSelector:
2002 case DeclarationName::ObjCMultiArgSelector:
2003 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2004 break;
2005 case DeclarationName::CXXConstructorName:
2006 case DeclarationName::CXXDestructorName:
2007 case DeclarationName::CXXConversionFunctionName:
2008 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
2009 break;
2010 case DeclarationName::CXXOperatorName:
2011 ID.AddInteger(Name.getCXXOverloadedOperator());
2012 break;
2013 case DeclarationName::CXXLiteralOperatorName:
2014 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2015 case DeclarationName::CXXUsingDirective:
2016 break;
2017 }
2018
2019 return ID.ComputeHash();
2020 }
2021
2022 std::pair<unsigned,unsigned>
2023 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2024 data_type_ref Lookup) {
2025 unsigned KeyLen = 1;
2026 switch (Name.getNameKind()) {
2027 case DeclarationName::Identifier:
2028 case DeclarationName::ObjCZeroArgSelector:
2029 case DeclarationName::ObjCOneArgSelector:
2030 case DeclarationName::ObjCMultiArgSelector:
2031 case DeclarationName::CXXConstructorName:
2032 case DeclarationName::CXXDestructorName:
2033 case DeclarationName::CXXConversionFunctionName:
2034 case DeclarationName::CXXLiteralOperatorName:
2035 KeyLen += 4;
2036 break;
2037 case DeclarationName::CXXOperatorName:
2038 KeyLen += 1;
2039 break;
2040 case DeclarationName::CXXUsingDirective:
2041 break;
2042 }
2043 clang::io::Emit16(Out, KeyLen);
2044
2045 // 2 bytes for num of decls and 4 for each DeclID.
2046 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2047 clang::io::Emit16(Out, DataLen);
2048
2049 return std::make_pair(KeyLen, DataLen);
2050 }
2051
2052 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2053 using namespace clang::io;
2054
2055 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2056 Emit8(Out, Name.getNameKind());
2057 switch (Name.getNameKind()) {
2058 case DeclarationName::Identifier:
2059 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2060 break;
2061 case DeclarationName::ObjCZeroArgSelector:
2062 case DeclarationName::ObjCOneArgSelector:
2063 case DeclarationName::ObjCMultiArgSelector:
2064 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2065 break;
2066 case DeclarationName::CXXConstructorName:
2067 case DeclarationName::CXXDestructorName:
2068 case DeclarationName::CXXConversionFunctionName:
2069 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2070 break;
2071 case DeclarationName::CXXOperatorName:
2072 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2073 Emit8(Out, Name.getCXXOverloadedOperator());
2074 break;
2075 case DeclarationName::CXXLiteralOperatorName:
2076 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2077 break;
2078 case DeclarationName::CXXUsingDirective:
2079 break;
2080 }
2081 }
2082
2083 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2084 data_type Lookup, unsigned DataLen) {
2085 uint64_t Start = Out.tell(); (void)Start;
2086 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2087 for (; Lookup.first != Lookup.second; ++Lookup.first)
2088 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2089
2090 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2091 }
2092};
2093} // end anonymous namespace
2094
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002095/// \brief Write the block containing all of the declaration IDs
2096/// visible from the given DeclContext.
2097///
2098/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002099/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002100uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2101 DeclContext *DC) {
2102 if (DC->getPrimaryContext() != DC)
2103 return 0;
2104
2105 // Since there is no name lookup into functions or methods, don't bother to
2106 // build a visible-declarations table for these entities.
2107 if (DC->isFunctionOrMethod())
2108 return 0;
2109
2110 // If not in C++, we perform name lookup for the translation unit via the
2111 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2112 // FIXME: In C++ we need the visible declarations in order to "see" the
2113 // friend declarations, is there a way to do this without writing the table ?
2114 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2115 return 0;
2116
2117 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00002118 if (DC->hasExternalVisibleStorage())
2119 DC->MaterializeVisibleDeclsFromExternalStorage();
2120 else
2121 DC->lookup(DeclarationName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002122
2123 // Serialize the contents of the mapping used for lookup. Note that,
2124 // although we have two very different code paths, the serialized
2125 // representation is the same for both cases: a declaration name,
2126 // followed by a size, followed by references to the visible
2127 // declarations that have that name.
2128 uint64_t Offset = Stream.GetCurrentBitNo();
2129 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2130 if (!Map || Map->empty())
2131 return 0;
2132
2133 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2134 ASTDeclContextNameLookupTrait Trait(*this);
2135
2136 // Create the on-disk hash table representation.
2137 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2138 D != DEnd; ++D) {
2139 DeclarationName Name = D->first;
2140 DeclContext::lookup_result Result = D->second.getLookupResult();
2141 Generator.insert(Name, Result, Trait);
2142 }
2143
2144 // Create the on-disk hash table in a buffer.
2145 llvm::SmallString<4096> LookupTable;
2146 uint32_t BucketOffset;
2147 {
2148 llvm::raw_svector_ostream Out(LookupTable);
2149 // Make sure that no bucket is at offset 0
2150 clang::io::Emit32(Out, 0);
2151 BucketOffset = Generator.Emit(Out, Trait);
2152 }
2153
2154 // Write the lookup table
2155 RecordData Record;
2156 Record.push_back(DECL_CONTEXT_VISIBLE);
2157 Record.push_back(BucketOffset);
2158 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2159 LookupTable.str());
2160
2161 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2162 ++NumVisibleDeclContexts;
2163 return Offset;
2164}
2165
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002166/// \brief Write an UPDATE_VISIBLE block for the given context.
2167///
2168/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2169/// DeclContext in a dependent AST file. As such, they only exist for the TU
2170/// (in C++) and for namespaces.
2171void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002172 // Make the context build its lookup table, but don't make it load external
2173 // decls.
2174 DC->lookup(DeclarationName());
2175
2176 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2177 if (!Map || Map->empty())
2178 return;
2179
2180 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2181 ASTDeclContextNameLookupTrait Trait(*this);
2182
2183 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002184 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2185 D != DEnd; ++D) {
2186 DeclarationName Name = D->first;
2187 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002188 // For any name that appears in this table, the results are complete, i.e.
2189 // they overwrite results from previous PCHs. Merging is always a mess.
2190 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002191 }
2192
2193 // Create the on-disk hash table in a buffer.
2194 llvm::SmallString<4096> LookupTable;
2195 uint32_t BucketOffset;
2196 {
2197 llvm::raw_svector_ostream Out(LookupTable);
2198 // Make sure that no bucket is at offset 0
2199 clang::io::Emit32(Out, 0);
2200 BucketOffset = Generator.Emit(Out, Trait);
2201 }
2202
2203 // Write the lookup table
2204 RecordData Record;
2205 Record.push_back(UPDATE_VISIBLE);
2206 Record.push_back(getDeclID(cast<Decl>(DC)));
2207 Record.push_back(BucketOffset);
2208 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2209}
2210
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002211//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002212// General Serialization Routines
2213//===----------------------------------------------------------------------===//
2214
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002215/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002216void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00002217 Record.push_back(Attrs.size());
Sean Huntcf807c42010-08-18 23:23:40 +00002218 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2219 const Attr * A = *i;
2220 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2221 AddSourceLocation(A->getLocation(), Record);
2222 Record.push_back(A->isInherited());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002223
Sean Huntcf807c42010-08-18 23:23:40 +00002224#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00002225
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002226 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002227}
2228
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002229void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002230 Record.push_back(Str.size());
2231 Record.insert(Record.end(), Str.begin(), Str.end());
2232}
2233
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002234/// \brief Note that the identifier II occurs at the given offset
2235/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002236void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002237 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00002238 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002239 // up earlier in the chain and thus don't need an offset.
2240 if (ID >= FirstIdentID)
2241 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002242}
2243
Douglas Gregor83941df2009-04-25 17:48:32 +00002244/// \brief Note that the selector Sel occurs at the given offset
2245/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002246void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00002247 unsigned ID = SelectorIDs[Sel];
2248 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00002249 // Don't record offsets for selectors that are also available in a different
2250 // file.
2251 if (ID < FirstSelectorID)
2252 return;
2253 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00002254}
2255
Sebastian Redla4232eb2010-08-18 23:56:21 +00002256ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregor89d99802010-11-30 06:16:57 +00002257 : Stream(Stream), Chain(0), SerializationListener(0),
2258 FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002259 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redle58aa892010-08-04 18:21:41 +00002260 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor77424bc2010-10-02 19:29:26 +00002261 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2262 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00002263 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor7c789c12010-10-29 22:39:52 +00002264 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
2265 NextCXXBaseSpecifiersID(1)
2266{
Sebastian Redl30c514c2010-07-14 23:45:08 +00002267}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002268
Sebastian Redla4232eb2010-08-18 23:56:21 +00002269void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002270 const char *isysroot) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002271 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002272 Stream.Emit((unsigned)'C', 8);
2273 Stream.Emit((unsigned)'P', 8);
2274 Stream.Emit((unsigned)'C', 8);
2275 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002276
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002277 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002278
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002279 if (Chain)
Sebastian Redla4232eb2010-08-18 23:56:21 +00002280 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002281 else
Sebastian Redla4232eb2010-08-18 23:56:21 +00002282 WriteASTCore(SemaRef, StatCalls, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002283}
2284
Sebastian Redla4232eb2010-08-18 23:56:21 +00002285void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002286 const char *isysroot) {
2287 using namespace llvm;
2288
2289 ASTContext &Context = SemaRef.Context;
2290 Preprocessor &PP = SemaRef.PP;
2291
Douglas Gregor2cf26342009-04-09 22:27:44 +00002292 // The translation unit is the first declaration we'll emit.
2293 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002294 ++NextDeclID;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002295 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002296
Douglas Gregor2deaea32009-04-22 18:49:13 +00002297 // Make sure that we emit IdentifierInfos (and any attached
2298 // declarations) for builtins.
2299 {
2300 IdentifierTable &Table = PP.getIdentifierTable();
2301 llvm::SmallVector<const char *, 32> BuiltinNames;
2302 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2303 Context.getLangOptions().NoBuiltin);
2304 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2305 getIdentifierRef(&Table.get(BuiltinNames[I]));
2306 }
2307
Chris Lattner63d65f82009-09-08 18:19:27 +00002308 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002309 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002310 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002311 RecordData TentativeDefinitions;
Sebastian Redle9d12b62010-01-31 22:27:38 +00002312 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2313 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner63d65f82009-09-08 18:19:27 +00002314 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002315
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002316 // Build a record containing all of the file scoped decls in this file.
2317 RecordData UnusedFileScopedDecls;
2318 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2319 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00002320
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002321 RecordData WeakUndeclaredIdentifiers;
2322 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2323 WeakUndeclaredIdentifiers.push_back(
2324 SemaRef.WeakUndeclaredIdentifiers.size());
2325 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2326 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2327 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2328 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2329 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2330 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2331 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2332 }
2333 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002334
Douglas Gregor14c22f22009-04-22 22:18:58 +00002335 // Build a record containing all of the locally-scoped external
2336 // declarations in this header file. Generally, this record will be
2337 // empty.
2338 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002339 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00002340 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002341 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002342 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2343 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2344 TD != TDEnd; ++TD)
2345 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2346
Douglas Gregorb81c1702009-04-27 20:06:05 +00002347 // Build a record containing all of the ext_vector declarations.
2348 RecordData ExtVectorDecls;
2349 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2350 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2351
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002352 // Build a record containing all of the VTable uses information.
2353 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00002354 if (!SemaRef.VTableUses.empty()) {
2355 VTableUses.push_back(SemaRef.VTableUses.size());
2356 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2357 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2358 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2359 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2360 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002361 }
2362
2363 // Build a record containing all of dynamic classes declarations.
2364 RecordData DynamicClasses;
2365 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2366 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2367
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002368 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002369 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002370 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00002371 I = SemaRef.PendingInstantiations.begin(),
2372 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2373 AddDeclRef(I->first, PendingInstantiations);
2374 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002375 }
2376 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2377 "There are local ones at end of translation unit!");
2378
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002379 // Build a record containing some declaration references.
2380 RecordData SemaDeclRefs;
2381 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2382 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2383 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2384 }
2385
Sebastian Redl3397c552010-08-18 23:56:27 +00002386 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002387 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002388 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl30c514c2010-07-14 23:45:08 +00002389 WriteMetadata(Context, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002390 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00002391 if (StatCalls && !isysroot)
Douglas Gregordd41ed52010-07-12 23:48:14 +00002392 WriteStatCache(*StatCalls);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002393 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002394 // Write the record of special types.
2395 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00002396
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002397 AddTypeRef(Context.getBuiltinVaListType(), Record);
2398 AddTypeRef(Context.getObjCIdType(), Record);
2399 AddTypeRef(Context.getObjCSelType(), Record);
2400 AddTypeRef(Context.getObjCProtoType(), Record);
2401 AddTypeRef(Context.getObjCClassType(), Record);
2402 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2403 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2404 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002405 AddTypeRef(Context.getjmp_bufType(), Record);
2406 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002407 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2408 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpadaaad32009-10-20 02:12:22 +00002409 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002410 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002411 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2412 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002413 Record.push_back(Context.isInt128Installed());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002414 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002415
Douglas Gregor366809a2009-04-26 03:49:13 +00002416 // Keep writing types and declarations until all types and
2417 // declarations have been written.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002418 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002419 WriteDeclsBlockAbbrevs();
2420 while (!DeclTypesToEmit.empty()) {
2421 DeclOrType DOT = DeclTypesToEmit.front();
2422 DeclTypesToEmit.pop();
2423 if (DOT.isType())
2424 WriteType(DOT.getType());
2425 else
2426 WriteDecl(Context, DOT.getDecl());
2427 }
2428 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002429
Douglas Gregor813a97b2009-10-17 17:25:45 +00002430 WritePreprocessor(PP);
Sebastian Redl059612d2010-08-03 21:58:15 +00002431 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002432 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002433 WriteIdentifierTable(PP);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002434
Sebastian Redl1476ed42010-07-16 16:36:56 +00002435 WriteTypeDeclOffsets();
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002436 WriteUserDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00002437
Douglas Gregor7c789c12010-10-29 22:39:52 +00002438 // Write the C++ base-specifier set offsets.
2439 if (!CXXBaseSpecifiersOffsets.empty()) {
2440 // Create a blob abbreviation for the C++ base specifiers offsets.
2441 using namespace llvm;
2442
2443 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2444 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2445 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2446 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2447 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2448
2449 // Write the selector offsets table.
2450 Record.clear();
2451 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2452 Record.push_back(CXXBaseSpecifiersOffsets.size());
2453 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
2454 (const char *)CXXBaseSpecifiersOffsets.data(),
2455 CXXBaseSpecifiersOffsets.size() * sizeof(uint32_t));
2456 }
2457
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002458 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002459 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002460 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002461
2462 // Write the record containing tentative definitions.
2463 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002464 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002465
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002466 // Write the record containing unused file scoped decls.
2467 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002468 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002469
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002470 // Write the record containing weak undeclared identifiers.
2471 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002472 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002473 WeakUndeclaredIdentifiers);
2474
Douglas Gregor14c22f22009-04-22 22:18:58 +00002475 // Write the record containing locally-scoped external definitions.
2476 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002477 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002478 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002479
2480 // Write the record containing ext_vector type names.
2481 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002482 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002483
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002484 // Write the record containing VTable uses information.
2485 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002486 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002487
2488 // Write the record containing dynamic classes declarations.
2489 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002490 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002491
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002492 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002493 if (!PendingInstantiations.empty())
2494 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002495
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002496 // Write the record containing declaration references of Sema.
2497 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002498 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002499
Douglas Gregor3e1af842009-04-17 22:13:46 +00002500 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002501 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002502 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002503 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002504 Record.push_back(NumLexicalDeclContexts);
2505 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002506 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002507 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002508}
2509
Sebastian Redla4232eb2010-08-18 23:56:21 +00002510void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002511 const char *isysroot) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002512 using namespace llvm;
2513
2514 ASTContext &Context = SemaRef.Context;
2515 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1476ed42010-07-16 16:36:56 +00002516
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002517 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002518 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl30c514c2010-07-14 23:45:08 +00002519 WriteMetadata(Context, isysroot);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002520 if (StatCalls && !isysroot)
2521 WriteStatCache(*StatCalls);
2522 // FIXME: Source manager block should only write new stuff, which could be
2523 // done by tracking the largest ID in the chain
2524 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002525
2526 // The special types are in the chained PCH.
2527
2528 // We don't start with the translation unit, but with its decls that
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002529 // don't come from the chained PCH.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002530 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002531 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl681d7232010-07-27 00:17:23 +00002532 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2533 E = TU->noload_decls_end();
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002534 I != E; ++I) {
Sebastian Redld692af72010-07-27 18:24:41 +00002535 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002536 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redl0b17c612010-08-13 00:28:03 +00002537 else if ((*I)->isChangedSinceDeserialization())
2538 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002539 }
Sebastian Redl681d7232010-07-27 00:17:23 +00002540 // We also need to write a lexical updates block for the TU.
Sebastian Redld692af72010-07-27 18:24:41 +00002541 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002542 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redld692af72010-07-27 18:24:41 +00002543 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2544 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2545 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002546 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redld692af72010-07-27 18:24:41 +00002547 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2548 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002549 NewGlobalDecls.size() * sizeof(KindDeclIDPair));
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00002550 // And a visible updates block for the DeclContexts.
2551 Abv = new llvm::BitCodeAbbrev();
2552 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2553 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2554 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2555 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2556 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2557 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002558
Sebastian Redl083abdf2010-07-27 23:01:28 +00002559 // Build a record containing all of the new tentative definitions in this
2560 // file, in TentativeDefinitions order.
2561 RecordData TentativeDefinitions;
2562 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2563 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2564 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2565 }
2566
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002567 // Build a record containing all of the file scoped decls in this file.
2568 RecordData UnusedFileScopedDecls;
2569 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2570 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2571 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002572 }
2573
Sebastian Redl40566802010-08-05 18:21:25 +00002574 // We write the entire table, overwriting the tables from the chain.
2575 RecordData WeakUndeclaredIdentifiers;
2576 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2577 WeakUndeclaredIdentifiers.push_back(
2578 SemaRef.WeakUndeclaredIdentifiers.size());
2579 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2580 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2581 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2582 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2583 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2584 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2585 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2586 }
2587 }
2588
Sebastian Redl083abdf2010-07-27 23:01:28 +00002589 // Build a record containing all of the locally-scoped external
2590 // declarations in this header file. Generally, this record will be
2591 // empty.
2592 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002593 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl083abdf2010-07-27 23:01:28 +00002594 // nondeterminstic!
2595 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2596 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2597 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2598 TD != TDEnd; ++TD) {
2599 if (TD->second->getPCHLevel() == 0)
2600 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2601 }
2602
2603 // Build a record containing all of the ext_vector declarations.
2604 RecordData ExtVectorDecls;
2605 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2606 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2607 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2608 }
2609
Sebastian Redl40566802010-08-05 18:21:25 +00002610 // Build a record containing all of the VTable uses information.
2611 // We write everything here, because it's too hard to determine whether
2612 // a use is new to this part.
2613 RecordData VTableUses;
2614 if (!SemaRef.VTableUses.empty()) {
2615 VTableUses.push_back(SemaRef.VTableUses.size());
2616 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2617 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2618 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2619 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2620 }
2621 }
2622
2623 // Build a record containing all of dynamic classes declarations.
2624 RecordData DynamicClasses;
2625 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2626 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
2627 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2628
2629 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002630 RecordData PendingInstantiations;
Sebastian Redl40566802010-08-05 18:21:25 +00002631 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00002632 I = SemaRef.PendingInstantiations.begin(),
2633 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl40566802010-08-05 18:21:25 +00002634 if (I->first->getPCHLevel() == 0) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002635 AddDeclRef(I->first, PendingInstantiations);
2636 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl40566802010-08-05 18:21:25 +00002637 }
2638 }
2639 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2640 "There are local ones at end of translation unit!");
2641
2642 // Build a record containing some declaration references.
2643 // It's not worth the effort to avoid duplication here.
2644 RecordData SemaDeclRefs;
2645 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2646 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2647 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2648 }
2649
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002650 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002651 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00002652 for (DeclsToRewriteTy::iterator
2653 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
2654 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002655 while (!DeclTypesToEmit.empty()) {
2656 DeclOrType DOT = DeclTypesToEmit.front();
2657 DeclTypesToEmit.pop();
2658 if (DOT.isType())
2659 WriteType(DOT.getType());
2660 else
2661 WriteDecl(Context, DOT.getDecl());
2662 }
2663 Stream.ExitBlock();
2664
Sebastian Redl083abdf2010-07-27 23:01:28 +00002665 WritePreprocessor(PP);
Sebastian Redla68340f2010-08-04 22:21:29 +00002666 WriteSelectors(SemaRef);
2667 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002668 WriteIdentifierTable(PP);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002669 WriteTypeDeclOffsets();
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002670 // FIXME: For chained PCH only write the new mappings (we currently
2671 // write all of them again).
2672 WriteUserDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl083abdf2010-07-27 23:01:28 +00002673
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002674 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl3397c552010-08-18 23:56:27 +00002675 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002676 RecordData FirstLatestDeclIDs;
2677 for (FirstLatestDeclMap::iterator
2678 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
2679 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
2680 "Expected first & second to be in different PCHs");
2681 AddDeclRef(I->first, FirstLatestDeclIDs);
2682 AddDeclRef(I->second, FirstLatestDeclIDs);
2683 }
2684 if (!FirstLatestDeclIDs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002685 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002686
Sebastian Redl083abdf2010-07-27 23:01:28 +00002687 // Write the record containing external, unnamed definitions.
2688 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002689 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002690
2691 // Write the record containing tentative definitions.
2692 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002693 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002694
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002695 // Write the record containing unused file scoped decls.
2696 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002697 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002698
Sebastian Redl40566802010-08-05 18:21:25 +00002699 // Write the record containing weak undeclared identifiers.
2700 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002701 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl40566802010-08-05 18:21:25 +00002702 WeakUndeclaredIdentifiers);
2703
Sebastian Redl083abdf2010-07-27 23:01:28 +00002704 // Write the record containing locally-scoped external definitions.
2705 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002706 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl083abdf2010-07-27 23:01:28 +00002707 LocallyScopedExternalDecls);
2708
2709 // Write the record containing ext_vector type names.
2710 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002711 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002712
Sebastian Redl40566802010-08-05 18:21:25 +00002713 // Write the record containing VTable uses information.
2714 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002715 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl40566802010-08-05 18:21:25 +00002716
2717 // Write the record containing dynamic classes declarations.
2718 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002719 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl40566802010-08-05 18:21:25 +00002720
2721 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002722 if (!PendingInstantiations.empty())
2723 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl40566802010-08-05 18:21:25 +00002724
2725 // Write the record containing declaration references of Sema.
2726 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002727 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002728
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00002729 // Write the updates to DeclContexts.
2730 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
2731 I = UpdatedDeclContexts.begin(),
2732 E = UpdatedDeclContexts.end();
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002733 I != E; ++I)
2734 WriteDeclContextVisibleUpdate(*I);
2735
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00002736 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002737
Sebastian Redl083abdf2010-07-27 23:01:28 +00002738 Record.clear();
2739 Record.push_back(NumStatements);
2740 Record.push_back(NumMacros);
2741 Record.push_back(NumLexicalDeclContexts);
2742 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00002743 WriteDeclReplacementsBlock();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002744 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002745 Stream.ExitBlock();
2746}
2747
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00002748void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002749 if (DeclUpdates.empty())
2750 return;
2751
2752 RecordData OffsetsRecord;
2753 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
2754 for (DeclUpdateMap::iterator
2755 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
2756 const Decl *D = I->first;
2757 UpdateRecord &URec = I->second;
2758
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00002759 if (DeclsToRewrite.count(D))
2760 continue; // The decl will be written completely,no need to store updates.
2761
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002762 uint64_t Offset = Stream.GetCurrentBitNo();
2763 Stream.EmitRecord(DECL_UPDATES, URec);
2764
2765 OffsetsRecord.push_back(GetDeclRef(D));
2766 OffsetsRecord.push_back(Offset);
2767 }
2768 Stream.ExitBlock();
2769 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
2770}
2771
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00002772void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00002773 if (ReplacedDecls.empty())
2774 return;
2775
2776 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002777 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00002778 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
2779 Record.push_back(I->first);
2780 Record.push_back(I->second);
2781 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002782 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00002783}
2784
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002785void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002786 Record.push_back(Loc.getRawEncoding());
2787}
2788
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002789void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002790 AddSourceLocation(Range.getBegin(), Record);
2791 AddSourceLocation(Range.getEnd(), Record);
2792}
2793
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002794void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002795 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00002796 const uint64_t *Words = Value.getRawData();
2797 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002798}
2799
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002800void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002801 Record.push_back(Value.isUnsigned());
2802 AddAPInt(Value, Record);
2803}
2804
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002805void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00002806 AddAPInt(Value.bitcastToAPInt(), Record);
2807}
2808
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002809void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002810 Record.push_back(getIdentifierRef(II));
2811}
2812
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002813IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002814 if (II == 0)
2815 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002816
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002817 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00002818 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002819 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00002820 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002821}
2822
Sebastian Redlf73c93f2010-09-15 19:54:06 +00002823MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002824 if (MD == 0)
2825 return 0;
Sebastian Redlf73c93f2010-09-15 19:54:06 +00002826
2827 MacroID &ID = MacroDefinitions[MD];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002828 if (ID == 0)
Douglas Gregor77424bc2010-10-02 19:29:26 +00002829 ID = NextMacroID++;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002830 return ID;
2831}
2832
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002833void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00002834 Record.push_back(getSelectorRef(SelRef));
2835}
2836
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002837SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00002838 if (Sel.getAsOpaquePtr() == 0) {
2839 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002840 }
2841
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002842 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00002843 if (SID == 0 && Chain) {
2844 // This might trigger a ReadSelector callback, which will set the ID for
2845 // this selector.
2846 Chain->LoadSelector(Sel);
2847 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002848 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00002849 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002850 }
Sebastian Redl5d050072010-08-04 17:20:04 +00002851 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002852}
2853
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002854void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00002855 AddDeclRef(Temp->getDestructor(), Record);
2856}
2857
Douglas Gregor7c789c12010-10-29 22:39:52 +00002858void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
2859 CXXBaseSpecifier const *BasesEnd,
2860 RecordDataImpl &Record) {
2861 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
2862 CXXBaseSpecifiersToWrite.push_back(
2863 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
2864 Bases, BasesEnd));
2865 Record.push_back(NextCXXBaseSpecifiersID++);
2866}
2867
Sebastian Redla4232eb2010-08-18 23:56:21 +00002868void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002869 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002870 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002871 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00002872 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002873 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00002874 break;
2875 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002876 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00002877 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00002878 case TemplateArgument::Template:
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002879 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2880 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00002881 break;
John McCall833ca992009-10-29 08:12:44 +00002882 case TemplateArgument::Null:
2883 case TemplateArgument::Integral:
2884 case TemplateArgument::Declaration:
2885 case TemplateArgument::Pack:
2886 break;
2887 }
2888}
2889
Sebastian Redla4232eb2010-08-18 23:56:21 +00002890void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002891 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002892 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002893
2894 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
2895 bool InfoHasSameExpr
2896 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
2897 Record.push_back(InfoHasSameExpr);
2898 if (InfoHasSameExpr)
2899 return; // Avoid storing the same expr twice.
2900 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002901 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
2902 Record);
2903}
2904
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002905void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00002906 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00002907 AddTypeRef(QualType(), Record);
2908 return;
2909 }
2910
John McCalla93c9342009-12-07 02:54:59 +00002911 AddTypeRef(TInfo->getType(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +00002912 TypeLocWriter TLW(*this, Record);
John McCalla93c9342009-12-07 02:54:59 +00002913 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002914 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00002915}
2916
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002917void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00002918 Record.push_back(GetOrCreateTypeID(T));
2919}
2920
2921TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00002922 return MakeTypeID(T,
2923 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
2924}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002925
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002926TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00002927 return MakeTypeID(T,
2928 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00002929}
2930
2931TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
2932 if (T.isNull())
2933 return TypeIdx();
2934 assert(!T.getLocalFastQualifiers());
2935
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002936 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002937 if (Idx.getIndex() == 0) {
Douglas Gregor366809a2009-04-26 03:49:13 +00002938 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00002939 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002940 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002941 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00002942 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00002943 return Idx;
2944}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002945
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002946TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00002947 if (T.isNull())
2948 return TypeIdx();
2949 assert(!T.getLocalFastQualifiers());
2950
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002951 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
2952 assert(I != TypeIdxs.end() && "Type not emitted!");
2953 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002954}
2955
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002956void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00002957 Record.push_back(GetDeclRef(D));
2958}
2959
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002960DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002961 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00002962 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002963 }
Douglas Gregor97475832010-10-05 18:37:06 +00002964 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002965 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00002966 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002967 // We haven't seen this declaration before. Give it a new ID and
2968 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002969 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002970 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redl0b17c612010-08-13 00:28:03 +00002971 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
2972 // We don't add it to the replacement collection here, because we don't
2973 // have the offset yet.
2974 DeclTypesToEmit.push(const_cast<Decl *>(D));
2975 // Reset the flag, so that we don't add this decl multiple times.
2976 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002977 }
2978
Sebastian Redl681d7232010-07-27 00:17:23 +00002979 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002980}
2981
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002982DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002983 if (D == 0)
2984 return 0;
2985
2986 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2987 return DeclIDs[D];
2988}
2989
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002990void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00002991 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002992 Record.push_back(Name.getNameKind());
2993 switch (Name.getNameKind()) {
2994 case DeclarationName::Identifier:
2995 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2996 break;
2997
2998 case DeclarationName::ObjCZeroArgSelector:
2999 case DeclarationName::ObjCOneArgSelector:
3000 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003001 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003002 break;
3003
3004 case DeclarationName::CXXConstructorName:
3005 case DeclarationName::CXXDestructorName:
3006 case DeclarationName::CXXConversionFunctionName:
3007 AddTypeRef(Name.getCXXNameType(), Record);
3008 break;
3009
3010 case DeclarationName::CXXOperatorName:
3011 Record.push_back(Name.getCXXOverloadedOperator());
3012 break;
3013
Sean Hunt3e518bd2009-11-29 07:34:05 +00003014 case DeclarationName::CXXLiteralOperatorName:
3015 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3016 break;
3017
Douglas Gregor2cf26342009-04-09 22:27:44 +00003018 case DeclarationName::CXXUsingDirective:
3019 // No extra data to emit
3020 break;
3021 }
3022}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003023
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003024void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003025 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003026 switch (Name.getNameKind()) {
3027 case DeclarationName::CXXConstructorName:
3028 case DeclarationName::CXXDestructorName:
3029 case DeclarationName::CXXConversionFunctionName:
3030 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3031 break;
3032
3033 case DeclarationName::CXXOperatorName:
3034 AddSourceLocation(
3035 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3036 Record);
3037 AddSourceLocation(
3038 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3039 Record);
3040 break;
3041
3042 case DeclarationName::CXXLiteralOperatorName:
3043 AddSourceLocation(
3044 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3045 Record);
3046 break;
3047
3048 case DeclarationName::Identifier:
3049 case DeclarationName::ObjCZeroArgSelector:
3050 case DeclarationName::ObjCOneArgSelector:
3051 case DeclarationName::ObjCMultiArgSelector:
3052 case DeclarationName::CXXUsingDirective:
3053 break;
3054 }
3055}
3056
3057void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003058 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003059 AddDeclarationName(NameInfo.getName(), Record);
3060 AddSourceLocation(NameInfo.getLoc(), Record);
3061 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3062}
3063
3064void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003065 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003066 AddNestedNameSpecifier(Info.NNS, Record);
3067 AddSourceRange(Info.NNSRange, Record);
3068 Record.push_back(Info.NumTemplParamLists);
3069 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3070 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3071}
3072
Sebastian Redla4232eb2010-08-18 23:56:21 +00003073void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003074 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003075 // Nested name specifiers usually aren't too long. I think that 8 would
3076 // typically accomodate the vast majority.
3077 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3078
3079 // Push each of the NNS's onto a stack for serialization in reverse order.
3080 while (NNS) {
3081 NestedNames.push_back(NNS);
3082 NNS = NNS->getPrefix();
3083 }
3084
3085 Record.push_back(NestedNames.size());
3086 while(!NestedNames.empty()) {
3087 NNS = NestedNames.pop_back_val();
3088 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3089 Record.push_back(Kind);
3090 switch (Kind) {
3091 case NestedNameSpecifier::Identifier:
3092 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3093 break;
3094
3095 case NestedNameSpecifier::Namespace:
3096 AddDeclRef(NNS->getAsNamespace(), Record);
3097 break;
3098
3099 case NestedNameSpecifier::TypeSpec:
3100 case NestedNameSpecifier::TypeSpecWithTemplate:
3101 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3102 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3103 break;
3104
3105 case NestedNameSpecifier::Global:
3106 // Don't need to write an associated value.
3107 break;
3108 }
3109 }
3110}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003111
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003112void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00003113 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003114 Record.push_back(Kind);
3115 switch (Kind) {
3116 case TemplateName::Template:
3117 AddDeclRef(Name.getAsTemplateDecl(), Record);
3118 break;
3119
3120 case TemplateName::OverloadedTemplate: {
3121 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3122 Record.push_back(OvT->size());
3123 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3124 I != E; ++I)
3125 AddDeclRef(*I, Record);
3126 break;
3127 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003128
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003129 case TemplateName::QualifiedTemplate: {
3130 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3131 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3132 Record.push_back(QualT->hasTemplateKeyword());
3133 AddDeclRef(QualT->getTemplateDecl(), Record);
3134 break;
3135 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003136
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003137 case TemplateName::DependentTemplate: {
3138 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3139 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3140 Record.push_back(DepT->isIdentifier());
3141 if (DepT->isIdentifier())
3142 AddIdentifierRef(DepT->getIdentifier(), Record);
3143 else
3144 Record.push_back(DepT->getOperator());
3145 break;
3146 }
3147 }
3148}
3149
Michael J. Spencer20249a12010-10-21 03:16:25 +00003150void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003151 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003152 Record.push_back(Arg.getKind());
3153 switch (Arg.getKind()) {
3154 case TemplateArgument::Null:
3155 break;
3156 case TemplateArgument::Type:
3157 AddTypeRef(Arg.getAsType(), Record);
3158 break;
3159 case TemplateArgument::Declaration:
3160 AddDeclRef(Arg.getAsDecl(), Record);
3161 break;
3162 case TemplateArgument::Integral:
3163 AddAPSInt(*Arg.getAsIntegral(), Record);
3164 AddTypeRef(Arg.getIntegralType(), Record);
3165 break;
3166 case TemplateArgument::Template:
3167 AddTemplateName(Arg.getAsTemplate(), Record);
3168 break;
3169 case TemplateArgument::Expression:
3170 AddStmt(Arg.getAsExpr());
3171 break;
3172 case TemplateArgument::Pack:
3173 Record.push_back(Arg.pack_size());
3174 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3175 I != E; ++I)
3176 AddTemplateArgument(*I, Record);
3177 break;
3178 }
3179}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003180
3181void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003182ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003183 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003184 assert(TemplateParams && "No TemplateParams!");
3185 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3186 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3187 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3188 Record.push_back(TemplateParams->size());
3189 for (TemplateParameterList::const_iterator
3190 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3191 P != PEnd; ++P)
3192 AddDeclRef(*P, Record);
3193}
3194
3195/// \brief Emit a template argument list.
3196void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003197ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003198 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003199 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00003200 Record.push_back(TemplateArgs->size());
3201 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003202 AddTemplateArgument(TemplateArgs->get(i), Record);
3203}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003204
3205
3206void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003207ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003208 Record.push_back(Set.size());
3209 for (UnresolvedSetImpl::const_iterator
3210 I = Set.begin(), E = Set.end(); I != E; ++I) {
3211 AddDeclRef(I.getDecl(), Record);
3212 Record.push_back(I.getAccess());
3213 }
3214}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003215
Sebastian Redla4232eb2010-08-18 23:56:21 +00003216void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003217 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003218 Record.push_back(Base.isVirtual());
3219 Record.push_back(Base.isBaseOfClass());
3220 Record.push_back(Base.getAccessSpecifierAsWritten());
Nick Lewycky56062202010-07-26 16:56:01 +00003221 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003222 AddSourceRange(Base.getSourceRange(), Record);
3223}
Sebastian Redl30c514c2010-07-14 23:45:08 +00003224
Douglas Gregor7c789c12010-10-29 22:39:52 +00003225void ASTWriter::FlushCXXBaseSpecifiers() {
3226 RecordData Record;
3227 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3228 Record.clear();
3229
3230 // Record the offset of this base-specifier set.
3231 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3232 if (Index == CXXBaseSpecifiersOffsets.size())
3233 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3234 else {
3235 if (Index > CXXBaseSpecifiersOffsets.size())
3236 CXXBaseSpecifiersOffsets.resize(Index + 1);
3237 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3238 }
3239
3240 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3241 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3242 Record.push_back(BEnd - B);
3243 for (; B != BEnd; ++B)
3244 AddCXXBaseSpecifier(*B, Record);
3245 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00003246
3247 // Flush any expressions that were written as part of the base specifiers.
3248 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003249 }
3250
3251 CXXBaseSpecifiersToWrite.clear();
3252}
3253
Sebastian Redla4232eb2010-08-18 23:56:21 +00003254void ASTWriter::AddCXXBaseOrMemberInitializers(
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003255 const CXXBaseOrMemberInitializer * const *BaseOrMembers,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003256 unsigned NumBaseOrMembers, RecordDataImpl &Record) {
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003257 Record.push_back(NumBaseOrMembers);
3258 for (unsigned i=0; i != NumBaseOrMembers; ++i) {
3259 const CXXBaseOrMemberInitializer *Init = BaseOrMembers[i];
3260
3261 Record.push_back(Init->isBaseInitializer());
3262 if (Init->isBaseInitializer()) {
3263 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3264 Record.push_back(Init->isBaseVirtual());
3265 } else {
Francois Pichet00eb3f92010-12-04 09:14:42 +00003266 Record.push_back(Init->isIndirectMemberInitializer());
3267 if (Init->isIndirectMemberInitializer())
3268 AddDeclRef(Init->getIndirectMember(), Record);
3269 else
3270 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003271 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00003272
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003273 AddSourceLocation(Init->getMemberLocation(), Record);
3274 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003275 AddSourceLocation(Init->getLParenLoc(), Record);
3276 AddSourceLocation(Init->getRParenLoc(), Record);
3277 Record.push_back(Init->isWritten());
3278 if (Init->isWritten()) {
3279 Record.push_back(Init->getSourceOrder());
3280 } else {
3281 Record.push_back(Init->getNumArrayIndices());
3282 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3283 AddDeclRef(Init->getArrayIndex(i), Record);
3284 }
3285 }
3286}
3287
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003288void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3289 assert(D->DefinitionData);
3290 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3291 Record.push_back(Data.UserDeclaredConstructor);
3292 Record.push_back(Data.UserDeclaredCopyConstructor);
3293 Record.push_back(Data.UserDeclaredCopyAssignment);
3294 Record.push_back(Data.UserDeclaredDestructor);
3295 Record.push_back(Data.Aggregate);
3296 Record.push_back(Data.PlainOldData);
3297 Record.push_back(Data.Empty);
3298 Record.push_back(Data.Polymorphic);
3299 Record.push_back(Data.Abstract);
3300 Record.push_back(Data.HasTrivialConstructor);
3301 Record.push_back(Data.HasTrivialCopyConstructor);
3302 Record.push_back(Data.HasTrivialCopyAssignment);
3303 Record.push_back(Data.HasTrivialDestructor);
3304 Record.push_back(Data.ComputedVisibleConversions);
3305 Record.push_back(Data.DeclaredDefaultConstructor);
3306 Record.push_back(Data.DeclaredCopyConstructor);
3307 Record.push_back(Data.DeclaredCopyAssignment);
3308 Record.push_back(Data.DeclaredDestructor);
3309
3310 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003311 if (Data.NumBases > 0)
3312 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3313 Record);
3314
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003315 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3316 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003317 if (Data.NumVBases > 0)
3318 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3319 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003320
3321 AddUnresolvedSet(Data.Conversions, Record);
3322 AddUnresolvedSet(Data.VisibleConversions, Record);
3323 // Data.Definition is the owning decl, no need to write it.
3324 AddDeclRef(Data.FirstFriend, Record);
3325}
3326
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003327void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003328 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003329 assert(!Chain && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003330 assert(FirstDeclID == NextDeclID &&
3331 FirstTypeID == NextTypeID &&
3332 FirstIdentID == NextIdentID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00003333 FirstSelectorID == NextSelectorID &&
Douglas Gregor77424bc2010-10-02 19:29:26 +00003334 FirstMacroID == NextMacroID &&
Douglas Gregor7c789c12010-10-29 22:39:52 +00003335 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003336 "Setting chain after writing has started.");
3337 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003338
3339 FirstDeclID += Chain->getTotalNumDecls();
3340 FirstTypeID += Chain->getTotalNumTypes();
3341 FirstIdentID += Chain->getTotalNumIdentifiers();
3342 FirstSelectorID += Chain->getTotalNumSelectors();
3343 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003344 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003345 NextDeclID = FirstDeclID;
3346 NextTypeID = FirstTypeID;
3347 NextIdentID = FirstIdentID;
3348 NextSelectorID = FirstSelectorID;
3349 NextMacroID = FirstMacroID;
Douglas Gregor7c789c12010-10-29 22:39:52 +00003350 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003351}
3352
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003353void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003354 IdentifierIDs[II] = ID;
3355}
3356
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003357void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00003358 // Always take the highest-numbered type index. This copes with an interesting
3359 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00003360 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00003361 // keep the higher-numbered entry so that we can properly write it out to
3362 // the AST file.
3363 TypeIdx &StoredIdx = TypeIdxs[T];
3364 if (Idx.getIndex() >= StoredIdx.getIndex())
3365 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003366}
3367
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003368void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1476ed42010-07-16 16:36:56 +00003369 DeclIDs[D] = ID;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003370}
Sebastian Redl5d050072010-08-04 17:20:04 +00003371
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003372void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003373 SelectorIDs[S] = ID;
3374}
Douglas Gregor77424bc2010-10-02 19:29:26 +00003375
Michael J. Spencer20249a12010-10-21 03:16:25 +00003376void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00003377 MacroDefinition *MD) {
3378 MacroDefinitions[MD] = ID;
3379}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003380
3381void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3382 assert(D->isDefinition());
3383 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3384 // We are interested when a PCH decl is modified.
3385 if (RD->getPCHLevel() > 0) {
3386 // A forward reference was mutated into a definition. Rewrite it.
3387 // FIXME: This happens during template instantiation, should we
3388 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00003389 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003390 }
3391
3392 for (CXXRecordDecl::redecl_iterator
3393 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3394 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3395 if (Redecl == RD)
3396 continue;
3397
3398 // We are interested when a PCH decl is modified.
3399 if (Redecl->getPCHLevel() > 0) {
3400 UpdateRecord &Record = DeclUpdates[Redecl];
3401 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3402 assert(Redecl->DefinitionData);
3403 assert(Redecl->DefinitionData->Definition == D);
3404 AddDeclRef(D, Record); // the DefinitionDecl
3405 }
3406 }
3407 }
3408}
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00003409void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3410 // TU and namespaces are handled elsewhere.
3411 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3412 return;
3413
3414 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3415 return; // Not a source decl added to a DeclContext from PCH.
3416
3417 AddUpdatedDeclContext(DC);
3418}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00003419
3420void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3421 assert(D->isImplicit());
3422 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3423 return; // Not a source member added to a class from PCH.
3424 if (!isa<CXXMethodDecl>(D))
3425 return; // We are interested in lazily declared implicit methods.
3426
3427 // A decl coming from PCH was modified.
3428 assert(RD->isDefinition());
3429 UpdateRecord &Record = DeclUpdates[RD];
3430 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3431 AddDeclRef(D, Record);
3432}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00003433
3434void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3435 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00003436 // The specializations set is kept in the canonical template.
3437 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00003438 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3439 return; // Not a source specialization added to a template from PCH.
3440
3441 UpdateRecord &Record = DeclUpdates[TD];
3442 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3443 AddDeclRef(D, Record);
3444}
Douglas Gregor89d99802010-11-30 06:16:57 +00003445
3446ASTSerializationListener::~ASTSerializationListener() { }