blob: cbf5ac79192bac9957b75dc1c0b607df988c0968 [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
Mike Stump1eb44332009-09-09 15:08:12 +0000231void
Sebastian Redl3397c552010-08-18 23:56:27 +0000232ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000233 const SubstTemplateTypeParmType *T) {
234 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
235 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000236 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000237}
238
239void
Sebastian Redl3397c552010-08-18 23:56:27 +0000240ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000241 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000242 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000243 Writer.AddTemplateName(T->getTemplateName(), Record);
244 Record.push_back(T->getNumArgs());
245 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
246 ArgI != ArgE; ++ArgI)
247 Writer.AddTemplateArgument(*ArgI, Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000248 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
249 : T->getCanonicalTypeInternal(),
250 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000251 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000252}
253
254void
Sebastian Redl3397c552010-08-18 23:56:27 +0000255ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000256 VisitArrayType(T);
257 Writer.AddStmt(T->getSizeExpr());
258 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000259 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000260}
261
262void
Sebastian Redl3397c552010-08-18 23:56:27 +0000263ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000264 const DependentSizedExtVectorType *T) {
265 // FIXME: Serialize this type (C++ only)
266 assert(false && "Cannot serialize dependent sized extended vector types");
267}
268
269void
Sebastian Redl3397c552010-08-18 23:56:27 +0000270ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000271 Record.push_back(T->getDepth());
272 Record.push_back(T->getIndex());
273 Record.push_back(T->isParameterPack());
274 Writer.AddIdentifierRef(T->getName(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000275 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000276}
277
278void
Sebastian Redl3397c552010-08-18 23:56:27 +0000279ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000280 Record.push_back(T->getKeyword());
281 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
282 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000283 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
284 : T->getCanonicalTypeInternal(),
285 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000286 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000287}
288
289void
Sebastian Redl3397c552010-08-18 23:56:27 +0000290ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000291 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000292 Record.push_back(T->getKeyword());
293 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
294 Writer.AddIdentifierRef(T->getIdentifier(), Record);
295 Record.push_back(T->getNumArgs());
296 for (DependentTemplateSpecializationType::iterator
297 I = T->begin(), E = T->end(); I != E; ++I)
298 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000299 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000300}
301
Douglas Gregor7536dd52010-12-20 02:24:11 +0000302void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
303 Writer.AddTypeRef(T->getPattern(), Record);
304 Code = TYPE_PACK_EXPANSION;
305}
306
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000307void ASTTypeWriter::VisitParenType(const ParenType *T) {
308 Writer.AddTypeRef(T->getInnerType(), Record);
309 Code = TYPE_PAREN;
310}
311
Sebastian Redl3397c552010-08-18 23:56:27 +0000312void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000313 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000314 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
315 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000316 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000317}
318
Sebastian Redl3397c552010-08-18 23:56:27 +0000319void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCall3cb0ebd2010-03-10 03:28:59 +0000320 Writer.AddDeclRef(T->getDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000321 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000322 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000323}
324
Sebastian Redl3397c552010-08-18 23:56:27 +0000325void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000326 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000327 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000328}
329
Sebastian Redl3397c552010-08-18 23:56:27 +0000330void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000331 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000332 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000333 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000334 E = T->qual_end(); I != E; ++I)
335 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000336 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000337}
338
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000339void
Sebastian Redl3397c552010-08-18 23:56:27 +0000340ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000341 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000342 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000343}
344
John McCalla1ee0c52009-10-16 21:56:05 +0000345namespace {
346
347class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000348 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000349 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000350
351public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000352 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000353 : Writer(Writer), Record(Record) { }
354
John McCall51bd8032009-10-18 01:05:36 +0000355#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000356#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000357 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000358#include "clang/AST/TypeLocNodes.def"
359
John McCall51bd8032009-10-18 01:05:36 +0000360 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
361 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000362};
363
364}
365
John McCall51bd8032009-10-18 01:05:36 +0000366void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
367 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000368}
John McCall51bd8032009-10-18 01:05:36 +0000369void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000370 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
371 if (TL.needsExtraLocalData()) {
372 Record.push_back(TL.getWrittenTypeSpec());
373 Record.push_back(TL.getWrittenSignSpec());
374 Record.push_back(TL.getWrittenWidthSpec());
375 Record.push_back(TL.hasModeAttr());
376 }
John McCalla1ee0c52009-10-16 21:56:05 +0000377}
John McCall51bd8032009-10-18 01:05:36 +0000378void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
379 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000380}
John McCall51bd8032009-10-18 01:05:36 +0000381void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
382 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000383}
John McCall51bd8032009-10-18 01:05:36 +0000384void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
385 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000386}
John McCall51bd8032009-10-18 01:05:36 +0000387void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
388 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000389}
John McCall51bd8032009-10-18 01:05:36 +0000390void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
391 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000392}
John McCall51bd8032009-10-18 01:05:36 +0000393void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
394 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000395}
John McCall51bd8032009-10-18 01:05:36 +0000396void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
397 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
398 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
399 Record.push_back(TL.getSizeExpr() ? 1 : 0);
400 if (TL.getSizeExpr())
401 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000402}
John McCall51bd8032009-10-18 01:05:36 +0000403void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
404 VisitArrayTypeLoc(TL);
405}
406void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
407 VisitArrayTypeLoc(TL);
408}
409void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
410 VisitArrayTypeLoc(TL);
411}
412void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
413 DependentSizedArrayTypeLoc TL) {
414 VisitArrayTypeLoc(TL);
415}
416void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
417 DependentSizedExtVectorTypeLoc TL) {
418 Writer.AddSourceLocation(TL.getNameLoc(), Record);
419}
420void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
421 Writer.AddSourceLocation(TL.getNameLoc(), Record);
422}
423void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
424 Writer.AddSourceLocation(TL.getNameLoc(), Record);
425}
426void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
427 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
428 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Douglas Gregordab60ad2010-10-01 18:44:50 +0000429 Record.push_back(TL.getTrailingReturn());
John McCall51bd8032009-10-18 01:05:36 +0000430 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
431 Writer.AddDeclRef(TL.getArg(i), Record);
432}
433void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
434 VisitFunctionTypeLoc(TL);
435}
436void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
437 VisitFunctionTypeLoc(TL);
438}
John McCalled976492009-12-04 22:46:56 +0000439void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
440 Writer.AddSourceLocation(TL.getNameLoc(), Record);
441}
John McCall51bd8032009-10-18 01:05:36 +0000442void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
443 Writer.AddSourceLocation(TL.getNameLoc(), Record);
444}
445void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000446 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
447 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
448 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000449}
450void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000451 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
452 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
453 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
454 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000455}
456void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
457 Writer.AddSourceLocation(TL.getNameLoc(), Record);
458}
459void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
460 Writer.AddSourceLocation(TL.getNameLoc(), Record);
461}
462void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
463 Writer.AddSourceLocation(TL.getNameLoc(), Record);
464}
John McCall51bd8032009-10-18 01:05:36 +0000465void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
466 Writer.AddSourceLocation(TL.getNameLoc(), Record);
467}
John McCall49a832b2009-10-18 09:09:24 +0000468void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
469 SubstTemplateTypeParmTypeLoc TL) {
470 Writer.AddSourceLocation(TL.getNameLoc(), Record);
471}
John McCall51bd8032009-10-18 01:05:36 +0000472void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
473 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000474 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
475 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
476 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
477 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000478 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
479 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000480}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000481void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
482 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
483 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
484}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000485void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000486 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
487 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000488}
John McCall3cb0ebd2010-03-10 03:28:59 +0000489void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
490 Writer.AddSourceLocation(TL.getNameLoc(), Record);
491}
Douglas Gregor4714c122010-03-31 17:34:00 +0000492void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000493 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
494 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000495 Writer.AddSourceLocation(TL.getNameLoc(), Record);
496}
John McCall33500952010-06-11 00:33:02 +0000497void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
498 DependentTemplateSpecializationTypeLoc TL) {
499 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
500 Writer.AddSourceRange(TL.getQualifierRange(), Record);
501 Writer.AddSourceLocation(TL.getNameLoc(), Record);
502 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
503 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
504 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000505 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
506 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000507}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000508void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
509 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
510}
John McCall51bd8032009-10-18 01:05:36 +0000511void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
512 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000513}
514void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
515 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000516 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
517 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
518 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
519 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000520}
John McCall54e14c42009-10-22 22:37:11 +0000521void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
522 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000523}
John McCalla1ee0c52009-10-16 21:56:05 +0000524
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000525//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000526// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000527//===----------------------------------------------------------------------===//
528
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000529static void EmitBlockID(unsigned ID, const char *Name,
530 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000531 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000532 Record.clear();
533 Record.push_back(ID);
534 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
535
536 // Emit the block name if present.
537 if (Name == 0 || Name[0] == 0) return;
538 Record.clear();
539 while (*Name)
540 Record.push_back(*Name++);
541 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
542}
543
544static void EmitRecordID(unsigned ID, const char *Name,
545 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000546 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000547 Record.clear();
548 Record.push_back(ID);
549 while (*Name)
550 Record.push_back(*Name++);
551 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000552}
553
554static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000555 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000556#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000557 RECORD(STMT_STOP);
558 RECORD(STMT_NULL_PTR);
559 RECORD(STMT_NULL);
560 RECORD(STMT_COMPOUND);
561 RECORD(STMT_CASE);
562 RECORD(STMT_DEFAULT);
563 RECORD(STMT_LABEL);
564 RECORD(STMT_IF);
565 RECORD(STMT_SWITCH);
566 RECORD(STMT_WHILE);
567 RECORD(STMT_DO);
568 RECORD(STMT_FOR);
569 RECORD(STMT_GOTO);
570 RECORD(STMT_INDIRECT_GOTO);
571 RECORD(STMT_CONTINUE);
572 RECORD(STMT_BREAK);
573 RECORD(STMT_RETURN);
574 RECORD(STMT_DECL);
575 RECORD(STMT_ASM);
576 RECORD(EXPR_PREDEFINED);
577 RECORD(EXPR_DECL_REF);
578 RECORD(EXPR_INTEGER_LITERAL);
579 RECORD(EXPR_FLOATING_LITERAL);
580 RECORD(EXPR_IMAGINARY_LITERAL);
581 RECORD(EXPR_STRING_LITERAL);
582 RECORD(EXPR_CHARACTER_LITERAL);
583 RECORD(EXPR_PAREN);
584 RECORD(EXPR_UNARY_OPERATOR);
585 RECORD(EXPR_SIZEOF_ALIGN_OF);
586 RECORD(EXPR_ARRAY_SUBSCRIPT);
587 RECORD(EXPR_CALL);
588 RECORD(EXPR_MEMBER);
589 RECORD(EXPR_BINARY_OPERATOR);
590 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
591 RECORD(EXPR_CONDITIONAL_OPERATOR);
592 RECORD(EXPR_IMPLICIT_CAST);
593 RECORD(EXPR_CSTYLE_CAST);
594 RECORD(EXPR_COMPOUND_LITERAL);
595 RECORD(EXPR_EXT_VECTOR_ELEMENT);
596 RECORD(EXPR_INIT_LIST);
597 RECORD(EXPR_DESIGNATED_INIT);
598 RECORD(EXPR_IMPLICIT_VALUE_INIT);
599 RECORD(EXPR_VA_ARG);
600 RECORD(EXPR_ADDR_LABEL);
601 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000602 RECORD(EXPR_CHOOSE);
603 RECORD(EXPR_GNU_NULL);
604 RECORD(EXPR_SHUFFLE_VECTOR);
605 RECORD(EXPR_BLOCK);
606 RECORD(EXPR_BLOCK_DECL_REF);
607 RECORD(EXPR_OBJC_STRING_LITERAL);
608 RECORD(EXPR_OBJC_ENCODE);
609 RECORD(EXPR_OBJC_SELECTOR_EXPR);
610 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
611 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
612 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
613 RECORD(EXPR_OBJC_KVC_REF_EXPR);
614 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000615 RECORD(STMT_OBJC_FOR_COLLECTION);
616 RECORD(STMT_OBJC_CATCH);
617 RECORD(STMT_OBJC_FINALLY);
618 RECORD(STMT_OBJC_AT_TRY);
619 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
620 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000621 RECORD(EXPR_CXX_OPERATOR_CALL);
622 RECORD(EXPR_CXX_CONSTRUCT);
623 RECORD(EXPR_CXX_STATIC_CAST);
624 RECORD(EXPR_CXX_DYNAMIC_CAST);
625 RECORD(EXPR_CXX_REINTERPRET_CAST);
626 RECORD(EXPR_CXX_CONST_CAST);
627 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
628 RECORD(EXPR_CXX_BOOL_LITERAL);
629 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000630#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000631}
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Sebastian Redla4232eb2010-08-18 23:56:21 +0000633void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000634 RecordData Record;
635 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000637#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
638#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000639
Sebastian Redl3397c552010-08-18 23:56:27 +0000640 // AST Top-Level Block.
Sebastian Redlf29f0a22010-08-18 23:57:22 +0000641 BLOCK(AST_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000642 RECORD(ORIGINAL_FILE_NAME);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000643 RECORD(TYPE_OFFSET);
644 RECORD(DECL_OFFSET);
645 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000646 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000647 RECORD(IDENTIFIER_OFFSET);
648 RECORD(IDENTIFIER_TABLE);
649 RECORD(EXTERNAL_DEFINITIONS);
650 RECORD(SPECIAL_TYPES);
651 RECORD(STATISTICS);
652 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000653 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000654 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
655 RECORD(SELECTOR_OFFSETS);
656 RECORD(METHOD_POOL);
657 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000658 RECORD(SOURCE_LOCATION_OFFSETS);
659 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000660 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000661 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000662 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000663 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redla93e3b52010-07-08 22:01:51 +0000664 RECORD(CHAINED_METADATA);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000665 RECORD(REFERENCED_SELECTOR_POOL);
Michael J. Spencer20249a12010-10-21 03:16:25 +0000666
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000667 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000668 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000669 RECORD(SM_SLOC_FILE_ENTRY);
670 RECORD(SM_SLOC_BUFFER_ENTRY);
671 RECORD(SM_SLOC_BUFFER_BLOB);
672 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
673 RECORD(SM_LINE_TABLE);
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000675 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000676 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000677 RECORD(PP_MACRO_OBJECT_LIKE);
678 RECORD(PP_MACRO_FUNCTION_LIKE);
679 RECORD(PP_TOKEN);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000680 RECORD(PP_MACRO_INSTANTIATION);
681 RECORD(PP_MACRO_DEFINITION);
Michael J. Spencer20249a12010-10-21 03:16:25 +0000682
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000683 // Decls and Types block.
684 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000685 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000686 RECORD(TYPE_COMPLEX);
687 RECORD(TYPE_POINTER);
688 RECORD(TYPE_BLOCK_POINTER);
689 RECORD(TYPE_LVALUE_REFERENCE);
690 RECORD(TYPE_RVALUE_REFERENCE);
691 RECORD(TYPE_MEMBER_POINTER);
692 RECORD(TYPE_CONSTANT_ARRAY);
693 RECORD(TYPE_INCOMPLETE_ARRAY);
694 RECORD(TYPE_VARIABLE_ARRAY);
695 RECORD(TYPE_VECTOR);
696 RECORD(TYPE_EXT_VECTOR);
697 RECORD(TYPE_FUNCTION_PROTO);
698 RECORD(TYPE_FUNCTION_NO_PROTO);
699 RECORD(TYPE_TYPEDEF);
700 RECORD(TYPE_TYPEOF_EXPR);
701 RECORD(TYPE_TYPEOF);
702 RECORD(TYPE_RECORD);
703 RECORD(TYPE_ENUM);
704 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000705 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000706 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000707 RECORD(DECL_TRANSLATION_UNIT);
708 RECORD(DECL_TYPEDEF);
709 RECORD(DECL_ENUM);
710 RECORD(DECL_RECORD);
711 RECORD(DECL_ENUM_CONSTANT);
712 RECORD(DECL_FUNCTION);
713 RECORD(DECL_OBJC_METHOD);
714 RECORD(DECL_OBJC_INTERFACE);
715 RECORD(DECL_OBJC_PROTOCOL);
716 RECORD(DECL_OBJC_IVAR);
717 RECORD(DECL_OBJC_AT_DEFS_FIELD);
718 RECORD(DECL_OBJC_CLASS);
719 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
720 RECORD(DECL_OBJC_CATEGORY);
721 RECORD(DECL_OBJC_CATEGORY_IMPL);
722 RECORD(DECL_OBJC_IMPLEMENTATION);
723 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
724 RECORD(DECL_OBJC_PROPERTY);
725 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000726 RECORD(DECL_FIELD);
727 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000728 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000729 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000730 RECORD(DECL_FILE_SCOPE_ASM);
731 RECORD(DECL_BLOCK);
732 RECORD(DECL_CONTEXT_LEXICAL);
733 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000734 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattner0558df22009-04-27 00:49:53 +0000735 AddStmtsExprs(Stream, Record);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000736#undef RECORD
737#undef BLOCK
738 Stream.ExitBlock();
739}
740
Douglas Gregore650c8c2009-07-07 00:12:59 +0000741/// \brief Adjusts the given filename to only write out the portion of the
742/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000743///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000744/// \param Filename the file name to adjust.
745///
746/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
747/// the returned filename will be adjusted by this system root.
748///
749/// \returns either the original filename (if it needs no adjustment) or the
750/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000751static const char *
Douglas Gregore650c8c2009-07-07 00:12:59 +0000752adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
753 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Douglas Gregore650c8c2009-07-07 00:12:59 +0000755 if (!isysroot)
756 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Douglas Gregore650c8c2009-07-07 00:12:59 +0000758 // Verify that the filename and the system root have the same prefix.
759 unsigned Pos = 0;
760 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
761 if (Filename[Pos] != isysroot[Pos])
762 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Douglas Gregore650c8c2009-07-07 00:12:59 +0000764 // We hit the end of the filename before we hit the end of the system root.
765 if (!Filename[Pos])
766 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000767
Douglas Gregore650c8c2009-07-07 00:12:59 +0000768 // If the file name has a '/' at the current position, skip over the '/'.
769 // We distinguish sysroot-based includes from absolute includes by the
770 // absence of '/' at the beginning of sysroot-based includes.
771 if (Filename[Pos] == '/')
772 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000773
Douglas Gregore650c8c2009-07-07 00:12:59 +0000774 return Filename + Pos;
775}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000776
Sebastian Redl3397c552010-08-18 23:56:27 +0000777/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Sebastian Redla4232eb2010-08-18 23:56:21 +0000778void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000779 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000780
Douglas Gregore650c8c2009-07-07 00:12:59 +0000781 // Metadata
782 const TargetInfo &Target = Context.Target;
783 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl77f46032010-07-09 21:00:24 +0000784 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000785 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl3397c552010-08-18 23:56:27 +0000786 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
787 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregore650c8c2009-07-07 00:12:59 +0000788 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
789 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
790 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl77f46032010-07-09 21:00:24 +0000791 // Target triple or chained PCH name
792 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregore650c8c2009-07-07 00:12:59 +0000793 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Douglas Gregore650c8c2009-07-07 00:12:59 +0000795 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000796 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
797 Record.push_back(VERSION_MAJOR);
798 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000799 Record.push_back(CLANG_VERSION_MAJOR);
800 Record.push_back(CLANG_VERSION_MINOR);
801 Record.push_back(isysroot != 0);
Sebastian Redl77f46032010-07-09 21:00:24 +0000802 // FIXME: This writes the absolute path for chained headers.
803 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
804 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Douglas Gregorb64c1932009-05-12 01:31:05 +0000806 // Original file name
807 SourceManager &SM = Context.getSourceManager();
808 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
809 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000810 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregorb64c1932009-05-12 01:31:05 +0000811 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
812 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
813
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000814 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000815
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000816 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000817
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +0000818 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000819 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000820 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000821 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000822 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000823 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000824 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000825
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000826 // Repository branch/version information.
827 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000828 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000829 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
830 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000831 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000832 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000833 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
834 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +0000835}
836
837/// \brief Write the LangOptions structure.
Sebastian Redla4232eb2010-08-18 23:56:21 +0000838void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000839 RecordData Record;
840 Record.push_back(LangOpts.Trigraphs);
841 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
842 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
843 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
844 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000845 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000846 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
847 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
848 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
849 Record.push_back(LangOpts.C99); // C99 Support
850 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencerdae4ac42010-10-21 05:21:48 +0000851 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
852 // already saved elsewhere.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000853 Record.push_back(LangOpts.CPlusPlus); // C++ Support
854 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000855 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +0000856
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000857 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
858 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000859 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000860 // modern abi enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000861 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000862 // modern abi enabled.
Ted Kremenekc32647d2010-12-23 21:35:43 +0000863 Record.push_back(LangOpts.ObjCDefaultSynthProperties); // Objective-C auto-synthesized
864 // properties enabled.
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +0000865 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000867 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000868 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
869 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000870 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000871 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar73482882010-02-10 18:48:44 +0000872 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000873
874 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
875 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
876 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
877
Chris Lattnerea5ce472009-04-27 07:35:58 +0000878 // Whether static initializers are protected by locks.
879 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +0000880 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000881 Record.push_back(LangOpts.Blocks); // block extension to C
882 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
883 // they are unused.
884 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
885 // (modulo the platform support).
886
Chris Lattnera4d71452010-06-26 21:25:03 +0000887 Record.push_back(LangOpts.getSignedOverflowBehavior());
888 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000889
890 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +0000891 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000892 // defined.
893 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
894 // opposed to __DYNAMIC__).
895 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
896
897 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
898 // used (instead of C99 semantics).
899 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +0000900 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
901 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +0000902 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
903 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +0000904 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000905 Record.push_back(LangOpts.getGCMode());
906 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000907 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000908 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000909 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne08a53262010-12-01 19:14:57 +0000910 Record.push_back(LangOpts.CUDA);
Mike Stump9c276ae2009-12-12 01:27:46 +0000911 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson92f58222009-08-22 22:30:33 +0000912 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +0000913 Record.push_back(LangOpts.SpellChecking);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000914 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000915}
916
Douglas Gregor14f79002009-04-10 03:52:48 +0000917//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000918// stat cache Serialization
919//===----------------------------------------------------------------------===//
920
921namespace {
922// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +0000923class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000924public:
925 typedef const char * key_type;
926 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000927
Chris Lattner74e976b2010-11-23 19:28:12 +0000928 typedef struct stat data_type;
929 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000930
931 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000932 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000933 }
Mike Stump1eb44332009-09-09 15:08:12 +0000934
935 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000936 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
937 data_type_ref Data) {
938 unsigned StrLen = strlen(path);
939 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +0000940 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000941 clang::io::Emit8(Out, DataLen);
942 return std::make_pair(StrLen + 1, DataLen);
943 }
Mike Stump1eb44332009-09-09 15:08:12 +0000944
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000945 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
946 Out.write(path, KeyLen);
947 }
Mike Stump1eb44332009-09-09 15:08:12 +0000948
Chris Lattner74e976b2010-11-23 19:28:12 +0000949 void EmitData(llvm::raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000950 data_type_ref Data, unsigned DataLen) {
951 using namespace clang::io;
952 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +0000953
Chris Lattner74e976b2010-11-23 19:28:12 +0000954 Emit32(Out, (uint32_t) Data.st_ino);
955 Emit32(Out, (uint32_t) Data.st_dev);
956 Emit16(Out, (uint16_t) Data.st_mode);
957 Emit64(Out, (uint64_t) Data.st_mtime);
958 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000959
960 assert(Out.tell() - Start == DataLen && "Wrong data length");
961 }
962};
963} // end anonymous namespace
964
Sebastian Redl3397c552010-08-18 23:56:27 +0000965/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +0000966void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000967 // Build the on-disk hash table containing information about every
968 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +0000969 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000970 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000971 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000972 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000973 Stat != StatEnd; ++Stat, ++NumStatEntries) {
974 const char *Filename = Stat->first();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000975 Generator.insert(Filename, Stat->second);
976 }
Mike Stump1eb44332009-09-09 15:08:12 +0000977
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000978 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000979 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000980 uint32_t BucketOffset;
981 {
982 llvm::raw_svector_ostream Out(StatCacheData);
983 // Make sure that no bucket is at offset 0
984 clang::io::Emit32(Out, 0);
985 BucketOffset = Generator.Emit(Out);
986 }
987
988 // Create a blob abbreviation
989 using namespace llvm;
990 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000991 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000992 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
993 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
994 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
995 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
996
997 // Write the stat cache
998 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000999 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001000 Record.push_back(BucketOffset);
1001 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001002 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001003}
1004
1005//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001006// Source Manager Serialization
1007//===----------------------------------------------------------------------===//
1008
1009/// \brief Create an abbreviation for the SLocEntry that refers to a
1010/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001011static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001012 using namespace llvm;
1013 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001014 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001015 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1016 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1017 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1018 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001019 // FileEntry fields.
1020 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1021 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor12fab312010-03-16 16:35:32 +00001022 // HeaderFileInfo fields.
1023 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
1024 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
1025 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
1026 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregor14f79002009-04-10 03:52:48 +00001027 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001028 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001029}
1030
1031/// \brief Create an abbreviation for the SLocEntry that refers to a
1032/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001033static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001034 using namespace llvm;
1035 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001036 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001037 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1038 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1039 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1040 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1041 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001042 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001043}
1044
1045/// \brief Create an abbreviation for the SLocEntry that refers to a
1046/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001047static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001048 using namespace llvm;
1049 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001050 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001051 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001052 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001053}
1054
1055/// \brief Create an abbreviation for the SLocEntry that refers to an
1056/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001057static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001058 using namespace llvm;
1059 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001060 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001061 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1062 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1063 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1064 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001065 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001066 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001067}
1068
1069/// \brief Writes the block containing the serialized form of the
1070/// source manager.
1071///
1072/// TODO: We should probably use an on-disk hash table (stored in a
1073/// blob), indexed based on the file name, so that we only create
1074/// entries for files that we actually need. In the common case (no
1075/// errors), we probably won't have to create file entries for any of
1076/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001077void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001078 const Preprocessor &PP,
1079 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001080 RecordData Record;
1081
Chris Lattnerf04ad692009-04-10 17:16:57 +00001082 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001083 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001084
1085 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001086 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1087 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1088 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1089 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001090
Douglas Gregorbd945002009-04-13 16:31:14 +00001091 // Write the line table.
1092 if (SourceMgr.hasLineTable()) {
1093 LineTableInfo &LineTable = SourceMgr.getLineTable();
1094
1095 // Emit the file names
1096 Record.push_back(LineTable.getNumFilenames());
1097 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1098 // Emit the file name
1099 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001100 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +00001101 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1102 Record.push_back(FilenameLen);
1103 if (FilenameLen)
1104 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1105 }
Mike Stump1eb44332009-09-09 15:08:12 +00001106
Douglas Gregorbd945002009-04-13 16:31:14 +00001107 // Emit the line entries
1108 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1109 L != LEnd; ++L) {
1110 // Emit the file ID
1111 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +00001112
Douglas Gregorbd945002009-04-13 16:31:14 +00001113 // Emit the line entries
1114 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001115 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +00001116 LEEnd = L->second.end();
1117 LE != LEEnd; ++LE) {
1118 Record.push_back(LE->FileOffset);
1119 Record.push_back(LE->LineNo);
1120 Record.push_back(LE->FilenameID);
1121 Record.push_back((unsigned)LE->FileKind);
1122 Record.push_back(LE->IncludeOffset);
1123 }
Douglas Gregorbd945002009-04-13 16:31:14 +00001124 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001125 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001126 }
1127
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001128 // Write out the source location entry table. We skip the first
1129 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001130 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001131 RecordData PreloadSLocs;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001132 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1133 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1134 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1135 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001136 // Get this source location entry.
1137 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001138
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001139 // Record the offset of this source-location entry.
1140 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1141
1142 // Figure out which record code to use.
1143 unsigned Code;
1144 if (SLoc->isFile()) {
1145 if (SLoc->getFile().getContentCache()->Entry)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001146 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001147 else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001148 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001149 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001150 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001151 Record.clear();
1152 Record.push_back(Code);
1153
1154 Record.push_back(SLoc->getOffset());
1155 if (SLoc->isFile()) {
1156 const SrcMgr::FileInfo &File = SLoc->getFile();
1157 Record.push_back(File.getIncludeLoc().getRawEncoding());
1158 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1159 Record.push_back(File.hasLineDirectives());
1160
1161 const SrcMgr::ContentCache *Content = File.getContentCache();
1162 if (Content->Entry) {
1163 // The source location entry is a file. The blob associated
1164 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001165
Douglas Gregor2d52be52010-03-21 22:49:54 +00001166 // Emit size/modification time for this file.
1167 Record.push_back(Content->Entry->getSize());
1168 Record.push_back(Content->Entry->getModificationTime());
1169
Douglas Gregor12fab312010-03-16 16:35:32 +00001170 // Emit header-search information associated with this file.
1171 HeaderFileInfo HFI;
1172 HeaderSearch &HS = PP.getHeaderSearchInfo();
1173 if (Content->Entry->getUID() < HS.header_file_size())
1174 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1175 Record.push_back(HFI.isImport);
1176 Record.push_back(HFI.DirInfo);
1177 Record.push_back(HFI.NumIncludes);
1178 AddIdentifierRef(HFI.ControllingMacro, Record);
1179
Douglas Gregore650c8c2009-07-07 00:12:59 +00001180 // Turn the file name into an absolute path, if it isn't already.
1181 const char *Filename = Content->Entry->getName();
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001182 llvm::SmallString<128> FilePath(Filename);
1183 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001184 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Douglas Gregore650c8c2009-07-07 00:12:59 +00001186 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001187 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001188
1189 // FIXME: For now, preload all file source locations, so that
1190 // we get the appropriate File entries in the reader. This is
1191 // a temporary measure.
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001192 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001193 } else {
1194 // The source location entry is a buffer. The blob associated
1195 // with this entry contains the contents of the buffer.
1196
1197 // We add one to the size so that we capture the trailing NULL
1198 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1199 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001200 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001201 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001202 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001203 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1204 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001205 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001206 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001207 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001208 llvm::StringRef(Buffer->getBufferStart(),
1209 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001210
1211 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001212 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001213 }
1214 } else {
1215 // The source location entry is an instantiation.
1216 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1217 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1218 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1219 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1220
1221 // Compute the token length for this macro expansion.
1222 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001223 if (I + 1 != N)
1224 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001225 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1226 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1227 }
1228 }
1229
Douglas Gregorc9490c02009-04-16 22:23:12 +00001230 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001231
1232 if (SLocEntryOffsets.empty())
1233 return;
1234
Sebastian Redl3397c552010-08-18 23:56:27 +00001235 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001236 // table is used for lazily loading source-location information.
1237 using namespace llvm;
1238 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001239 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001240 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1241 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1242 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1243 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001244
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001245 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001246 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001247 Record.push_back(SLocEntryOffsets.size());
Sebastian Redl8db9fae2010-09-22 20:19:08 +00001248 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1249 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001250 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001251 (const char *)data(SLocEntryOffsets),
Chris Lattner090d9b52009-04-27 19:01:47 +00001252 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001253
Sebastian Redl3397c552010-08-18 23:56:27 +00001254 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001255 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001256 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001257}
1258
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001259//===----------------------------------------------------------------------===//
1260// Preprocessor Serialization
1261//===----------------------------------------------------------------------===//
1262
Chris Lattner0b1fb982009-04-10 17:15:23 +00001263/// \brief Writes the block containing the serialized form of the
1264/// preprocessor.
1265///
Sebastian Redla4232eb2010-08-18 23:56:21 +00001266void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001267 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001268
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001269 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1270 if (PP.getCounterValue() != 0) {
1271 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001272 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001273 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001274 }
1275
1276 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001277 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001278
Sebastian Redl3397c552010-08-18 23:56:27 +00001279 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001280 // FIXME: use diagnostics subsystem for localization etc.
1281 if (PP.SawDateOrTime())
1282 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001283
Douglas Gregorecdcb882010-10-20 22:00:55 +00001284
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001285 // Loop over all the macro definitions that are live at the end of the file,
1286 // emitting each to the PP section.
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001287 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001288 unsigned InclusionAbbrev = 0;
1289 if (PPRec) {
1290 using namespace llvm;
1291 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1292 Abbrev->Add(BitCodeAbbrevOp(PP_INCLUSION_DIRECTIVE));
1293 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1294 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1295 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1296 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1297 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1298 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1299 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Michael J. Spencer20249a12010-10-21 03:16:25 +00001300 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorecdcb882010-10-20 22:00:55 +00001301 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001302
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001303 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1304 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001305 // FIXME: This emits macros in hash table order, we should do it in a stable
1306 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001307 MacroInfo *MI = I->second;
1308
Sebastian Redl3397c552010-08-18 23:56:27 +00001309 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001310 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl3397c552010-08-18 23:56:27 +00001311 // Also skip macros from a AST file if we're chaining.
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001312
1313 // FIXME: There is a (probably minor) optimization we could do here, if
1314 // the macro comes from the original PCH but the identifier comes from a
1315 // chained PCH, by storing the offset into the original PCH rather than
1316 // writing the macro definition a second time.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001317 if (MI->isBuiltinMacro() ||
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001318 (Chain && I->first->isFromAST() && MI->isFromAST()))
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001319 continue;
1320
Chris Lattner7356a312009-04-11 21:15:38 +00001321 AddIdentifierRef(I->first, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001322 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001323 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1324 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001325
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001326 unsigned Code;
1327 if (MI->isObjectLike()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001328 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001329 } else {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001330 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001331
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001332 Record.push_back(MI->isC99Varargs());
1333 Record.push_back(MI->isGNUVarargs());
1334 Record.push_back(MI->getNumArgs());
1335 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1336 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001337 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001338 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001339
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001340 // If we have a detailed preprocessing record, record the macro definition
1341 // ID that corresponds to this macro.
1342 if (PPRec)
1343 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer20249a12010-10-21 03:16:25 +00001344
Douglas Gregorc9490c02009-04-16 22:23:12 +00001345 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001346 Record.clear();
1347
Chris Lattnerdf961c22009-04-10 18:08:30 +00001348 // Emit the tokens array.
1349 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1350 // Note that we know that the preprocessor does not have any annotation
1351 // tokens in it because they are created by the parser, and thus can't be
1352 // in a macro definition.
1353 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001354
Chris Lattnerdf961c22009-04-10 18:08:30 +00001355 Record.push_back(Tok.getLocation().getRawEncoding());
1356 Record.push_back(Tok.getLength());
1357
Chris Lattnerdf961c22009-04-10 18:08:30 +00001358 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1359 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001360 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001361
Chris Lattnerdf961c22009-04-10 18:08:30 +00001362 // FIXME: Should translate token kind to a stable encoding.
1363 Record.push_back(Tok.getKind());
1364 // FIXME: Should translate token flags to a stable encoding.
1365 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001366
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001367 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001368 Record.clear();
1369 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001370 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001371 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001372
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001373 // If the preprocessor has a preprocessing record, emit it.
1374 unsigned NumPreprocessingRecords = 0;
1375 if (PPRec) {
Sebastian Redl196f5572010-09-27 23:20:01 +00001376 unsigned IndexBase = Chain ? PPRec->getNumPreallocatedEntities() : 0;
Sebastian Redlb57a6242010-09-27 22:18:47 +00001377 for (PreprocessingRecord::iterator E = PPRec->begin(Chain),
1378 EEnd = PPRec->end(Chain);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001379 E != EEnd; ++E) {
1380 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001381
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001382 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1383 // Record this macro definition's location.
Sebastian Redlf73c93f2010-09-15 19:54:06 +00001384 MacroID ID = getMacroDefinitionID(MD);
Douglas Gregor89d99802010-11-30 06:16:57 +00001385
Douglas Gregor77424bc2010-10-02 19:29:26 +00001386 // Don't write the macro definition if it is from another AST file.
1387 if (ID < FirstMacroID)
1388 continue;
Douglas Gregor89d99802010-11-30 06:16:57 +00001389
1390 // Notify the serialization listener that we're serializing this entity.
1391 if (SerializationListener)
1392 SerializationListener->SerializedPreprocessedEntity(*E,
1393 Stream.GetCurrentBitNo());
Michael J. Spencer20249a12010-10-21 03:16:25 +00001394
Douglas Gregor77424bc2010-10-02 19:29:26 +00001395 unsigned Position = ID - FirstMacroID;
1396 if (Position != MacroDefinitionOffsets.size()) {
1397 if (Position > MacroDefinitionOffsets.size())
1398 MacroDefinitionOffsets.resize(Position + 1);
Douglas Gregor89d99802010-11-30 06:16:57 +00001399
Michael J. Spencer20249a12010-10-21 03:16:25 +00001400 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001401 } else
1402 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor89d99802010-11-30 06:16:57 +00001403
Sebastian Redlb57a6242010-09-27 22:18:47 +00001404 Record.push_back(IndexBase + NumPreprocessingRecords++);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001405 Record.push_back(ID);
1406 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1407 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1408 AddIdentifierRef(MD->getName(), Record);
1409 AddSourceLocation(MD->getLocation(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001410 Stream.EmitRecord(PP_MACRO_DEFINITION, Record);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001411 continue;
1412 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001413
Douglas Gregor89d99802010-11-30 06:16:57 +00001414 // Notify the serialization listener that we're serializing this entity.
1415 if (SerializationListener)
1416 SerializationListener->SerializedPreprocessedEntity(*E,
1417 Stream.GetCurrentBitNo());
1418
1419 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1420 Record.push_back(IndexBase + NumPreprocessingRecords++);
1421 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1422 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1423 AddIdentifierRef(MI->getName(), Record);
1424 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1425 Stream.EmitRecord(PP_MACRO_INSTANTIATION, Record);
1426 continue;
1427 }
1428
Douglas Gregorecdcb882010-10-20 22:00:55 +00001429 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1430 Record.push_back(PP_INCLUSION_DIRECTIVE);
1431 Record.push_back(IndexBase + NumPreprocessingRecords++);
1432 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1433 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1434 Record.push_back(ID->getFileName().size());
1435 Record.push_back(ID->wasInQuotes());
1436 Record.push_back(static_cast<unsigned>(ID->getKind()));
1437 llvm::SmallString<64> Buffer;
1438 Buffer += ID->getFileName();
1439 Buffer += ID->getFile()->getName();
1440 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1441 continue;
1442 }
Douglas Gregor89d99802010-11-30 06:16:57 +00001443
1444 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001445 }
1446 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001447
Douglas Gregorc9490c02009-04-16 22:23:12 +00001448 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001449
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001450 // Write the offsets table for the preprocessing record.
1451 if (NumPreprocessingRecords > 0) {
1452 // Write the offsets table for identifier IDs.
1453 using namespace llvm;
1454 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001455 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001456 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1457 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1458 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1459 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001460
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001461 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001462 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001463 Record.push_back(NumPreprocessingRecords);
1464 Record.push_back(MacroDefinitionOffsets.size());
1465 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001466 (const char *)data(MacroDefinitionOffsets),
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001467 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1468 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001469}
1470
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001471void ASTWriter::WriteUserDiagnosticMappings(const Diagnostic &Diag) {
1472 RecordData Record;
1473 for (unsigned i = 0; i != diag::DIAG_UPPER_LIMIT; ++i) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001474 diag::Mapping Map = Diag.getDiagnosticMappingInfo(i,Diag.GetCurDiagState());
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001475 if (Map & 0x8) { // user mapping.
1476 Record.push_back(i);
1477 Record.push_back(Map & 0x7);
1478 }
1479 }
1480
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00001481 if (!Record.empty())
1482 Stream.EmitRecord(DIAG_USER_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001483}
1484
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001485//===----------------------------------------------------------------------===//
1486// Type Serialization
1487//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001488
Sebastian Redl3397c552010-08-18 23:56:27 +00001489/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001490void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00001491 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001492 if (Idx.getIndex() == 0) // we haven't seen this type before.
1493 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00001494
Douglas Gregor97475832010-10-05 18:37:06 +00001495 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00001496
Douglas Gregor2cf26342009-04-09 22:27:44 +00001497 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001498 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00001499 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001500 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00001501 else if (TypeOffsets.size() < Index) {
1502 TypeOffsets.resize(Index + 1);
1503 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001504 }
1505
1506 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001507
Douglas Gregor2cf26342009-04-09 22:27:44 +00001508 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00001509 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001510
Douglas Gregora4923eb2009-11-16 21:35:15 +00001511 if (T.hasLocalNonFastQualifiers()) {
1512 Qualifiers Qs = T.getLocalQualifiers();
1513 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001514 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001515 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00001516 } else {
1517 switch (T->getTypeClass()) {
1518 // For all of the concrete, non-dependent types, call the
1519 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001520#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001521 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001522#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001523#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001524 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001525 }
1526
1527 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001528 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001529
1530 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001531 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001532}
1533
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001534//===----------------------------------------------------------------------===//
1535// Declaration Serialization
1536//===----------------------------------------------------------------------===//
1537
Douglas Gregor2cf26342009-04-09 22:27:44 +00001538/// \brief Write the block containing all of the declaration IDs
1539/// lexically declared within the given DeclContext.
1540///
1541/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1542/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001543uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001544 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001545 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001546 return 0;
1547
Douglas Gregorc9490c02009-04-16 22:23:12 +00001548 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001549 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001550 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001551 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001552 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1553 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001554 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001555
Douglas Gregor25123082009-04-22 22:34:57 +00001556 ++NumLexicalDeclContexts;
Sebastian Redl681d7232010-07-27 00:17:23 +00001557 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001558 reinterpret_cast<char*>(Decls.data()),
1559 Decls.size() * sizeof(KindDeclIDPair));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001560 return Offset;
1561}
1562
Sebastian Redla4232eb2010-08-18 23:56:21 +00001563void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00001564 using namespace llvm;
1565 RecordData Record;
1566
1567 // Write the type offsets array
1568 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001569 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001570 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1571 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1572 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1573 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001574 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00001575 Record.push_back(TypeOffsets.size());
1576 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001577 (const char *)data(TypeOffsets),
Sebastian Redl1476ed42010-07-16 16:36:56 +00001578 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1579
1580 // Write the declaration offsets array
1581 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001582 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001583 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1584 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1585 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1586 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001587 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00001588 Record.push_back(DeclOffsets.size());
1589 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001590 (const char *)data(DeclOffsets),
Sebastian Redl1476ed42010-07-16 16:36:56 +00001591 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1592}
1593
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001594//===----------------------------------------------------------------------===//
1595// Global Method Pool and Selector Serialization
1596//===----------------------------------------------------------------------===//
1597
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001598namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001599// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00001600class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00001601 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001602
1603public:
1604 typedef Selector key_type;
1605 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001606
Sebastian Redl5d050072010-08-04 17:20:04 +00001607 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001608 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00001609 ObjCMethodList Instance, Factory;
1610 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001611 typedef const data_type& data_type_ref;
1612
Sebastian Redl3397c552010-08-18 23:56:27 +00001613 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001614
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001615 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00001616 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001617 }
Mike Stump1eb44332009-09-09 15:08:12 +00001618
1619 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001620 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1621 data_type_ref Methods) {
1622 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1623 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00001624 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1625 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001626 Method = Method->Next)
1627 if (Method->Method)
1628 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00001629 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001630 Method = Method->Next)
1631 if (Method->Method)
1632 DataLen += 4;
1633 clang::io::Emit16(Out, DataLen);
1634 return std::make_pair(KeyLen, DataLen);
1635 }
Mike Stump1eb44332009-09-09 15:08:12 +00001636
Douglas Gregor83941df2009-04-25 17:48:32 +00001637 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00001638 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00001639 assert((Start >> 32) == 0 && "Selector key offset too large");
1640 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001641 unsigned N = Sel.getNumArgs();
1642 clang::io::Emit16(Out, N);
1643 if (N == 0)
1644 N = 1;
1645 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001646 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001647 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1648 }
Mike Stump1eb44332009-09-09 15:08:12 +00001649
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001650 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00001651 data_type_ref Methods, unsigned DataLen) {
1652 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00001653 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001654 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00001655 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001656 Method = Method->Next)
1657 if (Method->Method)
1658 ++NumInstanceMethods;
1659
1660 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00001661 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001662 Method = Method->Next)
1663 if (Method->Method)
1664 ++NumFactoryMethods;
1665
1666 clang::io::Emit16(Out, NumInstanceMethods);
1667 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00001668 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001669 Method = Method->Next)
1670 if (Method->Method)
1671 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00001672 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001673 Method = Method->Next)
1674 if (Method->Method)
1675 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00001676
1677 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001678 }
1679};
1680} // end anonymous namespace
1681
Sebastian Redl059612d2010-08-03 21:58:15 +00001682/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001683///
1684/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00001685/// in an on-disk hash table indexed by the selector. The hash table also
1686/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001687void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001688 using namespace llvm;
1689
Sebastian Redl059612d2010-08-03 21:58:15 +00001690 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00001691 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00001692 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00001693 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00001694 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001695 {
Sebastian Redl3397c552010-08-18 23:56:27 +00001696 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001697 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001698
Sebastian Redl059612d2010-08-03 21:58:15 +00001699 // Create the on-disk hash table representation. We walk through every
1700 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00001701 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001702 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00001703 I = SelectorIDs.begin(), E = SelectorIDs.end();
1704 I != E; ++I) {
1705 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00001706 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00001707 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00001708 I->second,
1709 ObjCMethodList(),
1710 ObjCMethodList()
1711 };
1712 if (F != SemaRef.MethodPool.end()) {
1713 Data.Instance = F->second.first;
1714 Data.Factory = F->second.second;
1715 }
Sebastian Redl3397c552010-08-18 23:56:27 +00001716 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00001717 // changed.
1718 if (Chain && I->second < FirstSelectorID) {
1719 // Selector already exists. Did it change?
1720 bool changed = false;
1721 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
1722 M = M->Next) {
1723 if (M->Method->getPCHLevel() == 0)
1724 changed = true;
1725 }
1726 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
1727 M = M->Next) {
1728 if (M->Method->getPCHLevel() == 0)
1729 changed = true;
1730 }
1731 if (!changed)
1732 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00001733 } else if (Data.Instance.Method || Data.Factory.Method) {
1734 // A new method pool entry.
1735 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00001736 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001737 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001738 }
1739
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001740 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001741 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001742 uint32_t BucketOffset;
1743 {
Sebastian Redl3397c552010-08-18 23:56:27 +00001744 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001745 llvm::raw_svector_ostream Out(MethodPool);
1746 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001747 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001748 BucketOffset = Generator.Emit(Out, Trait);
1749 }
1750
1751 // Create a blob abbreviation
1752 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001753 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001754 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00001755 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001756 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1757 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1758
Douglas Gregor83941df2009-04-25 17:48:32 +00001759 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001760 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001761 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001762 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00001763 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001764 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00001765
1766 // Create a blob abbreviation for the selector table offsets.
1767 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001768 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00001769 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor83941df2009-04-25 17:48:32 +00001770 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1771 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1772
1773 // Write the selector offsets table.
1774 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001775 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00001776 Record.push_back(SelectorOffsets.size());
1777 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001778 (const char *)data(SelectorOffsets),
Douglas Gregor83941df2009-04-25 17:48:32 +00001779 SelectorOffsets.size() * 4);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001780 }
1781}
1782
Sebastian Redl3397c552010-08-18 23:56:27 +00001783/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001784void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00001785 using namespace llvm;
1786 if (SemaRef.ReferencedSelectors.empty())
1787 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00001788
Fariborz Jahanian32019832010-07-23 19:11:11 +00001789 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00001790
Sebastian Redl3397c552010-08-18 23:56:27 +00001791 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00001792 // very tricky to fix, and given that @selector shouldn't really appear in
1793 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00001794 for (DenseMap<Selector, SourceLocation>::iterator S =
1795 SemaRef.ReferencedSelectors.begin(),
1796 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
1797 Selector Sel = (*S).first;
1798 SourceLocation Loc = (*S).second;
1799 AddSelectorRef(Sel, Record);
1800 AddSourceLocation(Loc, Record);
1801 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001802 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00001803}
1804
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001805//===----------------------------------------------------------------------===//
1806// Identifier Table Serialization
1807//===----------------------------------------------------------------------===//
1808
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001809namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00001810class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00001811 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00001812 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001813
Douglas Gregora92193e2009-04-28 21:18:29 +00001814 /// \brief Determines whether this is an "interesting" identifier
1815 /// that needs a full IdentifierInfo structure written into the hash
1816 /// table.
1817 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1818 return II->isPoisoned() ||
1819 II->isExtensionToken() ||
1820 II->hasMacroDefinition() ||
1821 II->getObjCOrBuiltinID() ||
1822 II->getFETokenInfo<void>();
1823 }
1824
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001825public:
1826 typedef const IdentifierInfo* key_type;
1827 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001828
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001829 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001830 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001831
Sebastian Redl3397c552010-08-18 23:56:27 +00001832 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00001833 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001834
1835 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001836 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001837 }
Mike Stump1eb44332009-09-09 15:08:12 +00001838
1839 std::pair<unsigned,unsigned>
1840 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001841 IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00001842 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00001843 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1844 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00001845 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00001846 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00001847 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00001848 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00001849 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1850 DEnd = IdentifierResolver::end();
1851 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001852 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00001853 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001854 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00001855 // We emit the key length after the data length so that every
1856 // string is preceded by a 16-bit length. This matches the PTH
1857 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00001858 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001859 return std::make_pair(KeyLen, DataLen);
1860 }
Mike Stump1eb44332009-09-09 15:08:12 +00001861
1862 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001863 unsigned KeyLen) {
1864 // Record the location of the key data. This is used when generating
1865 // the mapping from persistent IDs to strings.
1866 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00001867 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001868 }
Mike Stump1eb44332009-09-09 15:08:12 +00001869
1870 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001871 IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00001872 if (!isInterestingIdentifier(II)) {
1873 clang::io::Emit32(Out, ID << 1);
1874 return;
1875 }
Douglas Gregor5998da52009-04-28 21:32:13 +00001876
Douglas Gregora92193e2009-04-28 21:18:29 +00001877 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001878 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001879 bool hasMacroDefinition =
1880 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00001881 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00001882 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001883 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1884 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1885 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00001886 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001887 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00001888 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001889
Douglas Gregor37e26842009-04-21 23:56:24 +00001890 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00001891 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00001892
Douglas Gregor668c1a42009-04-21 22:25:48 +00001893 // Emit the declaration IDs in reverse order, because the
1894 // IdentifierResolver provides the declarations as they would be
1895 // visible (e.g., the function "stat" would come before the struct
1896 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1897 // adds declarations to the end of the list (so we need to see the
1898 // struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00001899 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump1eb44332009-09-09 15:08:12 +00001900 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00001901 IdentifierResolver::end());
1902 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1903 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001904 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00001905 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001906 }
1907};
1908} // end anonymous namespace
1909
Sebastian Redl3397c552010-08-18 23:56:27 +00001910/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001911///
1912/// The identifier table consists of a blob containing string data
1913/// (the actual identifiers themselves) and a separate "offsets" index
1914/// that maps identifier IDs to locations within the blob.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001915void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001916 using namespace llvm;
1917
1918 // Create and write out the blob that contains the identifier
1919 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001920 {
Sebastian Redl3397c552010-08-18 23:56:27 +00001921 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001922 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump1eb44332009-09-09 15:08:12 +00001923
Douglas Gregor92b059e2009-04-28 20:33:11 +00001924 // Look for any identifiers that were named while processing the
1925 // headers, but are otherwise not needed. We add these to the hash
1926 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00001927 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00001928 // file.
1929 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1930 IDEnd = PP.getIdentifierTable().end();
1931 ID != IDEnd; ++ID)
1932 getIdentifierRef(ID->second);
1933
Sebastian Redlf2f0f032010-07-23 23:49:55 +00001934 // Create the on-disk hash table representation. We only store offsets
1935 // for identifiers that appear here for the first time.
1936 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001937 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00001938 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1939 ID != IDEnd; ++ID) {
1940 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001941 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001942 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001943 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001944
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001945 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001946 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00001947 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001948 {
Sebastian Redl3397c552010-08-18 23:56:27 +00001949 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001950 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001951 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001952 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001953 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001954 }
1955
1956 // Create a blob abbreviation
1957 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001958 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001959 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001960 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00001961 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001962
1963 // Write the identifier table
1964 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001965 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001966 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001967 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001968 }
1969
1970 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001971 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001972 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001973 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1974 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1975 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1976
1977 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001978 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001979 Record.push_back(IdentifierOffsets.size());
1980 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001981 (const char *)data(IdentifierOffsets),
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001982 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregorafaf3082009-04-11 00:14:32 +00001983}
1984
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001985//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001986// DeclContext's Name Lookup Table Serialization
1987//===----------------------------------------------------------------------===//
1988
1989namespace {
1990// Trait used for the on-disk hash table used in the method pool.
1991class ASTDeclContextNameLookupTrait {
1992 ASTWriter &Writer;
1993
1994public:
1995 typedef DeclarationName key_type;
1996 typedef key_type key_type_ref;
1997
1998 typedef DeclContext::lookup_result data_type;
1999 typedef const data_type& data_type_ref;
2000
2001 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2002
2003 unsigned ComputeHash(DeclarationName Name) {
2004 llvm::FoldingSetNodeID ID;
2005 ID.AddInteger(Name.getNameKind());
2006
2007 switch (Name.getNameKind()) {
2008 case DeclarationName::Identifier:
2009 ID.AddString(Name.getAsIdentifierInfo()->getName());
2010 break;
2011 case DeclarationName::ObjCZeroArgSelector:
2012 case DeclarationName::ObjCOneArgSelector:
2013 case DeclarationName::ObjCMultiArgSelector:
2014 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2015 break;
2016 case DeclarationName::CXXConstructorName:
2017 case DeclarationName::CXXDestructorName:
2018 case DeclarationName::CXXConversionFunctionName:
2019 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
2020 break;
2021 case DeclarationName::CXXOperatorName:
2022 ID.AddInteger(Name.getCXXOverloadedOperator());
2023 break;
2024 case DeclarationName::CXXLiteralOperatorName:
2025 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2026 case DeclarationName::CXXUsingDirective:
2027 break;
2028 }
2029
2030 return ID.ComputeHash();
2031 }
2032
2033 std::pair<unsigned,unsigned>
2034 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2035 data_type_ref Lookup) {
2036 unsigned KeyLen = 1;
2037 switch (Name.getNameKind()) {
2038 case DeclarationName::Identifier:
2039 case DeclarationName::ObjCZeroArgSelector:
2040 case DeclarationName::ObjCOneArgSelector:
2041 case DeclarationName::ObjCMultiArgSelector:
2042 case DeclarationName::CXXConstructorName:
2043 case DeclarationName::CXXDestructorName:
2044 case DeclarationName::CXXConversionFunctionName:
2045 case DeclarationName::CXXLiteralOperatorName:
2046 KeyLen += 4;
2047 break;
2048 case DeclarationName::CXXOperatorName:
2049 KeyLen += 1;
2050 break;
2051 case DeclarationName::CXXUsingDirective:
2052 break;
2053 }
2054 clang::io::Emit16(Out, KeyLen);
2055
2056 // 2 bytes for num of decls and 4 for each DeclID.
2057 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2058 clang::io::Emit16(Out, DataLen);
2059
2060 return std::make_pair(KeyLen, DataLen);
2061 }
2062
2063 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2064 using namespace clang::io;
2065
2066 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2067 Emit8(Out, Name.getNameKind());
2068 switch (Name.getNameKind()) {
2069 case DeclarationName::Identifier:
2070 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2071 break;
2072 case DeclarationName::ObjCZeroArgSelector:
2073 case DeclarationName::ObjCOneArgSelector:
2074 case DeclarationName::ObjCMultiArgSelector:
2075 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2076 break;
2077 case DeclarationName::CXXConstructorName:
2078 case DeclarationName::CXXDestructorName:
2079 case DeclarationName::CXXConversionFunctionName:
2080 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2081 break;
2082 case DeclarationName::CXXOperatorName:
2083 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2084 Emit8(Out, Name.getCXXOverloadedOperator());
2085 break;
2086 case DeclarationName::CXXLiteralOperatorName:
2087 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2088 break;
2089 case DeclarationName::CXXUsingDirective:
2090 break;
2091 }
2092 }
2093
2094 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2095 data_type Lookup, unsigned DataLen) {
2096 uint64_t Start = Out.tell(); (void)Start;
2097 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2098 for (; Lookup.first != Lookup.second; ++Lookup.first)
2099 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2100
2101 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2102 }
2103};
2104} // end anonymous namespace
2105
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002106/// \brief Write the block containing all of the declaration IDs
2107/// visible from the given DeclContext.
2108///
2109/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002110/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002111uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2112 DeclContext *DC) {
2113 if (DC->getPrimaryContext() != DC)
2114 return 0;
2115
2116 // Since there is no name lookup into functions or methods, don't bother to
2117 // build a visible-declarations table for these entities.
2118 if (DC->isFunctionOrMethod())
2119 return 0;
2120
2121 // If not in C++, we perform name lookup for the translation unit via the
2122 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2123 // FIXME: In C++ we need the visible declarations in order to "see" the
2124 // friend declarations, is there a way to do this without writing the table ?
2125 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2126 return 0;
2127
2128 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00002129 if (DC->hasExternalVisibleStorage())
2130 DC->MaterializeVisibleDeclsFromExternalStorage();
2131 else
2132 DC->lookup(DeclarationName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002133
2134 // Serialize the contents of the mapping used for lookup. Note that,
2135 // although we have two very different code paths, the serialized
2136 // representation is the same for both cases: a declaration name,
2137 // followed by a size, followed by references to the visible
2138 // declarations that have that name.
2139 uint64_t Offset = Stream.GetCurrentBitNo();
2140 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2141 if (!Map || Map->empty())
2142 return 0;
2143
2144 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2145 ASTDeclContextNameLookupTrait Trait(*this);
2146
2147 // Create the on-disk hash table representation.
2148 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2149 D != DEnd; ++D) {
2150 DeclarationName Name = D->first;
2151 DeclContext::lookup_result Result = D->second.getLookupResult();
2152 Generator.insert(Name, Result, Trait);
2153 }
2154
2155 // Create the on-disk hash table in a buffer.
2156 llvm::SmallString<4096> LookupTable;
2157 uint32_t BucketOffset;
2158 {
2159 llvm::raw_svector_ostream Out(LookupTable);
2160 // Make sure that no bucket is at offset 0
2161 clang::io::Emit32(Out, 0);
2162 BucketOffset = Generator.Emit(Out, Trait);
2163 }
2164
2165 // Write the lookup table
2166 RecordData Record;
2167 Record.push_back(DECL_CONTEXT_VISIBLE);
2168 Record.push_back(BucketOffset);
2169 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2170 LookupTable.str());
2171
2172 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2173 ++NumVisibleDeclContexts;
2174 return Offset;
2175}
2176
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002177/// \brief Write an UPDATE_VISIBLE block for the given context.
2178///
2179/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2180/// DeclContext in a dependent AST file. As such, they only exist for the TU
2181/// (in C++) and for namespaces.
2182void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002183 // Make the context build its lookup table, but don't make it load external
2184 // decls.
2185 DC->lookup(DeclarationName());
2186
2187 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2188 if (!Map || Map->empty())
2189 return;
2190
2191 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2192 ASTDeclContextNameLookupTrait Trait(*this);
2193
2194 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002195 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2196 D != DEnd; ++D) {
2197 DeclarationName Name = D->first;
2198 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002199 // For any name that appears in this table, the results are complete, i.e.
2200 // they overwrite results from previous PCHs. Merging is always a mess.
2201 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002202 }
2203
2204 // Create the on-disk hash table in a buffer.
2205 llvm::SmallString<4096> LookupTable;
2206 uint32_t BucketOffset;
2207 {
2208 llvm::raw_svector_ostream Out(LookupTable);
2209 // Make sure that no bucket is at offset 0
2210 clang::io::Emit32(Out, 0);
2211 BucketOffset = Generator.Emit(Out, Trait);
2212 }
2213
2214 // Write the lookup table
2215 RecordData Record;
2216 Record.push_back(UPDATE_VISIBLE);
2217 Record.push_back(getDeclID(cast<Decl>(DC)));
2218 Record.push_back(BucketOffset);
2219 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2220}
2221
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002222//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002223// General Serialization Routines
2224//===----------------------------------------------------------------------===//
2225
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002226/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002227void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00002228 Record.push_back(Attrs.size());
Sean Huntcf807c42010-08-18 23:23:40 +00002229 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2230 const Attr * A = *i;
2231 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2232 AddSourceLocation(A->getLocation(), Record);
2233 Record.push_back(A->isInherited());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002234
Sean Huntcf807c42010-08-18 23:23:40 +00002235#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00002236
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002237 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002238}
2239
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002240void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002241 Record.push_back(Str.size());
2242 Record.insert(Record.end(), Str.begin(), Str.end());
2243}
2244
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002245/// \brief Note that the identifier II occurs at the given offset
2246/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002247void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002248 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00002249 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002250 // up earlier in the chain and thus don't need an offset.
2251 if (ID >= FirstIdentID)
2252 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002253}
2254
Douglas Gregor83941df2009-04-25 17:48:32 +00002255/// \brief Note that the selector Sel occurs at the given offset
2256/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002257void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00002258 unsigned ID = SelectorIDs[Sel];
2259 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00002260 // Don't record offsets for selectors that are also available in a different
2261 // file.
2262 if (ID < FirstSelectorID)
2263 return;
2264 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00002265}
2266
Sebastian Redla4232eb2010-08-18 23:56:21 +00002267ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregor89d99802010-11-30 06:16:57 +00002268 : Stream(Stream), Chain(0), SerializationListener(0),
2269 FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002270 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redle58aa892010-08-04 18:21:41 +00002271 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor77424bc2010-10-02 19:29:26 +00002272 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2273 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00002274 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor7c789c12010-10-29 22:39:52 +00002275 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
2276 NextCXXBaseSpecifiersID(1)
2277{
Sebastian Redl30c514c2010-07-14 23:45:08 +00002278}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002279
Sebastian Redla4232eb2010-08-18 23:56:21 +00002280void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002281 const char *isysroot) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002282 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002283 Stream.Emit((unsigned)'C', 8);
2284 Stream.Emit((unsigned)'P', 8);
2285 Stream.Emit((unsigned)'C', 8);
2286 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002287
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002288 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002289
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002290 if (Chain)
Sebastian Redla4232eb2010-08-18 23:56:21 +00002291 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002292 else
Sebastian Redla4232eb2010-08-18 23:56:21 +00002293 WriteASTCore(SemaRef, StatCalls, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002294}
2295
Sebastian Redla4232eb2010-08-18 23:56:21 +00002296void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002297 const char *isysroot) {
2298 using namespace llvm;
2299
2300 ASTContext &Context = SemaRef.Context;
2301 Preprocessor &PP = SemaRef.PP;
2302
Douglas Gregor2cf26342009-04-09 22:27:44 +00002303 // The translation unit is the first declaration we'll emit.
2304 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002305 ++NextDeclID;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002306 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002307
Douglas Gregor2deaea32009-04-22 18:49:13 +00002308 // Make sure that we emit IdentifierInfos (and any attached
2309 // declarations) for builtins.
2310 {
2311 IdentifierTable &Table = PP.getIdentifierTable();
2312 llvm::SmallVector<const char *, 32> BuiltinNames;
2313 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2314 Context.getLangOptions().NoBuiltin);
2315 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2316 getIdentifierRef(&Table.get(BuiltinNames[I]));
2317 }
2318
Chris Lattner63d65f82009-09-08 18:19:27 +00002319 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002320 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002321 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002322 RecordData TentativeDefinitions;
Sebastian Redle9d12b62010-01-31 22:27:38 +00002323 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2324 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner63d65f82009-09-08 18:19:27 +00002325 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002326
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002327 // Build a record containing all of the file scoped decls in this file.
2328 RecordData UnusedFileScopedDecls;
2329 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2330 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00002331
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002332 RecordData WeakUndeclaredIdentifiers;
2333 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2334 WeakUndeclaredIdentifiers.push_back(
2335 SemaRef.WeakUndeclaredIdentifiers.size());
2336 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2337 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2338 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2339 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2340 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2341 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2342 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2343 }
2344 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002345
Douglas Gregor14c22f22009-04-22 22:18:58 +00002346 // Build a record containing all of the locally-scoped external
2347 // declarations in this header file. Generally, this record will be
2348 // empty.
2349 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002350 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00002351 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002352 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002353 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2354 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2355 TD != TDEnd; ++TD)
2356 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2357
Douglas Gregorb81c1702009-04-27 20:06:05 +00002358 // Build a record containing all of the ext_vector declarations.
2359 RecordData ExtVectorDecls;
2360 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2361 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2362
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002363 // Build a record containing all of the VTable uses information.
2364 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00002365 if (!SemaRef.VTableUses.empty()) {
2366 VTableUses.push_back(SemaRef.VTableUses.size());
2367 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2368 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2369 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2370 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2371 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002372 }
2373
2374 // Build a record containing all of dynamic classes declarations.
2375 RecordData DynamicClasses;
2376 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2377 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2378
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002379 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002380 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002381 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00002382 I = SemaRef.PendingInstantiations.begin(),
2383 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2384 AddDeclRef(I->first, PendingInstantiations);
2385 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002386 }
2387 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2388 "There are local ones at end of translation unit!");
2389
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002390 // Build a record containing some declaration references.
2391 RecordData SemaDeclRefs;
2392 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2393 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2394 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2395 }
2396
Sebastian Redl3397c552010-08-18 23:56:27 +00002397 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002398 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002399 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl30c514c2010-07-14 23:45:08 +00002400 WriteMetadata(Context, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002401 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00002402 if (StatCalls && !isysroot)
Douglas Gregordd41ed52010-07-12 23:48:14 +00002403 WriteStatCache(*StatCalls);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002404 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002405 // Write the record of special types.
2406 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00002407
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002408 AddTypeRef(Context.getBuiltinVaListType(), Record);
2409 AddTypeRef(Context.getObjCIdType(), Record);
2410 AddTypeRef(Context.getObjCSelType(), Record);
2411 AddTypeRef(Context.getObjCProtoType(), Record);
2412 AddTypeRef(Context.getObjCClassType(), Record);
2413 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2414 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2415 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002416 AddTypeRef(Context.getjmp_bufType(), Record);
2417 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002418 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2419 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpadaaad32009-10-20 02:12:22 +00002420 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002421 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002422 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2423 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002424 Record.push_back(Context.isInt128Installed());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002425 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002426
Douglas Gregor366809a2009-04-26 03:49:13 +00002427 // Keep writing types and declarations until all types and
2428 // declarations have been written.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002429 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002430 WriteDeclsBlockAbbrevs();
2431 while (!DeclTypesToEmit.empty()) {
2432 DeclOrType DOT = DeclTypesToEmit.front();
2433 DeclTypesToEmit.pop();
2434 if (DOT.isType())
2435 WriteType(DOT.getType());
2436 else
2437 WriteDecl(Context, DOT.getDecl());
2438 }
2439 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002440
Douglas Gregor813a97b2009-10-17 17:25:45 +00002441 WritePreprocessor(PP);
Sebastian Redl059612d2010-08-03 21:58:15 +00002442 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002443 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002444 WriteIdentifierTable(PP);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002445
Sebastian Redl1476ed42010-07-16 16:36:56 +00002446 WriteTypeDeclOffsets();
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002447 WriteUserDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00002448
Douglas Gregor7c789c12010-10-29 22:39:52 +00002449 // Write the C++ base-specifier set offsets.
2450 if (!CXXBaseSpecifiersOffsets.empty()) {
2451 // Create a blob abbreviation for the C++ base specifiers offsets.
2452 using namespace llvm;
2453
2454 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2455 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2456 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2457 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2458 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2459
2460 // Write the selector offsets table.
2461 Record.clear();
2462 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2463 Record.push_back(CXXBaseSpecifiersOffsets.size());
2464 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
2465 (const char *)CXXBaseSpecifiersOffsets.data(),
2466 CXXBaseSpecifiersOffsets.size() * sizeof(uint32_t));
2467 }
2468
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002469 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002470 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002471 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002472
2473 // Write the record containing tentative definitions.
2474 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002475 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002476
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002477 // Write the record containing unused file scoped decls.
2478 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002479 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002480
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002481 // Write the record containing weak undeclared identifiers.
2482 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002483 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002484 WeakUndeclaredIdentifiers);
2485
Douglas Gregor14c22f22009-04-22 22:18:58 +00002486 // Write the record containing locally-scoped external definitions.
2487 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002488 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002489 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002490
2491 // Write the record containing ext_vector type names.
2492 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002493 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002494
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002495 // Write the record containing VTable uses information.
2496 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002497 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002498
2499 // Write the record containing dynamic classes declarations.
2500 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002501 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002502
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002503 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002504 if (!PendingInstantiations.empty())
2505 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002506
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002507 // Write the record containing declaration references of Sema.
2508 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002509 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002510
Douglas Gregor3e1af842009-04-17 22:13:46 +00002511 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002512 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002513 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002514 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002515 Record.push_back(NumLexicalDeclContexts);
2516 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002517 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002518 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002519}
2520
Sebastian Redla4232eb2010-08-18 23:56:21 +00002521void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002522 const char *isysroot) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002523 using namespace llvm;
2524
2525 ASTContext &Context = SemaRef.Context;
2526 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1476ed42010-07-16 16:36:56 +00002527
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002528 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002529 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl30c514c2010-07-14 23:45:08 +00002530 WriteMetadata(Context, isysroot);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002531 if (StatCalls && !isysroot)
2532 WriteStatCache(*StatCalls);
2533 // FIXME: Source manager block should only write new stuff, which could be
2534 // done by tracking the largest ID in the chain
2535 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002536
2537 // The special types are in the chained PCH.
2538
2539 // We don't start with the translation unit, but with its decls that
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002540 // don't come from the chained PCH.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002541 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002542 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl681d7232010-07-27 00:17:23 +00002543 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2544 E = TU->noload_decls_end();
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002545 I != E; ++I) {
Sebastian Redld692af72010-07-27 18:24:41 +00002546 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002547 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redl0b17c612010-08-13 00:28:03 +00002548 else if ((*I)->isChangedSinceDeserialization())
2549 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002550 }
Sebastian Redl681d7232010-07-27 00:17:23 +00002551 // We also need to write a lexical updates block for the TU.
Sebastian Redld692af72010-07-27 18:24:41 +00002552 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002553 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redld692af72010-07-27 18:24:41 +00002554 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2555 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2556 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002557 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redld692af72010-07-27 18:24:41 +00002558 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2559 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002560 NewGlobalDecls.size() * sizeof(KindDeclIDPair));
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00002561 // And a visible updates block for the DeclContexts.
2562 Abv = new llvm::BitCodeAbbrev();
2563 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2564 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2565 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2566 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2567 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2568 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002569
Sebastian Redl083abdf2010-07-27 23:01:28 +00002570 // Build a record containing all of the new tentative definitions in this
2571 // file, in TentativeDefinitions order.
2572 RecordData TentativeDefinitions;
2573 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2574 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2575 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2576 }
2577
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002578 // Build a record containing all of the file scoped decls in this file.
2579 RecordData UnusedFileScopedDecls;
2580 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2581 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2582 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002583 }
2584
Sebastian Redl40566802010-08-05 18:21:25 +00002585 // We write the entire table, overwriting the tables from the chain.
2586 RecordData WeakUndeclaredIdentifiers;
2587 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2588 WeakUndeclaredIdentifiers.push_back(
2589 SemaRef.WeakUndeclaredIdentifiers.size());
2590 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2591 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2592 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2593 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2594 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2595 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2596 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2597 }
2598 }
2599
Sebastian Redl083abdf2010-07-27 23:01:28 +00002600 // Build a record containing all of the locally-scoped external
2601 // declarations in this header file. Generally, this record will be
2602 // empty.
2603 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002604 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl083abdf2010-07-27 23:01:28 +00002605 // nondeterminstic!
2606 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2607 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2608 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2609 TD != TDEnd; ++TD) {
2610 if (TD->second->getPCHLevel() == 0)
2611 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2612 }
2613
2614 // Build a record containing all of the ext_vector declarations.
2615 RecordData ExtVectorDecls;
2616 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2617 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2618 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2619 }
2620
Sebastian Redl40566802010-08-05 18:21:25 +00002621 // Build a record containing all of the VTable uses information.
2622 // We write everything here, because it's too hard to determine whether
2623 // a use is new to this part.
2624 RecordData VTableUses;
2625 if (!SemaRef.VTableUses.empty()) {
2626 VTableUses.push_back(SemaRef.VTableUses.size());
2627 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2628 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2629 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2630 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2631 }
2632 }
2633
2634 // Build a record containing all of dynamic classes declarations.
2635 RecordData DynamicClasses;
2636 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2637 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
2638 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2639
2640 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002641 RecordData PendingInstantiations;
Sebastian Redl40566802010-08-05 18:21:25 +00002642 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00002643 I = SemaRef.PendingInstantiations.begin(),
2644 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl40566802010-08-05 18:21:25 +00002645 if (I->first->getPCHLevel() == 0) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002646 AddDeclRef(I->first, PendingInstantiations);
2647 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl40566802010-08-05 18:21:25 +00002648 }
2649 }
2650 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2651 "There are local ones at end of translation unit!");
2652
2653 // Build a record containing some declaration references.
2654 // It's not worth the effort to avoid duplication here.
2655 RecordData SemaDeclRefs;
2656 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2657 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2658 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2659 }
2660
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002661 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002662 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00002663 for (DeclsToRewriteTy::iterator
2664 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
2665 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002666 while (!DeclTypesToEmit.empty()) {
2667 DeclOrType DOT = DeclTypesToEmit.front();
2668 DeclTypesToEmit.pop();
2669 if (DOT.isType())
2670 WriteType(DOT.getType());
2671 else
2672 WriteDecl(Context, DOT.getDecl());
2673 }
2674 Stream.ExitBlock();
2675
Sebastian Redl083abdf2010-07-27 23:01:28 +00002676 WritePreprocessor(PP);
Sebastian Redla68340f2010-08-04 22:21:29 +00002677 WriteSelectors(SemaRef);
2678 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002679 WriteIdentifierTable(PP);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002680 WriteTypeDeclOffsets();
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002681 // FIXME: For chained PCH only write the new mappings (we currently
2682 // write all of them again).
2683 WriteUserDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl083abdf2010-07-27 23:01:28 +00002684
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002685 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl3397c552010-08-18 23:56:27 +00002686 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002687 RecordData FirstLatestDeclIDs;
2688 for (FirstLatestDeclMap::iterator
2689 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
2690 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
2691 "Expected first & second to be in different PCHs");
2692 AddDeclRef(I->first, FirstLatestDeclIDs);
2693 AddDeclRef(I->second, FirstLatestDeclIDs);
2694 }
2695 if (!FirstLatestDeclIDs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002696 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002697
Sebastian Redl083abdf2010-07-27 23:01:28 +00002698 // Write the record containing external, unnamed definitions.
2699 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002700 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002701
2702 // Write the record containing tentative definitions.
2703 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002704 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002705
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002706 // Write the record containing unused file scoped decls.
2707 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002708 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002709
Sebastian Redl40566802010-08-05 18:21:25 +00002710 // Write the record containing weak undeclared identifiers.
2711 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002712 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl40566802010-08-05 18:21:25 +00002713 WeakUndeclaredIdentifiers);
2714
Sebastian Redl083abdf2010-07-27 23:01:28 +00002715 // Write the record containing locally-scoped external definitions.
2716 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002717 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl083abdf2010-07-27 23:01:28 +00002718 LocallyScopedExternalDecls);
2719
2720 // Write the record containing ext_vector type names.
2721 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002722 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002723
Sebastian Redl40566802010-08-05 18:21:25 +00002724 // Write the record containing VTable uses information.
2725 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002726 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl40566802010-08-05 18:21:25 +00002727
2728 // Write the record containing dynamic classes declarations.
2729 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002730 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl40566802010-08-05 18:21:25 +00002731
2732 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002733 if (!PendingInstantiations.empty())
2734 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl40566802010-08-05 18:21:25 +00002735
2736 // Write the record containing declaration references of Sema.
2737 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002738 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002739
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00002740 // Write the updates to DeclContexts.
2741 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
2742 I = UpdatedDeclContexts.begin(),
2743 E = UpdatedDeclContexts.end();
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002744 I != E; ++I)
2745 WriteDeclContextVisibleUpdate(*I);
2746
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00002747 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002748
Sebastian Redl083abdf2010-07-27 23:01:28 +00002749 Record.clear();
2750 Record.push_back(NumStatements);
2751 Record.push_back(NumMacros);
2752 Record.push_back(NumLexicalDeclContexts);
2753 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00002754 WriteDeclReplacementsBlock();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002755 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002756 Stream.ExitBlock();
2757}
2758
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00002759void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002760 if (DeclUpdates.empty())
2761 return;
2762
2763 RecordData OffsetsRecord;
2764 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
2765 for (DeclUpdateMap::iterator
2766 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
2767 const Decl *D = I->first;
2768 UpdateRecord &URec = I->second;
2769
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00002770 if (DeclsToRewrite.count(D))
2771 continue; // The decl will be written completely,no need to store updates.
2772
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002773 uint64_t Offset = Stream.GetCurrentBitNo();
2774 Stream.EmitRecord(DECL_UPDATES, URec);
2775
2776 OffsetsRecord.push_back(GetDeclRef(D));
2777 OffsetsRecord.push_back(Offset);
2778 }
2779 Stream.ExitBlock();
2780 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
2781}
2782
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00002783void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00002784 if (ReplacedDecls.empty())
2785 return;
2786
2787 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002788 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00002789 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
2790 Record.push_back(I->first);
2791 Record.push_back(I->second);
2792 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002793 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00002794}
2795
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002796void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002797 Record.push_back(Loc.getRawEncoding());
2798}
2799
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002800void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002801 AddSourceLocation(Range.getBegin(), Record);
2802 AddSourceLocation(Range.getEnd(), Record);
2803}
2804
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002805void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002806 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00002807 const uint64_t *Words = Value.getRawData();
2808 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002809}
2810
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002811void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002812 Record.push_back(Value.isUnsigned());
2813 AddAPInt(Value, Record);
2814}
2815
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002816void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00002817 AddAPInt(Value.bitcastToAPInt(), Record);
2818}
2819
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002820void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002821 Record.push_back(getIdentifierRef(II));
2822}
2823
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002824IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002825 if (II == 0)
2826 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002827
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002828 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00002829 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002830 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00002831 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002832}
2833
Sebastian Redlf73c93f2010-09-15 19:54:06 +00002834MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002835 if (MD == 0)
2836 return 0;
Sebastian Redlf73c93f2010-09-15 19:54:06 +00002837
2838 MacroID &ID = MacroDefinitions[MD];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002839 if (ID == 0)
Douglas Gregor77424bc2010-10-02 19:29:26 +00002840 ID = NextMacroID++;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002841 return ID;
2842}
2843
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002844void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00002845 Record.push_back(getSelectorRef(SelRef));
2846}
2847
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002848SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00002849 if (Sel.getAsOpaquePtr() == 0) {
2850 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002851 }
2852
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002853 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00002854 if (SID == 0 && Chain) {
2855 // This might trigger a ReadSelector callback, which will set the ID for
2856 // this selector.
2857 Chain->LoadSelector(Sel);
2858 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002859 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00002860 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002861 }
Sebastian Redl5d050072010-08-04 17:20:04 +00002862 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002863}
2864
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002865void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00002866 AddDeclRef(Temp->getDestructor(), Record);
2867}
2868
Douglas Gregor7c789c12010-10-29 22:39:52 +00002869void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
2870 CXXBaseSpecifier const *BasesEnd,
2871 RecordDataImpl &Record) {
2872 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
2873 CXXBaseSpecifiersToWrite.push_back(
2874 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
2875 Bases, BasesEnd));
2876 Record.push_back(NextCXXBaseSpecifiersID++);
2877}
2878
Sebastian Redla4232eb2010-08-18 23:56:21 +00002879void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002880 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002881 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002882 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00002883 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002884 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00002885 break;
2886 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002887 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00002888 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00002889 case TemplateArgument::Template:
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002890 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2891 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00002892 break;
John McCall833ca992009-10-29 08:12:44 +00002893 case TemplateArgument::Null:
2894 case TemplateArgument::Integral:
2895 case TemplateArgument::Declaration:
2896 case TemplateArgument::Pack:
2897 break;
2898 }
2899}
2900
Sebastian Redla4232eb2010-08-18 23:56:21 +00002901void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002902 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002903 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002904
2905 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
2906 bool InfoHasSameExpr
2907 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
2908 Record.push_back(InfoHasSameExpr);
2909 if (InfoHasSameExpr)
2910 return; // Avoid storing the same expr twice.
2911 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002912 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
2913 Record);
2914}
2915
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002916void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00002917 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00002918 AddTypeRef(QualType(), Record);
2919 return;
2920 }
2921
John McCalla93c9342009-12-07 02:54:59 +00002922 AddTypeRef(TInfo->getType(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +00002923 TypeLocWriter TLW(*this, Record);
John McCalla93c9342009-12-07 02:54:59 +00002924 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002925 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00002926}
2927
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002928void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00002929 Record.push_back(GetOrCreateTypeID(T));
2930}
2931
2932TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00002933 return MakeTypeID(T,
2934 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
2935}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002936
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002937TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00002938 return MakeTypeID(T,
2939 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00002940}
2941
2942TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
2943 if (T.isNull())
2944 return TypeIdx();
2945 assert(!T.getLocalFastQualifiers());
2946
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002947 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002948 if (Idx.getIndex() == 0) {
Douglas Gregor366809a2009-04-26 03:49:13 +00002949 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00002950 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002951 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002952 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00002953 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00002954 return Idx;
2955}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002956
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002957TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00002958 if (T.isNull())
2959 return TypeIdx();
2960 assert(!T.getLocalFastQualifiers());
2961
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002962 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
2963 assert(I != TypeIdxs.end() && "Type not emitted!");
2964 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002965}
2966
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002967void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00002968 Record.push_back(GetDeclRef(D));
2969}
2970
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002971DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002972 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00002973 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002974 }
Douglas Gregor97475832010-10-05 18:37:06 +00002975 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002976 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00002977 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002978 // We haven't seen this declaration before. Give it a new ID and
2979 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002980 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002981 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redl0b17c612010-08-13 00:28:03 +00002982 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
2983 // We don't add it to the replacement collection here, because we don't
2984 // have the offset yet.
2985 DeclTypesToEmit.push(const_cast<Decl *>(D));
2986 // Reset the flag, so that we don't add this decl multiple times.
2987 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002988 }
2989
Sebastian Redl681d7232010-07-27 00:17:23 +00002990 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002991}
2992
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002993DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002994 if (D == 0)
2995 return 0;
2996
2997 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2998 return DeclIDs[D];
2999}
3000
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003001void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00003002 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00003003 Record.push_back(Name.getNameKind());
3004 switch (Name.getNameKind()) {
3005 case DeclarationName::Identifier:
3006 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3007 break;
3008
3009 case DeclarationName::ObjCZeroArgSelector:
3010 case DeclarationName::ObjCOneArgSelector:
3011 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003012 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003013 break;
3014
3015 case DeclarationName::CXXConstructorName:
3016 case DeclarationName::CXXDestructorName:
3017 case DeclarationName::CXXConversionFunctionName:
3018 AddTypeRef(Name.getCXXNameType(), Record);
3019 break;
3020
3021 case DeclarationName::CXXOperatorName:
3022 Record.push_back(Name.getCXXOverloadedOperator());
3023 break;
3024
Sean Hunt3e518bd2009-11-29 07:34:05 +00003025 case DeclarationName::CXXLiteralOperatorName:
3026 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3027 break;
3028
Douglas Gregor2cf26342009-04-09 22:27:44 +00003029 case DeclarationName::CXXUsingDirective:
3030 // No extra data to emit
3031 break;
3032 }
3033}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003034
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003035void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003036 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003037 switch (Name.getNameKind()) {
3038 case DeclarationName::CXXConstructorName:
3039 case DeclarationName::CXXDestructorName:
3040 case DeclarationName::CXXConversionFunctionName:
3041 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3042 break;
3043
3044 case DeclarationName::CXXOperatorName:
3045 AddSourceLocation(
3046 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3047 Record);
3048 AddSourceLocation(
3049 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3050 Record);
3051 break;
3052
3053 case DeclarationName::CXXLiteralOperatorName:
3054 AddSourceLocation(
3055 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3056 Record);
3057 break;
3058
3059 case DeclarationName::Identifier:
3060 case DeclarationName::ObjCZeroArgSelector:
3061 case DeclarationName::ObjCOneArgSelector:
3062 case DeclarationName::ObjCMultiArgSelector:
3063 case DeclarationName::CXXUsingDirective:
3064 break;
3065 }
3066}
3067
3068void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003069 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003070 AddDeclarationName(NameInfo.getName(), Record);
3071 AddSourceLocation(NameInfo.getLoc(), Record);
3072 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3073}
3074
3075void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003076 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003077 AddNestedNameSpecifier(Info.NNS, Record);
3078 AddSourceRange(Info.NNSRange, Record);
3079 Record.push_back(Info.NumTemplParamLists);
3080 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3081 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3082}
3083
Sebastian Redla4232eb2010-08-18 23:56:21 +00003084void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003085 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003086 // Nested name specifiers usually aren't too long. I think that 8 would
3087 // typically accomodate the vast majority.
3088 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3089
3090 // Push each of the NNS's onto a stack for serialization in reverse order.
3091 while (NNS) {
3092 NestedNames.push_back(NNS);
3093 NNS = NNS->getPrefix();
3094 }
3095
3096 Record.push_back(NestedNames.size());
3097 while(!NestedNames.empty()) {
3098 NNS = NestedNames.pop_back_val();
3099 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3100 Record.push_back(Kind);
3101 switch (Kind) {
3102 case NestedNameSpecifier::Identifier:
3103 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3104 break;
3105
3106 case NestedNameSpecifier::Namespace:
3107 AddDeclRef(NNS->getAsNamespace(), Record);
3108 break;
3109
3110 case NestedNameSpecifier::TypeSpec:
3111 case NestedNameSpecifier::TypeSpecWithTemplate:
3112 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3113 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3114 break;
3115
3116 case NestedNameSpecifier::Global:
3117 // Don't need to write an associated value.
3118 break;
3119 }
3120 }
3121}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003122
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003123void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00003124 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003125 Record.push_back(Kind);
3126 switch (Kind) {
3127 case TemplateName::Template:
3128 AddDeclRef(Name.getAsTemplateDecl(), Record);
3129 break;
3130
3131 case TemplateName::OverloadedTemplate: {
3132 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3133 Record.push_back(OvT->size());
3134 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3135 I != E; ++I)
3136 AddDeclRef(*I, Record);
3137 break;
3138 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003139
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003140 case TemplateName::QualifiedTemplate: {
3141 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3142 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3143 Record.push_back(QualT->hasTemplateKeyword());
3144 AddDeclRef(QualT->getTemplateDecl(), Record);
3145 break;
3146 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003147
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003148 case TemplateName::DependentTemplate: {
3149 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3150 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3151 Record.push_back(DepT->isIdentifier());
3152 if (DepT->isIdentifier())
3153 AddIdentifierRef(DepT->getIdentifier(), Record);
3154 else
3155 Record.push_back(DepT->getOperator());
3156 break;
3157 }
3158 }
3159}
3160
Michael J. Spencer20249a12010-10-21 03:16:25 +00003161void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003162 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003163 Record.push_back(Arg.getKind());
3164 switch (Arg.getKind()) {
3165 case TemplateArgument::Null:
3166 break;
3167 case TemplateArgument::Type:
3168 AddTypeRef(Arg.getAsType(), Record);
3169 break;
3170 case TemplateArgument::Declaration:
3171 AddDeclRef(Arg.getAsDecl(), Record);
3172 break;
3173 case TemplateArgument::Integral:
3174 AddAPSInt(*Arg.getAsIntegral(), Record);
3175 AddTypeRef(Arg.getIntegralType(), Record);
3176 break;
3177 case TemplateArgument::Template:
3178 AddTemplateName(Arg.getAsTemplate(), Record);
3179 break;
3180 case TemplateArgument::Expression:
3181 AddStmt(Arg.getAsExpr());
3182 break;
3183 case TemplateArgument::Pack:
3184 Record.push_back(Arg.pack_size());
3185 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3186 I != E; ++I)
3187 AddTemplateArgument(*I, Record);
3188 break;
3189 }
3190}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003191
3192void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003193ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003194 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003195 assert(TemplateParams && "No TemplateParams!");
3196 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3197 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3198 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3199 Record.push_back(TemplateParams->size());
3200 for (TemplateParameterList::const_iterator
3201 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3202 P != PEnd; ++P)
3203 AddDeclRef(*P, Record);
3204}
3205
3206/// \brief Emit a template argument list.
3207void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003208ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003209 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003210 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00003211 Record.push_back(TemplateArgs->size());
3212 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003213 AddTemplateArgument(TemplateArgs->get(i), Record);
3214}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003215
3216
3217void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003218ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003219 Record.push_back(Set.size());
3220 for (UnresolvedSetImpl::const_iterator
3221 I = Set.begin(), E = Set.end(); I != E; ++I) {
3222 AddDeclRef(I.getDecl(), Record);
3223 Record.push_back(I.getAccess());
3224 }
3225}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003226
Sebastian Redla4232eb2010-08-18 23:56:21 +00003227void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003228 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003229 Record.push_back(Base.isVirtual());
3230 Record.push_back(Base.isBaseOfClass());
3231 Record.push_back(Base.getAccessSpecifierAsWritten());
Nick Lewycky56062202010-07-26 16:56:01 +00003232 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003233 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00003234 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3235 : SourceLocation(),
3236 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003237}
Sebastian Redl30c514c2010-07-14 23:45:08 +00003238
Douglas Gregor7c789c12010-10-29 22:39:52 +00003239void ASTWriter::FlushCXXBaseSpecifiers() {
3240 RecordData Record;
3241 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3242 Record.clear();
3243
3244 // Record the offset of this base-specifier set.
3245 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3246 if (Index == CXXBaseSpecifiersOffsets.size())
3247 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3248 else {
3249 if (Index > CXXBaseSpecifiersOffsets.size())
3250 CXXBaseSpecifiersOffsets.resize(Index + 1);
3251 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3252 }
3253
3254 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3255 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3256 Record.push_back(BEnd - B);
3257 for (; B != BEnd; ++B)
3258 AddCXXBaseSpecifier(*B, Record);
3259 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00003260
3261 // Flush any expressions that were written as part of the base specifiers.
3262 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003263 }
3264
3265 CXXBaseSpecifiersToWrite.clear();
3266}
3267
Sebastian Redla4232eb2010-08-18 23:56:21 +00003268void ASTWriter::AddCXXBaseOrMemberInitializers(
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003269 const CXXBaseOrMemberInitializer * const *BaseOrMembers,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003270 unsigned NumBaseOrMembers, RecordDataImpl &Record) {
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003271 Record.push_back(NumBaseOrMembers);
3272 for (unsigned i=0; i != NumBaseOrMembers; ++i) {
3273 const CXXBaseOrMemberInitializer *Init = BaseOrMembers[i];
3274
3275 Record.push_back(Init->isBaseInitializer());
3276 if (Init->isBaseInitializer()) {
3277 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3278 Record.push_back(Init->isBaseVirtual());
3279 } else {
Francois Pichet00eb3f92010-12-04 09:14:42 +00003280 Record.push_back(Init->isIndirectMemberInitializer());
3281 if (Init->isIndirectMemberInitializer())
3282 AddDeclRef(Init->getIndirectMember(), Record);
3283 else
3284 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003285 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00003286
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003287 AddSourceLocation(Init->getMemberLocation(), Record);
3288 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003289 AddSourceLocation(Init->getLParenLoc(), Record);
3290 AddSourceLocation(Init->getRParenLoc(), Record);
3291 Record.push_back(Init->isWritten());
3292 if (Init->isWritten()) {
3293 Record.push_back(Init->getSourceOrder());
3294 } else {
3295 Record.push_back(Init->getNumArrayIndices());
3296 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3297 AddDeclRef(Init->getArrayIndex(i), Record);
3298 }
3299 }
3300}
3301
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003302void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3303 assert(D->DefinitionData);
3304 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3305 Record.push_back(Data.UserDeclaredConstructor);
3306 Record.push_back(Data.UserDeclaredCopyConstructor);
3307 Record.push_back(Data.UserDeclaredCopyAssignment);
3308 Record.push_back(Data.UserDeclaredDestructor);
3309 Record.push_back(Data.Aggregate);
3310 Record.push_back(Data.PlainOldData);
3311 Record.push_back(Data.Empty);
3312 Record.push_back(Data.Polymorphic);
3313 Record.push_back(Data.Abstract);
3314 Record.push_back(Data.HasTrivialConstructor);
3315 Record.push_back(Data.HasTrivialCopyConstructor);
3316 Record.push_back(Data.HasTrivialCopyAssignment);
3317 Record.push_back(Data.HasTrivialDestructor);
3318 Record.push_back(Data.ComputedVisibleConversions);
3319 Record.push_back(Data.DeclaredDefaultConstructor);
3320 Record.push_back(Data.DeclaredCopyConstructor);
3321 Record.push_back(Data.DeclaredCopyAssignment);
3322 Record.push_back(Data.DeclaredDestructor);
3323
3324 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003325 if (Data.NumBases > 0)
3326 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3327 Record);
3328
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003329 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3330 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003331 if (Data.NumVBases > 0)
3332 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3333 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003334
3335 AddUnresolvedSet(Data.Conversions, Record);
3336 AddUnresolvedSet(Data.VisibleConversions, Record);
3337 // Data.Definition is the owning decl, no need to write it.
3338 AddDeclRef(Data.FirstFriend, Record);
3339}
3340
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003341void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003342 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003343 assert(!Chain && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003344 assert(FirstDeclID == NextDeclID &&
3345 FirstTypeID == NextTypeID &&
3346 FirstIdentID == NextIdentID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00003347 FirstSelectorID == NextSelectorID &&
Douglas Gregor77424bc2010-10-02 19:29:26 +00003348 FirstMacroID == NextMacroID &&
Douglas Gregor7c789c12010-10-29 22:39:52 +00003349 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003350 "Setting chain after writing has started.");
3351 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003352
3353 FirstDeclID += Chain->getTotalNumDecls();
3354 FirstTypeID += Chain->getTotalNumTypes();
3355 FirstIdentID += Chain->getTotalNumIdentifiers();
3356 FirstSelectorID += Chain->getTotalNumSelectors();
3357 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003358 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003359 NextDeclID = FirstDeclID;
3360 NextTypeID = FirstTypeID;
3361 NextIdentID = FirstIdentID;
3362 NextSelectorID = FirstSelectorID;
3363 NextMacroID = FirstMacroID;
Douglas Gregor7c789c12010-10-29 22:39:52 +00003364 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003365}
3366
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003367void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003368 IdentifierIDs[II] = ID;
3369}
3370
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003371void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00003372 // Always take the highest-numbered type index. This copes with an interesting
3373 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00003374 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00003375 // keep the higher-numbered entry so that we can properly write it out to
3376 // the AST file.
3377 TypeIdx &StoredIdx = TypeIdxs[T];
3378 if (Idx.getIndex() >= StoredIdx.getIndex())
3379 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003380}
3381
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003382void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1476ed42010-07-16 16:36:56 +00003383 DeclIDs[D] = ID;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003384}
Sebastian Redl5d050072010-08-04 17:20:04 +00003385
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003386void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003387 SelectorIDs[S] = ID;
3388}
Douglas Gregor77424bc2010-10-02 19:29:26 +00003389
Michael J. Spencer20249a12010-10-21 03:16:25 +00003390void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00003391 MacroDefinition *MD) {
3392 MacroDefinitions[MD] = ID;
3393}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003394
3395void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3396 assert(D->isDefinition());
3397 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3398 // We are interested when a PCH decl is modified.
3399 if (RD->getPCHLevel() > 0) {
3400 // A forward reference was mutated into a definition. Rewrite it.
3401 // FIXME: This happens during template instantiation, should we
3402 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00003403 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003404 }
3405
3406 for (CXXRecordDecl::redecl_iterator
3407 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3408 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3409 if (Redecl == RD)
3410 continue;
3411
3412 // We are interested when a PCH decl is modified.
3413 if (Redecl->getPCHLevel() > 0) {
3414 UpdateRecord &Record = DeclUpdates[Redecl];
3415 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3416 assert(Redecl->DefinitionData);
3417 assert(Redecl->DefinitionData->Definition == D);
3418 AddDeclRef(D, Record); // the DefinitionDecl
3419 }
3420 }
3421 }
3422}
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00003423void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3424 // TU and namespaces are handled elsewhere.
3425 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3426 return;
3427
3428 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3429 return; // Not a source decl added to a DeclContext from PCH.
3430
3431 AddUpdatedDeclContext(DC);
3432}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00003433
3434void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3435 assert(D->isImplicit());
3436 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3437 return; // Not a source member added to a class from PCH.
3438 if (!isa<CXXMethodDecl>(D))
3439 return; // We are interested in lazily declared implicit methods.
3440
3441 // A decl coming from PCH was modified.
3442 assert(RD->isDefinition());
3443 UpdateRecord &Record = DeclUpdates[RD];
3444 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3445 AddDeclRef(D, Record);
3446}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00003447
3448void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3449 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00003450 // The specializations set is kept in the canonical template.
3451 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00003452 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3453 return; // Not a source specialization added to a template from PCH.
3454
3455 UpdateRecord &Record = DeclUpdates[TD];
3456 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3457 AddDeclRef(D, Record);
3458}
Douglas Gregor89d99802010-11-30 06:16:57 +00003459
3460ASTSerializationListener::~ASTSerializationListener() { }