blob: 345cc8e74c7bf227f2a29f981c5741d53d852271 [file] [log] [blame]
Sebastian Redl4ee2ad02010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redla4232eb2010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregor2cf26342009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl7faa2ec2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Douglas Gregor89d99802010-11-30 06:16:57 +000015#include "clang/Serialization/ASTSerializationListener.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000016#include "ASTCommon.h"
Douglas Gregore737f502010-08-12 20:07:10 +000017#include "clang/Sema/Sema.h"
18#include "clang/Sema/IdentifierResolver.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclContextInternals.h"
John McCall2a7fb272010-08-25 05:32:35 +000022#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000023#include "clang/AST/DeclFriend.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000024#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000025#include "clang/AST/ExprCXX.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000026#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000027#include "clang/AST/TypeLocVisitor.h"
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000028#include "clang/Serialization/ASTReader.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000029#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000030#include "clang/Lex/PreprocessingRecord.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000031#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000032#include "clang/Lex/HeaderSearch.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000033#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000034#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000035#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000036#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000037#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000038#include "clang/Basic/TargetInfo.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000039#include "clang/Basic/Version.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000040#include "llvm/ADT/APFloat.h"
41#include "llvm/ADT/APInt.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000042#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000043#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000044#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000045#include "llvm/Support/Path.h"
Chris Lattner3c304bd2009-04-11 18:40:46 +000046#include <cstdio>
Douglas Gregor2cf26342009-04-09 22:27:44 +000047using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000048using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000049
Sebastian Redlade50002010-07-30 17:03:48 +000050template <typename T, typename Allocator>
51T *data(std::vector<T, Allocator> &v) {
52 return v.empty() ? 0 : &v.front();
53}
54template <typename T, typename Allocator>
55const T *data(const std::vector<T, Allocator> &v) {
56 return v.empty() ? 0 : &v.front();
57}
58
Douglas Gregor2cf26342009-04-09 22:27:44 +000059//===----------------------------------------------------------------------===//
60// Type serialization
61//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000062
Douglas Gregor2cf26342009-04-09 22:27:44 +000063namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000064 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000065 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000066 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000067
68 public:
69 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000070 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000071
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000072 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000073 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000074
75 void VisitArrayType(const ArrayType *T);
76 void VisitFunctionType(const FunctionType *T);
77 void VisitTagType(const TagType *T);
78
79#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
80#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000081#include "clang/AST/TypeNodes.def"
82 };
83}
84
Sebastian Redl3397c552010-08-18 23:56:27 +000085void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000086 assert(false && "Built-in types are never serialized");
87}
88
Sebastian Redl3397c552010-08-18 23:56:27 +000089void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000090 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +000091 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +000092}
93
Sebastian Redl3397c552010-08-18 23:56:27 +000094void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000095 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +000096 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +000097}
98
Sebastian Redl3397c552010-08-18 23:56:27 +000099void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000100 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000101 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000102}
103
Sebastian Redl3397c552010-08-18 23:56:27 +0000104void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000105 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000106 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000107}
108
Sebastian Redl3397c552010-08-18 23:56:27 +0000109void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000110 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000111 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000112}
113
Sebastian Redl3397c552010-08-18 23:56:27 +0000114void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000115 Writer.AddTypeRef(T->getPointeeType(), Record);
116 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000117 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000118}
119
Sebastian Redl3397c552010-08-18 23:56:27 +0000120void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000121 Writer.AddTypeRef(T->getElementType(), Record);
122 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000123 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000124}
125
Sebastian Redl3397c552010-08-18 23:56:27 +0000126void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000127 VisitArrayType(T);
128 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000129 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000130}
131
Sebastian Redl3397c552010-08-18 23:56:27 +0000132void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000133 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000134 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000135}
136
Sebastian Redl3397c552010-08-18 23:56:27 +0000137void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000138 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000139 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
140 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000141 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000142 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000143}
144
Sebastian Redl3397c552010-08-18 23:56:27 +0000145void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000146 Writer.AddTypeRef(T->getElementType(), Record);
147 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000148 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000149 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000150}
151
Sebastian Redl3397c552010-08-18 23:56:27 +0000152void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000153 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000154 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000155}
156
Sebastian Redl3397c552010-08-18 23:56:27 +0000157void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000158 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000159 FunctionType::ExtInfo C = T->getExtInfo();
160 Record.push_back(C.getNoReturn());
Rafael Espindola425ef722010-03-30 22:15:11 +0000161 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000162 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000163 Record.push_back(C.getCC());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000164}
165
Sebastian Redl3397c552010-08-18 23:56:27 +0000166void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000167 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000168 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000169}
170
Sebastian Redl3397c552010-08-18 23:56:27 +0000171void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000172 VisitFunctionType(T);
173 Record.push_back(T->getNumArgs());
174 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
175 Writer.AddTypeRef(T->getArgType(I), Record);
176 Record.push_back(T->isVariadic());
177 Record.push_back(T->getTypeQuals());
Sebastian Redl465226e2009-05-27 22:11:52 +0000178 Record.push_back(T->hasExceptionSpec());
179 Record.push_back(T->hasAnyExceptionSpec());
180 Record.push_back(T->getNumExceptions());
181 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
182 Writer.AddTypeRef(T->getExceptionType(I), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000183 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000184}
185
Sebastian Redl3397c552010-08-18 23:56:27 +0000186void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000187 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000188 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000189}
John McCalled976492009-12-04 22:46:56 +0000190
Sebastian Redl3397c552010-08-18 23:56:27 +0000191void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000192 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000193 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
194 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000195 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000196}
197
Sebastian Redl3397c552010-08-18 23:56:27 +0000198void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000199 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000200 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000201}
202
Sebastian Redl3397c552010-08-18 23:56:27 +0000203void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000204 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000205 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000206}
207
Sebastian Redl3397c552010-08-18 23:56:27 +0000208void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson395b4752009-06-24 19:06:50 +0000209 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000210 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000211}
212
Sebastian Redl3397c552010-08-18 23:56:27 +0000213void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000214 Record.push_back(T->isDependentType());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000215 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000216 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000217 "Cannot serialize in the middle of a type definition");
218}
219
Sebastian Redl3397c552010-08-18 23:56:27 +0000220void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000221 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000222 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000223}
224
Sebastian Redl3397c552010-08-18 23:56:27 +0000225void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000226 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000227 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000228}
229
Mike Stump1eb44332009-09-09 15:08:12 +0000230void
Sebastian Redl3397c552010-08-18 23:56:27 +0000231ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000232 const SubstTemplateTypeParmType *T) {
233 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
234 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000235 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000236}
237
238void
Sebastian Redl3397c552010-08-18 23:56:27 +0000239ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000240 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000241 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000242 Writer.AddTemplateName(T->getTemplateName(), Record);
243 Record.push_back(T->getNumArgs());
244 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
245 ArgI != ArgE; ++ArgI)
246 Writer.AddTemplateArgument(*ArgI, Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000247 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
248 : T->getCanonicalTypeInternal(),
249 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000250 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000251}
252
253void
Sebastian Redl3397c552010-08-18 23:56:27 +0000254ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000255 VisitArrayType(T);
256 Writer.AddStmt(T->getSizeExpr());
257 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000258 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000259}
260
261void
Sebastian Redl3397c552010-08-18 23:56:27 +0000262ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000263 const DependentSizedExtVectorType *T) {
264 // FIXME: Serialize this type (C++ only)
265 assert(false && "Cannot serialize dependent sized extended vector types");
266}
267
268void
Sebastian Redl3397c552010-08-18 23:56:27 +0000269ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000270 Record.push_back(T->getDepth());
271 Record.push_back(T->getIndex());
272 Record.push_back(T->isParameterPack());
273 Writer.AddIdentifierRef(T->getName(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000274 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000275}
276
277void
Sebastian Redl3397c552010-08-18 23:56:27 +0000278ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000279 Record.push_back(T->getKeyword());
280 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
281 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000282 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
283 : T->getCanonicalTypeInternal(),
284 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000285 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000286}
287
288void
Sebastian Redl3397c552010-08-18 23:56:27 +0000289ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000290 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000291 Record.push_back(T->getKeyword());
292 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
293 Writer.AddIdentifierRef(T->getIdentifier(), Record);
294 Record.push_back(T->getNumArgs());
295 for (DependentTemplateSpecializationType::iterator
296 I = T->begin(), E = T->end(); I != E; ++I)
297 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000298 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000299}
300
Douglas Gregor7536dd52010-12-20 02:24:11 +0000301void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
302 Writer.AddTypeRef(T->getPattern(), Record);
303 Code = TYPE_PACK_EXPANSION;
304}
305
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000306void ASTTypeWriter::VisitParenType(const ParenType *T) {
307 Writer.AddTypeRef(T->getInnerType(), Record);
308 Code = TYPE_PAREN;
309}
310
Sebastian Redl3397c552010-08-18 23:56:27 +0000311void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000312 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000313 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
314 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000315 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000316}
317
Sebastian Redl3397c552010-08-18 23:56:27 +0000318void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCall3cb0ebd2010-03-10 03:28:59 +0000319 Writer.AddDeclRef(T->getDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000320 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000321 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000322}
323
Sebastian Redl3397c552010-08-18 23:56:27 +0000324void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000325 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000326 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000327}
328
Sebastian Redl3397c552010-08-18 23:56:27 +0000329void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000330 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000331 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000332 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000333 E = T->qual_end(); I != E; ++I)
334 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000335 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000336}
337
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000338void
Sebastian Redl3397c552010-08-18 23:56:27 +0000339ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000340 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000341 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000342}
343
John McCalla1ee0c52009-10-16 21:56:05 +0000344namespace {
345
346class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000347 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000348 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000349
350public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000351 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000352 : Writer(Writer), Record(Record) { }
353
John McCall51bd8032009-10-18 01:05:36 +0000354#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000355#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000356 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000357#include "clang/AST/TypeLocNodes.def"
358
John McCall51bd8032009-10-18 01:05:36 +0000359 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
360 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000361};
362
363}
364
John McCall51bd8032009-10-18 01:05:36 +0000365void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
366 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000367}
John McCall51bd8032009-10-18 01:05:36 +0000368void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000369 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
370 if (TL.needsExtraLocalData()) {
371 Record.push_back(TL.getWrittenTypeSpec());
372 Record.push_back(TL.getWrittenSignSpec());
373 Record.push_back(TL.getWrittenWidthSpec());
374 Record.push_back(TL.hasModeAttr());
375 }
John McCalla1ee0c52009-10-16 21:56:05 +0000376}
John McCall51bd8032009-10-18 01:05:36 +0000377void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
378 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000379}
John McCall51bd8032009-10-18 01:05:36 +0000380void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
381 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000382}
John McCall51bd8032009-10-18 01:05:36 +0000383void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
384 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000385}
John McCall51bd8032009-10-18 01:05:36 +0000386void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
387 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000388}
John McCall51bd8032009-10-18 01:05:36 +0000389void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
390 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000391}
John McCall51bd8032009-10-18 01:05:36 +0000392void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
393 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000394}
John McCall51bd8032009-10-18 01:05:36 +0000395void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
396 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
397 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
398 Record.push_back(TL.getSizeExpr() ? 1 : 0);
399 if (TL.getSizeExpr())
400 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000401}
John McCall51bd8032009-10-18 01:05:36 +0000402void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
403 VisitArrayTypeLoc(TL);
404}
405void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
406 VisitArrayTypeLoc(TL);
407}
408void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
409 VisitArrayTypeLoc(TL);
410}
411void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
412 DependentSizedArrayTypeLoc TL) {
413 VisitArrayTypeLoc(TL);
414}
415void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
416 DependentSizedExtVectorTypeLoc TL) {
417 Writer.AddSourceLocation(TL.getNameLoc(), Record);
418}
419void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
420 Writer.AddSourceLocation(TL.getNameLoc(), Record);
421}
422void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
423 Writer.AddSourceLocation(TL.getNameLoc(), Record);
424}
425void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
426 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
427 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Douglas Gregordab60ad2010-10-01 18:44:50 +0000428 Record.push_back(TL.getTrailingReturn());
John McCall51bd8032009-10-18 01:05:36 +0000429 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
430 Writer.AddDeclRef(TL.getArg(i), Record);
431}
432void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
433 VisitFunctionTypeLoc(TL);
434}
435void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
436 VisitFunctionTypeLoc(TL);
437}
John McCalled976492009-12-04 22:46:56 +0000438void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
439 Writer.AddSourceLocation(TL.getNameLoc(), Record);
440}
John McCall51bd8032009-10-18 01:05:36 +0000441void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
442 Writer.AddSourceLocation(TL.getNameLoc(), Record);
443}
444void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000445 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
446 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
447 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000448}
449void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000450 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
451 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
452 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
453 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000454}
455void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
456 Writer.AddSourceLocation(TL.getNameLoc(), Record);
457}
458void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
459 Writer.AddSourceLocation(TL.getNameLoc(), Record);
460}
461void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
462 Writer.AddSourceLocation(TL.getNameLoc(), Record);
463}
John McCall51bd8032009-10-18 01:05:36 +0000464void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
465 Writer.AddSourceLocation(TL.getNameLoc(), Record);
466}
John McCall49a832b2009-10-18 09:09:24 +0000467void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
468 SubstTemplateTypeParmTypeLoc TL) {
469 Writer.AddSourceLocation(TL.getNameLoc(), Record);
470}
John McCall51bd8032009-10-18 01:05:36 +0000471void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
472 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000473 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
474 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
475 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
476 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000477 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
478 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000479}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000480void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
481 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
482 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
483}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000484void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000485 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
486 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000487}
John McCall3cb0ebd2010-03-10 03:28:59 +0000488void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
489 Writer.AddSourceLocation(TL.getNameLoc(), Record);
490}
Douglas Gregor4714c122010-03-31 17:34:00 +0000491void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000492 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
493 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000494 Writer.AddSourceLocation(TL.getNameLoc(), Record);
495}
John McCall33500952010-06-11 00:33:02 +0000496void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
497 DependentTemplateSpecializationTypeLoc TL) {
498 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
499 Writer.AddSourceRange(TL.getQualifierRange(), Record);
500 Writer.AddSourceLocation(TL.getNameLoc(), Record);
501 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
502 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
503 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000504 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
505 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000506}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000507void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
508 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
509}
John McCall51bd8032009-10-18 01:05:36 +0000510void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
511 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000512}
513void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
514 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000515 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
516 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
517 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
518 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000519}
John McCall54e14c42009-10-22 22:37:11 +0000520void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
521 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000522}
John McCalla1ee0c52009-10-16 21:56:05 +0000523
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000524//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000525// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000526//===----------------------------------------------------------------------===//
527
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000528static void EmitBlockID(unsigned ID, const char *Name,
529 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000530 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000531 Record.clear();
532 Record.push_back(ID);
533 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
534
535 // Emit the block name if present.
536 if (Name == 0 || Name[0] == 0) return;
537 Record.clear();
538 while (*Name)
539 Record.push_back(*Name++);
540 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
541}
542
543static void EmitRecordID(unsigned ID, const char *Name,
544 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000545 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000546 Record.clear();
547 Record.push_back(ID);
548 while (*Name)
549 Record.push_back(*Name++);
550 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000551}
552
553static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000554 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000555#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000556 RECORD(STMT_STOP);
557 RECORD(STMT_NULL_PTR);
558 RECORD(STMT_NULL);
559 RECORD(STMT_COMPOUND);
560 RECORD(STMT_CASE);
561 RECORD(STMT_DEFAULT);
562 RECORD(STMT_LABEL);
563 RECORD(STMT_IF);
564 RECORD(STMT_SWITCH);
565 RECORD(STMT_WHILE);
566 RECORD(STMT_DO);
567 RECORD(STMT_FOR);
568 RECORD(STMT_GOTO);
569 RECORD(STMT_INDIRECT_GOTO);
570 RECORD(STMT_CONTINUE);
571 RECORD(STMT_BREAK);
572 RECORD(STMT_RETURN);
573 RECORD(STMT_DECL);
574 RECORD(STMT_ASM);
575 RECORD(EXPR_PREDEFINED);
576 RECORD(EXPR_DECL_REF);
577 RECORD(EXPR_INTEGER_LITERAL);
578 RECORD(EXPR_FLOATING_LITERAL);
579 RECORD(EXPR_IMAGINARY_LITERAL);
580 RECORD(EXPR_STRING_LITERAL);
581 RECORD(EXPR_CHARACTER_LITERAL);
582 RECORD(EXPR_PAREN);
583 RECORD(EXPR_UNARY_OPERATOR);
584 RECORD(EXPR_SIZEOF_ALIGN_OF);
585 RECORD(EXPR_ARRAY_SUBSCRIPT);
586 RECORD(EXPR_CALL);
587 RECORD(EXPR_MEMBER);
588 RECORD(EXPR_BINARY_OPERATOR);
589 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
590 RECORD(EXPR_CONDITIONAL_OPERATOR);
591 RECORD(EXPR_IMPLICIT_CAST);
592 RECORD(EXPR_CSTYLE_CAST);
593 RECORD(EXPR_COMPOUND_LITERAL);
594 RECORD(EXPR_EXT_VECTOR_ELEMENT);
595 RECORD(EXPR_INIT_LIST);
596 RECORD(EXPR_DESIGNATED_INIT);
597 RECORD(EXPR_IMPLICIT_VALUE_INIT);
598 RECORD(EXPR_VA_ARG);
599 RECORD(EXPR_ADDR_LABEL);
600 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000601 RECORD(EXPR_CHOOSE);
602 RECORD(EXPR_GNU_NULL);
603 RECORD(EXPR_SHUFFLE_VECTOR);
604 RECORD(EXPR_BLOCK);
605 RECORD(EXPR_BLOCK_DECL_REF);
606 RECORD(EXPR_OBJC_STRING_LITERAL);
607 RECORD(EXPR_OBJC_ENCODE);
608 RECORD(EXPR_OBJC_SELECTOR_EXPR);
609 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
610 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
611 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
612 RECORD(EXPR_OBJC_KVC_REF_EXPR);
613 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000614 RECORD(STMT_OBJC_FOR_COLLECTION);
615 RECORD(STMT_OBJC_CATCH);
616 RECORD(STMT_OBJC_FINALLY);
617 RECORD(STMT_OBJC_AT_TRY);
618 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
619 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000620 RECORD(EXPR_CXX_OPERATOR_CALL);
621 RECORD(EXPR_CXX_CONSTRUCT);
622 RECORD(EXPR_CXX_STATIC_CAST);
623 RECORD(EXPR_CXX_DYNAMIC_CAST);
624 RECORD(EXPR_CXX_REINTERPRET_CAST);
625 RECORD(EXPR_CXX_CONST_CAST);
626 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
627 RECORD(EXPR_CXX_BOOL_LITERAL);
628 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000629#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000630}
Mike Stump1eb44332009-09-09 15:08:12 +0000631
Sebastian Redla4232eb2010-08-18 23:56:21 +0000632void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000633 RecordData Record;
634 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000635
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000636#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
637#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Sebastian Redl3397c552010-08-18 23:56:27 +0000639 // AST Top-Level Block.
Sebastian Redlf29f0a22010-08-18 23:57:22 +0000640 BLOCK(AST_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000641 RECORD(ORIGINAL_FILE_NAME);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000642 RECORD(TYPE_OFFSET);
643 RECORD(DECL_OFFSET);
644 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000645 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000646 RECORD(IDENTIFIER_OFFSET);
647 RECORD(IDENTIFIER_TABLE);
648 RECORD(EXTERNAL_DEFINITIONS);
649 RECORD(SPECIAL_TYPES);
650 RECORD(STATISTICS);
651 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000652 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000653 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
654 RECORD(SELECTOR_OFFSETS);
655 RECORD(METHOD_POOL);
656 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000657 RECORD(SOURCE_LOCATION_OFFSETS);
658 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000659 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000660 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000661 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000662 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redla93e3b52010-07-08 22:01:51 +0000663 RECORD(CHAINED_METADATA);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000664 RECORD(REFERENCED_SELECTOR_POOL);
Michael J. Spencer20249a12010-10-21 03:16:25 +0000665
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000666 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000667 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000668 RECORD(SM_SLOC_FILE_ENTRY);
669 RECORD(SM_SLOC_BUFFER_ENTRY);
670 RECORD(SM_SLOC_BUFFER_BLOB);
671 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
672 RECORD(SM_LINE_TABLE);
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000674 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000675 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000676 RECORD(PP_MACRO_OBJECT_LIKE);
677 RECORD(PP_MACRO_FUNCTION_LIKE);
678 RECORD(PP_TOKEN);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000679 RECORD(PP_MACRO_INSTANTIATION);
680 RECORD(PP_MACRO_DEFINITION);
Michael J. Spencer20249a12010-10-21 03:16:25 +0000681
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000682 // Decls and Types block.
683 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000684 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000685 RECORD(TYPE_COMPLEX);
686 RECORD(TYPE_POINTER);
687 RECORD(TYPE_BLOCK_POINTER);
688 RECORD(TYPE_LVALUE_REFERENCE);
689 RECORD(TYPE_RVALUE_REFERENCE);
690 RECORD(TYPE_MEMBER_POINTER);
691 RECORD(TYPE_CONSTANT_ARRAY);
692 RECORD(TYPE_INCOMPLETE_ARRAY);
693 RECORD(TYPE_VARIABLE_ARRAY);
694 RECORD(TYPE_VECTOR);
695 RECORD(TYPE_EXT_VECTOR);
696 RECORD(TYPE_FUNCTION_PROTO);
697 RECORD(TYPE_FUNCTION_NO_PROTO);
698 RECORD(TYPE_TYPEDEF);
699 RECORD(TYPE_TYPEOF_EXPR);
700 RECORD(TYPE_TYPEOF);
701 RECORD(TYPE_RECORD);
702 RECORD(TYPE_ENUM);
703 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000704 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000705 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000706 RECORD(DECL_TRANSLATION_UNIT);
707 RECORD(DECL_TYPEDEF);
708 RECORD(DECL_ENUM);
709 RECORD(DECL_RECORD);
710 RECORD(DECL_ENUM_CONSTANT);
711 RECORD(DECL_FUNCTION);
712 RECORD(DECL_OBJC_METHOD);
713 RECORD(DECL_OBJC_INTERFACE);
714 RECORD(DECL_OBJC_PROTOCOL);
715 RECORD(DECL_OBJC_IVAR);
716 RECORD(DECL_OBJC_AT_DEFS_FIELD);
717 RECORD(DECL_OBJC_CLASS);
718 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
719 RECORD(DECL_OBJC_CATEGORY);
720 RECORD(DECL_OBJC_CATEGORY_IMPL);
721 RECORD(DECL_OBJC_IMPLEMENTATION);
722 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
723 RECORD(DECL_OBJC_PROPERTY);
724 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000725 RECORD(DECL_FIELD);
726 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000727 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000728 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000729 RECORD(DECL_FILE_SCOPE_ASM);
730 RECORD(DECL_BLOCK);
731 RECORD(DECL_CONTEXT_LEXICAL);
732 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000733 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattner0558df22009-04-27 00:49:53 +0000734 AddStmtsExprs(Stream, Record);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000735#undef RECORD
736#undef BLOCK
737 Stream.ExitBlock();
738}
739
Douglas Gregore650c8c2009-07-07 00:12:59 +0000740/// \brief Adjusts the given filename to only write out the portion of the
741/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000742///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000743/// \param Filename the file name to adjust.
744///
745/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
746/// the returned filename will be adjusted by this system root.
747///
748/// \returns either the original filename (if it needs no adjustment) or the
749/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000750static const char *
Douglas Gregore650c8c2009-07-07 00:12:59 +0000751adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
752 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000753
Douglas Gregore650c8c2009-07-07 00:12:59 +0000754 if (!isysroot)
755 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Douglas Gregore650c8c2009-07-07 00:12:59 +0000757 // Verify that the filename and the system root have the same prefix.
758 unsigned Pos = 0;
759 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
760 if (Filename[Pos] != isysroot[Pos])
761 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000762
Douglas Gregore650c8c2009-07-07 00:12:59 +0000763 // We hit the end of the filename before we hit the end of the system root.
764 if (!Filename[Pos])
765 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000766
Douglas Gregore650c8c2009-07-07 00:12:59 +0000767 // If the file name has a '/' at the current position, skip over the '/'.
768 // We distinguish sysroot-based includes from absolute includes by the
769 // absence of '/' at the beginning of sysroot-based includes.
770 if (Filename[Pos] == '/')
771 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000772
Douglas Gregore650c8c2009-07-07 00:12:59 +0000773 return Filename + Pos;
774}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000775
Sebastian Redl3397c552010-08-18 23:56:27 +0000776/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Sebastian Redla4232eb2010-08-18 23:56:21 +0000777void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000778 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000779
Douglas Gregore650c8c2009-07-07 00:12:59 +0000780 // Metadata
781 const TargetInfo &Target = Context.Target;
782 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl77f46032010-07-09 21:00:24 +0000783 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000784 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl3397c552010-08-18 23:56:27 +0000785 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
786 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregore650c8c2009-07-07 00:12:59 +0000787 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
788 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
789 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl77f46032010-07-09 21:00:24 +0000790 // Target triple or chained PCH name
791 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregore650c8c2009-07-07 00:12:59 +0000792 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Douglas Gregore650c8c2009-07-07 00:12:59 +0000794 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000795 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
796 Record.push_back(VERSION_MAJOR);
797 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000798 Record.push_back(CLANG_VERSION_MAJOR);
799 Record.push_back(CLANG_VERSION_MINOR);
800 Record.push_back(isysroot != 0);
Sebastian Redl77f46032010-07-09 21:00:24 +0000801 // FIXME: This writes the absolute path for chained headers.
802 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
803 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000804
Douglas Gregorb64c1932009-05-12 01:31:05 +0000805 // Original file name
806 SourceManager &SM = Context.getSourceManager();
807 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
808 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000809 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregorb64c1932009-05-12 01:31:05 +0000810 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
811 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
812
813 llvm::sys::Path MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000814
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000815 MainFilePath.makeAbsolute();
Douglas Gregorb64c1932009-05-12 01:31:05 +0000816
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +0000817 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000818 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000819 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000820 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000821 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000822 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000823 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000824
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000825 // Repository branch/version information.
826 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000827 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000828 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
829 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000830 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000831 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000832 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
833 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +0000834}
835
836/// \brief Write the LangOptions structure.
Sebastian Redla4232eb2010-08-18 23:56:21 +0000837void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000838 RecordData Record;
839 Record.push_back(LangOpts.Trigraphs);
840 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
841 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
842 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
843 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000844 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000845 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
846 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
847 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
848 Record.push_back(LangOpts.C99); // C99 Support
849 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencerdae4ac42010-10-21 05:21:48 +0000850 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
851 // already saved elsewhere.
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000852 Record.push_back(LangOpts.CPlusPlus); // C++ Support
853 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000854 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +0000855
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000856 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
857 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000858 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000859 // modern abi enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000860 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000861 // modern abi enabled.
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +0000862 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump1eb44332009-09-09 15:08:12 +0000863
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000864 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000865 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
866 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000867 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000868 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar73482882010-02-10 18:48:44 +0000869 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000870
871 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
872 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
873 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
874
Chris Lattnerea5ce472009-04-27 07:35:58 +0000875 // Whether static initializers are protected by locks.
876 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +0000877 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000878 Record.push_back(LangOpts.Blocks); // block extension to C
879 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
880 // they are unused.
881 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
882 // (modulo the platform support).
883
Chris Lattnera4d71452010-06-26 21:25:03 +0000884 Record.push_back(LangOpts.getSignedOverflowBehavior());
885 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000886
887 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +0000888 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000889 // defined.
890 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
891 // opposed to __DYNAMIC__).
892 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
893
894 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
895 // used (instead of C99 semantics).
896 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +0000897 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
898 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +0000899 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
900 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +0000901 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000902 Record.push_back(LangOpts.getGCMode());
903 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000904 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000905 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000906 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne08a53262010-12-01 19:14:57 +0000907 Record.push_back(LangOpts.CUDA);
Mike Stump9c276ae2009-12-12 01:27:46 +0000908 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson92f58222009-08-22 22:30:33 +0000909 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +0000910 Record.push_back(LangOpts.SpellChecking);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000911 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000912}
913
Douglas Gregor14f79002009-04-10 03:52:48 +0000914//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000915// stat cache Serialization
916//===----------------------------------------------------------------------===//
917
918namespace {
919// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +0000920class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000921public:
922 typedef const char * key_type;
923 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000924
Chris Lattner74e976b2010-11-23 19:28:12 +0000925 typedef struct stat data_type;
926 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000927
928 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000929 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000930 }
Mike Stump1eb44332009-09-09 15:08:12 +0000931
932 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000933 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
934 data_type_ref Data) {
935 unsigned StrLen = strlen(path);
936 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +0000937 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000938 clang::io::Emit8(Out, DataLen);
939 return std::make_pair(StrLen + 1, DataLen);
940 }
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000942 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
943 Out.write(path, KeyLen);
944 }
Mike Stump1eb44332009-09-09 15:08:12 +0000945
Chris Lattner74e976b2010-11-23 19:28:12 +0000946 void EmitData(llvm::raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000947 data_type_ref Data, unsigned DataLen) {
948 using namespace clang::io;
949 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +0000950
Chris Lattner74e976b2010-11-23 19:28:12 +0000951 Emit32(Out, (uint32_t) Data.st_ino);
952 Emit32(Out, (uint32_t) Data.st_dev);
953 Emit16(Out, (uint16_t) Data.st_mode);
954 Emit64(Out, (uint64_t) Data.st_mtime);
955 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000956
957 assert(Out.tell() - Start == DataLen && "Wrong data length");
958 }
959};
960} // end anonymous namespace
961
Sebastian Redl3397c552010-08-18 23:56:27 +0000962/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +0000963void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000964 // Build the on-disk hash table containing information about every
965 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +0000966 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000967 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000968 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000969 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000970 Stat != StatEnd; ++Stat, ++NumStatEntries) {
971 const char *Filename = Stat->first();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000972 Generator.insert(Filename, Stat->second);
973 }
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000975 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000976 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000977 uint32_t BucketOffset;
978 {
979 llvm::raw_svector_ostream Out(StatCacheData);
980 // Make sure that no bucket is at offset 0
981 clang::io::Emit32(Out, 0);
982 BucketOffset = Generator.Emit(Out);
983 }
984
985 // Create a blob abbreviation
986 using namespace llvm;
987 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000988 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000989 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
990 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
991 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
992 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
993
994 // Write the stat cache
995 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000996 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000997 Record.push_back(BucketOffset);
998 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000999 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001000}
1001
1002//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001003// Source Manager Serialization
1004//===----------------------------------------------------------------------===//
1005
1006/// \brief Create an abbreviation for the SLocEntry that refers to a
1007/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001008static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001009 using namespace llvm;
1010 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001011 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001012 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1013 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1014 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1015 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001016 // FileEntry fields.
1017 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1018 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor12fab312010-03-16 16:35:32 +00001019 // HeaderFileInfo fields.
1020 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
1021 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
1022 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
1023 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregor14f79002009-04-10 03:52:48 +00001024 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001025 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001026}
1027
1028/// \brief Create an abbreviation for the SLocEntry that refers to a
1029/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001030static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001031 using namespace llvm;
1032 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001033 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001034 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1035 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1036 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1037 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1038 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001039 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001040}
1041
1042/// \brief Create an abbreviation for the SLocEntry that refers to a
1043/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001044static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001045 using namespace llvm;
1046 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001047 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001048 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001049 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001050}
1051
1052/// \brief Create an abbreviation for the SLocEntry that refers to an
1053/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001054static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001055 using namespace llvm;
1056 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001057 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001058 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1059 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1060 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1061 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001062 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001063 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001064}
1065
1066/// \brief Writes the block containing the serialized form of the
1067/// source manager.
1068///
1069/// TODO: We should probably use an on-disk hash table (stored in a
1070/// blob), indexed based on the file name, so that we only create
1071/// entries for files that we actually need. In the common case (no
1072/// errors), we probably won't have to create file entries for any of
1073/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001074void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001075 const Preprocessor &PP,
1076 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001077 RecordData Record;
1078
Chris Lattnerf04ad692009-04-10 17:16:57 +00001079 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001080 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001081
1082 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001083 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1084 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1085 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1086 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001087
Douglas Gregorbd945002009-04-13 16:31:14 +00001088 // Write the line table.
1089 if (SourceMgr.hasLineTable()) {
1090 LineTableInfo &LineTable = SourceMgr.getLineTable();
1091
1092 // Emit the file names
1093 Record.push_back(LineTable.getNumFilenames());
1094 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1095 // Emit the file name
1096 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001097 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +00001098 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1099 Record.push_back(FilenameLen);
1100 if (FilenameLen)
1101 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1102 }
Mike Stump1eb44332009-09-09 15:08:12 +00001103
Douglas Gregorbd945002009-04-13 16:31:14 +00001104 // Emit the line entries
1105 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1106 L != LEnd; ++L) {
1107 // Emit the file ID
1108 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +00001109
Douglas Gregorbd945002009-04-13 16:31:14 +00001110 // Emit the line entries
1111 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001112 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +00001113 LEEnd = L->second.end();
1114 LE != LEEnd; ++LE) {
1115 Record.push_back(LE->FileOffset);
1116 Record.push_back(LE->LineNo);
1117 Record.push_back(LE->FilenameID);
1118 Record.push_back((unsigned)LE->FileKind);
1119 Record.push_back(LE->IncludeOffset);
1120 }
Douglas Gregorbd945002009-04-13 16:31:14 +00001121 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001122 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001123 }
1124
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001125 // Write out the source location entry table. We skip the first
1126 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001127 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001128 RecordData PreloadSLocs;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001129 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1130 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1131 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1132 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001133 // Get this source location entry.
1134 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001135
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001136 // Record the offset of this source-location entry.
1137 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1138
1139 // Figure out which record code to use.
1140 unsigned Code;
1141 if (SLoc->isFile()) {
1142 if (SLoc->getFile().getContentCache()->Entry)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001143 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001144 else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001145 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001146 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001147 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001148 Record.clear();
1149 Record.push_back(Code);
1150
1151 Record.push_back(SLoc->getOffset());
1152 if (SLoc->isFile()) {
1153 const SrcMgr::FileInfo &File = SLoc->getFile();
1154 Record.push_back(File.getIncludeLoc().getRawEncoding());
1155 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1156 Record.push_back(File.hasLineDirectives());
1157
1158 const SrcMgr::ContentCache *Content = File.getContentCache();
1159 if (Content->Entry) {
1160 // The source location entry is a file. The blob associated
1161 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001162
Douglas Gregor2d52be52010-03-21 22:49:54 +00001163 // Emit size/modification time for this file.
1164 Record.push_back(Content->Entry->getSize());
1165 Record.push_back(Content->Entry->getModificationTime());
1166
Douglas Gregor12fab312010-03-16 16:35:32 +00001167 // Emit header-search information associated with this file.
1168 HeaderFileInfo HFI;
1169 HeaderSearch &HS = PP.getHeaderSearchInfo();
1170 if (Content->Entry->getUID() < HS.header_file_size())
1171 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1172 Record.push_back(HFI.isImport);
1173 Record.push_back(HFI.DirInfo);
1174 Record.push_back(HFI.NumIncludes);
1175 AddIdentifierRef(HFI.ControllingMacro, Record);
1176
Douglas Gregore650c8c2009-07-07 00:12:59 +00001177 // Turn the file name into an absolute path, if it isn't already.
1178 const char *Filename = Content->Entry->getName();
1179 llvm::sys::Path FilePath(Filename, strlen(Filename));
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001180 FilePath.makeAbsolute();
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001181 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001182
Douglas Gregore650c8c2009-07-07 00:12:59 +00001183 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001184 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001185
1186 // FIXME: For now, preload all file source locations, so that
1187 // we get the appropriate File entries in the reader. This is
1188 // a temporary measure.
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001189 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001190 } else {
1191 // The source location entry is a buffer. The blob associated
1192 // with this entry contains the contents of the buffer.
1193
1194 // We add one to the size so that we capture the trailing NULL
1195 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1196 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001197 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001198 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001199 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001200 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1201 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001202 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001203 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001204 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001205 llvm::StringRef(Buffer->getBufferStart(),
1206 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001207
1208 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001209 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001210 }
1211 } else {
1212 // The source location entry is an instantiation.
1213 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1214 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1215 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1216 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1217
1218 // Compute the token length for this macro expansion.
1219 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001220 if (I + 1 != N)
1221 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001222 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1223 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1224 }
1225 }
1226
Douglas Gregorc9490c02009-04-16 22:23:12 +00001227 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001228
1229 if (SLocEntryOffsets.empty())
1230 return;
1231
Sebastian Redl3397c552010-08-18 23:56:27 +00001232 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001233 // table is used for lazily loading source-location information.
1234 using namespace llvm;
1235 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001236 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001237 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1238 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1239 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1240 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001241
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001242 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001243 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001244 Record.push_back(SLocEntryOffsets.size());
Sebastian Redl8db9fae2010-09-22 20:19:08 +00001245 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1246 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001247 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001248 (const char *)data(SLocEntryOffsets),
Chris Lattner090d9b52009-04-27 19:01:47 +00001249 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001250
Sebastian Redl3397c552010-08-18 23:56:27 +00001251 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001252 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001253 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001254}
1255
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001256//===----------------------------------------------------------------------===//
1257// Preprocessor Serialization
1258//===----------------------------------------------------------------------===//
1259
Chris Lattner0b1fb982009-04-10 17:15:23 +00001260/// \brief Writes the block containing the serialized form of the
1261/// preprocessor.
1262///
Sebastian Redla4232eb2010-08-18 23:56:21 +00001263void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001264 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001265
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001266 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1267 if (PP.getCounterValue() != 0) {
1268 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001269 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001270 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001271 }
1272
1273 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001274 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001275
Sebastian Redl3397c552010-08-18 23:56:27 +00001276 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001277 // FIXME: use diagnostics subsystem for localization etc.
1278 if (PP.SawDateOrTime())
1279 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001280
Douglas Gregorecdcb882010-10-20 22:00:55 +00001281
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001282 // Loop over all the macro definitions that are live at the end of the file,
1283 // emitting each to the PP section.
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001284 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Douglas Gregorecdcb882010-10-20 22:00:55 +00001285 unsigned InclusionAbbrev = 0;
1286 if (PPRec) {
1287 using namespace llvm;
1288 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1289 Abbrev->Add(BitCodeAbbrevOp(PP_INCLUSION_DIRECTIVE));
1290 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1291 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1292 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1293 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1294 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1295 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1296 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Michael J. Spencer20249a12010-10-21 03:16:25 +00001297 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorecdcb882010-10-20 22:00:55 +00001298 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001299
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001300 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1301 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001302 // FIXME: This emits macros in hash table order, we should do it in a stable
1303 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001304 MacroInfo *MI = I->second;
1305
Sebastian Redl3397c552010-08-18 23:56:27 +00001306 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001307 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl3397c552010-08-18 23:56:27 +00001308 // Also skip macros from a AST file if we're chaining.
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001309
1310 // FIXME: There is a (probably minor) optimization we could do here, if
1311 // the macro comes from the original PCH but the identifier comes from a
1312 // chained PCH, by storing the offset into the original PCH rather than
1313 // writing the macro definition a second time.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001314 if (MI->isBuiltinMacro() ||
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001315 (Chain && I->first->isFromAST() && MI->isFromAST()))
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001316 continue;
1317
Chris Lattner7356a312009-04-11 21:15:38 +00001318 AddIdentifierRef(I->first, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001319 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001320 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1321 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001322
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001323 unsigned Code;
1324 if (MI->isObjectLike()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001325 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001326 } else {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001327 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001328
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001329 Record.push_back(MI->isC99Varargs());
1330 Record.push_back(MI->isGNUVarargs());
1331 Record.push_back(MI->getNumArgs());
1332 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1333 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001334 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001335 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001336
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001337 // If we have a detailed preprocessing record, record the macro definition
1338 // ID that corresponds to this macro.
1339 if (PPRec)
1340 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer20249a12010-10-21 03:16:25 +00001341
Douglas Gregorc9490c02009-04-16 22:23:12 +00001342 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001343 Record.clear();
1344
Chris Lattnerdf961c22009-04-10 18:08:30 +00001345 // Emit the tokens array.
1346 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1347 // Note that we know that the preprocessor does not have any annotation
1348 // tokens in it because they are created by the parser, and thus can't be
1349 // in a macro definition.
1350 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001351
Chris Lattnerdf961c22009-04-10 18:08:30 +00001352 Record.push_back(Tok.getLocation().getRawEncoding());
1353 Record.push_back(Tok.getLength());
1354
Chris Lattnerdf961c22009-04-10 18:08:30 +00001355 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1356 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001357 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001358
Chris Lattnerdf961c22009-04-10 18:08:30 +00001359 // FIXME: Should translate token kind to a stable encoding.
1360 Record.push_back(Tok.getKind());
1361 // FIXME: Should translate token flags to a stable encoding.
1362 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001363
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001364 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001365 Record.clear();
1366 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001367 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001368 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001369
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001370 // If the preprocessor has a preprocessing record, emit it.
1371 unsigned NumPreprocessingRecords = 0;
1372 if (PPRec) {
Sebastian Redl196f5572010-09-27 23:20:01 +00001373 unsigned IndexBase = Chain ? PPRec->getNumPreallocatedEntities() : 0;
Sebastian Redlb57a6242010-09-27 22:18:47 +00001374 for (PreprocessingRecord::iterator E = PPRec->begin(Chain),
1375 EEnd = PPRec->end(Chain);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001376 E != EEnd; ++E) {
1377 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001378
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001379 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1380 // Record this macro definition's location.
Sebastian Redlf73c93f2010-09-15 19:54:06 +00001381 MacroID ID = getMacroDefinitionID(MD);
Douglas Gregor89d99802010-11-30 06:16:57 +00001382
Douglas Gregor77424bc2010-10-02 19:29:26 +00001383 // Don't write the macro definition if it is from another AST file.
1384 if (ID < FirstMacroID)
1385 continue;
Douglas Gregor89d99802010-11-30 06:16:57 +00001386
1387 // Notify the serialization listener that we're serializing this entity.
1388 if (SerializationListener)
1389 SerializationListener->SerializedPreprocessedEntity(*E,
1390 Stream.GetCurrentBitNo());
Michael J. Spencer20249a12010-10-21 03:16:25 +00001391
Douglas Gregor77424bc2010-10-02 19:29:26 +00001392 unsigned Position = ID - FirstMacroID;
1393 if (Position != MacroDefinitionOffsets.size()) {
1394 if (Position > MacroDefinitionOffsets.size())
1395 MacroDefinitionOffsets.resize(Position + 1);
Douglas Gregor89d99802010-11-30 06:16:57 +00001396
Michael J. Spencer20249a12010-10-21 03:16:25 +00001397 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001398 } else
1399 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor89d99802010-11-30 06:16:57 +00001400
Sebastian Redlb57a6242010-09-27 22:18:47 +00001401 Record.push_back(IndexBase + NumPreprocessingRecords++);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001402 Record.push_back(ID);
1403 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1404 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1405 AddIdentifierRef(MD->getName(), Record);
1406 AddSourceLocation(MD->getLocation(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001407 Stream.EmitRecord(PP_MACRO_DEFINITION, Record);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001408 continue;
1409 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001410
Douglas Gregor89d99802010-11-30 06:16:57 +00001411 // Notify the serialization listener that we're serializing this entity.
1412 if (SerializationListener)
1413 SerializationListener->SerializedPreprocessedEntity(*E,
1414 Stream.GetCurrentBitNo());
1415
1416 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1417 Record.push_back(IndexBase + NumPreprocessingRecords++);
1418 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1419 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1420 AddIdentifierRef(MI->getName(), Record);
1421 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1422 Stream.EmitRecord(PP_MACRO_INSTANTIATION, Record);
1423 continue;
1424 }
1425
Douglas Gregorecdcb882010-10-20 22:00:55 +00001426 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1427 Record.push_back(PP_INCLUSION_DIRECTIVE);
1428 Record.push_back(IndexBase + NumPreprocessingRecords++);
1429 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1430 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1431 Record.push_back(ID->getFileName().size());
1432 Record.push_back(ID->wasInQuotes());
1433 Record.push_back(static_cast<unsigned>(ID->getKind()));
1434 llvm::SmallString<64> Buffer;
1435 Buffer += ID->getFileName();
1436 Buffer += ID->getFile()->getName();
1437 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1438 continue;
1439 }
Douglas Gregor89d99802010-11-30 06:16:57 +00001440
1441 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001442 }
1443 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001444
Douglas Gregorc9490c02009-04-16 22:23:12 +00001445 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001446
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001447 // Write the offsets table for the preprocessing record.
1448 if (NumPreprocessingRecords > 0) {
1449 // Write the offsets table for identifier IDs.
1450 using namespace llvm;
1451 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001452 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001453 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1454 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1455 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1456 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001457
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001458 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001459 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001460 Record.push_back(NumPreprocessingRecords);
1461 Record.push_back(MacroDefinitionOffsets.size());
1462 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001463 (const char *)data(MacroDefinitionOffsets),
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001464 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1465 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001466}
1467
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001468void ASTWriter::WriteUserDiagnosticMappings(const Diagnostic &Diag) {
1469 RecordData Record;
1470 for (unsigned i = 0; i != diag::DIAG_UPPER_LIMIT; ++i) {
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001471 diag::Mapping Map = Diag.getDiagnosticMappingInfo(i,Diag.GetCurDiagState());
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001472 if (Map & 0x8) { // user mapping.
1473 Record.push_back(i);
1474 Record.push_back(Map & 0x7);
1475 }
1476 }
1477
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00001478 if (!Record.empty())
1479 Stream.EmitRecord(DIAG_USER_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001480}
1481
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001482//===----------------------------------------------------------------------===//
1483// Type Serialization
1484//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001485
Sebastian Redl3397c552010-08-18 23:56:27 +00001486/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001487void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00001488 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001489 if (Idx.getIndex() == 0) // we haven't seen this type before.
1490 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00001491
Douglas Gregor97475832010-10-05 18:37:06 +00001492 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00001493
Douglas Gregor2cf26342009-04-09 22:27:44 +00001494 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001495 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00001496 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001497 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00001498 else if (TypeOffsets.size() < Index) {
1499 TypeOffsets.resize(Index + 1);
1500 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001501 }
1502
1503 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001504
Douglas Gregor2cf26342009-04-09 22:27:44 +00001505 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00001506 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001507
Douglas Gregora4923eb2009-11-16 21:35:15 +00001508 if (T.hasLocalNonFastQualifiers()) {
1509 Qualifiers Qs = T.getLocalQualifiers();
1510 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001511 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001512 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00001513 } else {
1514 switch (T->getTypeClass()) {
1515 // For all of the concrete, non-dependent types, call the
1516 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001517#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001518 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001519#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001520#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001521 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001522 }
1523
1524 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001525 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001526
1527 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001528 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001529}
1530
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001531//===----------------------------------------------------------------------===//
1532// Declaration Serialization
1533//===----------------------------------------------------------------------===//
1534
Douglas Gregor2cf26342009-04-09 22:27:44 +00001535/// \brief Write the block containing all of the declaration IDs
1536/// lexically declared within the given DeclContext.
1537///
1538/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1539/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001540uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001541 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001542 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001543 return 0;
1544
Douglas Gregorc9490c02009-04-16 22:23:12 +00001545 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001546 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001547 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001548 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001549 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1550 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001551 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001552
Douglas Gregor25123082009-04-22 22:34:57 +00001553 ++NumLexicalDeclContexts;
Sebastian Redl681d7232010-07-27 00:17:23 +00001554 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001555 reinterpret_cast<char*>(Decls.data()),
1556 Decls.size() * sizeof(KindDeclIDPair));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001557 return Offset;
1558}
1559
Sebastian Redla4232eb2010-08-18 23:56:21 +00001560void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00001561 using namespace llvm;
1562 RecordData Record;
1563
1564 // Write the type offsets array
1565 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001566 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001567 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1568 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1569 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1570 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001571 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00001572 Record.push_back(TypeOffsets.size());
1573 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001574 (const char *)data(TypeOffsets),
Sebastian Redl1476ed42010-07-16 16:36:56 +00001575 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1576
1577 // Write the declaration offsets array
1578 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001579 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001580 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1581 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1582 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1583 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001584 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00001585 Record.push_back(DeclOffsets.size());
1586 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001587 (const char *)data(DeclOffsets),
Sebastian Redl1476ed42010-07-16 16:36:56 +00001588 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1589}
1590
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001591//===----------------------------------------------------------------------===//
1592// Global Method Pool and Selector Serialization
1593//===----------------------------------------------------------------------===//
1594
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001595namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001596// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00001597class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00001598 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001599
1600public:
1601 typedef Selector key_type;
1602 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001603
Sebastian Redl5d050072010-08-04 17:20:04 +00001604 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001605 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00001606 ObjCMethodList Instance, Factory;
1607 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001608 typedef const data_type& data_type_ref;
1609
Sebastian Redl3397c552010-08-18 23:56:27 +00001610 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001611
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001612 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00001613 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001614 }
Mike Stump1eb44332009-09-09 15:08:12 +00001615
1616 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001617 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1618 data_type_ref Methods) {
1619 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1620 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00001621 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1622 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001623 Method = Method->Next)
1624 if (Method->Method)
1625 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00001626 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001627 Method = Method->Next)
1628 if (Method->Method)
1629 DataLen += 4;
1630 clang::io::Emit16(Out, DataLen);
1631 return std::make_pair(KeyLen, DataLen);
1632 }
Mike Stump1eb44332009-09-09 15:08:12 +00001633
Douglas Gregor83941df2009-04-25 17:48:32 +00001634 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00001635 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00001636 assert((Start >> 32) == 0 && "Selector key offset too large");
1637 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001638 unsigned N = Sel.getNumArgs();
1639 clang::io::Emit16(Out, N);
1640 if (N == 0)
1641 N = 1;
1642 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001643 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001644 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1645 }
Mike Stump1eb44332009-09-09 15:08:12 +00001646
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001647 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00001648 data_type_ref Methods, unsigned DataLen) {
1649 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00001650 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001651 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00001652 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001653 Method = Method->Next)
1654 if (Method->Method)
1655 ++NumInstanceMethods;
1656
1657 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00001658 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001659 Method = Method->Next)
1660 if (Method->Method)
1661 ++NumFactoryMethods;
1662
1663 clang::io::Emit16(Out, NumInstanceMethods);
1664 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00001665 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001666 Method = Method->Next)
1667 if (Method->Method)
1668 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00001669 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001670 Method = Method->Next)
1671 if (Method->Method)
1672 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00001673
1674 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001675 }
1676};
1677} // end anonymous namespace
1678
Sebastian Redl059612d2010-08-03 21:58:15 +00001679/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001680///
1681/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00001682/// in an on-disk hash table indexed by the selector. The hash table also
1683/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001684void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001685 using namespace llvm;
1686
Sebastian Redl059612d2010-08-03 21:58:15 +00001687 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00001688 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00001689 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00001690 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00001691 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001692 {
Sebastian Redl3397c552010-08-18 23:56:27 +00001693 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001694 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00001695
Sebastian Redl059612d2010-08-03 21:58:15 +00001696 // Create the on-disk hash table representation. We walk through every
1697 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00001698 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001699 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00001700 I = SelectorIDs.begin(), E = SelectorIDs.end();
1701 I != E; ++I) {
1702 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00001703 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00001704 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00001705 I->second,
1706 ObjCMethodList(),
1707 ObjCMethodList()
1708 };
1709 if (F != SemaRef.MethodPool.end()) {
1710 Data.Instance = F->second.first;
1711 Data.Factory = F->second.second;
1712 }
Sebastian Redl3397c552010-08-18 23:56:27 +00001713 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00001714 // changed.
1715 if (Chain && I->second < FirstSelectorID) {
1716 // Selector already exists. Did it change?
1717 bool changed = false;
1718 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
1719 M = M->Next) {
1720 if (M->Method->getPCHLevel() == 0)
1721 changed = true;
1722 }
1723 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
1724 M = M->Next) {
1725 if (M->Method->getPCHLevel() == 0)
1726 changed = true;
1727 }
1728 if (!changed)
1729 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00001730 } else if (Data.Instance.Method || Data.Factory.Method) {
1731 // A new method pool entry.
1732 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00001733 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001734 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001735 }
1736
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001737 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001738 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001739 uint32_t BucketOffset;
1740 {
Sebastian Redl3397c552010-08-18 23:56:27 +00001741 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001742 llvm::raw_svector_ostream Out(MethodPool);
1743 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001744 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001745 BucketOffset = Generator.Emit(Out, Trait);
1746 }
1747
1748 // Create a blob abbreviation
1749 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001750 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001751 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00001752 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001753 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1754 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1755
Douglas Gregor83941df2009-04-25 17:48:32 +00001756 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001757 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001758 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001759 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00001760 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001761 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00001762
1763 // Create a blob abbreviation for the selector table offsets.
1764 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001765 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00001766 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor83941df2009-04-25 17:48:32 +00001767 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1768 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1769
1770 // Write the selector offsets table.
1771 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001772 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00001773 Record.push_back(SelectorOffsets.size());
1774 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001775 (const char *)data(SelectorOffsets),
Douglas Gregor83941df2009-04-25 17:48:32 +00001776 SelectorOffsets.size() * 4);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001777 }
1778}
1779
Sebastian Redl3397c552010-08-18 23:56:27 +00001780/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001781void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00001782 using namespace llvm;
1783 if (SemaRef.ReferencedSelectors.empty())
1784 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00001785
Fariborz Jahanian32019832010-07-23 19:11:11 +00001786 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00001787
Sebastian Redl3397c552010-08-18 23:56:27 +00001788 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00001789 // very tricky to fix, and given that @selector shouldn't really appear in
1790 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00001791 for (DenseMap<Selector, SourceLocation>::iterator S =
1792 SemaRef.ReferencedSelectors.begin(),
1793 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
1794 Selector Sel = (*S).first;
1795 SourceLocation Loc = (*S).second;
1796 AddSelectorRef(Sel, Record);
1797 AddSourceLocation(Loc, Record);
1798 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001799 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00001800}
1801
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001802//===----------------------------------------------------------------------===//
1803// Identifier Table Serialization
1804//===----------------------------------------------------------------------===//
1805
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001806namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00001807class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00001808 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00001809 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001810
Douglas Gregora92193e2009-04-28 21:18:29 +00001811 /// \brief Determines whether this is an "interesting" identifier
1812 /// that needs a full IdentifierInfo structure written into the hash
1813 /// table.
1814 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1815 return II->isPoisoned() ||
1816 II->isExtensionToken() ||
1817 II->hasMacroDefinition() ||
1818 II->getObjCOrBuiltinID() ||
1819 II->getFETokenInfo<void>();
1820 }
1821
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001822public:
1823 typedef const IdentifierInfo* key_type;
1824 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001825
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001826 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001827 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001828
Sebastian Redl3397c552010-08-18 23:56:27 +00001829 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00001830 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001831
1832 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001833 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001834 }
Mike Stump1eb44332009-09-09 15:08:12 +00001835
1836 std::pair<unsigned,unsigned>
1837 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001838 IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00001839 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00001840 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1841 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00001842 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00001843 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00001844 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00001845 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00001846 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1847 DEnd = IdentifierResolver::end();
1848 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001849 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00001850 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001851 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00001852 // We emit the key length after the data length so that every
1853 // string is preceded by a 16-bit length. This matches the PTH
1854 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00001855 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001856 return std::make_pair(KeyLen, DataLen);
1857 }
Mike Stump1eb44332009-09-09 15:08:12 +00001858
1859 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001860 unsigned KeyLen) {
1861 // Record the location of the key data. This is used when generating
1862 // the mapping from persistent IDs to strings.
1863 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00001864 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001865 }
Mike Stump1eb44332009-09-09 15:08:12 +00001866
1867 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001868 IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00001869 if (!isInterestingIdentifier(II)) {
1870 clang::io::Emit32(Out, ID << 1);
1871 return;
1872 }
Douglas Gregor5998da52009-04-28 21:32:13 +00001873
Douglas Gregora92193e2009-04-28 21:18:29 +00001874 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001875 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001876 bool hasMacroDefinition =
1877 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00001878 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00001879 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001880 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1881 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1882 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00001883 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001884 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00001885 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001886
Douglas Gregor37e26842009-04-21 23:56:24 +00001887 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00001888 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00001889
Douglas Gregor668c1a42009-04-21 22:25:48 +00001890 // Emit the declaration IDs in reverse order, because the
1891 // IdentifierResolver provides the declarations as they would be
1892 // visible (e.g., the function "stat" would come before the struct
1893 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1894 // adds declarations to the end of the list (so we need to see the
1895 // struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00001896 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump1eb44332009-09-09 15:08:12 +00001897 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00001898 IdentifierResolver::end());
1899 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1900 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001901 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00001902 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001903 }
1904};
1905} // end anonymous namespace
1906
Sebastian Redl3397c552010-08-18 23:56:27 +00001907/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001908///
1909/// The identifier table consists of a blob containing string data
1910/// (the actual identifiers themselves) and a separate "offsets" index
1911/// that maps identifier IDs to locations within the blob.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001912void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001913 using namespace llvm;
1914
1915 // Create and write out the blob that contains the identifier
1916 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001917 {
Sebastian Redl3397c552010-08-18 23:56:27 +00001918 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001919 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump1eb44332009-09-09 15:08:12 +00001920
Douglas Gregor92b059e2009-04-28 20:33:11 +00001921 // Look for any identifiers that were named while processing the
1922 // headers, but are otherwise not needed. We add these to the hash
1923 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00001924 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00001925 // file.
1926 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1927 IDEnd = PP.getIdentifierTable().end();
1928 ID != IDEnd; ++ID)
1929 getIdentifierRef(ID->second);
1930
Sebastian Redlf2f0f032010-07-23 23:49:55 +00001931 // Create the on-disk hash table representation. We only store offsets
1932 // for identifiers that appear here for the first time.
1933 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001934 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00001935 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1936 ID != IDEnd; ++ID) {
1937 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001938 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001939 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001940 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001941
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001942 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001943 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00001944 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001945 {
Sebastian Redl3397c552010-08-18 23:56:27 +00001946 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001947 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001948 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001949 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001950 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001951 }
1952
1953 // Create a blob abbreviation
1954 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001955 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001956 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001957 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00001958 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001959
1960 // Write the identifier table
1961 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001962 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001963 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001964 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001965 }
1966
1967 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001968 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001969 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001970 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1971 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1972 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1973
1974 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001975 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001976 Record.push_back(IdentifierOffsets.size());
1977 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001978 (const char *)data(IdentifierOffsets),
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001979 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregorafaf3082009-04-11 00:14:32 +00001980}
1981
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001982//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00001983// DeclContext's Name Lookup Table Serialization
1984//===----------------------------------------------------------------------===//
1985
1986namespace {
1987// Trait used for the on-disk hash table used in the method pool.
1988class ASTDeclContextNameLookupTrait {
1989 ASTWriter &Writer;
1990
1991public:
1992 typedef DeclarationName key_type;
1993 typedef key_type key_type_ref;
1994
1995 typedef DeclContext::lookup_result data_type;
1996 typedef const data_type& data_type_ref;
1997
1998 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
1999
2000 unsigned ComputeHash(DeclarationName Name) {
2001 llvm::FoldingSetNodeID ID;
2002 ID.AddInteger(Name.getNameKind());
2003
2004 switch (Name.getNameKind()) {
2005 case DeclarationName::Identifier:
2006 ID.AddString(Name.getAsIdentifierInfo()->getName());
2007 break;
2008 case DeclarationName::ObjCZeroArgSelector:
2009 case DeclarationName::ObjCOneArgSelector:
2010 case DeclarationName::ObjCMultiArgSelector:
2011 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2012 break;
2013 case DeclarationName::CXXConstructorName:
2014 case DeclarationName::CXXDestructorName:
2015 case DeclarationName::CXXConversionFunctionName:
2016 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
2017 break;
2018 case DeclarationName::CXXOperatorName:
2019 ID.AddInteger(Name.getCXXOverloadedOperator());
2020 break;
2021 case DeclarationName::CXXLiteralOperatorName:
2022 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2023 case DeclarationName::CXXUsingDirective:
2024 break;
2025 }
2026
2027 return ID.ComputeHash();
2028 }
2029
2030 std::pair<unsigned,unsigned>
2031 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2032 data_type_ref Lookup) {
2033 unsigned KeyLen = 1;
2034 switch (Name.getNameKind()) {
2035 case DeclarationName::Identifier:
2036 case DeclarationName::ObjCZeroArgSelector:
2037 case DeclarationName::ObjCOneArgSelector:
2038 case DeclarationName::ObjCMultiArgSelector:
2039 case DeclarationName::CXXConstructorName:
2040 case DeclarationName::CXXDestructorName:
2041 case DeclarationName::CXXConversionFunctionName:
2042 case DeclarationName::CXXLiteralOperatorName:
2043 KeyLen += 4;
2044 break;
2045 case DeclarationName::CXXOperatorName:
2046 KeyLen += 1;
2047 break;
2048 case DeclarationName::CXXUsingDirective:
2049 break;
2050 }
2051 clang::io::Emit16(Out, KeyLen);
2052
2053 // 2 bytes for num of decls and 4 for each DeclID.
2054 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2055 clang::io::Emit16(Out, DataLen);
2056
2057 return std::make_pair(KeyLen, DataLen);
2058 }
2059
2060 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2061 using namespace clang::io;
2062
2063 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2064 Emit8(Out, Name.getNameKind());
2065 switch (Name.getNameKind()) {
2066 case DeclarationName::Identifier:
2067 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2068 break;
2069 case DeclarationName::ObjCZeroArgSelector:
2070 case DeclarationName::ObjCOneArgSelector:
2071 case DeclarationName::ObjCMultiArgSelector:
2072 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2073 break;
2074 case DeclarationName::CXXConstructorName:
2075 case DeclarationName::CXXDestructorName:
2076 case DeclarationName::CXXConversionFunctionName:
2077 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2078 break;
2079 case DeclarationName::CXXOperatorName:
2080 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2081 Emit8(Out, Name.getCXXOverloadedOperator());
2082 break;
2083 case DeclarationName::CXXLiteralOperatorName:
2084 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2085 break;
2086 case DeclarationName::CXXUsingDirective:
2087 break;
2088 }
2089 }
2090
2091 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2092 data_type Lookup, unsigned DataLen) {
2093 uint64_t Start = Out.tell(); (void)Start;
2094 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2095 for (; Lookup.first != Lookup.second; ++Lookup.first)
2096 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2097
2098 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2099 }
2100};
2101} // end anonymous namespace
2102
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002103/// \brief Write the block containing all of the declaration IDs
2104/// visible from the given DeclContext.
2105///
2106/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002107/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002108uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2109 DeclContext *DC) {
2110 if (DC->getPrimaryContext() != DC)
2111 return 0;
2112
2113 // Since there is no name lookup into functions or methods, don't bother to
2114 // build a visible-declarations table for these entities.
2115 if (DC->isFunctionOrMethod())
2116 return 0;
2117
2118 // If not in C++, we perform name lookup for the translation unit via the
2119 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2120 // FIXME: In C++ we need the visible declarations in order to "see" the
2121 // friend declarations, is there a way to do this without writing the table ?
2122 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2123 return 0;
2124
2125 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00002126 if (DC->hasExternalVisibleStorage())
2127 DC->MaterializeVisibleDeclsFromExternalStorage();
2128 else
2129 DC->lookup(DeclarationName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002130
2131 // Serialize the contents of the mapping used for lookup. Note that,
2132 // although we have two very different code paths, the serialized
2133 // representation is the same for both cases: a declaration name,
2134 // followed by a size, followed by references to the visible
2135 // declarations that have that name.
2136 uint64_t Offset = Stream.GetCurrentBitNo();
2137 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2138 if (!Map || Map->empty())
2139 return 0;
2140
2141 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2142 ASTDeclContextNameLookupTrait Trait(*this);
2143
2144 // Create the on-disk hash table representation.
2145 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2146 D != DEnd; ++D) {
2147 DeclarationName Name = D->first;
2148 DeclContext::lookup_result Result = D->second.getLookupResult();
2149 Generator.insert(Name, Result, Trait);
2150 }
2151
2152 // Create the on-disk hash table in a buffer.
2153 llvm::SmallString<4096> LookupTable;
2154 uint32_t BucketOffset;
2155 {
2156 llvm::raw_svector_ostream Out(LookupTable);
2157 // Make sure that no bucket is at offset 0
2158 clang::io::Emit32(Out, 0);
2159 BucketOffset = Generator.Emit(Out, Trait);
2160 }
2161
2162 // Write the lookup table
2163 RecordData Record;
2164 Record.push_back(DECL_CONTEXT_VISIBLE);
2165 Record.push_back(BucketOffset);
2166 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2167 LookupTable.str());
2168
2169 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2170 ++NumVisibleDeclContexts;
2171 return Offset;
2172}
2173
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002174/// \brief Write an UPDATE_VISIBLE block for the given context.
2175///
2176/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2177/// DeclContext in a dependent AST file. As such, they only exist for the TU
2178/// (in C++) and for namespaces.
2179void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002180 // Make the context build its lookup table, but don't make it load external
2181 // decls.
2182 DC->lookup(DeclarationName());
2183
2184 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2185 if (!Map || Map->empty())
2186 return;
2187
2188 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2189 ASTDeclContextNameLookupTrait Trait(*this);
2190
2191 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002192 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2193 D != DEnd; ++D) {
2194 DeclarationName Name = D->first;
2195 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002196 // For any name that appears in this table, the results are complete, i.e.
2197 // they overwrite results from previous PCHs. Merging is always a mess.
2198 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002199 }
2200
2201 // Create the on-disk hash table in a buffer.
2202 llvm::SmallString<4096> LookupTable;
2203 uint32_t BucketOffset;
2204 {
2205 llvm::raw_svector_ostream Out(LookupTable);
2206 // Make sure that no bucket is at offset 0
2207 clang::io::Emit32(Out, 0);
2208 BucketOffset = Generator.Emit(Out, Trait);
2209 }
2210
2211 // Write the lookup table
2212 RecordData Record;
2213 Record.push_back(UPDATE_VISIBLE);
2214 Record.push_back(getDeclID(cast<Decl>(DC)));
2215 Record.push_back(BucketOffset);
2216 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2217}
2218
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002219//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002220// General Serialization Routines
2221//===----------------------------------------------------------------------===//
2222
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002223/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002224void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00002225 Record.push_back(Attrs.size());
Sean Huntcf807c42010-08-18 23:23:40 +00002226 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2227 const Attr * A = *i;
2228 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2229 AddSourceLocation(A->getLocation(), Record);
2230 Record.push_back(A->isInherited());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002231
Sean Huntcf807c42010-08-18 23:23:40 +00002232#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00002233
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002234 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002235}
2236
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002237void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002238 Record.push_back(Str.size());
2239 Record.insert(Record.end(), Str.begin(), Str.end());
2240}
2241
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002242/// \brief Note that the identifier II occurs at the given offset
2243/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002244void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002245 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00002246 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002247 // up earlier in the chain and thus don't need an offset.
2248 if (ID >= FirstIdentID)
2249 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002250}
2251
Douglas Gregor83941df2009-04-25 17:48:32 +00002252/// \brief Note that the selector Sel occurs at the given offset
2253/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002254void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00002255 unsigned ID = SelectorIDs[Sel];
2256 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00002257 // Don't record offsets for selectors that are also available in a different
2258 // file.
2259 if (ID < FirstSelectorID)
2260 return;
2261 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00002262}
2263
Sebastian Redla4232eb2010-08-18 23:56:21 +00002264ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregor89d99802010-11-30 06:16:57 +00002265 : Stream(Stream), Chain(0), SerializationListener(0),
2266 FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002267 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redle58aa892010-08-04 18:21:41 +00002268 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor77424bc2010-10-02 19:29:26 +00002269 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2270 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00002271 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor7c789c12010-10-29 22:39:52 +00002272 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
2273 NextCXXBaseSpecifiersID(1)
2274{
Sebastian Redl30c514c2010-07-14 23:45:08 +00002275}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002276
Sebastian Redla4232eb2010-08-18 23:56:21 +00002277void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002278 const char *isysroot) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002279 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002280 Stream.Emit((unsigned)'C', 8);
2281 Stream.Emit((unsigned)'P', 8);
2282 Stream.Emit((unsigned)'C', 8);
2283 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002284
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002285 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002286
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002287 if (Chain)
Sebastian Redla4232eb2010-08-18 23:56:21 +00002288 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002289 else
Sebastian Redla4232eb2010-08-18 23:56:21 +00002290 WriteASTCore(SemaRef, StatCalls, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002291}
2292
Sebastian Redla4232eb2010-08-18 23:56:21 +00002293void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002294 const char *isysroot) {
2295 using namespace llvm;
2296
2297 ASTContext &Context = SemaRef.Context;
2298 Preprocessor &PP = SemaRef.PP;
2299
Douglas Gregor2cf26342009-04-09 22:27:44 +00002300 // The translation unit is the first declaration we'll emit.
2301 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002302 ++NextDeclID;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002303 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002304
Douglas Gregor2deaea32009-04-22 18:49:13 +00002305 // Make sure that we emit IdentifierInfos (and any attached
2306 // declarations) for builtins.
2307 {
2308 IdentifierTable &Table = PP.getIdentifierTable();
2309 llvm::SmallVector<const char *, 32> BuiltinNames;
2310 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2311 Context.getLangOptions().NoBuiltin);
2312 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2313 getIdentifierRef(&Table.get(BuiltinNames[I]));
2314 }
2315
Chris Lattner63d65f82009-09-08 18:19:27 +00002316 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002317 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002318 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002319 RecordData TentativeDefinitions;
Sebastian Redle9d12b62010-01-31 22:27:38 +00002320 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2321 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner63d65f82009-09-08 18:19:27 +00002322 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002323
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002324 // Build a record containing all of the file scoped decls in this file.
2325 RecordData UnusedFileScopedDecls;
2326 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2327 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00002328
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002329 RecordData WeakUndeclaredIdentifiers;
2330 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2331 WeakUndeclaredIdentifiers.push_back(
2332 SemaRef.WeakUndeclaredIdentifiers.size());
2333 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2334 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2335 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2336 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2337 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2338 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2339 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2340 }
2341 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002342
Douglas Gregor14c22f22009-04-22 22:18:58 +00002343 // Build a record containing all of the locally-scoped external
2344 // declarations in this header file. Generally, this record will be
2345 // empty.
2346 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002347 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00002348 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002349 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002350 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2351 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2352 TD != TDEnd; ++TD)
2353 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2354
Douglas Gregorb81c1702009-04-27 20:06:05 +00002355 // Build a record containing all of the ext_vector declarations.
2356 RecordData ExtVectorDecls;
2357 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2358 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2359
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002360 // Build a record containing all of the VTable uses information.
2361 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00002362 if (!SemaRef.VTableUses.empty()) {
2363 VTableUses.push_back(SemaRef.VTableUses.size());
2364 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2365 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2366 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2367 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2368 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002369 }
2370
2371 // Build a record containing all of dynamic classes declarations.
2372 RecordData DynamicClasses;
2373 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2374 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2375
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002376 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002377 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002378 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00002379 I = SemaRef.PendingInstantiations.begin(),
2380 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2381 AddDeclRef(I->first, PendingInstantiations);
2382 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002383 }
2384 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2385 "There are local ones at end of translation unit!");
2386
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002387 // Build a record containing some declaration references.
2388 RecordData SemaDeclRefs;
2389 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2390 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2391 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2392 }
2393
Sebastian Redl3397c552010-08-18 23:56:27 +00002394 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002395 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002396 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl30c514c2010-07-14 23:45:08 +00002397 WriteMetadata(Context, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002398 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00002399 if (StatCalls && !isysroot)
Douglas Gregordd41ed52010-07-12 23:48:14 +00002400 WriteStatCache(*StatCalls);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002401 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002402 // Write the record of special types.
2403 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00002404
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002405 AddTypeRef(Context.getBuiltinVaListType(), Record);
2406 AddTypeRef(Context.getObjCIdType(), Record);
2407 AddTypeRef(Context.getObjCSelType(), Record);
2408 AddTypeRef(Context.getObjCProtoType(), Record);
2409 AddTypeRef(Context.getObjCClassType(), Record);
2410 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2411 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2412 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002413 AddTypeRef(Context.getjmp_bufType(), Record);
2414 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002415 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2416 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpadaaad32009-10-20 02:12:22 +00002417 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002418 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002419 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2420 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002421 Record.push_back(Context.isInt128Installed());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002422 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002423
Douglas Gregor366809a2009-04-26 03:49:13 +00002424 // Keep writing types and declarations until all types and
2425 // declarations have been written.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002426 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002427 WriteDeclsBlockAbbrevs();
2428 while (!DeclTypesToEmit.empty()) {
2429 DeclOrType DOT = DeclTypesToEmit.front();
2430 DeclTypesToEmit.pop();
2431 if (DOT.isType())
2432 WriteType(DOT.getType());
2433 else
2434 WriteDecl(Context, DOT.getDecl());
2435 }
2436 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002437
Douglas Gregor813a97b2009-10-17 17:25:45 +00002438 WritePreprocessor(PP);
Sebastian Redl059612d2010-08-03 21:58:15 +00002439 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002440 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002441 WriteIdentifierTable(PP);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002442
Sebastian Redl1476ed42010-07-16 16:36:56 +00002443 WriteTypeDeclOffsets();
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002444 WriteUserDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00002445
Douglas Gregor7c789c12010-10-29 22:39:52 +00002446 // Write the C++ base-specifier set offsets.
2447 if (!CXXBaseSpecifiersOffsets.empty()) {
2448 // Create a blob abbreviation for the C++ base specifiers offsets.
2449 using namespace llvm;
2450
2451 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2452 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2453 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2454 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2455 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2456
2457 // Write the selector offsets table.
2458 Record.clear();
2459 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2460 Record.push_back(CXXBaseSpecifiersOffsets.size());
2461 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
2462 (const char *)CXXBaseSpecifiersOffsets.data(),
2463 CXXBaseSpecifiersOffsets.size() * sizeof(uint32_t));
2464 }
2465
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002466 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002467 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002468 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002469
2470 // Write the record containing tentative definitions.
2471 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002472 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002473
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002474 // Write the record containing unused file scoped decls.
2475 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002476 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002477
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002478 // Write the record containing weak undeclared identifiers.
2479 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002480 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002481 WeakUndeclaredIdentifiers);
2482
Douglas Gregor14c22f22009-04-22 22:18:58 +00002483 // Write the record containing locally-scoped external definitions.
2484 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002485 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002486 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002487
2488 // Write the record containing ext_vector type names.
2489 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002490 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002491
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002492 // Write the record containing VTable uses information.
2493 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002494 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002495
2496 // Write the record containing dynamic classes declarations.
2497 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002498 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002499
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002500 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002501 if (!PendingInstantiations.empty())
2502 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002503
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002504 // Write the record containing declaration references of Sema.
2505 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002506 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002507
Douglas Gregor3e1af842009-04-17 22:13:46 +00002508 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002509 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002510 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002511 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002512 Record.push_back(NumLexicalDeclContexts);
2513 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002514 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002515 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002516}
2517
Sebastian Redla4232eb2010-08-18 23:56:21 +00002518void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002519 const char *isysroot) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002520 using namespace llvm;
2521
2522 ASTContext &Context = SemaRef.Context;
2523 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1476ed42010-07-16 16:36:56 +00002524
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002525 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002526 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl30c514c2010-07-14 23:45:08 +00002527 WriteMetadata(Context, isysroot);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002528 if (StatCalls && !isysroot)
2529 WriteStatCache(*StatCalls);
2530 // FIXME: Source manager block should only write new stuff, which could be
2531 // done by tracking the largest ID in the chain
2532 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002533
2534 // The special types are in the chained PCH.
2535
2536 // We don't start with the translation unit, but with its decls that
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002537 // don't come from the chained PCH.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002538 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002539 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl681d7232010-07-27 00:17:23 +00002540 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2541 E = TU->noload_decls_end();
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002542 I != E; ++I) {
Sebastian Redld692af72010-07-27 18:24:41 +00002543 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002544 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redl0b17c612010-08-13 00:28:03 +00002545 else if ((*I)->isChangedSinceDeserialization())
2546 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002547 }
Sebastian Redl681d7232010-07-27 00:17:23 +00002548 // We also need to write a lexical updates block for the TU.
Sebastian Redld692af72010-07-27 18:24:41 +00002549 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002550 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redld692af72010-07-27 18:24:41 +00002551 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2552 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2553 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002554 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redld692af72010-07-27 18:24:41 +00002555 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2556 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002557 NewGlobalDecls.size() * sizeof(KindDeclIDPair));
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00002558 // And a visible updates block for the DeclContexts.
2559 Abv = new llvm::BitCodeAbbrev();
2560 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2561 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2562 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2563 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2564 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2565 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002566
Sebastian Redl083abdf2010-07-27 23:01:28 +00002567 // Build a record containing all of the new tentative definitions in this
2568 // file, in TentativeDefinitions order.
2569 RecordData TentativeDefinitions;
2570 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2571 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2572 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2573 }
2574
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002575 // Build a record containing all of the file scoped decls in this file.
2576 RecordData UnusedFileScopedDecls;
2577 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2578 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2579 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002580 }
2581
Sebastian Redl40566802010-08-05 18:21:25 +00002582 // We write the entire table, overwriting the tables from the chain.
2583 RecordData WeakUndeclaredIdentifiers;
2584 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2585 WeakUndeclaredIdentifiers.push_back(
2586 SemaRef.WeakUndeclaredIdentifiers.size());
2587 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2588 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2589 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2590 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2591 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2592 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2593 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2594 }
2595 }
2596
Sebastian Redl083abdf2010-07-27 23:01:28 +00002597 // Build a record containing all of the locally-scoped external
2598 // declarations in this header file. Generally, this record will be
2599 // empty.
2600 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002601 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl083abdf2010-07-27 23:01:28 +00002602 // nondeterminstic!
2603 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2604 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2605 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2606 TD != TDEnd; ++TD) {
2607 if (TD->second->getPCHLevel() == 0)
2608 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2609 }
2610
2611 // Build a record containing all of the ext_vector declarations.
2612 RecordData ExtVectorDecls;
2613 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2614 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2615 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2616 }
2617
Sebastian Redl40566802010-08-05 18:21:25 +00002618 // Build a record containing all of the VTable uses information.
2619 // We write everything here, because it's too hard to determine whether
2620 // a use is new to this part.
2621 RecordData VTableUses;
2622 if (!SemaRef.VTableUses.empty()) {
2623 VTableUses.push_back(SemaRef.VTableUses.size());
2624 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2625 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2626 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2627 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2628 }
2629 }
2630
2631 // Build a record containing all of dynamic classes declarations.
2632 RecordData DynamicClasses;
2633 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2634 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
2635 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2636
2637 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002638 RecordData PendingInstantiations;
Sebastian Redl40566802010-08-05 18:21:25 +00002639 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00002640 I = SemaRef.PendingInstantiations.begin(),
2641 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl40566802010-08-05 18:21:25 +00002642 if (I->first->getPCHLevel() == 0) {
Chandler Carruth62c78d52010-08-25 08:44:16 +00002643 AddDeclRef(I->first, PendingInstantiations);
2644 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl40566802010-08-05 18:21:25 +00002645 }
2646 }
2647 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2648 "There are local ones at end of translation unit!");
2649
2650 // Build a record containing some declaration references.
2651 // It's not worth the effort to avoid duplication here.
2652 RecordData SemaDeclRefs;
2653 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2654 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2655 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2656 }
2657
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002658 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002659 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00002660 for (DeclsToRewriteTy::iterator
2661 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
2662 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002663 while (!DeclTypesToEmit.empty()) {
2664 DeclOrType DOT = DeclTypesToEmit.front();
2665 DeclTypesToEmit.pop();
2666 if (DOT.isType())
2667 WriteType(DOT.getType());
2668 else
2669 WriteDecl(Context, DOT.getDecl());
2670 }
2671 Stream.ExitBlock();
2672
Sebastian Redl083abdf2010-07-27 23:01:28 +00002673 WritePreprocessor(PP);
Sebastian Redla68340f2010-08-04 22:21:29 +00002674 WriteSelectors(SemaRef);
2675 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002676 WriteIdentifierTable(PP);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002677 WriteTypeDeclOffsets();
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00002678 // FIXME: For chained PCH only write the new mappings (we currently
2679 // write all of them again).
2680 WriteUserDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl083abdf2010-07-27 23:01:28 +00002681
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002682 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl3397c552010-08-18 23:56:27 +00002683 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002684 RecordData FirstLatestDeclIDs;
2685 for (FirstLatestDeclMap::iterator
2686 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
2687 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
2688 "Expected first & second to be in different PCHs");
2689 AddDeclRef(I->first, FirstLatestDeclIDs);
2690 AddDeclRef(I->second, FirstLatestDeclIDs);
2691 }
2692 if (!FirstLatestDeclIDs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002693 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002694
Sebastian Redl083abdf2010-07-27 23:01:28 +00002695 // Write the record containing external, unnamed definitions.
2696 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002697 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002698
2699 // Write the record containing tentative definitions.
2700 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002701 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002702
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002703 // Write the record containing unused file scoped decls.
2704 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002705 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002706
Sebastian Redl40566802010-08-05 18:21:25 +00002707 // Write the record containing weak undeclared identifiers.
2708 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002709 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl40566802010-08-05 18:21:25 +00002710 WeakUndeclaredIdentifiers);
2711
Sebastian Redl083abdf2010-07-27 23:01:28 +00002712 // Write the record containing locally-scoped external definitions.
2713 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002714 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl083abdf2010-07-27 23:01:28 +00002715 LocallyScopedExternalDecls);
2716
2717 // Write the record containing ext_vector type names.
2718 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002719 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002720
Sebastian Redl40566802010-08-05 18:21:25 +00002721 // Write the record containing VTable uses information.
2722 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002723 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl40566802010-08-05 18:21:25 +00002724
2725 // Write the record containing dynamic classes declarations.
2726 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002727 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl40566802010-08-05 18:21:25 +00002728
2729 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002730 if (!PendingInstantiations.empty())
2731 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl40566802010-08-05 18:21:25 +00002732
2733 // Write the record containing declaration references of Sema.
2734 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002735 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002736
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00002737 // Write the updates to DeclContexts.
2738 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
2739 I = UpdatedDeclContexts.begin(),
2740 E = UpdatedDeclContexts.end();
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002741 I != E; ++I)
2742 WriteDeclContextVisibleUpdate(*I);
2743
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00002744 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002745
Sebastian Redl083abdf2010-07-27 23:01:28 +00002746 Record.clear();
2747 Record.push_back(NumStatements);
2748 Record.push_back(NumMacros);
2749 Record.push_back(NumLexicalDeclContexts);
2750 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00002751 WriteDeclReplacementsBlock();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002752 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002753 Stream.ExitBlock();
2754}
2755
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00002756void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002757 if (DeclUpdates.empty())
2758 return;
2759
2760 RecordData OffsetsRecord;
2761 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
2762 for (DeclUpdateMap::iterator
2763 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
2764 const Decl *D = I->first;
2765 UpdateRecord &URec = I->second;
2766
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00002767 if (DeclsToRewrite.count(D))
2768 continue; // The decl will be written completely,no need to store updates.
2769
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002770 uint64_t Offset = Stream.GetCurrentBitNo();
2771 Stream.EmitRecord(DECL_UPDATES, URec);
2772
2773 OffsetsRecord.push_back(GetDeclRef(D));
2774 OffsetsRecord.push_back(Offset);
2775 }
2776 Stream.ExitBlock();
2777 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
2778}
2779
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00002780void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00002781 if (ReplacedDecls.empty())
2782 return;
2783
2784 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002785 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00002786 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
2787 Record.push_back(I->first);
2788 Record.push_back(I->second);
2789 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002790 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00002791}
2792
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002793void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002794 Record.push_back(Loc.getRawEncoding());
2795}
2796
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002797void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002798 AddSourceLocation(Range.getBegin(), Record);
2799 AddSourceLocation(Range.getEnd(), Record);
2800}
2801
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002802void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002803 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00002804 const uint64_t *Words = Value.getRawData();
2805 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002806}
2807
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002808void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002809 Record.push_back(Value.isUnsigned());
2810 AddAPInt(Value, Record);
2811}
2812
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002813void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00002814 AddAPInt(Value.bitcastToAPInt(), Record);
2815}
2816
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002817void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002818 Record.push_back(getIdentifierRef(II));
2819}
2820
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002821IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002822 if (II == 0)
2823 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002824
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002825 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00002826 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002827 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00002828 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002829}
2830
Sebastian Redlf73c93f2010-09-15 19:54:06 +00002831MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002832 if (MD == 0)
2833 return 0;
Sebastian Redlf73c93f2010-09-15 19:54:06 +00002834
2835 MacroID &ID = MacroDefinitions[MD];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002836 if (ID == 0)
Douglas Gregor77424bc2010-10-02 19:29:26 +00002837 ID = NextMacroID++;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002838 return ID;
2839}
2840
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002841void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00002842 Record.push_back(getSelectorRef(SelRef));
2843}
2844
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002845SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00002846 if (Sel.getAsOpaquePtr() == 0) {
2847 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002848 }
2849
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002850 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00002851 if (SID == 0 && Chain) {
2852 // This might trigger a ReadSelector callback, which will set the ID for
2853 // this selector.
2854 Chain->LoadSelector(Sel);
2855 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002856 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00002857 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002858 }
Sebastian Redl5d050072010-08-04 17:20:04 +00002859 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002860}
2861
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002862void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00002863 AddDeclRef(Temp->getDestructor(), Record);
2864}
2865
Douglas Gregor7c789c12010-10-29 22:39:52 +00002866void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
2867 CXXBaseSpecifier const *BasesEnd,
2868 RecordDataImpl &Record) {
2869 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
2870 CXXBaseSpecifiersToWrite.push_back(
2871 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
2872 Bases, BasesEnd));
2873 Record.push_back(NextCXXBaseSpecifiersID++);
2874}
2875
Sebastian Redla4232eb2010-08-18 23:56:21 +00002876void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002877 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002878 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002879 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00002880 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002881 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00002882 break;
2883 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002884 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00002885 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00002886 case TemplateArgument::Template:
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002887 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2888 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00002889 break;
John McCall833ca992009-10-29 08:12:44 +00002890 case TemplateArgument::Null:
2891 case TemplateArgument::Integral:
2892 case TemplateArgument::Declaration:
2893 case TemplateArgument::Pack:
2894 break;
2895 }
2896}
2897
Sebastian Redla4232eb2010-08-18 23:56:21 +00002898void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002899 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002900 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002901
2902 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
2903 bool InfoHasSameExpr
2904 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
2905 Record.push_back(InfoHasSameExpr);
2906 if (InfoHasSameExpr)
2907 return; // Avoid storing the same expr twice.
2908 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002909 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
2910 Record);
2911}
2912
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002913void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00002914 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00002915 AddTypeRef(QualType(), Record);
2916 return;
2917 }
2918
John McCalla93c9342009-12-07 02:54:59 +00002919 AddTypeRef(TInfo->getType(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +00002920 TypeLocWriter TLW(*this, Record);
John McCalla93c9342009-12-07 02:54:59 +00002921 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002922 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00002923}
2924
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002925void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00002926 Record.push_back(GetOrCreateTypeID(T));
2927}
2928
2929TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00002930 return MakeTypeID(T,
2931 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
2932}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002933
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002934TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00002935 return MakeTypeID(T,
2936 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00002937}
2938
2939TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
2940 if (T.isNull())
2941 return TypeIdx();
2942 assert(!T.getLocalFastQualifiers());
2943
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00002944 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002945 if (Idx.getIndex() == 0) {
Douglas Gregor366809a2009-04-26 03:49:13 +00002946 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00002947 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00002948 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002949 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00002950 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00002951 return Idx;
2952}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002953
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002954TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00002955 if (T.isNull())
2956 return TypeIdx();
2957 assert(!T.getLocalFastQualifiers());
2958
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002959 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
2960 assert(I != TypeIdxs.end() && "Type not emitted!");
2961 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002962}
2963
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00002964void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00002965 Record.push_back(GetDeclRef(D));
2966}
2967
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002968DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002969 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00002970 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002971 }
Douglas Gregor97475832010-10-05 18:37:06 +00002972 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002973 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00002974 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002975 // We haven't seen this declaration before. Give it a new ID and
2976 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002977 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002978 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redl0b17c612010-08-13 00:28:03 +00002979 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
2980 // We don't add it to the replacement collection here, because we don't
2981 // have the offset yet.
2982 DeclTypesToEmit.push(const_cast<Decl *>(D));
2983 // Reset the flag, so that we don't add this decl multiple times.
2984 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002985 }
2986
Sebastian Redl681d7232010-07-27 00:17:23 +00002987 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002988}
2989
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002990DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002991 if (D == 0)
2992 return 0;
2993
2994 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2995 return DeclIDs[D];
2996}
2997
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002998void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00002999 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00003000 Record.push_back(Name.getNameKind());
3001 switch (Name.getNameKind()) {
3002 case DeclarationName::Identifier:
3003 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3004 break;
3005
3006 case DeclarationName::ObjCZeroArgSelector:
3007 case DeclarationName::ObjCOneArgSelector:
3008 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003009 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003010 break;
3011
3012 case DeclarationName::CXXConstructorName:
3013 case DeclarationName::CXXDestructorName:
3014 case DeclarationName::CXXConversionFunctionName:
3015 AddTypeRef(Name.getCXXNameType(), Record);
3016 break;
3017
3018 case DeclarationName::CXXOperatorName:
3019 Record.push_back(Name.getCXXOverloadedOperator());
3020 break;
3021
Sean Hunt3e518bd2009-11-29 07:34:05 +00003022 case DeclarationName::CXXLiteralOperatorName:
3023 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3024 break;
3025
Douglas Gregor2cf26342009-04-09 22:27:44 +00003026 case DeclarationName::CXXUsingDirective:
3027 // No extra data to emit
3028 break;
3029 }
3030}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003031
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003032void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003033 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003034 switch (Name.getNameKind()) {
3035 case DeclarationName::CXXConstructorName:
3036 case DeclarationName::CXXDestructorName:
3037 case DeclarationName::CXXConversionFunctionName:
3038 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3039 break;
3040
3041 case DeclarationName::CXXOperatorName:
3042 AddSourceLocation(
3043 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3044 Record);
3045 AddSourceLocation(
3046 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3047 Record);
3048 break;
3049
3050 case DeclarationName::CXXLiteralOperatorName:
3051 AddSourceLocation(
3052 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3053 Record);
3054 break;
3055
3056 case DeclarationName::Identifier:
3057 case DeclarationName::ObjCZeroArgSelector:
3058 case DeclarationName::ObjCOneArgSelector:
3059 case DeclarationName::ObjCMultiArgSelector:
3060 case DeclarationName::CXXUsingDirective:
3061 break;
3062 }
3063}
3064
3065void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003066 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003067 AddDeclarationName(NameInfo.getName(), Record);
3068 AddSourceLocation(NameInfo.getLoc(), Record);
3069 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3070}
3071
3072void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003073 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003074 AddNestedNameSpecifier(Info.NNS, Record);
3075 AddSourceRange(Info.NNSRange, Record);
3076 Record.push_back(Info.NumTemplParamLists);
3077 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3078 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3079}
3080
Sebastian Redla4232eb2010-08-18 23:56:21 +00003081void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003082 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003083 // Nested name specifiers usually aren't too long. I think that 8 would
3084 // typically accomodate the vast majority.
3085 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3086
3087 // Push each of the NNS's onto a stack for serialization in reverse order.
3088 while (NNS) {
3089 NestedNames.push_back(NNS);
3090 NNS = NNS->getPrefix();
3091 }
3092
3093 Record.push_back(NestedNames.size());
3094 while(!NestedNames.empty()) {
3095 NNS = NestedNames.pop_back_val();
3096 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3097 Record.push_back(Kind);
3098 switch (Kind) {
3099 case NestedNameSpecifier::Identifier:
3100 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3101 break;
3102
3103 case NestedNameSpecifier::Namespace:
3104 AddDeclRef(NNS->getAsNamespace(), Record);
3105 break;
3106
3107 case NestedNameSpecifier::TypeSpec:
3108 case NestedNameSpecifier::TypeSpecWithTemplate:
3109 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3110 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3111 break;
3112
3113 case NestedNameSpecifier::Global:
3114 // Don't need to write an associated value.
3115 break;
3116 }
3117 }
3118}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003119
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003120void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00003121 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003122 Record.push_back(Kind);
3123 switch (Kind) {
3124 case TemplateName::Template:
3125 AddDeclRef(Name.getAsTemplateDecl(), Record);
3126 break;
3127
3128 case TemplateName::OverloadedTemplate: {
3129 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3130 Record.push_back(OvT->size());
3131 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3132 I != E; ++I)
3133 AddDeclRef(*I, Record);
3134 break;
3135 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003136
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003137 case TemplateName::QualifiedTemplate: {
3138 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3139 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3140 Record.push_back(QualT->hasTemplateKeyword());
3141 AddDeclRef(QualT->getTemplateDecl(), Record);
3142 break;
3143 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003144
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003145 case TemplateName::DependentTemplate: {
3146 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3147 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3148 Record.push_back(DepT->isIdentifier());
3149 if (DepT->isIdentifier())
3150 AddIdentifierRef(DepT->getIdentifier(), Record);
3151 else
3152 Record.push_back(DepT->getOperator());
3153 break;
3154 }
3155 }
3156}
3157
Michael J. Spencer20249a12010-10-21 03:16:25 +00003158void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003159 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003160 Record.push_back(Arg.getKind());
3161 switch (Arg.getKind()) {
3162 case TemplateArgument::Null:
3163 break;
3164 case TemplateArgument::Type:
3165 AddTypeRef(Arg.getAsType(), Record);
3166 break;
3167 case TemplateArgument::Declaration:
3168 AddDeclRef(Arg.getAsDecl(), Record);
3169 break;
3170 case TemplateArgument::Integral:
3171 AddAPSInt(*Arg.getAsIntegral(), Record);
3172 AddTypeRef(Arg.getIntegralType(), Record);
3173 break;
3174 case TemplateArgument::Template:
3175 AddTemplateName(Arg.getAsTemplate(), Record);
3176 break;
3177 case TemplateArgument::Expression:
3178 AddStmt(Arg.getAsExpr());
3179 break;
3180 case TemplateArgument::Pack:
3181 Record.push_back(Arg.pack_size());
3182 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3183 I != E; ++I)
3184 AddTemplateArgument(*I, Record);
3185 break;
3186 }
3187}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003188
3189void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003190ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003191 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003192 assert(TemplateParams && "No TemplateParams!");
3193 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3194 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3195 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3196 Record.push_back(TemplateParams->size());
3197 for (TemplateParameterList::const_iterator
3198 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3199 P != PEnd; ++P)
3200 AddDeclRef(*P, Record);
3201}
3202
3203/// \brief Emit a template argument list.
3204void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003205ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003206 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003207 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00003208 Record.push_back(TemplateArgs->size());
3209 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003210 AddTemplateArgument(TemplateArgs->get(i), Record);
3211}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003212
3213
3214void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003215ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003216 Record.push_back(Set.size());
3217 for (UnresolvedSetImpl::const_iterator
3218 I = Set.begin(), E = Set.end(); I != E; ++I) {
3219 AddDeclRef(I.getDecl(), Record);
3220 Record.push_back(I.getAccess());
3221 }
3222}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003223
Sebastian Redla4232eb2010-08-18 23:56:21 +00003224void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003225 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003226 Record.push_back(Base.isVirtual());
3227 Record.push_back(Base.isBaseOfClass());
3228 Record.push_back(Base.getAccessSpecifierAsWritten());
Nick Lewycky56062202010-07-26 16:56:01 +00003229 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003230 AddSourceRange(Base.getSourceRange(), Record);
3231}
Sebastian Redl30c514c2010-07-14 23:45:08 +00003232
Douglas Gregor7c789c12010-10-29 22:39:52 +00003233void ASTWriter::FlushCXXBaseSpecifiers() {
3234 RecordData Record;
3235 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3236 Record.clear();
3237
3238 // Record the offset of this base-specifier set.
3239 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3240 if (Index == CXXBaseSpecifiersOffsets.size())
3241 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3242 else {
3243 if (Index > CXXBaseSpecifiersOffsets.size())
3244 CXXBaseSpecifiersOffsets.resize(Index + 1);
3245 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3246 }
3247
3248 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3249 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3250 Record.push_back(BEnd - B);
3251 for (; B != BEnd; ++B)
3252 AddCXXBaseSpecifier(*B, Record);
3253 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00003254
3255 // Flush any expressions that were written as part of the base specifiers.
3256 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003257 }
3258
3259 CXXBaseSpecifiersToWrite.clear();
3260}
3261
Sebastian Redla4232eb2010-08-18 23:56:21 +00003262void ASTWriter::AddCXXBaseOrMemberInitializers(
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003263 const CXXBaseOrMemberInitializer * const *BaseOrMembers,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003264 unsigned NumBaseOrMembers, RecordDataImpl &Record) {
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003265 Record.push_back(NumBaseOrMembers);
3266 for (unsigned i=0; i != NumBaseOrMembers; ++i) {
3267 const CXXBaseOrMemberInitializer *Init = BaseOrMembers[i];
3268
3269 Record.push_back(Init->isBaseInitializer());
3270 if (Init->isBaseInitializer()) {
3271 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3272 Record.push_back(Init->isBaseVirtual());
3273 } else {
Francois Pichet00eb3f92010-12-04 09:14:42 +00003274 Record.push_back(Init->isIndirectMemberInitializer());
3275 if (Init->isIndirectMemberInitializer())
3276 AddDeclRef(Init->getIndirectMember(), Record);
3277 else
3278 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003279 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00003280
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003281 AddSourceLocation(Init->getMemberLocation(), Record);
3282 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003283 AddSourceLocation(Init->getLParenLoc(), Record);
3284 AddSourceLocation(Init->getRParenLoc(), Record);
3285 Record.push_back(Init->isWritten());
3286 if (Init->isWritten()) {
3287 Record.push_back(Init->getSourceOrder());
3288 } else {
3289 Record.push_back(Init->getNumArrayIndices());
3290 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3291 AddDeclRef(Init->getArrayIndex(i), Record);
3292 }
3293 }
3294}
3295
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003296void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3297 assert(D->DefinitionData);
3298 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3299 Record.push_back(Data.UserDeclaredConstructor);
3300 Record.push_back(Data.UserDeclaredCopyConstructor);
3301 Record.push_back(Data.UserDeclaredCopyAssignment);
3302 Record.push_back(Data.UserDeclaredDestructor);
3303 Record.push_back(Data.Aggregate);
3304 Record.push_back(Data.PlainOldData);
3305 Record.push_back(Data.Empty);
3306 Record.push_back(Data.Polymorphic);
3307 Record.push_back(Data.Abstract);
3308 Record.push_back(Data.HasTrivialConstructor);
3309 Record.push_back(Data.HasTrivialCopyConstructor);
3310 Record.push_back(Data.HasTrivialCopyAssignment);
3311 Record.push_back(Data.HasTrivialDestructor);
3312 Record.push_back(Data.ComputedVisibleConversions);
3313 Record.push_back(Data.DeclaredDefaultConstructor);
3314 Record.push_back(Data.DeclaredCopyConstructor);
3315 Record.push_back(Data.DeclaredCopyAssignment);
3316 Record.push_back(Data.DeclaredDestructor);
3317
3318 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003319 if (Data.NumBases > 0)
3320 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3321 Record);
3322
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003323 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3324 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003325 if (Data.NumVBases > 0)
3326 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3327 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003328
3329 AddUnresolvedSet(Data.Conversions, Record);
3330 AddUnresolvedSet(Data.VisibleConversions, Record);
3331 // Data.Definition is the owning decl, no need to write it.
3332 AddDeclRef(Data.FirstFriend, Record);
3333}
3334
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003335void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003336 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003337 assert(!Chain && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003338 assert(FirstDeclID == NextDeclID &&
3339 FirstTypeID == NextTypeID &&
3340 FirstIdentID == NextIdentID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00003341 FirstSelectorID == NextSelectorID &&
Douglas Gregor77424bc2010-10-02 19:29:26 +00003342 FirstMacroID == NextMacroID &&
Douglas Gregor7c789c12010-10-29 22:39:52 +00003343 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003344 "Setting chain after writing has started.");
3345 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003346
3347 FirstDeclID += Chain->getTotalNumDecls();
3348 FirstTypeID += Chain->getTotalNumTypes();
3349 FirstIdentID += Chain->getTotalNumIdentifiers();
3350 FirstSelectorID += Chain->getTotalNumSelectors();
3351 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003352 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003353 NextDeclID = FirstDeclID;
3354 NextTypeID = FirstTypeID;
3355 NextIdentID = FirstIdentID;
3356 NextSelectorID = FirstSelectorID;
3357 NextMacroID = FirstMacroID;
Douglas Gregor7c789c12010-10-29 22:39:52 +00003358 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003359}
3360
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003361void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003362 IdentifierIDs[II] = ID;
3363}
3364
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003365void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00003366 // Always take the highest-numbered type index. This copes with an interesting
3367 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00003368 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00003369 // keep the higher-numbered entry so that we can properly write it out to
3370 // the AST file.
3371 TypeIdx &StoredIdx = TypeIdxs[T];
3372 if (Idx.getIndex() >= StoredIdx.getIndex())
3373 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003374}
3375
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003376void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1476ed42010-07-16 16:36:56 +00003377 DeclIDs[D] = ID;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003378}
Sebastian Redl5d050072010-08-04 17:20:04 +00003379
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003380void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003381 SelectorIDs[S] = ID;
3382}
Douglas Gregor77424bc2010-10-02 19:29:26 +00003383
Michael J. Spencer20249a12010-10-21 03:16:25 +00003384void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00003385 MacroDefinition *MD) {
3386 MacroDefinitions[MD] = ID;
3387}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003388
3389void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3390 assert(D->isDefinition());
3391 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3392 // We are interested when a PCH decl is modified.
3393 if (RD->getPCHLevel() > 0) {
3394 // A forward reference was mutated into a definition. Rewrite it.
3395 // FIXME: This happens during template instantiation, should we
3396 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00003397 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003398 }
3399
3400 for (CXXRecordDecl::redecl_iterator
3401 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3402 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3403 if (Redecl == RD)
3404 continue;
3405
3406 // We are interested when a PCH decl is modified.
3407 if (Redecl->getPCHLevel() > 0) {
3408 UpdateRecord &Record = DeclUpdates[Redecl];
3409 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3410 assert(Redecl->DefinitionData);
3411 assert(Redecl->DefinitionData->Definition == D);
3412 AddDeclRef(D, Record); // the DefinitionDecl
3413 }
3414 }
3415 }
3416}
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00003417void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3418 // TU and namespaces are handled elsewhere.
3419 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3420 return;
3421
3422 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3423 return; // Not a source decl added to a DeclContext from PCH.
3424
3425 AddUpdatedDeclContext(DC);
3426}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00003427
3428void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3429 assert(D->isImplicit());
3430 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3431 return; // Not a source member added to a class from PCH.
3432 if (!isa<CXXMethodDecl>(D))
3433 return; // We are interested in lazily declared implicit methods.
3434
3435 // A decl coming from PCH was modified.
3436 assert(RD->isDefinition());
3437 UpdateRecord &Record = DeclUpdates[RD];
3438 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3439 AddDeclRef(D, Record);
3440}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00003441
3442void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3443 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00003444 // The specializations set is kept in the canonical template.
3445 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00003446 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3447 return; // Not a source specialization added to a template from PCH.
3448
3449 UpdateRecord &Record = DeclUpdates[TD];
3450 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3451 AddDeclRef(D, Record);
3452}
Douglas Gregor89d99802010-11-30 06:16:57 +00003453
3454ASTSerializationListener::~ASTSerializationListener() { }