blob: 1e296e85143fdd65b902a596013a62437312d845 [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"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000044#include "llvm/Support/FileSystem.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000045#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000046#include "llvm/Support/Path.h"
Chris Lattner3c304bd2009-04-11 18:40:46 +000047#include <cstdio>
Douglas Gregor2cf26342009-04-09 22:27:44 +000048using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000049using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000050
Sebastian Redlade50002010-07-30 17:03:48 +000051template <typename T, typename Allocator>
52T *data(std::vector<T, Allocator> &v) {
53 return v.empty() ? 0 : &v.front();
54}
55template <typename T, typename Allocator>
56const T *data(const std::vector<T, Allocator> &v) {
57 return v.empty() ? 0 : &v.front();
58}
59
Douglas Gregor2cf26342009-04-09 22:27:44 +000060//===----------------------------------------------------------------------===//
61// Type serialization
62//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000063
Douglas Gregor2cf26342009-04-09 22:27:44 +000064namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000065 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000066 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000067 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000068
69 public:
70 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000071 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000072
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000073 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000074 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000075
76 void VisitArrayType(const ArrayType *T);
77 void VisitFunctionType(const FunctionType *T);
78 void VisitTagType(const TagType *T);
79
80#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
81#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000082#include "clang/AST/TypeNodes.def"
83 };
84}
85
Sebastian Redl3397c552010-08-18 23:56:27 +000086void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000087 assert(false && "Built-in types are never serialized");
88}
89
Sebastian Redl3397c552010-08-18 23:56:27 +000090void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000091 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +000092 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +000093}
94
Sebastian Redl3397c552010-08-18 23:56:27 +000095void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000096 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +000097 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +000098}
99
Sebastian Redl3397c552010-08-18 23:56:27 +0000100void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000101 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000102 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000103}
104
Sebastian Redl3397c552010-08-18 23:56:27 +0000105void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000106 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000107 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000108}
109
Sebastian Redl3397c552010-08-18 23:56:27 +0000110void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000111 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000112 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000113}
114
Sebastian Redl3397c552010-08-18 23:56:27 +0000115void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000116 Writer.AddTypeRef(T->getPointeeType(), Record);
117 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000118 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000119}
120
Sebastian Redl3397c552010-08-18 23:56:27 +0000121void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000122 Writer.AddTypeRef(T->getElementType(), Record);
123 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000124 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000125}
126
Sebastian Redl3397c552010-08-18 23:56:27 +0000127void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000128 VisitArrayType(T);
129 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000130 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000131}
132
Sebastian Redl3397c552010-08-18 23:56:27 +0000133void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000134 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000135 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000136}
137
Sebastian Redl3397c552010-08-18 23:56:27 +0000138void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000139 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000140 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
141 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000142 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000143 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000144}
145
Sebastian Redl3397c552010-08-18 23:56:27 +0000146void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000147 Writer.AddTypeRef(T->getElementType(), Record);
148 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000149 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000150 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000151}
152
Sebastian Redl3397c552010-08-18 23:56:27 +0000153void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000154 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000155 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000156}
157
Sebastian Redl3397c552010-08-18 23:56:27 +0000158void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000159 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000160 FunctionType::ExtInfo C = T->getExtInfo();
161 Record.push_back(C.getNoReturn());
Rafael Espindola425ef722010-03-30 22:15:11 +0000162 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000163 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000164 Record.push_back(C.getCC());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000165}
166
Sebastian Redl3397c552010-08-18 23:56:27 +0000167void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000168 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000169 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000170}
171
Sebastian Redl3397c552010-08-18 23:56:27 +0000172void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000173 VisitFunctionType(T);
174 Record.push_back(T->getNumArgs());
175 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
176 Writer.AddTypeRef(T->getArgType(I), Record);
177 Record.push_back(T->isVariadic());
178 Record.push_back(T->getTypeQuals());
Sebastian Redl465226e2009-05-27 22:11:52 +0000179 Record.push_back(T->hasExceptionSpec());
180 Record.push_back(T->hasAnyExceptionSpec());
181 Record.push_back(T->getNumExceptions());
182 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
183 Writer.AddTypeRef(T->getExceptionType(I), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000184 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000185}
186
Sebastian Redl3397c552010-08-18 23:56:27 +0000187void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000188 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000189 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000190}
John McCalled976492009-12-04 22:46:56 +0000191
Sebastian Redl3397c552010-08-18 23:56:27 +0000192void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000193 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000194 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
195 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000196 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000197}
198
Sebastian Redl3397c552010-08-18 23:56:27 +0000199void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000200 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000201 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000202}
203
Sebastian Redl3397c552010-08-18 23:56:27 +0000204void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000205 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000206 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000207}
208
Sebastian Redl3397c552010-08-18 23:56:27 +0000209void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson395b4752009-06-24 19:06:50 +0000210 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000211 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000212}
213
Sebastian Redl3397c552010-08-18 23:56:27 +0000214void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000215 Record.push_back(T->isDependentType());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000216 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000217 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000218 "Cannot serialize in the middle of a type definition");
219}
220
Sebastian Redl3397c552010-08-18 23:56:27 +0000221void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000222 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000223 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000224}
225
Sebastian Redl3397c552010-08-18 23:56:27 +0000226void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000227 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000228 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000229}
230
John McCall9d156a72011-01-06 01:58:22 +0000231void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
232 Writer.AddTypeRef(T->getModifiedType(), Record);
233 Writer.AddTypeRef(T->getEquivalentType(), Record);
234 Record.push_back(T->getAttrKind());
235 Code = TYPE_ATTRIBUTED;
236}
237
Mike Stump1eb44332009-09-09 15:08:12 +0000238void
Sebastian Redl3397c552010-08-18 23:56:27 +0000239ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000240 const SubstTemplateTypeParmType *T) {
241 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
242 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000243 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000244}
245
246void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000247ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
248 const SubstTemplateTypeParmPackType *T) {
249 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
250 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
251 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
252}
253
254void
Sebastian Redl3397c552010-08-18 23:56:27 +0000255ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000256 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000257 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000258 Writer.AddTemplateName(T->getTemplateName(), Record);
259 Record.push_back(T->getNumArgs());
260 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
261 ArgI != ArgE; ++ArgI)
262 Writer.AddTemplateArgument(*ArgI, Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000263 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
264 : T->getCanonicalTypeInternal(),
265 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000266 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000267}
268
269void
Sebastian Redl3397c552010-08-18 23:56:27 +0000270ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000271 VisitArrayType(T);
272 Writer.AddStmt(T->getSizeExpr());
273 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000274 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000275}
276
277void
Sebastian Redl3397c552010-08-18 23:56:27 +0000278ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000279 const DependentSizedExtVectorType *T) {
280 // FIXME: Serialize this type (C++ only)
281 assert(false && "Cannot serialize dependent sized extended vector types");
282}
283
284void
Sebastian Redl3397c552010-08-18 23:56:27 +0000285ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000286 Record.push_back(T->getDepth());
287 Record.push_back(T->getIndex());
288 Record.push_back(T->isParameterPack());
289 Writer.AddIdentifierRef(T->getName(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000290 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000291}
292
293void
Sebastian Redl3397c552010-08-18 23:56:27 +0000294ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000295 Record.push_back(T->getKeyword());
296 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
297 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000298 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
299 : T->getCanonicalTypeInternal(),
300 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000301 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000302}
303
304void
Sebastian Redl3397c552010-08-18 23:56:27 +0000305ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000306 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000307 Record.push_back(T->getKeyword());
308 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
309 Writer.AddIdentifierRef(T->getIdentifier(), Record);
310 Record.push_back(T->getNumArgs());
311 for (DependentTemplateSpecializationType::iterator
312 I = T->begin(), E = T->end(); I != E; ++I)
313 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000314 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000315}
316
Douglas Gregor7536dd52010-12-20 02:24:11 +0000317void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
318 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregorcded4f62011-01-14 17:04:44 +0000319 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
320 Record.push_back(*NumExpansions + 1);
321 else
322 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000323 Code = TYPE_PACK_EXPANSION;
324}
325
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000326void ASTTypeWriter::VisitParenType(const ParenType *T) {
327 Writer.AddTypeRef(T->getInnerType(), Record);
328 Code = TYPE_PAREN;
329}
330
Sebastian Redl3397c552010-08-18 23:56:27 +0000331void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000332 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000333 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
334 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000335 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000336}
337
Sebastian Redl3397c552010-08-18 23:56:27 +0000338void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCall3cb0ebd2010-03-10 03:28:59 +0000339 Writer.AddDeclRef(T->getDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000340 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000341 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000342}
343
Sebastian Redl3397c552010-08-18 23:56:27 +0000344void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000345 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000346 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000347}
348
Sebastian Redl3397c552010-08-18 23:56:27 +0000349void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000350 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000351 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000352 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000353 E = T->qual_end(); I != E; ++I)
354 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000355 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000356}
357
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000358void
Sebastian Redl3397c552010-08-18 23:56:27 +0000359ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000360 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000361 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000362}
363
John McCalla1ee0c52009-10-16 21:56:05 +0000364namespace {
365
366class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000367 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000368 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000369
370public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000371 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000372 : Writer(Writer), Record(Record) { }
373
John McCall51bd8032009-10-18 01:05:36 +0000374#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000375#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000376 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000377#include "clang/AST/TypeLocNodes.def"
378
John McCall51bd8032009-10-18 01:05:36 +0000379 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
380 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000381};
382
383}
384
John McCall51bd8032009-10-18 01:05:36 +0000385void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
386 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000387}
John McCall51bd8032009-10-18 01:05:36 +0000388void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000389 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
390 if (TL.needsExtraLocalData()) {
391 Record.push_back(TL.getWrittenTypeSpec());
392 Record.push_back(TL.getWrittenSignSpec());
393 Record.push_back(TL.getWrittenWidthSpec());
394 Record.push_back(TL.hasModeAttr());
395 }
John McCalla1ee0c52009-10-16 21:56:05 +0000396}
John McCall51bd8032009-10-18 01:05:36 +0000397void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
398 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000399}
John McCall51bd8032009-10-18 01:05:36 +0000400void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
401 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000402}
John McCall51bd8032009-10-18 01:05:36 +0000403void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
404 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000405}
John McCall51bd8032009-10-18 01:05:36 +0000406void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
407 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000408}
John McCall51bd8032009-10-18 01:05:36 +0000409void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
410 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000411}
John McCall51bd8032009-10-18 01:05:36 +0000412void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
413 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000414}
John McCall51bd8032009-10-18 01:05:36 +0000415void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
416 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
417 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
418 Record.push_back(TL.getSizeExpr() ? 1 : 0);
419 if (TL.getSizeExpr())
420 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000421}
John McCall51bd8032009-10-18 01:05:36 +0000422void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
423 VisitArrayTypeLoc(TL);
424}
425void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
426 VisitArrayTypeLoc(TL);
427}
428void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
429 VisitArrayTypeLoc(TL);
430}
431void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
432 DependentSizedArrayTypeLoc TL) {
433 VisitArrayTypeLoc(TL);
434}
435void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
436 DependentSizedExtVectorTypeLoc TL) {
437 Writer.AddSourceLocation(TL.getNameLoc(), Record);
438}
439void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
440 Writer.AddSourceLocation(TL.getNameLoc(), Record);
441}
442void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
443 Writer.AddSourceLocation(TL.getNameLoc(), Record);
444}
445void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
446 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
447 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Douglas Gregordab60ad2010-10-01 18:44:50 +0000448 Record.push_back(TL.getTrailingReturn());
John McCall51bd8032009-10-18 01:05:36 +0000449 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
450 Writer.AddDeclRef(TL.getArg(i), Record);
451}
452void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
453 VisitFunctionTypeLoc(TL);
454}
455void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
456 VisitFunctionTypeLoc(TL);
457}
John McCalled976492009-12-04 22:46:56 +0000458void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
459 Writer.AddSourceLocation(TL.getNameLoc(), Record);
460}
John McCall51bd8032009-10-18 01:05:36 +0000461void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
462 Writer.AddSourceLocation(TL.getNameLoc(), Record);
463}
464void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000465 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
466 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
467 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000468}
469void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000470 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
471 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
472 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
473 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000474}
475void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
476 Writer.AddSourceLocation(TL.getNameLoc(), Record);
477}
478void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
479 Writer.AddSourceLocation(TL.getNameLoc(), Record);
480}
481void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
482 Writer.AddSourceLocation(TL.getNameLoc(), Record);
483}
John McCall9d156a72011-01-06 01:58:22 +0000484void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
485 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
486 if (TL.hasAttrOperand()) {
487 SourceRange range = TL.getAttrOperandParensRange();
488 Writer.AddSourceLocation(range.getBegin(), Record);
489 Writer.AddSourceLocation(range.getEnd(), Record);
490 }
491 if (TL.hasAttrExprOperand()) {
492 Expr *operand = TL.getAttrExprOperand();
493 Record.push_back(operand ? 1 : 0);
494 if (operand) Writer.AddStmt(operand);
495 } else if (TL.hasAttrEnumOperand()) {
496 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
497 }
498}
John McCall51bd8032009-10-18 01:05:36 +0000499void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
500 Writer.AddSourceLocation(TL.getNameLoc(), Record);
501}
John McCall49a832b2009-10-18 09:09:24 +0000502void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
503 SubstTemplateTypeParmTypeLoc TL) {
504 Writer.AddSourceLocation(TL.getNameLoc(), Record);
505}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000506void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
507 SubstTemplateTypeParmPackTypeLoc TL) {
508 Writer.AddSourceLocation(TL.getNameLoc(), Record);
509}
John McCall51bd8032009-10-18 01:05:36 +0000510void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
511 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000512 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
513 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
514 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
515 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000516 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
517 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000518}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000519void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
520 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
521 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
522}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000523void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000524 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
525 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000526}
John McCall3cb0ebd2010-03-10 03:28:59 +0000527void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
528 Writer.AddSourceLocation(TL.getNameLoc(), Record);
529}
Douglas Gregor4714c122010-03-31 17:34:00 +0000530void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000531 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
532 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000533 Writer.AddSourceLocation(TL.getNameLoc(), Record);
534}
John McCall33500952010-06-11 00:33:02 +0000535void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
536 DependentTemplateSpecializationTypeLoc TL) {
537 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
538 Writer.AddSourceRange(TL.getQualifierRange(), Record);
539 Writer.AddSourceLocation(TL.getNameLoc(), Record);
540 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
541 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
542 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000543 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
544 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000545}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000546void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
547 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
548}
John McCall51bd8032009-10-18 01:05:36 +0000549void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
550 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000551}
552void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
553 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000554 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
555 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
556 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
557 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000558}
John McCall54e14c42009-10-22 22:37:11 +0000559void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
560 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000561}
John McCalla1ee0c52009-10-16 21:56:05 +0000562
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000563//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000564// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000565//===----------------------------------------------------------------------===//
566
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000567static void EmitBlockID(unsigned ID, const char *Name,
568 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000569 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000570 Record.clear();
571 Record.push_back(ID);
572 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
573
574 // Emit the block name if present.
575 if (Name == 0 || Name[0] == 0) return;
576 Record.clear();
577 while (*Name)
578 Record.push_back(*Name++);
579 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
580}
581
582static void EmitRecordID(unsigned ID, const char *Name,
583 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000584 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000585 Record.clear();
586 Record.push_back(ID);
587 while (*Name)
588 Record.push_back(*Name++);
589 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000590}
591
592static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000593 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000594#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000595 RECORD(STMT_STOP);
596 RECORD(STMT_NULL_PTR);
597 RECORD(STMT_NULL);
598 RECORD(STMT_COMPOUND);
599 RECORD(STMT_CASE);
600 RECORD(STMT_DEFAULT);
601 RECORD(STMT_LABEL);
602 RECORD(STMT_IF);
603 RECORD(STMT_SWITCH);
604 RECORD(STMT_WHILE);
605 RECORD(STMT_DO);
606 RECORD(STMT_FOR);
607 RECORD(STMT_GOTO);
608 RECORD(STMT_INDIRECT_GOTO);
609 RECORD(STMT_CONTINUE);
610 RECORD(STMT_BREAK);
611 RECORD(STMT_RETURN);
612 RECORD(STMT_DECL);
613 RECORD(STMT_ASM);
614 RECORD(EXPR_PREDEFINED);
615 RECORD(EXPR_DECL_REF);
616 RECORD(EXPR_INTEGER_LITERAL);
617 RECORD(EXPR_FLOATING_LITERAL);
618 RECORD(EXPR_IMAGINARY_LITERAL);
619 RECORD(EXPR_STRING_LITERAL);
620 RECORD(EXPR_CHARACTER_LITERAL);
621 RECORD(EXPR_PAREN);
622 RECORD(EXPR_UNARY_OPERATOR);
623 RECORD(EXPR_SIZEOF_ALIGN_OF);
624 RECORD(EXPR_ARRAY_SUBSCRIPT);
625 RECORD(EXPR_CALL);
626 RECORD(EXPR_MEMBER);
627 RECORD(EXPR_BINARY_OPERATOR);
628 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
629 RECORD(EXPR_CONDITIONAL_OPERATOR);
630 RECORD(EXPR_IMPLICIT_CAST);
631 RECORD(EXPR_CSTYLE_CAST);
632 RECORD(EXPR_COMPOUND_LITERAL);
633 RECORD(EXPR_EXT_VECTOR_ELEMENT);
634 RECORD(EXPR_INIT_LIST);
635 RECORD(EXPR_DESIGNATED_INIT);
636 RECORD(EXPR_IMPLICIT_VALUE_INIT);
637 RECORD(EXPR_VA_ARG);
638 RECORD(EXPR_ADDR_LABEL);
639 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000640 RECORD(EXPR_CHOOSE);
641 RECORD(EXPR_GNU_NULL);
642 RECORD(EXPR_SHUFFLE_VECTOR);
643 RECORD(EXPR_BLOCK);
644 RECORD(EXPR_BLOCK_DECL_REF);
645 RECORD(EXPR_OBJC_STRING_LITERAL);
646 RECORD(EXPR_OBJC_ENCODE);
647 RECORD(EXPR_OBJC_SELECTOR_EXPR);
648 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
649 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
650 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
651 RECORD(EXPR_OBJC_KVC_REF_EXPR);
652 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000653 RECORD(STMT_OBJC_FOR_COLLECTION);
654 RECORD(STMT_OBJC_CATCH);
655 RECORD(STMT_OBJC_FINALLY);
656 RECORD(STMT_OBJC_AT_TRY);
657 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
658 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000659 RECORD(EXPR_CXX_OPERATOR_CALL);
660 RECORD(EXPR_CXX_CONSTRUCT);
661 RECORD(EXPR_CXX_STATIC_CAST);
662 RECORD(EXPR_CXX_DYNAMIC_CAST);
663 RECORD(EXPR_CXX_REINTERPRET_CAST);
664 RECORD(EXPR_CXX_CONST_CAST);
665 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
666 RECORD(EXPR_CXX_BOOL_LITERAL);
667 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000668#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000669}
Mike Stump1eb44332009-09-09 15:08:12 +0000670
Sebastian Redla4232eb2010-08-18 23:56:21 +0000671void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000672 RecordData Record;
673 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000675#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
676#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000677
Sebastian Redl3397c552010-08-18 23:56:27 +0000678 // AST Top-Level Block.
Sebastian Redlf29f0a22010-08-18 23:57:22 +0000679 BLOCK(AST_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000680 RECORD(ORIGINAL_FILE_NAME);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000681 RECORD(TYPE_OFFSET);
682 RECORD(DECL_OFFSET);
683 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000684 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000685 RECORD(IDENTIFIER_OFFSET);
686 RECORD(IDENTIFIER_TABLE);
687 RECORD(EXTERNAL_DEFINITIONS);
688 RECORD(SPECIAL_TYPES);
689 RECORD(STATISTICS);
690 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000691 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000692 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
693 RECORD(SELECTOR_OFFSETS);
694 RECORD(METHOD_POOL);
695 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000696 RECORD(SOURCE_LOCATION_OFFSETS);
697 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000698 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000699 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000700 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000701 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redla93e3b52010-07-08 22:01:51 +0000702 RECORD(CHAINED_METADATA);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000703 RECORD(REFERENCED_SELECTOR_POOL);
Michael J. Spencer20249a12010-10-21 03:16:25 +0000704
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000705 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000706 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000707 RECORD(SM_SLOC_FILE_ENTRY);
708 RECORD(SM_SLOC_BUFFER_ENTRY);
709 RECORD(SM_SLOC_BUFFER_BLOB);
710 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
711 RECORD(SM_LINE_TABLE);
Mike Stump1eb44332009-09-09 15:08:12 +0000712
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000713 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000714 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000715 RECORD(PP_MACRO_OBJECT_LIKE);
716 RECORD(PP_MACRO_FUNCTION_LIKE);
717 RECORD(PP_TOKEN);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000718 RECORD(PP_MACRO_INSTANTIATION);
719 RECORD(PP_MACRO_DEFINITION);
Michael J. Spencer20249a12010-10-21 03:16:25 +0000720
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000721 // Decls and Types block.
722 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000723 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000724 RECORD(TYPE_COMPLEX);
725 RECORD(TYPE_POINTER);
726 RECORD(TYPE_BLOCK_POINTER);
727 RECORD(TYPE_LVALUE_REFERENCE);
728 RECORD(TYPE_RVALUE_REFERENCE);
729 RECORD(TYPE_MEMBER_POINTER);
730 RECORD(TYPE_CONSTANT_ARRAY);
731 RECORD(TYPE_INCOMPLETE_ARRAY);
732 RECORD(TYPE_VARIABLE_ARRAY);
733 RECORD(TYPE_VECTOR);
734 RECORD(TYPE_EXT_VECTOR);
735 RECORD(TYPE_FUNCTION_PROTO);
736 RECORD(TYPE_FUNCTION_NO_PROTO);
737 RECORD(TYPE_TYPEDEF);
738 RECORD(TYPE_TYPEOF_EXPR);
739 RECORD(TYPE_TYPEOF);
740 RECORD(TYPE_RECORD);
741 RECORD(TYPE_ENUM);
742 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000743 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000744 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000745 RECORD(DECL_TRANSLATION_UNIT);
746 RECORD(DECL_TYPEDEF);
747 RECORD(DECL_ENUM);
748 RECORD(DECL_RECORD);
749 RECORD(DECL_ENUM_CONSTANT);
750 RECORD(DECL_FUNCTION);
751 RECORD(DECL_OBJC_METHOD);
752 RECORD(DECL_OBJC_INTERFACE);
753 RECORD(DECL_OBJC_PROTOCOL);
754 RECORD(DECL_OBJC_IVAR);
755 RECORD(DECL_OBJC_AT_DEFS_FIELD);
756 RECORD(DECL_OBJC_CLASS);
757 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
758 RECORD(DECL_OBJC_CATEGORY);
759 RECORD(DECL_OBJC_CATEGORY_IMPL);
760 RECORD(DECL_OBJC_IMPLEMENTATION);
761 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
762 RECORD(DECL_OBJC_PROPERTY);
763 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000764 RECORD(DECL_FIELD);
765 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000766 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000767 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000768 RECORD(DECL_FILE_SCOPE_ASM);
769 RECORD(DECL_BLOCK);
770 RECORD(DECL_CONTEXT_LEXICAL);
771 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000772 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattner0558df22009-04-27 00:49:53 +0000773 AddStmtsExprs(Stream, Record);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000774#undef RECORD
775#undef BLOCK
776 Stream.ExitBlock();
777}
778
Douglas Gregore650c8c2009-07-07 00:12:59 +0000779/// \brief Adjusts the given filename to only write out the portion of the
780/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000781///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000782/// \param Filename the file name to adjust.
783///
784/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
785/// the returned filename will be adjusted by this system root.
786///
787/// \returns either the original filename (if it needs no adjustment) or the
788/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000789static const char *
Douglas Gregore650c8c2009-07-07 00:12:59 +0000790adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
791 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000792
Douglas Gregore650c8c2009-07-07 00:12:59 +0000793 if (!isysroot)
794 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000795
Douglas Gregore650c8c2009-07-07 00:12:59 +0000796 // Verify that the filename and the system root have the same prefix.
797 unsigned Pos = 0;
798 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
799 if (Filename[Pos] != isysroot[Pos])
800 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000801
Douglas Gregore650c8c2009-07-07 00:12:59 +0000802 // We hit the end of the filename before we hit the end of the system root.
803 if (!Filename[Pos])
804 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Douglas Gregore650c8c2009-07-07 00:12:59 +0000806 // If the file name has a '/' at the current position, skip over the '/'.
807 // We distinguish sysroot-based includes from absolute includes by the
808 // absence of '/' at the beginning of sysroot-based includes.
809 if (Filename[Pos] == '/')
810 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000811
Douglas Gregore650c8c2009-07-07 00:12:59 +0000812 return Filename + Pos;
813}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000814
Sebastian Redl3397c552010-08-18 23:56:27 +0000815/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Sebastian Redla4232eb2010-08-18 23:56:21 +0000816void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000817 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000818
Douglas Gregore650c8c2009-07-07 00:12:59 +0000819 // Metadata
820 const TargetInfo &Target = Context.Target;
821 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl77f46032010-07-09 21:00:24 +0000822 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000823 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl3397c552010-08-18 23:56:27 +0000824 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
825 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregore650c8c2009-07-07 00:12:59 +0000826 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
827 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
828 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl77f46032010-07-09 21:00:24 +0000829 // Target triple or chained PCH name
830 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregore650c8c2009-07-07 00:12:59 +0000831 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000832
Douglas Gregore650c8c2009-07-07 00:12:59 +0000833 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000834 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
835 Record.push_back(VERSION_MAJOR);
836 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000837 Record.push_back(CLANG_VERSION_MAJOR);
838 Record.push_back(CLANG_VERSION_MINOR);
839 Record.push_back(isysroot != 0);
Sebastian Redl77f46032010-07-09 21:00:24 +0000840 // FIXME: This writes the absolute path for chained headers.
841 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
842 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Douglas Gregorb64c1932009-05-12 01:31:05 +0000844 // Original file name
845 SourceManager &SM = Context.getSourceManager();
846 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
847 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000848 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregorb64c1932009-05-12 01:31:05 +0000849 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
850 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
851
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000852 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000853
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000854 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000855
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +0000856 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000857 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000858 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000859 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000860 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000861 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000862 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000863
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000864 // Repository branch/version information.
865 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000866 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000867 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
868 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000869 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000870 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000871 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
872 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +0000873}
874
875/// \brief Write the LangOptions structure.
Sebastian Redla4232eb2010-08-18 23:56:21 +0000876void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000877 RecordData Record;
878 Record.push_back(LangOpts.Trigraphs);
879 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
880 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
881 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
882 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000883 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000884 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
885 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
886 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
887 Record.push_back(LangOpts.C99); // C99 Support
888 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencerdae4ac42010-10-21 05:21:48 +0000889 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
890 // already saved elsewhere.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000891 Record.push_back(LangOpts.CPlusPlus); // C++ Support
892 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000893 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +0000894
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000895 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
896 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000897 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000898 // modern abi enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000899 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000900 // modern abi enabled.
Fariborz Jahanianf84109e2011-01-07 18:59:25 +0000901 Record.push_back(LangOpts.AppleKext); // Apple's kernel extensions ABI
Ted Kremenekc32647d2010-12-23 21:35:43 +0000902 Record.push_back(LangOpts.ObjCDefaultSynthProperties); // Objective-C auto-synthesized
903 // properties enabled.
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +0000904 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump1eb44332009-09-09 15:08:12 +0000905
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000906 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000907 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
908 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000909 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000910 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar73482882010-02-10 18:48:44 +0000911 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000912
913 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
914 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
915 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
916
Chris Lattnerea5ce472009-04-27 07:35:58 +0000917 // Whether static initializers are protected by locks.
918 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +0000919 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000920 Record.push_back(LangOpts.Blocks); // block extension to C
921 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
922 // they are unused.
923 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
924 // (modulo the platform support).
925
Chris Lattnera4d71452010-06-26 21:25:03 +0000926 Record.push_back(LangOpts.getSignedOverflowBehavior());
927 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000928
929 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +0000930 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000931 // defined.
932 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
933 // opposed to __DYNAMIC__).
934 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
935
936 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
937 // used (instead of C99 semantics).
938 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +0000939 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
940 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +0000941 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
942 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +0000943 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Argyrios Kyrtzidisb1bdced2011-01-15 02:56:16 +0000944 Record.push_back(LangOpts.ShortEnums); // Should the enum type be equivalent
945 // to the smallest integer type with
946 // enough room.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000947 Record.push_back(LangOpts.getGCMode());
948 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000949 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000950 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000951 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne08a53262010-12-01 19:14:57 +0000952 Record.push_back(LangOpts.CUDA);
Mike Stump9c276ae2009-12-12 01:27:46 +0000953 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson92f58222009-08-22 22:30:33 +0000954 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +0000955 Record.push_back(LangOpts.SpellChecking);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000956 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000957}
958
Douglas Gregor14f79002009-04-10 03:52:48 +0000959//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000960// stat cache Serialization
961//===----------------------------------------------------------------------===//
962
963namespace {
964// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +0000965class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000966public:
967 typedef const char * key_type;
968 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000969
Chris Lattner74e976b2010-11-23 19:28:12 +0000970 typedef struct stat data_type;
971 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000972
973 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000974 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000975 }
Mike Stump1eb44332009-09-09 15:08:12 +0000976
977 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000978 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
979 data_type_ref Data) {
980 unsigned StrLen = strlen(path);
981 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +0000982 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000983 clang::io::Emit8(Out, DataLen);
984 return std::make_pair(StrLen + 1, DataLen);
985 }
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000987 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
988 Out.write(path, KeyLen);
989 }
Mike Stump1eb44332009-09-09 15:08:12 +0000990
Chris Lattner74e976b2010-11-23 19:28:12 +0000991 void EmitData(llvm::raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000992 data_type_ref Data, unsigned DataLen) {
993 using namespace clang::io;
994 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +0000995
Chris Lattner74e976b2010-11-23 19:28:12 +0000996 Emit32(Out, (uint32_t) Data.st_ino);
997 Emit32(Out, (uint32_t) Data.st_dev);
998 Emit16(Out, (uint16_t) Data.st_mode);
999 Emit64(Out, (uint64_t) Data.st_mtime);
1000 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001001
1002 assert(Out.tell() - Start == DataLen && "Wrong data length");
1003 }
1004};
1005} // end anonymous namespace
1006
Sebastian Redl3397c552010-08-18 23:56:27 +00001007/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001008void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001009 // Build the on-disk hash table containing information about every
1010 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +00001011 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001012 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001013 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001014 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001015 Stat != StatEnd; ++Stat, ++NumStatEntries) {
1016 const char *Filename = Stat->first();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001017 Generator.insert(Filename, Stat->second);
1018 }
Mike Stump1eb44332009-09-09 15:08:12 +00001019
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001020 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001021 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001022 uint32_t BucketOffset;
1023 {
1024 llvm::raw_svector_ostream Out(StatCacheData);
1025 // Make sure that no bucket is at offset 0
1026 clang::io::Emit32(Out, 0);
1027 BucketOffset = Generator.Emit(Out);
1028 }
1029
1030 // Create a blob abbreviation
1031 using namespace llvm;
1032 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001033 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001034 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1035 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1036 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1037 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1038
1039 // Write the stat cache
1040 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001041 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001042 Record.push_back(BucketOffset);
1043 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001044 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001045}
1046
1047//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001048// Source Manager Serialization
1049//===----------------------------------------------------------------------===//
1050
1051/// \brief Create an abbreviation for the SLocEntry that refers to a
1052/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001053static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001054 using namespace llvm;
1055 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001056 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001057 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1058 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1059 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1060 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001061 // FileEntry fields.
1062 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1063 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor12fab312010-03-16 16:35:32 +00001064 // HeaderFileInfo fields.
1065 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
1066 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
1067 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
1068 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregor14f79002009-04-10 03:52:48 +00001069 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001070 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001071}
1072
1073/// \brief Create an abbreviation for the SLocEntry that refers to a
1074/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001075static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001076 using namespace llvm;
1077 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001078 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001079 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1080 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1081 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1082 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1083 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001084 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001085}
1086
1087/// \brief Create an abbreviation for the SLocEntry that refers to a
1088/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001089static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001090 using namespace llvm;
1091 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001092 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001093 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001094 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001095}
1096
1097/// \brief Create an abbreviation for the SLocEntry that refers to an
1098/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001099static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001100 using namespace llvm;
1101 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001102 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001103 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1104 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1105 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1106 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001107 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001108 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001109}
1110
1111/// \brief Writes the block containing the serialized form of the
1112/// source manager.
1113///
1114/// TODO: We should probably use an on-disk hash table (stored in a
1115/// blob), indexed based on the file name, so that we only create
1116/// entries for files that we actually need. In the common case (no
1117/// errors), we probably won't have to create file entries for any of
1118/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001119void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001120 const Preprocessor &PP,
1121 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001122 RecordData Record;
1123
Chris Lattnerf04ad692009-04-10 17:16:57 +00001124 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001125 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001126
1127 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001128 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1129 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1130 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1131 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001132
Douglas Gregorbd945002009-04-13 16:31:14 +00001133 // Write the line table.
1134 if (SourceMgr.hasLineTable()) {
1135 LineTableInfo &LineTable = SourceMgr.getLineTable();
1136
1137 // Emit the file names
1138 Record.push_back(LineTable.getNumFilenames());
1139 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1140 // Emit the file name
1141 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001142 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +00001143 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1144 Record.push_back(FilenameLen);
1145 if (FilenameLen)
1146 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1147 }
Mike Stump1eb44332009-09-09 15:08:12 +00001148
Douglas Gregorbd945002009-04-13 16:31:14 +00001149 // Emit the line entries
1150 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1151 L != LEnd; ++L) {
1152 // Emit the file ID
1153 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +00001154
Douglas Gregorbd945002009-04-13 16:31:14 +00001155 // Emit the line entries
1156 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001157 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +00001158 LEEnd = L->second.end();
1159 LE != LEEnd; ++LE) {
1160 Record.push_back(LE->FileOffset);
1161 Record.push_back(LE->LineNo);
1162 Record.push_back(LE->FilenameID);
1163 Record.push_back((unsigned)LE->FileKind);
1164 Record.push_back(LE->IncludeOffset);
1165 }
Douglas Gregorbd945002009-04-13 16:31:14 +00001166 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001167 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001168 }
1169
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001170 // Write out the source location entry table. We skip the first
1171 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001172 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001173 RecordData PreloadSLocs;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001174 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1175 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1176 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1177 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001178 // Get this source location entry.
1179 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001180
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001181 // Record the offset of this source-location entry.
1182 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1183
1184 // Figure out which record code to use.
1185 unsigned Code;
1186 if (SLoc->isFile()) {
1187 if (SLoc->getFile().getContentCache()->Entry)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001188 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001189 else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001190 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001191 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001192 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001193 Record.clear();
1194 Record.push_back(Code);
1195
1196 Record.push_back(SLoc->getOffset());
1197 if (SLoc->isFile()) {
1198 const SrcMgr::FileInfo &File = SLoc->getFile();
1199 Record.push_back(File.getIncludeLoc().getRawEncoding());
1200 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1201 Record.push_back(File.hasLineDirectives());
1202
1203 const SrcMgr::ContentCache *Content = File.getContentCache();
1204 if (Content->Entry) {
1205 // The source location entry is a file. The blob associated
1206 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001207
Douglas Gregor2d52be52010-03-21 22:49:54 +00001208 // Emit size/modification time for this file.
1209 Record.push_back(Content->Entry->getSize());
1210 Record.push_back(Content->Entry->getModificationTime());
1211
Douglas Gregor12fab312010-03-16 16:35:32 +00001212 // Emit header-search information associated with this file.
1213 HeaderFileInfo HFI;
1214 HeaderSearch &HS = PP.getHeaderSearchInfo();
1215 if (Content->Entry->getUID() < HS.header_file_size())
1216 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1217 Record.push_back(HFI.isImport);
1218 Record.push_back(HFI.DirInfo);
1219 Record.push_back(HFI.NumIncludes);
1220 AddIdentifierRef(HFI.ControllingMacro, Record);
1221
Douglas Gregore650c8c2009-07-07 00:12:59 +00001222 // Turn the file name into an absolute path, if it isn't already.
1223 const char *Filename = Content->Entry->getName();
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001224 llvm::SmallString<128> FilePath(Filename);
1225 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001226 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001227
Douglas Gregore650c8c2009-07-07 00:12:59 +00001228 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001229 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001230
1231 // FIXME: For now, preload all file source locations, so that
1232 // we get the appropriate File entries in the reader. This is
1233 // a temporary measure.
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001234 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001235 } else {
1236 // The source location entry is a buffer. The blob associated
1237 // with this entry contains the contents of the buffer.
1238
1239 // We add one to the size so that we capture the trailing NULL
1240 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1241 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001242 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001243 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001244 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001245 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1246 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001247 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001248 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001249 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001250 llvm::StringRef(Buffer->getBufferStart(),
1251 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001252
1253 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001254 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001255 }
1256 } else {
1257 // The source location entry is an instantiation.
1258 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1259 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1260 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1261 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1262
1263 // Compute the token length for this macro expansion.
1264 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001265 if (I + 1 != N)
1266 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001267 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1268 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1269 }
1270 }
1271
Douglas Gregorc9490c02009-04-16 22:23:12 +00001272 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001273
1274 if (SLocEntryOffsets.empty())
1275 return;
1276
Sebastian Redl3397c552010-08-18 23:56:27 +00001277 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001278 // table is used for lazily loading source-location information.
1279 using namespace llvm;
1280 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001281 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001282 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1283 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1284 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1285 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001286
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001287 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001288 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001289 Record.push_back(SLocEntryOffsets.size());
Sebastian Redl8db9fae2010-09-22 20:19:08 +00001290 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1291 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001292 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001293 (const char *)data(SLocEntryOffsets),
Chris Lattner090d9b52009-04-27 19:01:47 +00001294 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001295
Sebastian Redl3397c552010-08-18 23:56:27 +00001296 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001297 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001298 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001299}
1300
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001301//===----------------------------------------------------------------------===//
1302// Preprocessor Serialization
1303//===----------------------------------------------------------------------===//
1304
Chris Lattner0b1fb982009-04-10 17:15:23 +00001305/// \brief Writes the block containing the serialized form of the
1306/// preprocessor.
1307///
Sebastian Redla4232eb2010-08-18 23:56:21 +00001308void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001309 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001310
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001311 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1312 if (PP.getCounterValue() != 0) {
1313 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001314 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001315 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001316 }
1317
1318 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001319 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001320
Sebastian Redl3397c552010-08-18 23:56:27 +00001321 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001322 // FIXME: use diagnostics subsystem for localization etc.
1323 if (PP.SawDateOrTime())
1324 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001325
Douglas Gregorecdcb882010-10-20 22:00:55 +00001326
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001327 // Loop over all the macro definitions that are live at the end of the file,
1328 // emitting each to the PP section.
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001329 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001330 unsigned InclusionAbbrev = 0;
1331 if (PPRec) {
1332 using namespace llvm;
1333 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1334 Abbrev->Add(BitCodeAbbrevOp(PP_INCLUSION_DIRECTIVE));
1335 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1336 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1337 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1338 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1339 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1340 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1341 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Michael J. Spencer20249a12010-10-21 03:16:25 +00001342 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorecdcb882010-10-20 22:00:55 +00001343 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001344
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001345 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1346 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001347 // FIXME: This emits macros in hash table order, we should do it in a stable
1348 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001349 MacroInfo *MI = I->second;
1350
Sebastian Redl3397c552010-08-18 23:56:27 +00001351 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001352 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl3397c552010-08-18 23:56:27 +00001353 // Also skip macros from a AST file if we're chaining.
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001354
1355 // FIXME: There is a (probably minor) optimization we could do here, if
1356 // the macro comes from the original PCH but the identifier comes from a
1357 // chained PCH, by storing the offset into the original PCH rather than
1358 // writing the macro definition a second time.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001359 if (MI->isBuiltinMacro() ||
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001360 (Chain && I->first->isFromAST() && MI->isFromAST()))
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001361 continue;
1362
Chris Lattner7356a312009-04-11 21:15:38 +00001363 AddIdentifierRef(I->first, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001364 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001365 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1366 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001367
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001368 unsigned Code;
1369 if (MI->isObjectLike()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001370 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001371 } else {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001372 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001373
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001374 Record.push_back(MI->isC99Varargs());
1375 Record.push_back(MI->isGNUVarargs());
1376 Record.push_back(MI->getNumArgs());
1377 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1378 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001379 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001380 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001381
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001382 // If we have a detailed preprocessing record, record the macro definition
1383 // ID that corresponds to this macro.
1384 if (PPRec)
1385 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer20249a12010-10-21 03:16:25 +00001386
Douglas Gregorc9490c02009-04-16 22:23:12 +00001387 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001388 Record.clear();
1389
Chris Lattnerdf961c22009-04-10 18:08:30 +00001390 // Emit the tokens array.
1391 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1392 // Note that we know that the preprocessor does not have any annotation
1393 // tokens in it because they are created by the parser, and thus can't be
1394 // in a macro definition.
1395 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001396
Chris Lattnerdf961c22009-04-10 18:08:30 +00001397 Record.push_back(Tok.getLocation().getRawEncoding());
1398 Record.push_back(Tok.getLength());
1399
Chris Lattnerdf961c22009-04-10 18:08:30 +00001400 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1401 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001402 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001403
Chris Lattnerdf961c22009-04-10 18:08:30 +00001404 // FIXME: Should translate token kind to a stable encoding.
1405 Record.push_back(Tok.getKind());
1406 // FIXME: Should translate token flags to a stable encoding.
1407 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001408
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001409 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001410 Record.clear();
1411 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001412 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001413 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001414
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001415 // If the preprocessor has a preprocessing record, emit it.
1416 unsigned NumPreprocessingRecords = 0;
1417 if (PPRec) {
Sebastian Redl196f5572010-09-27 23:20:01 +00001418 unsigned IndexBase = Chain ? PPRec->getNumPreallocatedEntities() : 0;
Sebastian Redlb57a6242010-09-27 22:18:47 +00001419 for (PreprocessingRecord::iterator E = PPRec->begin(Chain),
1420 EEnd = PPRec->end(Chain);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001421 E != EEnd; ++E) {
1422 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001423
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001424 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1425 // Record this macro definition's location.
Sebastian Redlf73c93f2010-09-15 19:54:06 +00001426 MacroID ID = getMacroDefinitionID(MD);
Douglas Gregor89d99802010-11-30 06:16:57 +00001427
Douglas Gregor77424bc2010-10-02 19:29:26 +00001428 // Don't write the macro definition if it is from another AST file.
1429 if (ID < FirstMacroID)
1430 continue;
Douglas Gregor89d99802010-11-30 06:16:57 +00001431
1432 // Notify the serialization listener that we're serializing this entity.
1433 if (SerializationListener)
1434 SerializationListener->SerializedPreprocessedEntity(*E,
1435 Stream.GetCurrentBitNo());
Michael J. Spencer20249a12010-10-21 03:16:25 +00001436
Douglas Gregor77424bc2010-10-02 19:29:26 +00001437 unsigned Position = ID - FirstMacroID;
1438 if (Position != MacroDefinitionOffsets.size()) {
1439 if (Position > MacroDefinitionOffsets.size())
1440 MacroDefinitionOffsets.resize(Position + 1);
Douglas Gregor89d99802010-11-30 06:16:57 +00001441
Michael J. Spencer20249a12010-10-21 03:16:25 +00001442 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001443 } else
1444 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor89d99802010-11-30 06:16:57 +00001445
Sebastian Redlb57a6242010-09-27 22:18:47 +00001446 Record.push_back(IndexBase + NumPreprocessingRecords++);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001447 Record.push_back(ID);
1448 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1449 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1450 AddIdentifierRef(MD->getName(), Record);
1451 AddSourceLocation(MD->getLocation(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001452 Stream.EmitRecord(PP_MACRO_DEFINITION, Record);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001453 continue;
1454 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001455
Douglas Gregor89d99802010-11-30 06:16:57 +00001456 // Notify the serialization listener that we're serializing this entity.
1457 if (SerializationListener)
1458 SerializationListener->SerializedPreprocessedEntity(*E,
1459 Stream.GetCurrentBitNo());
1460
1461 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1462 Record.push_back(IndexBase + NumPreprocessingRecords++);
1463 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1464 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1465 AddIdentifierRef(MI->getName(), Record);
1466 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1467 Stream.EmitRecord(PP_MACRO_INSTANTIATION, Record);
1468 continue;
1469 }
1470
Douglas Gregorecdcb882010-10-20 22:00:55 +00001471 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1472 Record.push_back(PP_INCLUSION_DIRECTIVE);
1473 Record.push_back(IndexBase + NumPreprocessingRecords++);
1474 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1475 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1476 Record.push_back(ID->getFileName().size());
1477 Record.push_back(ID->wasInQuotes());
1478 Record.push_back(static_cast<unsigned>(ID->getKind()));
1479 llvm::SmallString<64> Buffer;
1480 Buffer += ID->getFileName();
1481 Buffer += ID->getFile()->getName();
1482 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1483 continue;
1484 }
Douglas Gregor89d99802010-11-30 06:16:57 +00001485
1486 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001487 }
1488 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001489
Douglas Gregorc9490c02009-04-16 22:23:12 +00001490 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001491
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001492 // Write the offsets table for the preprocessing record.
1493 if (NumPreprocessingRecords > 0) {
1494 // Write the offsets table for identifier IDs.
1495 using namespace llvm;
1496 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001497 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001498 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1499 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1500 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1501 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001502
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001503 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001504 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001505 Record.push_back(NumPreprocessingRecords);
1506 Record.push_back(MacroDefinitionOffsets.size());
1507 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001508 (const char *)data(MacroDefinitionOffsets),
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001509 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1510 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001511}
1512
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001513void ASTWriter::WritePragmaDiagnosticMappings(const Diagnostic &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001514 RecordData Record;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001515 for (Diagnostic::DiagStatePointsTy::const_iterator
1516 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1517 I != E; ++I) {
1518 const Diagnostic::DiagStatePoint &point = *I;
1519 if (point.Loc.isInvalid())
1520 continue;
1521
1522 Record.push_back(point.Loc.getRawEncoding());
1523 for (Diagnostic::DiagState::iterator
1524 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
1525 unsigned diag = I->first, map = I->second;
1526 if (map & 0x10) { // mapping from a diagnostic pragma.
1527 Record.push_back(diag);
1528 Record.push_back(map & 0x7);
1529 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001530 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001531 Record.push_back(-1); // mark the end of the diag/map pairs for this
1532 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001533 }
1534
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00001535 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001536 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001537}
1538
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001539//===----------------------------------------------------------------------===//
1540// Type Serialization
1541//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001542
Sebastian Redl3397c552010-08-18 23:56:27 +00001543/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001544void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00001545 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001546 if (Idx.getIndex() == 0) // we haven't seen this type before.
1547 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00001548
Douglas Gregor97475832010-10-05 18:37:06 +00001549 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00001550
Douglas Gregor2cf26342009-04-09 22:27:44 +00001551 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001552 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00001553 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001554 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00001555 else if (TypeOffsets.size() < Index) {
1556 TypeOffsets.resize(Index + 1);
1557 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001558 }
1559
1560 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001561
Douglas Gregor2cf26342009-04-09 22:27:44 +00001562 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00001563 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001564
Douglas Gregora4923eb2009-11-16 21:35:15 +00001565 if (T.hasLocalNonFastQualifiers()) {
1566 Qualifiers Qs = T.getLocalQualifiers();
1567 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001568 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001569 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00001570 } else {
1571 switch (T->getTypeClass()) {
1572 // For all of the concrete, non-dependent types, call the
1573 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001574#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001575 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001576#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001577#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001578 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001579 }
1580
1581 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001582 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001583
1584 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001585 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001586}
1587
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001588//===----------------------------------------------------------------------===//
1589// Declaration Serialization
1590//===----------------------------------------------------------------------===//
1591
Douglas Gregor2cf26342009-04-09 22:27:44 +00001592/// \brief Write the block containing all of the declaration IDs
1593/// lexically declared within the given DeclContext.
1594///
1595/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1596/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001597uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001598 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001599 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001600 return 0;
1601
Douglas Gregorc9490c02009-04-16 22:23:12 +00001602 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001603 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001604 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001605 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001606 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1607 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001608 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001609
Douglas Gregor25123082009-04-22 22:34:57 +00001610 ++NumLexicalDeclContexts;
Sebastian Redl681d7232010-07-27 00:17:23 +00001611 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001612 reinterpret_cast<char*>(Decls.data()),
1613 Decls.size() * sizeof(KindDeclIDPair));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001614 return Offset;
1615}
1616
Sebastian Redla4232eb2010-08-18 23:56:21 +00001617void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00001618 using namespace llvm;
1619 RecordData Record;
1620
1621 // Write the type offsets array
1622 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001623 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001624 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1625 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1626 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1627 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001628 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00001629 Record.push_back(TypeOffsets.size());
1630 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001631 (const char *)data(TypeOffsets),
Sebastian Redl1476ed42010-07-16 16:36:56 +00001632 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1633
1634 // Write the declaration offsets array
1635 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001636 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001637 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1638 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1639 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1640 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001641 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00001642 Record.push_back(DeclOffsets.size());
1643 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001644 (const char *)data(DeclOffsets),
Sebastian Redl1476ed42010-07-16 16:36:56 +00001645 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1646}
1647
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001648//===----------------------------------------------------------------------===//
1649// Global Method Pool and Selector Serialization
1650//===----------------------------------------------------------------------===//
1651
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001652namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001653// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00001654class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00001655 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001656
1657public:
1658 typedef Selector key_type;
1659 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001660
Sebastian Redl5d050072010-08-04 17:20:04 +00001661 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001662 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00001663 ObjCMethodList Instance, Factory;
1664 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001665 typedef const data_type& data_type_ref;
1666
Sebastian Redl3397c552010-08-18 23:56:27 +00001667 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001668
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001669 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00001670 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001671 }
Mike Stump1eb44332009-09-09 15:08:12 +00001672
1673 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001674 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1675 data_type_ref Methods) {
1676 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1677 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00001678 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1679 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001680 Method = Method->Next)
1681 if (Method->Method)
1682 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00001683 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001684 Method = Method->Next)
1685 if (Method->Method)
1686 DataLen += 4;
1687 clang::io::Emit16(Out, DataLen);
1688 return std::make_pair(KeyLen, DataLen);
1689 }
Mike Stump1eb44332009-09-09 15:08:12 +00001690
Douglas Gregor83941df2009-04-25 17:48:32 +00001691 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00001692 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00001693 assert((Start >> 32) == 0 && "Selector key offset too large");
1694 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001695 unsigned N = Sel.getNumArgs();
1696 clang::io::Emit16(Out, N);
1697 if (N == 0)
1698 N = 1;
1699 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001700 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001701 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1702 }
Mike Stump1eb44332009-09-09 15:08:12 +00001703
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001704 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00001705 data_type_ref Methods, unsigned DataLen) {
1706 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00001707 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001708 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00001709 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001710 Method = Method->Next)
1711 if (Method->Method)
1712 ++NumInstanceMethods;
1713
1714 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00001715 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001716 Method = Method->Next)
1717 if (Method->Method)
1718 ++NumFactoryMethods;
1719
1720 clang::io::Emit16(Out, NumInstanceMethods);
1721 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00001722 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001723 Method = Method->Next)
1724 if (Method->Method)
1725 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00001726 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001727 Method = Method->Next)
1728 if (Method->Method)
1729 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00001730
1731 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001732 }
1733};
1734} // end anonymous namespace
1735
Sebastian Redl059612d2010-08-03 21:58:15 +00001736/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001737///
1738/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00001739/// in an on-disk hash table indexed by the selector. The hash table also
1740/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001741void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001742 using namespace llvm;
1743
Sebastian Redl059612d2010-08-03 21:58:15 +00001744 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00001745 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00001746 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00001747 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00001748 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001749 {
Sebastian Redl3397c552010-08-18 23:56:27 +00001750 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001751 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001752
Sebastian Redl059612d2010-08-03 21:58:15 +00001753 // Create the on-disk hash table representation. We walk through every
1754 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00001755 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001756 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00001757 I = SelectorIDs.begin(), E = SelectorIDs.end();
1758 I != E; ++I) {
1759 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00001760 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00001761 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00001762 I->second,
1763 ObjCMethodList(),
1764 ObjCMethodList()
1765 };
1766 if (F != SemaRef.MethodPool.end()) {
1767 Data.Instance = F->second.first;
1768 Data.Factory = F->second.second;
1769 }
Sebastian Redl3397c552010-08-18 23:56:27 +00001770 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00001771 // changed.
1772 if (Chain && I->second < FirstSelectorID) {
1773 // Selector already exists. Did it change?
1774 bool changed = false;
1775 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
1776 M = M->Next) {
1777 if (M->Method->getPCHLevel() == 0)
1778 changed = true;
1779 }
1780 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
1781 M = M->Next) {
1782 if (M->Method->getPCHLevel() == 0)
1783 changed = true;
1784 }
1785 if (!changed)
1786 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00001787 } else if (Data.Instance.Method || Data.Factory.Method) {
1788 // A new method pool entry.
1789 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00001790 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001791 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001792 }
1793
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001794 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001795 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001796 uint32_t BucketOffset;
1797 {
Sebastian Redl3397c552010-08-18 23:56:27 +00001798 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001799 llvm::raw_svector_ostream Out(MethodPool);
1800 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001801 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001802 BucketOffset = Generator.Emit(Out, Trait);
1803 }
1804
1805 // Create a blob abbreviation
1806 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001807 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001808 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00001809 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001810 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1811 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1812
Douglas Gregor83941df2009-04-25 17:48:32 +00001813 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001814 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001815 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001816 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00001817 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001818 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00001819
1820 // Create a blob abbreviation for the selector table offsets.
1821 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001822 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00001823 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor83941df2009-04-25 17:48:32 +00001824 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1825 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1826
1827 // Write the selector offsets table.
1828 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001829 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00001830 Record.push_back(SelectorOffsets.size());
1831 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001832 (const char *)data(SelectorOffsets),
Douglas Gregor83941df2009-04-25 17:48:32 +00001833 SelectorOffsets.size() * 4);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001834 }
1835}
1836
Sebastian Redl3397c552010-08-18 23:56:27 +00001837/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001838void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00001839 using namespace llvm;
1840 if (SemaRef.ReferencedSelectors.empty())
1841 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00001842
Fariborz Jahanian32019832010-07-23 19:11:11 +00001843 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00001844
Sebastian Redl3397c552010-08-18 23:56:27 +00001845 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00001846 // very tricky to fix, and given that @selector shouldn't really appear in
1847 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00001848 for (DenseMap<Selector, SourceLocation>::iterator S =
1849 SemaRef.ReferencedSelectors.begin(),
1850 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
1851 Selector Sel = (*S).first;
1852 SourceLocation Loc = (*S).second;
1853 AddSelectorRef(Sel, Record);
1854 AddSourceLocation(Loc, Record);
1855 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001856 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00001857}
1858
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001859//===----------------------------------------------------------------------===//
1860// Identifier Table Serialization
1861//===----------------------------------------------------------------------===//
1862
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001863namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00001864class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00001865 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00001866 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001867
Douglas Gregora92193e2009-04-28 21:18:29 +00001868 /// \brief Determines whether this is an "interesting" identifier
1869 /// that needs a full IdentifierInfo structure written into the hash
1870 /// table.
1871 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1872 return II->isPoisoned() ||
1873 II->isExtensionToken() ||
1874 II->hasMacroDefinition() ||
1875 II->getObjCOrBuiltinID() ||
1876 II->getFETokenInfo<void>();
1877 }
1878
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001879public:
1880 typedef const IdentifierInfo* key_type;
1881 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001882
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001883 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001884 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001885
Sebastian Redl3397c552010-08-18 23:56:27 +00001886 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00001887 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001888
1889 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001890 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001891 }
Mike Stump1eb44332009-09-09 15:08:12 +00001892
1893 std::pair<unsigned,unsigned>
1894 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001895 IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00001896 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00001897 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1898 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00001899 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00001900 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00001901 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00001902 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00001903 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1904 DEnd = IdentifierResolver::end();
1905 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001906 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00001907 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001908 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00001909 // We emit the key length after the data length so that every
1910 // string is preceded by a 16-bit length. This matches the PTH
1911 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00001912 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001913 return std::make_pair(KeyLen, DataLen);
1914 }
Mike Stump1eb44332009-09-09 15:08:12 +00001915
1916 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001917 unsigned KeyLen) {
1918 // Record the location of the key data. This is used when generating
1919 // the mapping from persistent IDs to strings.
1920 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00001921 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001922 }
Mike Stump1eb44332009-09-09 15:08:12 +00001923
1924 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001925 IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00001926 if (!isInterestingIdentifier(II)) {
1927 clang::io::Emit32(Out, ID << 1);
1928 return;
1929 }
Douglas Gregor5998da52009-04-28 21:32:13 +00001930
Douglas Gregora92193e2009-04-28 21:18:29 +00001931 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001932 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001933 bool hasMacroDefinition =
1934 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00001935 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00001936 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001937 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1938 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1939 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00001940 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001941 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00001942 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001943
Douglas Gregor37e26842009-04-21 23:56:24 +00001944 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00001945 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00001946
Douglas Gregor668c1a42009-04-21 22:25:48 +00001947 // Emit the declaration IDs in reverse order, because the
1948 // IdentifierResolver provides the declarations as they would be
1949 // visible (e.g., the function "stat" would come before the struct
1950 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1951 // adds declarations to the end of the list (so we need to see the
1952 // struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00001953 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump1eb44332009-09-09 15:08:12 +00001954 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00001955 IdentifierResolver::end());
1956 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1957 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001958 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00001959 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001960 }
1961};
1962} // end anonymous namespace
1963
Sebastian Redl3397c552010-08-18 23:56:27 +00001964/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001965///
1966/// The identifier table consists of a blob containing string data
1967/// (the actual identifiers themselves) and a separate "offsets" index
1968/// that maps identifier IDs to locations within the blob.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001969void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001970 using namespace llvm;
1971
1972 // Create and write out the blob that contains the identifier
1973 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001974 {
Sebastian Redl3397c552010-08-18 23:56:27 +00001975 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001976 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump1eb44332009-09-09 15:08:12 +00001977
Douglas Gregor92b059e2009-04-28 20:33:11 +00001978 // Look for any identifiers that were named while processing the
1979 // headers, but are otherwise not needed. We add these to the hash
1980 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00001981 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00001982 // file.
1983 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1984 IDEnd = PP.getIdentifierTable().end();
1985 ID != IDEnd; ++ID)
1986 getIdentifierRef(ID->second);
1987
Sebastian Redlf2f0f032010-07-23 23:49:55 +00001988 // Create the on-disk hash table representation. We only store offsets
1989 // for identifiers that appear here for the first time.
1990 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001991 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00001992 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1993 ID != IDEnd; ++ID) {
1994 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001995 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001996 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001997 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001998
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001999 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002000 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002001 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002002 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002003 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002004 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002005 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002006 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002007 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002008 }
2009
2010 // Create a blob abbreviation
2011 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002012 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002013 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002014 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002015 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002016
2017 // Write the identifier table
2018 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002019 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002020 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002021 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002022 }
2023
2024 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002025 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002026 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002027 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2028 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2029 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2030
2031 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002032 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002033 Record.push_back(IdentifierOffsets.size());
2034 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00002035 (const char *)data(IdentifierOffsets),
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002036 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002037}
2038
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002039//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002040// DeclContext's Name Lookup Table Serialization
2041//===----------------------------------------------------------------------===//
2042
2043namespace {
2044// Trait used for the on-disk hash table used in the method pool.
2045class ASTDeclContextNameLookupTrait {
2046 ASTWriter &Writer;
2047
2048public:
2049 typedef DeclarationName key_type;
2050 typedef key_type key_type_ref;
2051
2052 typedef DeclContext::lookup_result data_type;
2053 typedef const data_type& data_type_ref;
2054
2055 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2056
2057 unsigned ComputeHash(DeclarationName Name) {
2058 llvm::FoldingSetNodeID ID;
2059 ID.AddInteger(Name.getNameKind());
2060
2061 switch (Name.getNameKind()) {
2062 case DeclarationName::Identifier:
2063 ID.AddString(Name.getAsIdentifierInfo()->getName());
2064 break;
2065 case DeclarationName::ObjCZeroArgSelector:
2066 case DeclarationName::ObjCOneArgSelector:
2067 case DeclarationName::ObjCMultiArgSelector:
2068 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2069 break;
2070 case DeclarationName::CXXConstructorName:
2071 case DeclarationName::CXXDestructorName:
2072 case DeclarationName::CXXConversionFunctionName:
2073 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
2074 break;
2075 case DeclarationName::CXXOperatorName:
2076 ID.AddInteger(Name.getCXXOverloadedOperator());
2077 break;
2078 case DeclarationName::CXXLiteralOperatorName:
2079 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2080 case DeclarationName::CXXUsingDirective:
2081 break;
2082 }
2083
2084 return ID.ComputeHash();
2085 }
2086
2087 std::pair<unsigned,unsigned>
2088 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2089 data_type_ref Lookup) {
2090 unsigned KeyLen = 1;
2091 switch (Name.getNameKind()) {
2092 case DeclarationName::Identifier:
2093 case DeclarationName::ObjCZeroArgSelector:
2094 case DeclarationName::ObjCOneArgSelector:
2095 case DeclarationName::ObjCMultiArgSelector:
2096 case DeclarationName::CXXConstructorName:
2097 case DeclarationName::CXXDestructorName:
2098 case DeclarationName::CXXConversionFunctionName:
2099 case DeclarationName::CXXLiteralOperatorName:
2100 KeyLen += 4;
2101 break;
2102 case DeclarationName::CXXOperatorName:
2103 KeyLen += 1;
2104 break;
2105 case DeclarationName::CXXUsingDirective:
2106 break;
2107 }
2108 clang::io::Emit16(Out, KeyLen);
2109
2110 // 2 bytes for num of decls and 4 for each DeclID.
2111 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2112 clang::io::Emit16(Out, DataLen);
2113
2114 return std::make_pair(KeyLen, DataLen);
2115 }
2116
2117 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2118 using namespace clang::io;
2119
2120 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2121 Emit8(Out, Name.getNameKind());
2122 switch (Name.getNameKind()) {
2123 case DeclarationName::Identifier:
2124 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2125 break;
2126 case DeclarationName::ObjCZeroArgSelector:
2127 case DeclarationName::ObjCOneArgSelector:
2128 case DeclarationName::ObjCMultiArgSelector:
2129 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2130 break;
2131 case DeclarationName::CXXConstructorName:
2132 case DeclarationName::CXXDestructorName:
2133 case DeclarationName::CXXConversionFunctionName:
2134 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2135 break;
2136 case DeclarationName::CXXOperatorName:
2137 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2138 Emit8(Out, Name.getCXXOverloadedOperator());
2139 break;
2140 case DeclarationName::CXXLiteralOperatorName:
2141 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2142 break;
2143 case DeclarationName::CXXUsingDirective:
2144 break;
2145 }
2146 }
2147
2148 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2149 data_type Lookup, unsigned DataLen) {
2150 uint64_t Start = Out.tell(); (void)Start;
2151 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2152 for (; Lookup.first != Lookup.second; ++Lookup.first)
2153 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2154
2155 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2156 }
2157};
2158} // end anonymous namespace
2159
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002160/// \brief Write the block containing all of the declaration IDs
2161/// visible from the given DeclContext.
2162///
2163/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002164/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002165uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2166 DeclContext *DC) {
2167 if (DC->getPrimaryContext() != DC)
2168 return 0;
2169
2170 // Since there is no name lookup into functions or methods, don't bother to
2171 // build a visible-declarations table for these entities.
2172 if (DC->isFunctionOrMethod())
2173 return 0;
2174
2175 // If not in C++, we perform name lookup for the translation unit via the
2176 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2177 // FIXME: In C++ we need the visible declarations in order to "see" the
2178 // friend declarations, is there a way to do this without writing the table ?
2179 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2180 return 0;
2181
2182 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00002183 if (DC->hasExternalVisibleStorage())
2184 DC->MaterializeVisibleDeclsFromExternalStorage();
2185 else
2186 DC->lookup(DeclarationName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002187
2188 // Serialize the contents of the mapping used for lookup. Note that,
2189 // although we have two very different code paths, the serialized
2190 // representation is the same for both cases: a declaration name,
2191 // followed by a size, followed by references to the visible
2192 // declarations that have that name.
2193 uint64_t Offset = Stream.GetCurrentBitNo();
2194 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2195 if (!Map || Map->empty())
2196 return 0;
2197
2198 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2199 ASTDeclContextNameLookupTrait Trait(*this);
2200
2201 // Create the on-disk hash table representation.
2202 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2203 D != DEnd; ++D) {
2204 DeclarationName Name = D->first;
2205 DeclContext::lookup_result Result = D->second.getLookupResult();
2206 Generator.insert(Name, Result, Trait);
2207 }
2208
2209 // Create the on-disk hash table in a buffer.
2210 llvm::SmallString<4096> LookupTable;
2211 uint32_t BucketOffset;
2212 {
2213 llvm::raw_svector_ostream Out(LookupTable);
2214 // Make sure that no bucket is at offset 0
2215 clang::io::Emit32(Out, 0);
2216 BucketOffset = Generator.Emit(Out, Trait);
2217 }
2218
2219 // Write the lookup table
2220 RecordData Record;
2221 Record.push_back(DECL_CONTEXT_VISIBLE);
2222 Record.push_back(BucketOffset);
2223 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2224 LookupTable.str());
2225
2226 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2227 ++NumVisibleDeclContexts;
2228 return Offset;
2229}
2230
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002231/// \brief Write an UPDATE_VISIBLE block for the given context.
2232///
2233/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2234/// DeclContext in a dependent AST file. As such, they only exist for the TU
2235/// (in C++) and for namespaces.
2236void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002237 // Make the context build its lookup table, but don't make it load external
2238 // decls.
2239 DC->lookup(DeclarationName());
2240
2241 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2242 if (!Map || Map->empty())
2243 return;
2244
2245 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2246 ASTDeclContextNameLookupTrait Trait(*this);
2247
2248 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002249 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2250 D != DEnd; ++D) {
2251 DeclarationName Name = D->first;
2252 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002253 // For any name that appears in this table, the results are complete, i.e.
2254 // they overwrite results from previous PCHs. Merging is always a mess.
2255 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002256 }
2257
2258 // Create the on-disk hash table in a buffer.
2259 llvm::SmallString<4096> LookupTable;
2260 uint32_t BucketOffset;
2261 {
2262 llvm::raw_svector_ostream Out(LookupTable);
2263 // Make sure that no bucket is at offset 0
2264 clang::io::Emit32(Out, 0);
2265 BucketOffset = Generator.Emit(Out, Trait);
2266 }
2267
2268 // Write the lookup table
2269 RecordData Record;
2270 Record.push_back(UPDATE_VISIBLE);
2271 Record.push_back(getDeclID(cast<Decl>(DC)));
2272 Record.push_back(BucketOffset);
2273 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2274}
2275
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002276//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002277// General Serialization Routines
2278//===----------------------------------------------------------------------===//
2279
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002280/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002281void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00002282 Record.push_back(Attrs.size());
Sean Huntcf807c42010-08-18 23:23:40 +00002283 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2284 const Attr * A = *i;
2285 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2286 AddSourceLocation(A->getLocation(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002287
Sean Huntcf807c42010-08-18 23:23:40 +00002288#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00002289
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002290 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002291}
2292
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002293void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002294 Record.push_back(Str.size());
2295 Record.insert(Record.end(), Str.begin(), Str.end());
2296}
2297
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002298/// \brief Note that the identifier II occurs at the given offset
2299/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002300void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002301 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00002302 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002303 // up earlier in the chain and thus don't need an offset.
2304 if (ID >= FirstIdentID)
2305 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002306}
2307
Douglas Gregor83941df2009-04-25 17:48:32 +00002308/// \brief Note that the selector Sel occurs at the given offset
2309/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002310void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00002311 unsigned ID = SelectorIDs[Sel];
2312 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00002313 // Don't record offsets for selectors that are also available in a different
2314 // file.
2315 if (ID < FirstSelectorID)
2316 return;
2317 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00002318}
2319
Sebastian Redla4232eb2010-08-18 23:56:21 +00002320ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregor89d99802010-11-30 06:16:57 +00002321 : Stream(Stream), Chain(0), SerializationListener(0),
2322 FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002323 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redle58aa892010-08-04 18:21:41 +00002324 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor77424bc2010-10-02 19:29:26 +00002325 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2326 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00002327 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor7c789c12010-10-29 22:39:52 +00002328 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
2329 NextCXXBaseSpecifiersID(1)
2330{
Sebastian Redl30c514c2010-07-14 23:45:08 +00002331}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002332
Sebastian Redla4232eb2010-08-18 23:56:21 +00002333void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002334 const char *isysroot) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002335 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002336 Stream.Emit((unsigned)'C', 8);
2337 Stream.Emit((unsigned)'P', 8);
2338 Stream.Emit((unsigned)'C', 8);
2339 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002340
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002341 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002342
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002343 if (Chain)
Sebastian Redla4232eb2010-08-18 23:56:21 +00002344 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002345 else
Sebastian Redla4232eb2010-08-18 23:56:21 +00002346 WriteASTCore(SemaRef, StatCalls, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002347}
2348
Sebastian Redla4232eb2010-08-18 23:56:21 +00002349void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002350 const char *isysroot) {
2351 using namespace llvm;
2352
2353 ASTContext &Context = SemaRef.Context;
2354 Preprocessor &PP = SemaRef.PP;
2355
Douglas Gregor2cf26342009-04-09 22:27:44 +00002356 // The translation unit is the first declaration we'll emit.
2357 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002358 ++NextDeclID;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002359 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002360
Douglas Gregor2deaea32009-04-22 18:49:13 +00002361 // Make sure that we emit IdentifierInfos (and any attached
2362 // declarations) for builtins.
2363 {
2364 IdentifierTable &Table = PP.getIdentifierTable();
2365 llvm::SmallVector<const char *, 32> BuiltinNames;
2366 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2367 Context.getLangOptions().NoBuiltin);
2368 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2369 getIdentifierRef(&Table.get(BuiltinNames[I]));
2370 }
2371
Chris Lattner63d65f82009-09-08 18:19:27 +00002372 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002373 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002374 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002375 RecordData TentativeDefinitions;
Sebastian Redle9d12b62010-01-31 22:27:38 +00002376 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2377 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner63d65f82009-09-08 18:19:27 +00002378 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002379
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002380 // Build a record containing all of the file scoped decls in this file.
2381 RecordData UnusedFileScopedDecls;
2382 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2383 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00002384
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002385 RecordData WeakUndeclaredIdentifiers;
2386 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2387 WeakUndeclaredIdentifiers.push_back(
2388 SemaRef.WeakUndeclaredIdentifiers.size());
2389 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2390 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2391 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2392 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2393 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2394 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2395 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2396 }
2397 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002398
Douglas Gregor14c22f22009-04-22 22:18:58 +00002399 // Build a record containing all of the locally-scoped external
2400 // declarations in this header file. Generally, this record will be
2401 // empty.
2402 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002403 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00002404 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002405 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002406 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2407 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2408 TD != TDEnd; ++TD)
2409 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2410
Douglas Gregorb81c1702009-04-27 20:06:05 +00002411 // Build a record containing all of the ext_vector declarations.
2412 RecordData ExtVectorDecls;
2413 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2414 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2415
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002416 // Build a record containing all of the VTable uses information.
2417 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00002418 if (!SemaRef.VTableUses.empty()) {
2419 VTableUses.push_back(SemaRef.VTableUses.size());
2420 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2421 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2422 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2423 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2424 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002425 }
2426
2427 // Build a record containing all of dynamic classes declarations.
2428 RecordData DynamicClasses;
2429 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2430 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2431
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002432 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002433 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002434 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00002435 I = SemaRef.PendingInstantiations.begin(),
2436 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2437 AddDeclRef(I->first, PendingInstantiations);
2438 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002439 }
2440 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2441 "There are local ones at end of translation unit!");
2442
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002443 // Build a record containing some declaration references.
2444 RecordData SemaDeclRefs;
2445 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2446 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2447 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2448 }
2449
Sebastian Redl3397c552010-08-18 23:56:27 +00002450 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002451 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002452 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl30c514c2010-07-14 23:45:08 +00002453 WriteMetadata(Context, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002454 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00002455 if (StatCalls && !isysroot)
Douglas Gregordd41ed52010-07-12 23:48:14 +00002456 WriteStatCache(*StatCalls);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002457 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002458 // Write the record of special types.
2459 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00002460
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002461 AddTypeRef(Context.getBuiltinVaListType(), Record);
2462 AddTypeRef(Context.getObjCIdType(), Record);
2463 AddTypeRef(Context.getObjCSelType(), Record);
2464 AddTypeRef(Context.getObjCProtoType(), Record);
2465 AddTypeRef(Context.getObjCClassType(), Record);
2466 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2467 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2468 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002469 AddTypeRef(Context.getjmp_bufType(), Record);
2470 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002471 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2472 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpadaaad32009-10-20 02:12:22 +00002473 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002474 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002475 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2476 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002477 Record.push_back(Context.isInt128Installed());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002478 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002479
Douglas Gregor366809a2009-04-26 03:49:13 +00002480 // Keep writing types and declarations until all types and
2481 // declarations have been written.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002482 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002483 WriteDeclsBlockAbbrevs();
2484 while (!DeclTypesToEmit.empty()) {
2485 DeclOrType DOT = DeclTypesToEmit.front();
2486 DeclTypesToEmit.pop();
2487 if (DOT.isType())
2488 WriteType(DOT.getType());
2489 else
2490 WriteDecl(Context, DOT.getDecl());
2491 }
2492 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002493
Douglas Gregor813a97b2009-10-17 17:25:45 +00002494 WritePreprocessor(PP);
Sebastian Redl059612d2010-08-03 21:58:15 +00002495 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002496 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002497 WriteIdentifierTable(PP);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002498
Sebastian Redl1476ed42010-07-16 16:36:56 +00002499 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002500 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00002501
Douglas Gregor7c789c12010-10-29 22:39:52 +00002502 // Write the C++ base-specifier set offsets.
2503 if (!CXXBaseSpecifiersOffsets.empty()) {
2504 // Create a blob abbreviation for the C++ base specifiers offsets.
2505 using namespace llvm;
2506
2507 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2508 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2509 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2510 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2511 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2512
2513 // Write the selector offsets table.
2514 Record.clear();
2515 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2516 Record.push_back(CXXBaseSpecifiersOffsets.size());
2517 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
2518 (const char *)CXXBaseSpecifiersOffsets.data(),
2519 CXXBaseSpecifiersOffsets.size() * sizeof(uint32_t));
2520 }
2521
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002522 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002523 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002524 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002525
2526 // Write the record containing tentative definitions.
2527 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002528 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002529
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002530 // Write the record containing unused file scoped decls.
2531 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002532 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002533
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002534 // Write the record containing weak undeclared identifiers.
2535 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002536 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002537 WeakUndeclaredIdentifiers);
2538
Douglas Gregor14c22f22009-04-22 22:18:58 +00002539 // Write the record containing locally-scoped external definitions.
2540 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002541 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002542 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002543
2544 // Write the record containing ext_vector type names.
2545 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002546 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002547
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002548 // Write the record containing VTable uses information.
2549 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002550 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002551
2552 // Write the record containing dynamic classes declarations.
2553 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002554 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002555
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002556 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002557 if (!PendingInstantiations.empty())
2558 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002559
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002560 // Write the record containing declaration references of Sema.
2561 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002562 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002563
Douglas Gregor3e1af842009-04-17 22:13:46 +00002564 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002565 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002566 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002567 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002568 Record.push_back(NumLexicalDeclContexts);
2569 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002570 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002571 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002572}
2573
Sebastian Redla4232eb2010-08-18 23:56:21 +00002574void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002575 const char *isysroot) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002576 using namespace llvm;
2577
2578 ASTContext &Context = SemaRef.Context;
2579 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1476ed42010-07-16 16:36:56 +00002580
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002581 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002582 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl30c514c2010-07-14 23:45:08 +00002583 WriteMetadata(Context, isysroot);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002584 if (StatCalls && !isysroot)
2585 WriteStatCache(*StatCalls);
2586 // FIXME: Source manager block should only write new stuff, which could be
2587 // done by tracking the largest ID in the chain
2588 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002589
2590 // The special types are in the chained PCH.
2591
2592 // We don't start with the translation unit, but with its decls that
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002593 // don't come from the chained PCH.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002594 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002595 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl681d7232010-07-27 00:17:23 +00002596 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2597 E = TU->noload_decls_end();
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002598 I != E; ++I) {
Sebastian Redld692af72010-07-27 18:24:41 +00002599 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002600 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redl0b17c612010-08-13 00:28:03 +00002601 else if ((*I)->isChangedSinceDeserialization())
2602 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002603 }
Sebastian Redl681d7232010-07-27 00:17:23 +00002604 // We also need to write a lexical updates block for the TU.
Sebastian Redld692af72010-07-27 18:24:41 +00002605 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002606 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redld692af72010-07-27 18:24:41 +00002607 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2608 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2609 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002610 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redld692af72010-07-27 18:24:41 +00002611 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2612 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002613 NewGlobalDecls.size() * sizeof(KindDeclIDPair));
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00002614 // And a visible updates block for the DeclContexts.
2615 Abv = new llvm::BitCodeAbbrev();
2616 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2617 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2618 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2619 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2620 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2621 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002622
Sebastian Redl083abdf2010-07-27 23:01:28 +00002623 // Build a record containing all of the new tentative definitions in this
2624 // file, in TentativeDefinitions order.
2625 RecordData TentativeDefinitions;
2626 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2627 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2628 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2629 }
2630
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002631 // Build a record containing all of the file scoped decls in this file.
2632 RecordData UnusedFileScopedDecls;
2633 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2634 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2635 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002636 }
2637
Sebastian Redl40566802010-08-05 18:21:25 +00002638 // We write the entire table, overwriting the tables from the chain.
2639 RecordData WeakUndeclaredIdentifiers;
2640 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2641 WeakUndeclaredIdentifiers.push_back(
2642 SemaRef.WeakUndeclaredIdentifiers.size());
2643 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2644 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2645 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2646 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2647 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2648 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2649 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2650 }
2651 }
2652
Sebastian Redl083abdf2010-07-27 23:01:28 +00002653 // Build a record containing all of the locally-scoped external
2654 // declarations in this header file. Generally, this record will be
2655 // empty.
2656 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002657 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl083abdf2010-07-27 23:01:28 +00002658 // nondeterminstic!
2659 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2660 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2661 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2662 TD != TDEnd; ++TD) {
2663 if (TD->second->getPCHLevel() == 0)
2664 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2665 }
2666
2667 // Build a record containing all of the ext_vector declarations.
2668 RecordData ExtVectorDecls;
2669 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2670 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2671 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2672 }
2673
Sebastian Redl40566802010-08-05 18:21:25 +00002674 // Build a record containing all of the VTable uses information.
2675 // We write everything here, because it's too hard to determine whether
2676 // a use is new to this part.
2677 RecordData VTableUses;
2678 if (!SemaRef.VTableUses.empty()) {
2679 VTableUses.push_back(SemaRef.VTableUses.size());
2680 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2681 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2682 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2683 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2684 }
2685 }
2686
2687 // Build a record containing all of dynamic classes declarations.
2688 RecordData DynamicClasses;
2689 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2690 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
2691 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2692
2693 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002694 RecordData PendingInstantiations;
Sebastian Redl40566802010-08-05 18:21:25 +00002695 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00002696 I = SemaRef.PendingInstantiations.begin(),
2697 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl40566802010-08-05 18:21:25 +00002698 if (I->first->getPCHLevel() == 0) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002699 AddDeclRef(I->first, PendingInstantiations);
2700 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl40566802010-08-05 18:21:25 +00002701 }
2702 }
2703 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2704 "There are local ones at end of translation unit!");
2705
2706 // Build a record containing some declaration references.
2707 // It's not worth the effort to avoid duplication here.
2708 RecordData SemaDeclRefs;
2709 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2710 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2711 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2712 }
2713
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002714 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002715 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00002716 for (DeclsToRewriteTy::iterator
2717 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
2718 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002719 while (!DeclTypesToEmit.empty()) {
2720 DeclOrType DOT = DeclTypesToEmit.front();
2721 DeclTypesToEmit.pop();
2722 if (DOT.isType())
2723 WriteType(DOT.getType());
2724 else
2725 WriteDecl(Context, DOT.getDecl());
2726 }
2727 Stream.ExitBlock();
2728
Sebastian Redl083abdf2010-07-27 23:01:28 +00002729 WritePreprocessor(PP);
Sebastian Redla68340f2010-08-04 22:21:29 +00002730 WriteSelectors(SemaRef);
2731 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002732 WriteIdentifierTable(PP);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002733 WriteTypeDeclOffsets();
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002734 // FIXME: For chained PCH only write the new mappings (we currently
2735 // write all of them again).
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002736 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl083abdf2010-07-27 23:01:28 +00002737
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002738 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl3397c552010-08-18 23:56:27 +00002739 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002740 RecordData FirstLatestDeclIDs;
2741 for (FirstLatestDeclMap::iterator
2742 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
2743 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
2744 "Expected first & second to be in different PCHs");
2745 AddDeclRef(I->first, FirstLatestDeclIDs);
2746 AddDeclRef(I->second, FirstLatestDeclIDs);
2747 }
2748 if (!FirstLatestDeclIDs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002749 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002750
Sebastian Redl083abdf2010-07-27 23:01:28 +00002751 // Write the record containing external, unnamed definitions.
2752 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002753 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002754
2755 // Write the record containing tentative definitions.
2756 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002757 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002758
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002759 // Write the record containing unused file scoped decls.
2760 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002761 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002762
Sebastian Redl40566802010-08-05 18:21:25 +00002763 // Write the record containing weak undeclared identifiers.
2764 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002765 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl40566802010-08-05 18:21:25 +00002766 WeakUndeclaredIdentifiers);
2767
Sebastian Redl083abdf2010-07-27 23:01:28 +00002768 // Write the record containing locally-scoped external definitions.
2769 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002770 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl083abdf2010-07-27 23:01:28 +00002771 LocallyScopedExternalDecls);
2772
2773 // Write the record containing ext_vector type names.
2774 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002775 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002776
Sebastian Redl40566802010-08-05 18:21:25 +00002777 // Write the record containing VTable uses information.
2778 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002779 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl40566802010-08-05 18:21:25 +00002780
2781 // Write the record containing dynamic classes declarations.
2782 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002783 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl40566802010-08-05 18:21:25 +00002784
2785 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002786 if (!PendingInstantiations.empty())
2787 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl40566802010-08-05 18:21:25 +00002788
2789 // Write the record containing declaration references of Sema.
2790 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002791 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002792
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00002793 // Write the updates to DeclContexts.
2794 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
2795 I = UpdatedDeclContexts.begin(),
2796 E = UpdatedDeclContexts.end();
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002797 I != E; ++I)
2798 WriteDeclContextVisibleUpdate(*I);
2799
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00002800 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002801
Sebastian Redl083abdf2010-07-27 23:01:28 +00002802 Record.clear();
2803 Record.push_back(NumStatements);
2804 Record.push_back(NumMacros);
2805 Record.push_back(NumLexicalDeclContexts);
2806 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00002807 WriteDeclReplacementsBlock();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002808 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002809 Stream.ExitBlock();
2810}
2811
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00002812void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002813 if (DeclUpdates.empty())
2814 return;
2815
2816 RecordData OffsetsRecord;
2817 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
2818 for (DeclUpdateMap::iterator
2819 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
2820 const Decl *D = I->first;
2821 UpdateRecord &URec = I->second;
2822
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00002823 if (DeclsToRewrite.count(D))
2824 continue; // The decl will be written completely,no need to store updates.
2825
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002826 uint64_t Offset = Stream.GetCurrentBitNo();
2827 Stream.EmitRecord(DECL_UPDATES, URec);
2828
2829 OffsetsRecord.push_back(GetDeclRef(D));
2830 OffsetsRecord.push_back(Offset);
2831 }
2832 Stream.ExitBlock();
2833 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
2834}
2835
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00002836void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00002837 if (ReplacedDecls.empty())
2838 return;
2839
2840 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002841 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00002842 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
2843 Record.push_back(I->first);
2844 Record.push_back(I->second);
2845 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002846 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00002847}
2848
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002849void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002850 Record.push_back(Loc.getRawEncoding());
2851}
2852
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002853void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002854 AddSourceLocation(Range.getBegin(), Record);
2855 AddSourceLocation(Range.getEnd(), Record);
2856}
2857
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002858void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002859 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00002860 const uint64_t *Words = Value.getRawData();
2861 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002862}
2863
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002864void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002865 Record.push_back(Value.isUnsigned());
2866 AddAPInt(Value, Record);
2867}
2868
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002869void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00002870 AddAPInt(Value.bitcastToAPInt(), Record);
2871}
2872
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002873void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002874 Record.push_back(getIdentifierRef(II));
2875}
2876
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002877IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002878 if (II == 0)
2879 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002880
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002881 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00002882 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002883 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00002884 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002885}
2886
Sebastian Redlf73c93f2010-09-15 19:54:06 +00002887MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002888 if (MD == 0)
2889 return 0;
Sebastian Redlf73c93f2010-09-15 19:54:06 +00002890
2891 MacroID &ID = MacroDefinitions[MD];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002892 if (ID == 0)
Douglas Gregor77424bc2010-10-02 19:29:26 +00002893 ID = NextMacroID++;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002894 return ID;
2895}
2896
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002897void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00002898 Record.push_back(getSelectorRef(SelRef));
2899}
2900
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002901SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00002902 if (Sel.getAsOpaquePtr() == 0) {
2903 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002904 }
2905
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002906 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00002907 if (SID == 0 && Chain) {
2908 // This might trigger a ReadSelector callback, which will set the ID for
2909 // this selector.
2910 Chain->LoadSelector(Sel);
2911 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002912 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00002913 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002914 }
Sebastian Redl5d050072010-08-04 17:20:04 +00002915 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002916}
2917
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002918void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00002919 AddDeclRef(Temp->getDestructor(), Record);
2920}
2921
Douglas Gregor7c789c12010-10-29 22:39:52 +00002922void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
2923 CXXBaseSpecifier const *BasesEnd,
2924 RecordDataImpl &Record) {
2925 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
2926 CXXBaseSpecifiersToWrite.push_back(
2927 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
2928 Bases, BasesEnd));
2929 Record.push_back(NextCXXBaseSpecifiersID++);
2930}
2931
Sebastian Redla4232eb2010-08-18 23:56:21 +00002932void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002933 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002934 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002935 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00002936 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002937 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00002938 break;
2939 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002940 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00002941 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00002942 case TemplateArgument::Template:
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002943 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2944 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00002945 break;
2946 case TemplateArgument::TemplateExpansion:
2947 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2948 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00002949 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00002950 break;
John McCall833ca992009-10-29 08:12:44 +00002951 case TemplateArgument::Null:
2952 case TemplateArgument::Integral:
2953 case TemplateArgument::Declaration:
2954 case TemplateArgument::Pack:
2955 break;
2956 }
2957}
2958
Sebastian Redla4232eb2010-08-18 23:56:21 +00002959void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002960 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002961 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002962
2963 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
2964 bool InfoHasSameExpr
2965 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
2966 Record.push_back(InfoHasSameExpr);
2967 if (InfoHasSameExpr)
2968 return; // Avoid storing the same expr twice.
2969 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002970 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
2971 Record);
2972}
2973
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002974void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00002975 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00002976 AddTypeRef(QualType(), Record);
2977 return;
2978 }
2979
John McCalla93c9342009-12-07 02:54:59 +00002980 AddTypeRef(TInfo->getType(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +00002981 TypeLocWriter TLW(*this, Record);
John McCalla93c9342009-12-07 02:54:59 +00002982 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002983 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00002984}
2985
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002986void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00002987 Record.push_back(GetOrCreateTypeID(T));
2988}
2989
2990TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00002991 return MakeTypeID(T,
2992 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
2993}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002994
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002995TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00002996 return MakeTypeID(T,
2997 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00002998}
2999
3000TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3001 if (T.isNull())
3002 return TypeIdx();
3003 assert(!T.getLocalFastQualifiers());
3004
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00003005 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003006 if (Idx.getIndex() == 0) {
Douglas Gregor366809a2009-04-26 03:49:13 +00003007 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00003008 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003009 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003010 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00003011 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003012 return Idx;
3013}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003014
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003015TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003016 if (T.isNull())
3017 return TypeIdx();
3018 assert(!T.getLocalFastQualifiers());
3019
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003020 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3021 assert(I != TypeIdxs.end() && "Type not emitted!");
3022 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003023}
3024
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003025void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003026 Record.push_back(GetDeclRef(D));
3027}
3028
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003029DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003030 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003031 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003032 }
Douglas Gregor97475832010-10-05 18:37:06 +00003033 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003034 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00003035 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003036 // We haven't seen this declaration before. Give it a new ID and
3037 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003038 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003039 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redl0b17c612010-08-13 00:28:03 +00003040 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3041 // We don't add it to the replacement collection here, because we don't
3042 // have the offset yet.
3043 DeclTypesToEmit.push(const_cast<Decl *>(D));
3044 // Reset the flag, so that we don't add this decl multiple times.
3045 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003046 }
3047
Sebastian Redl681d7232010-07-27 00:17:23 +00003048 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003049}
3050
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003051DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003052 if (D == 0)
3053 return 0;
3054
3055 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3056 return DeclIDs[D];
3057}
3058
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003059void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00003060 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00003061 Record.push_back(Name.getNameKind());
3062 switch (Name.getNameKind()) {
3063 case DeclarationName::Identifier:
3064 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3065 break;
3066
3067 case DeclarationName::ObjCZeroArgSelector:
3068 case DeclarationName::ObjCOneArgSelector:
3069 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003070 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003071 break;
3072
3073 case DeclarationName::CXXConstructorName:
3074 case DeclarationName::CXXDestructorName:
3075 case DeclarationName::CXXConversionFunctionName:
3076 AddTypeRef(Name.getCXXNameType(), Record);
3077 break;
3078
3079 case DeclarationName::CXXOperatorName:
3080 Record.push_back(Name.getCXXOverloadedOperator());
3081 break;
3082
Sean Hunt3e518bd2009-11-29 07:34:05 +00003083 case DeclarationName::CXXLiteralOperatorName:
3084 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3085 break;
3086
Douglas Gregor2cf26342009-04-09 22:27:44 +00003087 case DeclarationName::CXXUsingDirective:
3088 // No extra data to emit
3089 break;
3090 }
3091}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003092
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003093void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003094 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003095 switch (Name.getNameKind()) {
3096 case DeclarationName::CXXConstructorName:
3097 case DeclarationName::CXXDestructorName:
3098 case DeclarationName::CXXConversionFunctionName:
3099 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3100 break;
3101
3102 case DeclarationName::CXXOperatorName:
3103 AddSourceLocation(
3104 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3105 Record);
3106 AddSourceLocation(
3107 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3108 Record);
3109 break;
3110
3111 case DeclarationName::CXXLiteralOperatorName:
3112 AddSourceLocation(
3113 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3114 Record);
3115 break;
3116
3117 case DeclarationName::Identifier:
3118 case DeclarationName::ObjCZeroArgSelector:
3119 case DeclarationName::ObjCOneArgSelector:
3120 case DeclarationName::ObjCMultiArgSelector:
3121 case DeclarationName::CXXUsingDirective:
3122 break;
3123 }
3124}
3125
3126void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003127 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003128 AddDeclarationName(NameInfo.getName(), Record);
3129 AddSourceLocation(NameInfo.getLoc(), Record);
3130 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3131}
3132
3133void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003134 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003135 AddNestedNameSpecifier(Info.NNS, Record);
3136 AddSourceRange(Info.NNSRange, Record);
3137 Record.push_back(Info.NumTemplParamLists);
3138 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3139 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3140}
3141
Sebastian Redla4232eb2010-08-18 23:56:21 +00003142void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003143 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003144 // Nested name specifiers usually aren't too long. I think that 8 would
3145 // typically accomodate the vast majority.
3146 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3147
3148 // Push each of the NNS's onto a stack for serialization in reverse order.
3149 while (NNS) {
3150 NestedNames.push_back(NNS);
3151 NNS = NNS->getPrefix();
3152 }
3153
3154 Record.push_back(NestedNames.size());
3155 while(!NestedNames.empty()) {
3156 NNS = NestedNames.pop_back_val();
3157 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3158 Record.push_back(Kind);
3159 switch (Kind) {
3160 case NestedNameSpecifier::Identifier:
3161 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3162 break;
3163
3164 case NestedNameSpecifier::Namespace:
3165 AddDeclRef(NNS->getAsNamespace(), Record);
3166 break;
3167
3168 case NestedNameSpecifier::TypeSpec:
3169 case NestedNameSpecifier::TypeSpecWithTemplate:
3170 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3171 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3172 break;
3173
3174 case NestedNameSpecifier::Global:
3175 // Don't need to write an associated value.
3176 break;
3177 }
3178 }
3179}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003180
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003181void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00003182 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003183 Record.push_back(Kind);
3184 switch (Kind) {
3185 case TemplateName::Template:
3186 AddDeclRef(Name.getAsTemplateDecl(), Record);
3187 break;
3188
3189 case TemplateName::OverloadedTemplate: {
3190 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3191 Record.push_back(OvT->size());
3192 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3193 I != E; ++I)
3194 AddDeclRef(*I, Record);
3195 break;
3196 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003197
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003198 case TemplateName::QualifiedTemplate: {
3199 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3200 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3201 Record.push_back(QualT->hasTemplateKeyword());
3202 AddDeclRef(QualT->getTemplateDecl(), Record);
3203 break;
3204 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003205
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003206 case TemplateName::DependentTemplate: {
3207 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3208 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3209 Record.push_back(DepT->isIdentifier());
3210 if (DepT->isIdentifier())
3211 AddIdentifierRef(DepT->getIdentifier(), Record);
3212 else
3213 Record.push_back(DepT->getOperator());
3214 break;
3215 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00003216
3217 case TemplateName::SubstTemplateTemplateParmPack: {
3218 SubstTemplateTemplateParmPackStorage *SubstPack
3219 = Name.getAsSubstTemplateTemplateParmPack();
3220 AddDeclRef(SubstPack->getParameterPack(), Record);
3221 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
3222 break;
3223 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003224 }
3225}
3226
Michael J. Spencer20249a12010-10-21 03:16:25 +00003227void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003228 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003229 Record.push_back(Arg.getKind());
3230 switch (Arg.getKind()) {
3231 case TemplateArgument::Null:
3232 break;
3233 case TemplateArgument::Type:
3234 AddTypeRef(Arg.getAsType(), Record);
3235 break;
3236 case TemplateArgument::Declaration:
3237 AddDeclRef(Arg.getAsDecl(), Record);
3238 break;
3239 case TemplateArgument::Integral:
3240 AddAPSInt(*Arg.getAsIntegral(), Record);
3241 AddTypeRef(Arg.getIntegralType(), Record);
3242 break;
3243 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00003244 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3245 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00003246 case TemplateArgument::TemplateExpansion:
3247 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00003248 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3249 Record.push_back(*NumExpansions + 1);
3250 else
3251 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003252 break;
3253 case TemplateArgument::Expression:
3254 AddStmt(Arg.getAsExpr());
3255 break;
3256 case TemplateArgument::Pack:
3257 Record.push_back(Arg.pack_size());
3258 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3259 I != E; ++I)
3260 AddTemplateArgument(*I, Record);
3261 break;
3262 }
3263}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003264
3265void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003266ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003267 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003268 assert(TemplateParams && "No TemplateParams!");
3269 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3270 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3271 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3272 Record.push_back(TemplateParams->size());
3273 for (TemplateParameterList::const_iterator
3274 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3275 P != PEnd; ++P)
3276 AddDeclRef(*P, Record);
3277}
3278
3279/// \brief Emit a template argument list.
3280void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003281ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003282 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003283 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00003284 Record.push_back(TemplateArgs->size());
3285 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003286 AddTemplateArgument(TemplateArgs->get(i), Record);
3287}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003288
3289
3290void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003291ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003292 Record.push_back(Set.size());
3293 for (UnresolvedSetImpl::const_iterator
3294 I = Set.begin(), E = Set.end(); I != E; ++I) {
3295 AddDeclRef(I.getDecl(), Record);
3296 Record.push_back(I.getAccess());
3297 }
3298}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003299
Sebastian Redla4232eb2010-08-18 23:56:21 +00003300void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003301 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003302 Record.push_back(Base.isVirtual());
3303 Record.push_back(Base.isBaseOfClass());
3304 Record.push_back(Base.getAccessSpecifierAsWritten());
Nick Lewycky56062202010-07-26 16:56:01 +00003305 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003306 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00003307 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3308 : SourceLocation(),
3309 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003310}
Sebastian Redl30c514c2010-07-14 23:45:08 +00003311
Douglas Gregor7c789c12010-10-29 22:39:52 +00003312void ASTWriter::FlushCXXBaseSpecifiers() {
3313 RecordData Record;
3314 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3315 Record.clear();
3316
3317 // Record the offset of this base-specifier set.
3318 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3319 if (Index == CXXBaseSpecifiersOffsets.size())
3320 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3321 else {
3322 if (Index > CXXBaseSpecifiersOffsets.size())
3323 CXXBaseSpecifiersOffsets.resize(Index + 1);
3324 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3325 }
3326
3327 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3328 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3329 Record.push_back(BEnd - B);
3330 for (; B != BEnd; ++B)
3331 AddCXXBaseSpecifier(*B, Record);
3332 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00003333
3334 // Flush any expressions that were written as part of the base specifiers.
3335 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003336 }
3337
3338 CXXBaseSpecifiersToWrite.clear();
3339}
3340
Sean Huntcbb67482011-01-08 20:30:50 +00003341void ASTWriter::AddCXXCtorInitializers(
3342 const CXXCtorInitializer * const *CtorInitializers,
3343 unsigned NumCtorInitializers,
3344 RecordDataImpl &Record) {
3345 Record.push_back(NumCtorInitializers);
3346 for (unsigned i=0; i != NumCtorInitializers; ++i) {
3347 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003348
3349 Record.push_back(Init->isBaseInitializer());
3350 if (Init->isBaseInitializer()) {
3351 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3352 Record.push_back(Init->isBaseVirtual());
3353 } else {
Francois Pichet00eb3f92010-12-04 09:14:42 +00003354 Record.push_back(Init->isIndirectMemberInitializer());
3355 if (Init->isIndirectMemberInitializer())
3356 AddDeclRef(Init->getIndirectMember(), Record);
3357 else
3358 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003359 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00003360
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003361 AddSourceLocation(Init->getMemberLocation(), Record);
3362 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003363 AddSourceLocation(Init->getLParenLoc(), Record);
3364 AddSourceLocation(Init->getRParenLoc(), Record);
3365 Record.push_back(Init->isWritten());
3366 if (Init->isWritten()) {
3367 Record.push_back(Init->getSourceOrder());
3368 } else {
3369 Record.push_back(Init->getNumArrayIndices());
3370 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3371 AddDeclRef(Init->getArrayIndex(i), Record);
3372 }
3373 }
3374}
3375
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003376void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3377 assert(D->DefinitionData);
3378 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3379 Record.push_back(Data.UserDeclaredConstructor);
3380 Record.push_back(Data.UserDeclaredCopyConstructor);
3381 Record.push_back(Data.UserDeclaredCopyAssignment);
3382 Record.push_back(Data.UserDeclaredDestructor);
3383 Record.push_back(Data.Aggregate);
3384 Record.push_back(Data.PlainOldData);
3385 Record.push_back(Data.Empty);
3386 Record.push_back(Data.Polymorphic);
3387 Record.push_back(Data.Abstract);
3388 Record.push_back(Data.HasTrivialConstructor);
3389 Record.push_back(Data.HasTrivialCopyConstructor);
3390 Record.push_back(Data.HasTrivialCopyAssignment);
3391 Record.push_back(Data.HasTrivialDestructor);
3392 Record.push_back(Data.ComputedVisibleConversions);
3393 Record.push_back(Data.DeclaredDefaultConstructor);
3394 Record.push_back(Data.DeclaredCopyConstructor);
3395 Record.push_back(Data.DeclaredCopyAssignment);
3396 Record.push_back(Data.DeclaredDestructor);
3397
3398 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003399 if (Data.NumBases > 0)
3400 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3401 Record);
3402
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003403 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3404 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003405 if (Data.NumVBases > 0)
3406 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3407 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003408
3409 AddUnresolvedSet(Data.Conversions, Record);
3410 AddUnresolvedSet(Data.VisibleConversions, Record);
3411 // Data.Definition is the owning decl, no need to write it.
3412 AddDeclRef(Data.FirstFriend, Record);
3413}
3414
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003415void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003416 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003417 assert(!Chain && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003418 assert(FirstDeclID == NextDeclID &&
3419 FirstTypeID == NextTypeID &&
3420 FirstIdentID == NextIdentID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00003421 FirstSelectorID == NextSelectorID &&
Douglas Gregor77424bc2010-10-02 19:29:26 +00003422 FirstMacroID == NextMacroID &&
Douglas Gregor7c789c12010-10-29 22:39:52 +00003423 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003424 "Setting chain after writing has started.");
3425 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003426
3427 FirstDeclID += Chain->getTotalNumDecls();
3428 FirstTypeID += Chain->getTotalNumTypes();
3429 FirstIdentID += Chain->getTotalNumIdentifiers();
3430 FirstSelectorID += Chain->getTotalNumSelectors();
3431 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003432 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003433 NextDeclID = FirstDeclID;
3434 NextTypeID = FirstTypeID;
3435 NextIdentID = FirstIdentID;
3436 NextSelectorID = FirstSelectorID;
3437 NextMacroID = FirstMacroID;
Douglas Gregor7c789c12010-10-29 22:39:52 +00003438 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003439}
3440
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003441void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003442 IdentifierIDs[II] = ID;
3443}
3444
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003445void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00003446 // Always take the highest-numbered type index. This copes with an interesting
3447 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00003448 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00003449 // keep the higher-numbered entry so that we can properly write it out to
3450 // the AST file.
3451 TypeIdx &StoredIdx = TypeIdxs[T];
3452 if (Idx.getIndex() >= StoredIdx.getIndex())
3453 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003454}
3455
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003456void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1476ed42010-07-16 16:36:56 +00003457 DeclIDs[D] = ID;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003458}
Sebastian Redl5d050072010-08-04 17:20:04 +00003459
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003460void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003461 SelectorIDs[S] = ID;
3462}
Douglas Gregor77424bc2010-10-02 19:29:26 +00003463
Michael J. Spencer20249a12010-10-21 03:16:25 +00003464void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00003465 MacroDefinition *MD) {
3466 MacroDefinitions[MD] = ID;
3467}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003468
3469void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3470 assert(D->isDefinition());
3471 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3472 // We are interested when a PCH decl is modified.
3473 if (RD->getPCHLevel() > 0) {
3474 // A forward reference was mutated into a definition. Rewrite it.
3475 // FIXME: This happens during template instantiation, should we
3476 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00003477 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003478 }
3479
3480 for (CXXRecordDecl::redecl_iterator
3481 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3482 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3483 if (Redecl == RD)
3484 continue;
3485
3486 // We are interested when a PCH decl is modified.
3487 if (Redecl->getPCHLevel() > 0) {
3488 UpdateRecord &Record = DeclUpdates[Redecl];
3489 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3490 assert(Redecl->DefinitionData);
3491 assert(Redecl->DefinitionData->Definition == D);
3492 AddDeclRef(D, Record); // the DefinitionDecl
3493 }
3494 }
3495 }
3496}
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00003497void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3498 // TU and namespaces are handled elsewhere.
3499 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3500 return;
3501
3502 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3503 return; // Not a source decl added to a DeclContext from PCH.
3504
3505 AddUpdatedDeclContext(DC);
3506}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00003507
3508void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3509 assert(D->isImplicit());
3510 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3511 return; // Not a source member added to a class from PCH.
3512 if (!isa<CXXMethodDecl>(D))
3513 return; // We are interested in lazily declared implicit methods.
3514
3515 // A decl coming from PCH was modified.
3516 assert(RD->isDefinition());
3517 UpdateRecord &Record = DeclUpdates[RD];
3518 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3519 AddDeclRef(D, Record);
3520}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00003521
3522void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3523 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00003524 // The specializations set is kept in the canonical template.
3525 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00003526 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3527 return; // Not a source specialization added to a template from PCH.
3528
3529 UpdateRecord &Record = DeclUpdates[TD];
3530 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3531 AddDeclRef(D, Record);
3532}
Douglas Gregor89d99802010-11-30 06:16:57 +00003533
3534ASTSerializationListener::~ASTSerializationListener() { }