blob: eb312522c7ade559224604e9642fce74cdc32ba1 [file] [log] [blame]
Sebastian Redld6522cf2010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregoref84c4b2009-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 Redl55c0ad52010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregoref84c4b2009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl1914c6f2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Douglas Gregorf88e35b2010-11-30 06:16:57 +000015#include "clang/Serialization/ASTSerializationListener.h"
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +000016#include "ASTCommon.h"
Douglas Gregorc3a6ade2010-08-12 20:07:10 +000017#include "clang/Sema/Sema.h"
18#include "clang/Sema/IdentifierResolver.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclContextInternals.h"
John McCall19c1bfd2010-08-25 05:32:35 +000022#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000023#include "clang/AST/DeclFriend.h"
Douglas Gregorfeb84b02009-04-14 21:18:50 +000024#include "clang/AST/Expr.h"
John McCallbfd822c2010-08-24 07:32:53 +000025#include "clang/AST/ExprCXX.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000026#include "clang/AST/Type.h"
John McCall8f115c62009-10-16 21:56:05 +000027#include "clang/AST/TypeLocVisitor.h"
Sebastian Redlf5b13462010-08-18 23:57:17 +000028#include "clang/Serialization/ASTReader.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000029#include "clang/Lex/MacroInfo.h"
Douglas Gregoraae92242010-03-19 21:51:54 +000030#include "clang/Lex/PreprocessingRecord.h"
Chris Lattnerbaa52f42009-04-10 18:00:12 +000031#include "clang/Lex/Preprocessor.h"
Steve Naroff3fa455a2009-04-24 20:03:17 +000032#include "clang/Lex/HeaderSearch.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000033#include "clang/Basic/FileManager.h"
Chris Lattner226efd32010-11-23 19:19:34 +000034#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregore84a9da2009-04-20 20:36:09 +000035#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000036#include "clang/Basic/SourceManager.h"
Douglas Gregor4c7626e2009-04-13 16:31:14 +000037#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorbfbde532009-04-10 21:16:55 +000038#include "clang/Basic/TargetInfo.h"
Douglas Gregor7b71e632009-04-27 22:23:34 +000039#include "clang/Basic/Version.h"
Douglas Gregore0a3a512009-04-14 21:55:33 +000040#include "llvm/ADT/APFloat.h"
41#include "llvm/ADT/APInt.h"
Daniel Dunbarf8502d52009-10-17 23:52:28 +000042#include "llvm/ADT/StringExtras.h"
Douglas Gregoref84c4b2009-04-09 22:27:44 +000043#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencer740857f2010-12-21 16:45:57 +000044#include "llvm/Support/FileSystem.h"
Douglas Gregora7f71a92009-04-10 03:52:48 +000045#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000046#include "llvm/Support/Path.h"
Chris Lattner225dd6c2009-04-11 18:40:46 +000047#include <cstdio>
Douglas Gregoref84c4b2009-04-09 22:27:44 +000048using namespace clang;
Sebastian Redl539c5062010-08-18 23:57:32 +000049using namespace clang::serialization;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000050
Sebastian Redl3df5a082010-07-30 17:03:48 +000051template <typename T, typename Allocator>
52T *data(std::vector<T, Allocator> &v) {
53 return v.empty() ? 0 : &v.front();
54}
55template <typename T, typename Allocator>
56const T *data(const std::vector<T, Allocator> &v) {
57 return v.empty() ? 0 : &v.front();
58}
59
Douglas Gregoref84c4b2009-04-09 22:27:44 +000060//===----------------------------------------------------------------------===//
61// Type serialization
62//===----------------------------------------------------------------------===//
Chris Lattner7099dbc2009-04-27 06:16:06 +000063
Douglas Gregoref84c4b2009-04-09 22:27:44 +000064namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000065 class ASTTypeWriter {
Sebastian Redl55c0ad52010-08-18 23:56:21 +000066 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000067 ASTWriter::RecordDataImpl &Record;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000068
69 public:
70 /// \brief Type code that corresponds to the record generated.
Sebastian Redl539c5062010-08-18 23:57:32 +000071 TypeCode Code;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000072
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +000073 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl539c5062010-08-18 23:57:32 +000074 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregoref84c4b2009-04-09 22:27:44 +000075
76 void VisitArrayType(const ArrayType *T);
77 void VisitFunctionType(const FunctionType *T);
78 void VisitTagType(const TagType *T);
79
80#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
81#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +000082#include "clang/AST/TypeNodes.def"
83 };
84}
85
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000086void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000087 assert(false && "Built-in types are never serialized");
88}
89
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000090void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000091 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000092 Code = TYPE_COMPLEX;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000093}
94
Sebastian Redl42a0f6a2010-08-18 23:56:27 +000095void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +000096 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +000097 Code = TYPE_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +000098}
99
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000100void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000101 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000102 Code = TYPE_BLOCK_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000103}
104
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000105void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000106 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000107 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000108}
109
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000110void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000111 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000112 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000113}
114
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000115void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000116 Writer.AddTypeRef(T->getPointeeType(), Record);
117 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000118 Code = TYPE_MEMBER_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000119}
120
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000121void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000122 Writer.AddTypeRef(T->getElementType(), Record);
123 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall8ccfcb52009-09-24 19:53:00 +0000124 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000125}
126
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000127void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000128 VisitArrayType(T);
129 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000130 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000131}
132
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000133void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000134 VisitArrayType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000135 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000136}
137
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000138void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000139 VisitArrayType(T);
Douglas Gregor04318252009-07-06 15:59:29 +0000140 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
141 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +0000142 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000143 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000144}
145
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000146void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000147 Writer.AddTypeRef(T->getElementType(), Record);
148 Record.push_back(T->getNumElements());
Bob Wilsonaeb56442010-11-10 21:56:12 +0000149 Record.push_back(T->getVectorKind());
Sebastian Redl539c5062010-08-18 23:57:32 +0000150 Code = TYPE_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000151}
152
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000153void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000154 VisitVectorType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000155 Code = TYPE_EXT_VECTOR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000156}
157
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000158void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000159 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000160 FunctionType::ExtInfo C = T->getExtInfo();
161 Record.push_back(C.getNoReturn());
Rafael Espindola49b85ab2010-03-30 22:15:11 +0000162 Record.push_back(C.getRegParm());
Douglas Gregor8c940862010-01-18 17:14:39 +0000163 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000164 Record.push_back(C.getCC());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000165}
166
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000167void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000168 VisitFunctionType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000169 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000170}
171
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000172void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000173 VisitFunctionType(T);
174 Record.push_back(T->getNumArgs());
175 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
176 Writer.AddTypeRef(T->getArgType(I), Record);
177 Record.push_back(T->isVariadic());
178 Record.push_back(T->getTypeQuals());
Sebastian Redl5068f77ac2009-05-27 22:11:52 +0000179 Record.push_back(T->hasExceptionSpec());
180 Record.push_back(T->hasAnyExceptionSpec());
181 Record.push_back(T->getNumExceptions());
182 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
183 Writer.AddTypeRef(T->getExceptionType(I), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000184 Code = TYPE_FUNCTION_PROTO;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000185}
186
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000187void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCallb96ec562009-12-04 22:46:56 +0000188 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000189 Code = TYPE_UNRESOLVED_USING;
John McCallb96ec562009-12-04 22:46:56 +0000190}
John McCallb96ec562009-12-04 22:46:56 +0000191
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000192void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000193 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000194 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
195 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000196 Code = TYPE_TYPEDEF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000197}
198
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000199void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregor8f45df52009-04-16 22:23:12 +0000200 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000201 Code = TYPE_TYPEOF_EXPR;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000202}
203
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000204void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000205 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000206 Code = TYPE_TYPEOF;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000207}
208
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000209void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson81df7b82009-06-24 19:06:50 +0000210 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl539c5062010-08-18 23:57:32 +0000211 Code = TYPE_DECLTYPE;
Anders Carlsson81df7b82009-06-24 19:06:50 +0000212}
213
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000214void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000215 Record.push_back(T->isDependentType());
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000216 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump11289f42009-09-09 15:08:12 +0000217 assert(!T->isBeingDefined() &&
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000218 "Cannot serialize in the middle of a type definition");
219}
220
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000221void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000222 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000223 Code = TYPE_RECORD;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000224}
225
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000226void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000227 VisitTagType(T);
Sebastian Redl539c5062010-08-18 23:57:32 +0000228 Code = TYPE_ENUM;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000229}
230
John McCall81904512011-01-06 01:58:22 +0000231void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
232 Writer.AddTypeRef(T->getModifiedType(), Record);
233 Writer.AddTypeRef(T->getEquivalentType(), Record);
234 Record.push_back(T->getAttrKind());
235 Code = TYPE_ATTRIBUTED;
236}
237
Mike Stump11289f42009-09-09 15:08:12 +0000238void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000239ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCallcebee162009-10-18 09:09:24 +0000240 const SubstTemplateTypeParmType *T) {
241 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
242 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000243 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCallcebee162009-10-18 09:09:24 +0000244}
245
246void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000247ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000248 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisa4ed1812010-07-08 13:09:53 +0000249 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000250 Writer.AddTemplateName(T->getTemplateName(), Record);
251 Record.push_back(T->getNumArgs());
252 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
253 ArgI != ArgE; ++ArgI)
254 Writer.AddTemplateArgument(*ArgI, Record);
Argyrios Kyrtzidis45a83f92010-07-02 11:55:11 +0000255 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
256 : T->getCanonicalTypeInternal(),
257 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000258 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000259}
260
261void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000262ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidis4a57bd02010-06-30 08:49:25 +0000263 VisitArrayType(T);
264 Writer.AddStmt(T->getSizeExpr());
265 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000266 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000267}
268
269void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000270ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000271 const DependentSizedExtVectorType *T) {
272 // FIXME: Serialize this type (C++ only)
273 assert(false && "Cannot serialize dependent sized extended vector types");
274}
275
276void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000277ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000278 Record.push_back(T->getDepth());
279 Record.push_back(T->getIndex());
280 Record.push_back(T->isParameterPack());
281 Writer.AddIdentifierRef(T->getName(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000282 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000283}
284
285void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000286ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000287 Record.push_back(T->getKeyword());
288 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
289 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidise9290952010-07-02 11:55:24 +0000290 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
291 : T->getCanonicalTypeInternal(),
292 Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000293 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000294}
295
296void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000297ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +0000298 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000299 Record.push_back(T->getKeyword());
300 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
301 Writer.AddIdentifierRef(T->getIdentifier(), Record);
302 Record.push_back(T->getNumArgs());
303 for (DependentTemplateSpecializationType::iterator
304 I = T->begin(), E = T->end(); I != E; ++I)
305 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000306 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000307}
308
Douglas Gregord2fa7662010-12-20 02:24:11 +0000309void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
310 Writer.AddTypeRef(T->getPattern(), Record);
311 Code = TYPE_PACK_EXPANSION;
312}
313
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000314void ASTTypeWriter::VisitParenType(const ParenType *T) {
315 Writer.AddTypeRef(T->getInnerType(), Record);
316 Code = TYPE_PAREN;
317}
318
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000319void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara6150c882010-05-11 21:36:43 +0000320 Record.push_back(T->getKeyword());
Argyrios Kyrtzidisf0f7a792010-06-25 16:24:58 +0000321 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
322 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000323 Code = TYPE_ELABORATED;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000324}
325
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000326void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCalle78aac42010-03-10 03:28:59 +0000327 Writer.AddDeclRef(T->getDecl(), Record);
John McCall2408e322010-04-27 00:57:59 +0000328 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000329 Code = TYPE_INJECTED_CLASS_NAME;
John McCalle78aac42010-03-10 03:28:59 +0000330}
331
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000332void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregor1c283312010-08-11 12:19:30 +0000333 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000334 Code = TYPE_OBJC_INTERFACE;
John McCall8b07ec22010-05-15 11:32:37 +0000335}
336
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000337void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCall8b07ec22010-05-15 11:32:37 +0000338 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000339 Record.push_back(T->getNumProtocols());
John McCall8b07ec22010-05-15 11:32:37 +0000340 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff4fc95aa2009-05-27 16:21:00 +0000341 E = T->qual_end(); I != E; ++I)
342 Writer.AddDeclRef(*I, Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000343 Code = TYPE_OBJC_OBJECT;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000344}
345
Steve Narofffb4330f2009-06-17 22:40:22 +0000346void
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000347ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump11289f42009-09-09 15:08:12 +0000348 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +0000349 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000350}
351
John McCall8f115c62009-10-16 21:56:05 +0000352namespace {
353
354class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000355 ASTWriter &Writer;
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000356 ASTWriter::RecordDataImpl &Record;
John McCall8f115c62009-10-16 21:56:05 +0000357
358public:
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000359 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCall8f115c62009-10-16 21:56:05 +0000360 : Writer(Writer), Record(Record) { }
361
John McCall17001972009-10-18 01:05:36 +0000362#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCall8f115c62009-10-16 21:56:05 +0000363#define TYPELOC(CLASS, PARENT) \
John McCall17001972009-10-18 01:05:36 +0000364 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000365#include "clang/AST/TypeLocNodes.def"
366
John McCall17001972009-10-18 01:05:36 +0000367 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
368 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCall8f115c62009-10-16 21:56:05 +0000369};
370
371}
372
John McCall17001972009-10-18 01:05:36 +0000373void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
374 // nothing to do
John McCall8f115c62009-10-16 21:56:05 +0000375}
John McCall17001972009-10-18 01:05:36 +0000376void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorc9b7a592010-01-18 18:04:31 +0000377 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
378 if (TL.needsExtraLocalData()) {
379 Record.push_back(TL.getWrittenTypeSpec());
380 Record.push_back(TL.getWrittenSignSpec());
381 Record.push_back(TL.getWrittenWidthSpec());
382 Record.push_back(TL.hasModeAttr());
383 }
John McCall8f115c62009-10-16 21:56:05 +0000384}
John McCall17001972009-10-18 01:05:36 +0000385void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
386 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000387}
John McCall17001972009-10-18 01:05:36 +0000388void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
389 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000390}
John McCall17001972009-10-18 01:05:36 +0000391void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
392 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000393}
John McCall17001972009-10-18 01:05:36 +0000394void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
395 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000396}
John McCall17001972009-10-18 01:05:36 +0000397void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
398 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000399}
John McCall17001972009-10-18 01:05:36 +0000400void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
401 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall8f115c62009-10-16 21:56:05 +0000402}
John McCall17001972009-10-18 01:05:36 +0000403void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
404 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
405 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
406 Record.push_back(TL.getSizeExpr() ? 1 : 0);
407 if (TL.getSizeExpr())
408 Writer.AddStmt(TL.getSizeExpr());
John McCall8f115c62009-10-16 21:56:05 +0000409}
John McCall17001972009-10-18 01:05:36 +0000410void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
411 VisitArrayTypeLoc(TL);
412}
413void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
414 VisitArrayTypeLoc(TL);
415}
416void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
417 VisitArrayTypeLoc(TL);
418}
419void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
420 DependentSizedArrayTypeLoc TL) {
421 VisitArrayTypeLoc(TL);
422}
423void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
424 DependentSizedExtVectorTypeLoc TL) {
425 Writer.AddSourceLocation(TL.getNameLoc(), Record);
426}
427void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
428 Writer.AddSourceLocation(TL.getNameLoc(), Record);
429}
430void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
431 Writer.AddSourceLocation(TL.getNameLoc(), Record);
432}
433void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
434 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
435 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
Douglas Gregor7fb25412010-10-01 18:44:50 +0000436 Record.push_back(TL.getTrailingReturn());
John McCall17001972009-10-18 01:05:36 +0000437 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
438 Writer.AddDeclRef(TL.getArg(i), Record);
439}
440void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
441 VisitFunctionTypeLoc(TL);
442}
443void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
444 VisitFunctionTypeLoc(TL);
445}
John McCallb96ec562009-12-04 22:46:56 +0000446void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
447 Writer.AddSourceLocation(TL.getNameLoc(), Record);
448}
John McCall17001972009-10-18 01:05:36 +0000449void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
450 Writer.AddSourceLocation(TL.getNameLoc(), Record);
451}
452void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000453 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
454 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
455 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall17001972009-10-18 01:05:36 +0000456}
457void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCalle8595032010-01-13 20:03:27 +0000458 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
459 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
460 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
461 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000462}
463void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
464 Writer.AddSourceLocation(TL.getNameLoc(), Record);
465}
466void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
467 Writer.AddSourceLocation(TL.getNameLoc(), Record);
468}
469void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
470 Writer.AddSourceLocation(TL.getNameLoc(), Record);
471}
John McCall81904512011-01-06 01:58:22 +0000472void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
473 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
474 if (TL.hasAttrOperand()) {
475 SourceRange range = TL.getAttrOperandParensRange();
476 Writer.AddSourceLocation(range.getBegin(), Record);
477 Writer.AddSourceLocation(range.getEnd(), Record);
478 }
479 if (TL.hasAttrExprOperand()) {
480 Expr *operand = TL.getAttrExprOperand();
481 Record.push_back(operand ? 1 : 0);
482 if (operand) Writer.AddStmt(operand);
483 } else if (TL.hasAttrEnumOperand()) {
484 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
485 }
486}
John McCall17001972009-10-18 01:05:36 +0000487void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
488 Writer.AddSourceLocation(TL.getNameLoc(), Record);
489}
John McCallcebee162009-10-18 09:09:24 +0000490void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
491 SubstTemplateTypeParmTypeLoc TL) {
492 Writer.AddSourceLocation(TL.getNameLoc(), Record);
493}
John McCall17001972009-10-18 01:05:36 +0000494void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
495 TemplateSpecializationTypeLoc TL) {
John McCall0ad16662009-10-29 08:12:44 +0000496 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
497 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
498 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
499 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000500 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
501 TL.getArgLoc(i).getLocInfo(), Record);
John McCall17001972009-10-18 01:05:36 +0000502}
Abramo Bagnara924a8f32010-12-10 16:29:40 +0000503void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
504 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
505 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
506}
Abramo Bagnara6150c882010-05-11 21:36:43 +0000507void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000508 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
509 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall17001972009-10-18 01:05:36 +0000510}
John McCalle78aac42010-03-10 03:28:59 +0000511void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
512 Writer.AddSourceLocation(TL.getNameLoc(), Record);
513}
Douglas Gregorc1d2d8a2010-03-31 17:34:00 +0000514void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarad7548482010-05-19 21:37:53 +0000515 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
516 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall17001972009-10-18 01:05:36 +0000517 Writer.AddSourceLocation(TL.getNameLoc(), Record);
518}
John McCallc392f372010-06-11 00:33:02 +0000519void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
520 DependentTemplateSpecializationTypeLoc TL) {
521 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
522 Writer.AddSourceRange(TL.getQualifierRange(), Record);
523 Writer.AddSourceLocation(TL.getNameLoc(), Record);
524 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
525 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
526 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +0000527 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
528 TL.getArgLoc(I).getLocInfo(), Record);
John McCallc392f372010-06-11 00:33:02 +0000529}
Douglas Gregord2fa7662010-12-20 02:24:11 +0000530void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
531 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
532}
John McCall17001972009-10-18 01:05:36 +0000533void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
534 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCall8b07ec22010-05-15 11:32:37 +0000535}
536void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
537 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall17001972009-10-18 01:05:36 +0000538 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
539 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
540 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
541 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCall8f115c62009-10-16 21:56:05 +0000542}
John McCallfc93cf92009-10-22 22:37:11 +0000543void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
544 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCallfc93cf92009-10-22 22:37:11 +0000545}
John McCall8f115c62009-10-16 21:56:05 +0000546
Chris Lattner19cea4e2009-04-22 05:57:30 +0000547//===----------------------------------------------------------------------===//
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000548// ASTWriter Implementation
Douglas Gregoref84c4b2009-04-09 22:27:44 +0000549//===----------------------------------------------------------------------===//
550
Chris Lattner28fa4e62009-04-26 22:26:21 +0000551static void EmitBlockID(unsigned ID, const char *Name,
552 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000553 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000554 Record.clear();
555 Record.push_back(ID);
556 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
557
558 // Emit the block name if present.
559 if (Name == 0 || Name[0] == 0) return;
560 Record.clear();
561 while (*Name)
562 Record.push_back(*Name++);
563 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
564}
565
566static void EmitRecordID(unsigned ID, const char *Name,
567 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000568 ASTWriter::RecordDataImpl &Record) {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000569 Record.clear();
570 Record.push_back(ID);
571 while (*Name)
572 Record.push_back(*Name++);
573 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000574}
575
576static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +0000577 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl539c5062010-08-18 23:57:32 +0000578#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattnerccac3a62009-04-27 00:49:53 +0000579 RECORD(STMT_STOP);
580 RECORD(STMT_NULL_PTR);
581 RECORD(STMT_NULL);
582 RECORD(STMT_COMPOUND);
583 RECORD(STMT_CASE);
584 RECORD(STMT_DEFAULT);
585 RECORD(STMT_LABEL);
586 RECORD(STMT_IF);
587 RECORD(STMT_SWITCH);
588 RECORD(STMT_WHILE);
589 RECORD(STMT_DO);
590 RECORD(STMT_FOR);
591 RECORD(STMT_GOTO);
592 RECORD(STMT_INDIRECT_GOTO);
593 RECORD(STMT_CONTINUE);
594 RECORD(STMT_BREAK);
595 RECORD(STMT_RETURN);
596 RECORD(STMT_DECL);
597 RECORD(STMT_ASM);
598 RECORD(EXPR_PREDEFINED);
599 RECORD(EXPR_DECL_REF);
600 RECORD(EXPR_INTEGER_LITERAL);
601 RECORD(EXPR_FLOATING_LITERAL);
602 RECORD(EXPR_IMAGINARY_LITERAL);
603 RECORD(EXPR_STRING_LITERAL);
604 RECORD(EXPR_CHARACTER_LITERAL);
605 RECORD(EXPR_PAREN);
606 RECORD(EXPR_UNARY_OPERATOR);
607 RECORD(EXPR_SIZEOF_ALIGN_OF);
608 RECORD(EXPR_ARRAY_SUBSCRIPT);
609 RECORD(EXPR_CALL);
610 RECORD(EXPR_MEMBER);
611 RECORD(EXPR_BINARY_OPERATOR);
612 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
613 RECORD(EXPR_CONDITIONAL_OPERATOR);
614 RECORD(EXPR_IMPLICIT_CAST);
615 RECORD(EXPR_CSTYLE_CAST);
616 RECORD(EXPR_COMPOUND_LITERAL);
617 RECORD(EXPR_EXT_VECTOR_ELEMENT);
618 RECORD(EXPR_INIT_LIST);
619 RECORD(EXPR_DESIGNATED_INIT);
620 RECORD(EXPR_IMPLICIT_VALUE_INIT);
621 RECORD(EXPR_VA_ARG);
622 RECORD(EXPR_ADDR_LABEL);
623 RECORD(EXPR_STMT);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000624 RECORD(EXPR_CHOOSE);
625 RECORD(EXPR_GNU_NULL);
626 RECORD(EXPR_SHUFFLE_VECTOR);
627 RECORD(EXPR_BLOCK);
628 RECORD(EXPR_BLOCK_DECL_REF);
629 RECORD(EXPR_OBJC_STRING_LITERAL);
630 RECORD(EXPR_OBJC_ENCODE);
631 RECORD(EXPR_OBJC_SELECTOR_EXPR);
632 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
633 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
634 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
635 RECORD(EXPR_OBJC_KVC_REF_EXPR);
636 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000637 RECORD(STMT_OBJC_FOR_COLLECTION);
638 RECORD(STMT_OBJC_CATCH);
639 RECORD(STMT_OBJC_FINALLY);
640 RECORD(STMT_OBJC_AT_TRY);
641 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
642 RECORD(STMT_OBJC_AT_THROW);
Sam Weinige83b3ac2010-02-07 06:32:43 +0000643 RECORD(EXPR_CXX_OPERATOR_CALL);
644 RECORD(EXPR_CXX_CONSTRUCT);
645 RECORD(EXPR_CXX_STATIC_CAST);
646 RECORD(EXPR_CXX_DYNAMIC_CAST);
647 RECORD(EXPR_CXX_REINTERPRET_CAST);
648 RECORD(EXPR_CXX_CONST_CAST);
649 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
650 RECORD(EXPR_CXX_BOOL_LITERAL);
651 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattnerccac3a62009-04-27 00:49:53 +0000652#undef RECORD
Chris Lattner28fa4e62009-04-26 22:26:21 +0000653}
Mike Stump11289f42009-09-09 15:08:12 +0000654
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000655void ASTWriter::WriteBlockInfoBlock() {
Chris Lattner28fa4e62009-04-26 22:26:21 +0000656 RecordData Record;
657 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +0000658
Sebastian Redl539c5062010-08-18 23:57:32 +0000659#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
660#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump11289f42009-09-09 15:08:12 +0000661
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000662 // AST Top-Level Block.
Sebastian Redlf1642042010-08-18 23:57:22 +0000663 BLOCK(AST_BLOCK);
Zhongxing Xub027cdf2009-06-03 09:23:28 +0000664 RECORD(ORIGINAL_FILE_NAME);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000665 RECORD(TYPE_OFFSET);
666 RECORD(DECL_OFFSET);
667 RECORD(LANGUAGE_OPTIONS);
Douglas Gregor7b71e632009-04-27 22:23:34 +0000668 RECORD(METADATA);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000669 RECORD(IDENTIFIER_OFFSET);
670 RECORD(IDENTIFIER_TABLE);
671 RECORD(EXTERNAL_DEFINITIONS);
672 RECORD(SPECIAL_TYPES);
673 RECORD(STATISTICS);
674 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +0000675 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000676 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
677 RECORD(SELECTOR_OFFSETS);
678 RECORD(METHOD_POOL);
679 RECORD(PP_COUNTER_VALUE);
Douglas Gregor258ae542009-04-27 06:38:32 +0000680 RECORD(SOURCE_LOCATION_OFFSETS);
681 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregorc5046832009-04-27 18:38:38 +0000682 RECORD(STAT_CACHE);
Douglas Gregor61cac2b2009-04-27 20:06:05 +0000683 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek17437132010-01-22 20:59:36 +0000684 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregoraae92242010-03-19 21:51:54 +0000685 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redl595c5132010-07-08 22:01:51 +0000686 RECORD(CHAINED_METADATA);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +0000687 RECORD(REFERENCED_SELECTOR_POOL);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000688
Chris Lattner28fa4e62009-04-26 22:26:21 +0000689 // SourceManager Block.
Chris Lattner64031982009-04-27 00:40:25 +0000690 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000691 RECORD(SM_SLOC_FILE_ENTRY);
692 RECORD(SM_SLOC_BUFFER_ENTRY);
693 RECORD(SM_SLOC_BUFFER_BLOB);
694 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
695 RECORD(SM_LINE_TABLE);
Mike Stump11289f42009-09-09 15:08:12 +0000696
Chris Lattner28fa4e62009-04-26 22:26:21 +0000697 // Preprocessor Block.
Chris Lattner64031982009-04-27 00:40:25 +0000698 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000699 RECORD(PP_MACRO_OBJECT_LIKE);
700 RECORD(PP_MACRO_FUNCTION_LIKE);
701 RECORD(PP_TOKEN);
Douglas Gregoraae92242010-03-19 21:51:54 +0000702 RECORD(PP_MACRO_INSTANTIATION);
703 RECORD(PP_MACRO_DEFINITION);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +0000704
Douglas Gregor12bfa382009-10-17 00:13:19 +0000705 // Decls and Types block.
706 BLOCK(DECLTYPES_BLOCK);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000707 RECORD(TYPE_EXT_QUAL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000708 RECORD(TYPE_COMPLEX);
709 RECORD(TYPE_POINTER);
710 RECORD(TYPE_BLOCK_POINTER);
711 RECORD(TYPE_LVALUE_REFERENCE);
712 RECORD(TYPE_RVALUE_REFERENCE);
713 RECORD(TYPE_MEMBER_POINTER);
714 RECORD(TYPE_CONSTANT_ARRAY);
715 RECORD(TYPE_INCOMPLETE_ARRAY);
716 RECORD(TYPE_VARIABLE_ARRAY);
717 RECORD(TYPE_VECTOR);
718 RECORD(TYPE_EXT_VECTOR);
719 RECORD(TYPE_FUNCTION_PROTO);
720 RECORD(TYPE_FUNCTION_NO_PROTO);
721 RECORD(TYPE_TYPEDEF);
722 RECORD(TYPE_TYPEOF_EXPR);
723 RECORD(TYPE_TYPEOF);
724 RECORD(TYPE_RECORD);
725 RECORD(TYPE_ENUM);
726 RECORD(TYPE_OBJC_INTERFACE);
John McCall94f619a2010-05-16 02:12:35 +0000727 RECORD(TYPE_OBJC_OBJECT);
Steve Narofffb4330f2009-06-17 22:40:22 +0000728 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000729 RECORD(DECL_TRANSLATION_UNIT);
730 RECORD(DECL_TYPEDEF);
731 RECORD(DECL_ENUM);
732 RECORD(DECL_RECORD);
733 RECORD(DECL_ENUM_CONSTANT);
734 RECORD(DECL_FUNCTION);
735 RECORD(DECL_OBJC_METHOD);
736 RECORD(DECL_OBJC_INTERFACE);
737 RECORD(DECL_OBJC_PROTOCOL);
738 RECORD(DECL_OBJC_IVAR);
739 RECORD(DECL_OBJC_AT_DEFS_FIELD);
740 RECORD(DECL_OBJC_CLASS);
741 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
742 RECORD(DECL_OBJC_CATEGORY);
743 RECORD(DECL_OBJC_CATEGORY_IMPL);
744 RECORD(DECL_OBJC_IMPLEMENTATION);
745 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
746 RECORD(DECL_OBJC_PROPERTY);
747 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000748 RECORD(DECL_FIELD);
749 RECORD(DECL_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000750 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000751 RECORD(DECL_PARM_VAR);
Chris Lattnerdb397b62009-04-26 22:32:16 +0000752 RECORD(DECL_FILE_SCOPE_ASM);
753 RECORD(DECL_BLOCK);
754 RECORD(DECL_CONTEXT_LEXICAL);
755 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor12bfa382009-10-17 00:13:19 +0000756 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattnerccac3a62009-04-27 00:49:53 +0000757 AddStmtsExprs(Stream, Record);
Chris Lattner28fa4e62009-04-26 22:26:21 +0000758#undef RECORD
759#undef BLOCK
760 Stream.ExitBlock();
761}
762
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000763/// \brief Adjusts the given filename to only write out the portion of the
764/// filename that is not part of the system root directory.
Mike Stump11289f42009-09-09 15:08:12 +0000765///
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000766/// \param Filename the file name to adjust.
767///
768/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
769/// the returned filename will be adjusted by this system root.
770///
771/// \returns either the original filename (if it needs no adjustment) or the
772/// adjusted filename (which points into the @p Filename parameter).
Mike Stump11289f42009-09-09 15:08:12 +0000773static const char *
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000774adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
775 assert(Filename && "No file name to adjust?");
Mike Stump11289f42009-09-09 15:08:12 +0000776
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000777 if (!isysroot)
778 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000779
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000780 // Verify that the filename and the system root have the same prefix.
781 unsigned Pos = 0;
782 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
783 if (Filename[Pos] != isysroot[Pos])
784 return Filename; // Prefixes don't match.
Mike Stump11289f42009-09-09 15:08:12 +0000785
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000786 // We hit the end of the filename before we hit the end of the system root.
787 if (!Filename[Pos])
788 return Filename;
Mike Stump11289f42009-09-09 15:08:12 +0000789
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000790 // If the file name has a '/' at the current position, skip over the '/'.
791 // We distinguish sysroot-based includes from absolute includes by the
792 // absence of '/' at the beginning of sysroot-based includes.
793 if (Filename[Pos] == '/')
794 ++Pos;
Mike Stump11289f42009-09-09 15:08:12 +0000795
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000796 return Filename + Pos;
797}
Chris Lattner28fa4e62009-04-26 22:26:21 +0000798
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000799/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000800void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregorbfbde532009-04-10 21:16:55 +0000801 using namespace llvm;
Douglas Gregor45fe0362009-05-12 01:31:05 +0000802
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000803 // Metadata
804 const TargetInfo &Target = Context.Target;
805 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000806 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl539c5062010-08-18 23:57:32 +0000807 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000808 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
809 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000810 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
811 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
812 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000813 // Target triple or chained PCH name
814 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000815 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump11289f42009-09-09 15:08:12 +0000816
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000817 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000818 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
819 Record.push_back(VERSION_MAJOR);
820 Record.push_back(VERSION_MINOR);
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000821 Record.push_back(CLANG_VERSION_MAJOR);
822 Record.push_back(CLANG_VERSION_MINOR);
823 Record.push_back(isysroot != 0);
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000824 // FIXME: This writes the absolute path for chained headers.
825 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
826 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump11289f42009-09-09 15:08:12 +0000827
Douglas Gregor45fe0362009-05-12 01:31:05 +0000828 // Original file name
829 SourceManager &SM = Context.getSourceManager();
830 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
831 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000832 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregor45fe0362009-05-12 01:31:05 +0000833 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
834 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
835
Michael J. Spencer740857f2010-12-21 16:45:57 +0000836 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump11289f42009-09-09 15:08:12 +0000837
Michael J. Spencer740857f2010-12-21 16:45:57 +0000838 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000839
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +0000840 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +0000841 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000842 isysroot);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000843 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +0000844 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbar8100d012009-08-24 09:31:37 +0000845 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor45fe0362009-05-12 01:31:05 +0000846 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000847
Ted Kremenek18e066f2010-01-22 22:12:47 +0000848 // Repository branch/version information.
849 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +0000850 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenek18e066f2010-01-22 22:12:47 +0000851 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
852 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregord54f3a12009-10-05 21:07:28 +0000853 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +0000854 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenek18e066f2010-01-22 22:12:47 +0000855 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
856 getClangFullRepositoryVersion());
Douglas Gregorbfbde532009-04-10 21:16:55 +0000857}
858
859/// \brief Write the LangOptions structure.
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000860void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor55abb232009-04-10 20:39:37 +0000861 RecordData Record;
862 Record.push_back(LangOpts.Trigraphs);
863 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
864 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
865 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
866 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carruthe03aa552010-04-17 20:17:31 +0000867 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor55abb232009-04-10 20:39:37 +0000868 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
869 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
870 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
871 Record.push_back(LangOpts.C99); // C99 Support
872 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencer4992ca4b2010-10-21 05:21:48 +0000873 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
874 // already saved elsewhere.
Douglas Gregor55abb232009-04-10 20:39:37 +0000875 Record.push_back(LangOpts.CPlusPlus); // C++ Support
876 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor55abb232009-04-10 20:39:37 +0000877 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump11289f42009-09-09 15:08:12 +0000878
Douglas Gregor55abb232009-04-10 20:39:37 +0000879 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
880 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000881 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian45878032010-02-09 19:31:38 +0000882 // modern abi enabled.
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +0000883 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian45878032010-02-09 19:31:38 +0000884 // modern abi enabled.
Ted Kremenek1d56c9e2010-12-23 21:35:43 +0000885 Record.push_back(LangOpts.ObjCDefaultSynthProperties); // Objective-C auto-synthesized
886 // properties enabled.
Fariborz Jahanian62c56022010-04-22 21:01:59 +0000887 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump11289f42009-09-09 15:08:12 +0000888
Douglas Gregor55abb232009-04-10 20:39:37 +0000889 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +0000890 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
891 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +0000892 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +0000893 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar925152c2010-02-10 18:48:44 +0000894 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor55abb232009-04-10 20:39:37 +0000895
896 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
897 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
898 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
899
Chris Lattner258172e2009-04-27 07:35:58 +0000900 // Whether static initializers are protected by locks.
901 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +0000902 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +0000903 Record.push_back(LangOpts.Blocks); // block extension to C
904 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
905 // they are unused.
906 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
907 // (modulo the platform support).
908
Chris Lattner51924e512010-06-26 21:25:03 +0000909 Record.push_back(LangOpts.getSignedOverflowBehavior());
910 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor55abb232009-04-10 20:39:37 +0000911
912 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +0000913 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +0000914 // defined.
915 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
916 // opposed to __DYNAMIC__).
917 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
918
919 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
920 // used (instead of C99 semantics).
921 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +0000922 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
923 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000924 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
925 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +0000926 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor55abb232009-04-10 20:39:37 +0000927 Record.push_back(LangOpts.getGCMode());
928 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000929 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +0000930 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +0000931 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +0000932 Record.push_back(LangOpts.CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +0000933 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson9cedbef2009-08-22 22:30:33 +0000934 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +0000935 Record.push_back(LangOpts.SpellChecking);
Sebastian Redl539c5062010-08-18 23:57:32 +0000936 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +0000937}
938
Douglas Gregora7f71a92009-04-10 03:52:48 +0000939//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +0000940// stat cache Serialization
941//===----------------------------------------------------------------------===//
942
943namespace {
944// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000945class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +0000946public:
947 typedef const char * key_type;
948 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000949
Chris Lattner2a6fa472010-11-23 19:28:12 +0000950 typedef struct stat data_type;
951 typedef const data_type &data_type_ref;
Douglas Gregorc5046832009-04-27 18:38:38 +0000952
953 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000954 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000955 }
Mike Stump11289f42009-09-09 15:08:12 +0000956
957 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +0000958 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
959 data_type_ref Data) {
960 unsigned StrLen = strlen(path);
961 clang::io::Emit16(Out, StrLen);
Chris Lattner2a6fa472010-11-23 19:28:12 +0000962 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregorc5046832009-04-27 18:38:38 +0000963 clang::io::Emit8(Out, DataLen);
964 return std::make_pair(StrLen + 1, DataLen);
965 }
Mike Stump11289f42009-09-09 15:08:12 +0000966
Douglas Gregorc5046832009-04-27 18:38:38 +0000967 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
968 Out.write(path, KeyLen);
969 }
Mike Stump11289f42009-09-09 15:08:12 +0000970
Chris Lattner2a6fa472010-11-23 19:28:12 +0000971 void EmitData(llvm::raw_ostream &Out, key_type_ref,
Douglas Gregorc5046832009-04-27 18:38:38 +0000972 data_type_ref Data, unsigned DataLen) {
973 using namespace clang::io;
974 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +0000975
Chris Lattner2a6fa472010-11-23 19:28:12 +0000976 Emit32(Out, (uint32_t) Data.st_ino);
977 Emit32(Out, (uint32_t) Data.st_dev);
978 Emit16(Out, (uint16_t) Data.st_mode);
979 Emit64(Out, (uint64_t) Data.st_mtime);
980 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregorc5046832009-04-27 18:38:38 +0000981
982 assert(Out.tell() - Start == DataLen && "Wrong data length");
983 }
984};
985} // end anonymous namespace
986
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000987/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000988void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +0000989 // Build the on-disk hash table containing information about every
990 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000991 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +0000992 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000993 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +0000994 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000995 Stat != StatEnd; ++Stat, ++NumStatEntries) {
996 const char *Filename = Stat->first();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000997 Generator.insert(Filename, Stat->second);
998 }
Mike Stump11289f42009-09-09 15:08:12 +0000999
Douglas Gregorc5046832009-04-27 18:38:38 +00001000 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001001 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +00001002 uint32_t BucketOffset;
1003 {
1004 llvm::raw_svector_ostream Out(StatCacheData);
1005 // Make sure that no bucket is at offset 0
1006 clang::io::Emit32(Out, 0);
1007 BucketOffset = Generator.Emit(Out);
1008 }
1009
1010 // Create a blob abbreviation
1011 using namespace llvm;
1012 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001013 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +00001014 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1015 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1016 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1017 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1018
1019 // Write the stat cache
1020 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001021 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +00001022 Record.push_back(BucketOffset);
1023 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001024 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +00001025}
1026
1027//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +00001028// Source Manager Serialization
1029//===----------------------------------------------------------------------===//
1030
1031/// \brief Create an abbreviation for the SLocEntry that refers to a
1032/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001033static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001034 using namespace llvm;
1035 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001036 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001037 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1038 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1039 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1040 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001041 // FileEntry fields.
1042 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1043 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001044 // HeaderFileInfo fields.
1045 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
1046 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
1047 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
1048 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregora7f71a92009-04-10 03:52:48 +00001049 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001050 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001051}
1052
1053/// \brief Create an abbreviation for the SLocEntry that refers to a
1054/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001055static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001056 using namespace llvm;
1057 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001058 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001059 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1060 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1061 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1062 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1063 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001064 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001065}
1066
1067/// \brief Create an abbreviation for the SLocEntry that refers to a
1068/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001069static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001070 using namespace llvm;
1071 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001072 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001073 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001074 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001075}
1076
1077/// \brief Create an abbreviation for the SLocEntry that refers to an
1078/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001079static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001080 using namespace llvm;
1081 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001082 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001083 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1084 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1085 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1086 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001087 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001088 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001089}
1090
1091/// \brief Writes the block containing the serialized form of the
1092/// source manager.
1093///
1094/// TODO: We should probably use an on-disk hash table (stored in a
1095/// blob), indexed based on the file name, so that we only create
1096/// entries for files that we actually need. In the common case (no
1097/// errors), we probably won't have to create file entries for any of
1098/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001099void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001100 const Preprocessor &PP,
1101 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001102 RecordData Record;
1103
Chris Lattner0910e3b2009-04-10 17:16:57 +00001104 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001105 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001106
1107 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001108 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1109 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1110 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1111 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001112
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001113 // Write the line table.
1114 if (SourceMgr.hasLineTable()) {
1115 LineTableInfo &LineTable = SourceMgr.getLineTable();
1116
1117 // Emit the file names
1118 Record.push_back(LineTable.getNumFilenames());
1119 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1120 // Emit the file name
1121 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001122 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001123 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1124 Record.push_back(FilenameLen);
1125 if (FilenameLen)
1126 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1127 }
Mike Stump11289f42009-09-09 15:08:12 +00001128
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001129 // Emit the line entries
1130 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1131 L != LEnd; ++L) {
1132 // Emit the file ID
1133 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +00001134
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001135 // Emit the line entries
1136 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +00001137 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001138 LEEnd = L->second.end();
1139 LE != LEEnd; ++LE) {
1140 Record.push_back(LE->FileOffset);
1141 Record.push_back(LE->LineNo);
1142 Record.push_back(LE->FilenameID);
1143 Record.push_back((unsigned)LE->FileKind);
1144 Record.push_back(LE->IncludeOffset);
1145 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001146 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001147 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001148 }
1149
Douglas Gregor258ae542009-04-27 06:38:32 +00001150 // Write out the source location entry table. We skip the first
1151 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001152 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001153 RecordData PreloadSLocs;
Sebastian Redl5c415f32010-07-22 17:01:13 +00001154 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1155 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1156 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1157 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001158 // Get this source location entry.
1159 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001160
Douglas Gregor258ae542009-04-27 06:38:32 +00001161 // Record the offset of this source-location entry.
1162 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1163
1164 // Figure out which record code to use.
1165 unsigned Code;
1166 if (SLoc->isFile()) {
1167 if (SLoc->getFile().getContentCache()->Entry)
Sebastian Redl539c5062010-08-18 23:57:32 +00001168 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001169 else
Sebastian Redl539c5062010-08-18 23:57:32 +00001170 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001171 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001172 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001173 Record.clear();
1174 Record.push_back(Code);
1175
1176 Record.push_back(SLoc->getOffset());
1177 if (SLoc->isFile()) {
1178 const SrcMgr::FileInfo &File = SLoc->getFile();
1179 Record.push_back(File.getIncludeLoc().getRawEncoding());
1180 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1181 Record.push_back(File.hasLineDirectives());
1182
1183 const SrcMgr::ContentCache *Content = File.getContentCache();
1184 if (Content->Entry) {
1185 // The source location entry is a file. The blob associated
1186 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001187
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001188 // Emit size/modification time for this file.
1189 Record.push_back(Content->Entry->getSize());
1190 Record.push_back(Content->Entry->getModificationTime());
1191
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001192 // Emit header-search information associated with this file.
1193 HeaderFileInfo HFI;
1194 HeaderSearch &HS = PP.getHeaderSearchInfo();
1195 if (Content->Entry->getUID() < HS.header_file_size())
1196 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1197 Record.push_back(HFI.isImport);
1198 Record.push_back(HFI.DirInfo);
1199 Record.push_back(HFI.NumIncludes);
1200 AddIdentifierRef(HFI.ControllingMacro, Record);
1201
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001202 // Turn the file name into an absolute path, if it isn't already.
1203 const char *Filename = Content->Entry->getName();
Michael J. Spencer740857f2010-12-21 16:45:57 +00001204 llvm::SmallString<128> FilePath(Filename);
1205 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001206 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001207
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001208 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001209 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001210
1211 // FIXME: For now, preload all file source locations, so that
1212 // we get the appropriate File entries in the reader. This is
1213 // a temporary measure.
Sebastian Redl5c415f32010-07-22 17:01:13 +00001214 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001215 } else {
1216 // The source location entry is a buffer. The blob associated
1217 // with this entry contains the contents of the buffer.
1218
1219 // We add one to the size so that we capture the trailing NULL
1220 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1221 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001222 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001223 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001224 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001225 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1226 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001227 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001228 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001229 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001230 llvm::StringRef(Buffer->getBufferStart(),
1231 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001232
1233 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl5c415f32010-07-22 17:01:13 +00001234 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001235 }
1236 } else {
1237 // The source location entry is an instantiation.
1238 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1239 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1240 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1241 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1242
1243 // Compute the token length for this macro expansion.
1244 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001245 if (I + 1 != N)
1246 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001247 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1248 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1249 }
1250 }
1251
Douglas Gregor8f45df52009-04-16 22:23:12 +00001252 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001253
1254 if (SLocEntryOffsets.empty())
1255 return;
1256
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001257 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001258 // table is used for lazily loading source-location information.
1259 using namespace llvm;
1260 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001261 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001262 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1263 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1264 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1265 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001266
Douglas Gregor258ae542009-04-27 06:38:32 +00001267 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001268 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001269 Record.push_back(SLocEntryOffsets.size());
Sebastian Redlc1d035f2010-09-22 20:19:08 +00001270 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1271 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Douglas Gregor258ae542009-04-27 06:38:32 +00001272 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001273 (const char *)data(SLocEntryOffsets),
Chris Lattner12d61d32009-04-27 19:01:47 +00001274 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001275
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001276 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001277 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001278 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001279}
1280
Douglas Gregorc5046832009-04-27 18:38:38 +00001281//===----------------------------------------------------------------------===//
1282// Preprocessor Serialization
1283//===----------------------------------------------------------------------===//
1284
Chris Lattnereeffaef2009-04-10 17:15:23 +00001285/// \brief Writes the block containing the serialized form of the
1286/// preprocessor.
1287///
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001288void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001289 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001290
Chris Lattner0af3ba12009-04-13 01:29:17 +00001291 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1292 if (PP.getCounterValue() != 0) {
1293 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001294 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001295 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001296 }
1297
1298 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001299 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001300
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001301 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001302 // FIXME: use diagnostics subsystem for localization etc.
1303 if (PP.SawDateOrTime())
1304 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001305
Douglas Gregor796d76a2010-10-20 22:00:55 +00001306
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001307 // Loop over all the macro definitions that are live at the end of the file,
1308 // emitting each to the PP section.
Douglas Gregoraae92242010-03-19 21:51:54 +00001309 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Douglas Gregor796d76a2010-10-20 22:00:55 +00001310 unsigned InclusionAbbrev = 0;
1311 if (PPRec) {
1312 using namespace llvm;
1313 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1314 Abbrev->Add(BitCodeAbbrevOp(PP_INCLUSION_DIRECTIVE));
1315 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1316 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1317 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1318 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1319 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1320 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1321 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001322 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor796d76a2010-10-20 22:00:55 +00001323 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001324
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001325 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1326 I != E; ++I) {
Chris Lattner34321bc2009-04-10 21:41:48 +00001327 // FIXME: This emits macros in hash table order, we should do it in a stable
1328 // order so that output is reproducible.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001329 MacroInfo *MI = I->second;
1330
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001331 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001332 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001333 // Also skip macros from a AST file if we're chaining.
Douglas Gregoreb114da2010-10-01 01:03:07 +00001334
1335 // FIXME: There is a (probably minor) optimization we could do here, if
1336 // the macro comes from the original PCH but the identifier comes from a
1337 // chained PCH, by storing the offset into the original PCH rather than
1338 // writing the macro definition a second time.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001339 if (MI->isBuiltinMacro() ||
Douglas Gregoreb114da2010-10-01 01:03:07 +00001340 (Chain && I->first->isFromAST() && MI->isFromAST()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001341 continue;
1342
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001343 AddIdentifierRef(I->first, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001344 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001345 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1346 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001347
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001348 unsigned Code;
1349 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001350 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001351 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001352 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001353
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001354 Record.push_back(MI->isC99Varargs());
1355 Record.push_back(MI->isGNUVarargs());
1356 Record.push_back(MI->getNumArgs());
1357 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1358 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001359 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001360 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001361
Douglas Gregoraae92242010-03-19 21:51:54 +00001362 // If we have a detailed preprocessing record, record the macro definition
1363 // ID that corresponds to this macro.
1364 if (PPRec)
1365 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001366
Douglas Gregor8f45df52009-04-16 22:23:12 +00001367 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001368 Record.clear();
1369
Chris Lattner2199f5b2009-04-10 18:08:30 +00001370 // Emit the tokens array.
1371 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1372 // Note that we know that the preprocessor does not have any annotation
1373 // tokens in it because they are created by the parser, and thus can't be
1374 // in a macro definition.
1375 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001376
Chris Lattner2199f5b2009-04-10 18:08:30 +00001377 Record.push_back(Tok.getLocation().getRawEncoding());
1378 Record.push_back(Tok.getLength());
1379
Chris Lattner2199f5b2009-04-10 18:08:30 +00001380 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1381 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001382 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump11289f42009-09-09 15:08:12 +00001383
Chris Lattner2199f5b2009-04-10 18:08:30 +00001384 // FIXME: Should translate token kind to a stable encoding.
1385 Record.push_back(Tok.getKind());
1386 // FIXME: Should translate token flags to a stable encoding.
1387 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001388
Sebastian Redl539c5062010-08-18 23:57:32 +00001389 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001390 Record.clear();
1391 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001392 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001393 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001394
Douglas Gregoraae92242010-03-19 21:51:54 +00001395 // If the preprocessor has a preprocessing record, emit it.
1396 unsigned NumPreprocessingRecords = 0;
1397 if (PPRec) {
Sebastian Redl7abd8d52010-09-27 23:20:01 +00001398 unsigned IndexBase = Chain ? PPRec->getNumPreallocatedEntities() : 0;
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001399 for (PreprocessingRecord::iterator E = PPRec->begin(Chain),
1400 EEnd = PPRec->end(Chain);
Douglas Gregoraae92242010-03-19 21:51:54 +00001401 E != EEnd; ++E) {
1402 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001403
Douglas Gregoraae92242010-03-19 21:51:54 +00001404 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1405 // Record this macro definition's location.
Sebastian Redl50e26582010-09-15 19:54:06 +00001406 MacroID ID = getMacroDefinitionID(MD);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001407
Douglas Gregor91096292010-10-02 19:29:26 +00001408 // Don't write the macro definition if it is from another AST file.
1409 if (ID < FirstMacroID)
1410 continue;
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001411
1412 // Notify the serialization listener that we're serializing this entity.
1413 if (SerializationListener)
1414 SerializationListener->SerializedPreprocessedEntity(*E,
1415 Stream.GetCurrentBitNo());
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001416
Douglas Gregor91096292010-10-02 19:29:26 +00001417 unsigned Position = ID - FirstMacroID;
1418 if (Position != MacroDefinitionOffsets.size()) {
1419 if (Position > MacroDefinitionOffsets.size())
1420 MacroDefinitionOffsets.resize(Position + 1);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001421
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001422 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
Douglas Gregoraae92242010-03-19 21:51:54 +00001423 } else
1424 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001425
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001426 Record.push_back(IndexBase + NumPreprocessingRecords++);
Douglas Gregoraae92242010-03-19 21:51:54 +00001427 Record.push_back(ID);
1428 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1429 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1430 AddIdentifierRef(MD->getName(), Record);
1431 AddSourceLocation(MD->getLocation(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +00001432 Stream.EmitRecord(PP_MACRO_DEFINITION, Record);
Douglas Gregoraae92242010-03-19 21:51:54 +00001433 continue;
1434 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001435
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001436 // Notify the serialization listener that we're serializing this entity.
1437 if (SerializationListener)
1438 SerializationListener->SerializedPreprocessedEntity(*E,
1439 Stream.GetCurrentBitNo());
1440
1441 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1442 Record.push_back(IndexBase + NumPreprocessingRecords++);
1443 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1444 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1445 AddIdentifierRef(MI->getName(), Record);
1446 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1447 Stream.EmitRecord(PP_MACRO_INSTANTIATION, Record);
1448 continue;
1449 }
1450
Douglas Gregor796d76a2010-10-20 22:00:55 +00001451 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1452 Record.push_back(PP_INCLUSION_DIRECTIVE);
1453 Record.push_back(IndexBase + NumPreprocessingRecords++);
1454 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1455 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1456 Record.push_back(ID->getFileName().size());
1457 Record.push_back(ID->wasInQuotes());
1458 Record.push_back(static_cast<unsigned>(ID->getKind()));
1459 llvm::SmallString<64> Buffer;
1460 Buffer += ID->getFileName();
1461 Buffer += ID->getFile()->getName();
1462 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1463 continue;
1464 }
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001465
1466 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
Douglas Gregoraae92242010-03-19 21:51:54 +00001467 }
1468 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001469
Douglas Gregor8f45df52009-04-16 22:23:12 +00001470 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001471
Douglas Gregoraae92242010-03-19 21:51:54 +00001472 // Write the offsets table for the preprocessing record.
1473 if (NumPreprocessingRecords > 0) {
1474 // Write the offsets table for identifier IDs.
1475 using namespace llvm;
1476 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001477 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregoraae92242010-03-19 21:51:54 +00001478 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1479 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1480 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1481 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001482
Douglas Gregoraae92242010-03-19 21:51:54 +00001483 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001484 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001485 Record.push_back(NumPreprocessingRecords);
1486 Record.push_back(MacroDefinitionOffsets.size());
1487 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001488 (const char *)data(MacroDefinitionOffsets),
Douglas Gregoraae92242010-03-19 21:51:54 +00001489 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1490 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001491}
1492
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001493void ASTWriter::WriteUserDiagnosticMappings(const Diagnostic &Diag) {
1494 RecordData Record;
1495 for (unsigned i = 0; i != diag::DIAG_UPPER_LIMIT; ++i) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001496 diag::Mapping Map = Diag.getDiagnosticMappingInfo(i,Diag.GetCurDiagState());
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001497 if (Map & 0x8) { // user mapping.
1498 Record.push_back(i);
1499 Record.push_back(Map & 0x7);
1500 }
1501 }
1502
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00001503 if (!Record.empty())
1504 Stream.EmitRecord(DIAG_USER_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001505}
1506
Douglas Gregorc5046832009-04-27 18:38:38 +00001507//===----------------------------------------------------------------------===//
1508// Type Serialization
1509//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001510
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001511/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001512void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00001513 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001514 if (Idx.getIndex() == 0) // we haven't seen this type before.
1515 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00001516
Douglas Gregor9b3932c2010-10-05 18:37:06 +00001517 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00001518
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001519 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001520 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001521 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001522 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001523 else if (TypeOffsets.size() < Index) {
1524 TypeOffsets.resize(Index + 1);
1525 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001526 }
1527
1528 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001529
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001530 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001531 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001532
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001533 if (T.hasLocalNonFastQualifiers()) {
1534 Qualifiers Qs = T.getLocalQualifiers();
1535 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001536 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001537 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00001538 } else {
1539 switch (T->getTypeClass()) {
1540 // For all of the concrete, non-dependent types, call the
1541 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001542#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001543 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001544#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001545#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001546 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001547 }
1548
1549 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001550 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001551
1552 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001553 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001554}
1555
Douglas Gregorc5046832009-04-27 18:38:38 +00001556//===----------------------------------------------------------------------===//
1557// Declaration Serialization
1558//===----------------------------------------------------------------------===//
1559
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001560/// \brief Write the block containing all of the declaration IDs
1561/// lexically declared within the given DeclContext.
1562///
1563/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1564/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001565uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001566 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001567 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001568 return 0;
1569
Douglas Gregor8f45df52009-04-16 22:23:12 +00001570 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001571 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001572 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001573 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001574 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1575 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001576 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001577
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001578 ++NumLexicalDeclContexts;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001579 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001580 reinterpret_cast<char*>(Decls.data()),
1581 Decls.size() * sizeof(KindDeclIDPair));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001582 return Offset;
1583}
1584
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001585void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001586 using namespace llvm;
1587 RecordData Record;
1588
1589 // Write the type offsets array
1590 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001591 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001592 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1593 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1594 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1595 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001596 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001597 Record.push_back(TypeOffsets.size());
1598 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001599 (const char *)data(TypeOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001600 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1601
1602 // Write the declaration offsets array
1603 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001604 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001605 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1606 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1607 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1608 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001609 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001610 Record.push_back(DeclOffsets.size());
1611 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001612 (const char *)data(DeclOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001613 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1614}
1615
Douglas Gregorc5046832009-04-27 18:38:38 +00001616//===----------------------------------------------------------------------===//
1617// Global Method Pool and Selector Serialization
1618//===----------------------------------------------------------------------===//
1619
Douglas Gregore84a9da2009-04-20 20:36:09 +00001620namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001621// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001622class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001623 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001624
1625public:
1626 typedef Selector key_type;
1627 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001628
Sebastian Redl834bb972010-08-04 17:20:04 +00001629 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00001630 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00001631 ObjCMethodList Instance, Factory;
1632 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00001633 typedef const data_type& data_type_ref;
1634
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001635 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001636
Douglas Gregorc78d3462009-04-24 21:10:55 +00001637 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00001638 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001639 }
Mike Stump11289f42009-09-09 15:08:12 +00001640
1641 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001642 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1643 data_type_ref Methods) {
1644 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1645 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00001646 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1647 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001648 Method = Method->Next)
1649 if (Method->Method)
1650 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00001651 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001652 Method = Method->Next)
1653 if (Method->Method)
1654 DataLen += 4;
1655 clang::io::Emit16(Out, DataLen);
1656 return std::make_pair(KeyLen, DataLen);
1657 }
Mike Stump11289f42009-09-09 15:08:12 +00001658
Douglas Gregor95c13f52009-04-25 17:48:32 +00001659 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001660 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001661 assert((Start >> 32) == 0 && "Selector key offset too large");
1662 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001663 unsigned N = Sel.getNumArgs();
1664 clang::io::Emit16(Out, N);
1665 if (N == 0)
1666 N = 1;
1667 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001668 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001669 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1670 }
Mike Stump11289f42009-09-09 15:08:12 +00001671
Douglas Gregorc78d3462009-04-24 21:10:55 +00001672 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001673 data_type_ref Methods, unsigned DataLen) {
1674 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00001675 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001676 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001677 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001678 Method = Method->Next)
1679 if (Method->Method)
1680 ++NumInstanceMethods;
1681
1682 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001683 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001684 Method = Method->Next)
1685 if (Method->Method)
1686 ++NumFactoryMethods;
1687
1688 clang::io::Emit16(Out, NumInstanceMethods);
1689 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00001690 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001691 Method = Method->Next)
1692 if (Method->Method)
1693 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00001694 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001695 Method = Method->Next)
1696 if (Method->Method)
1697 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001698
1699 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001700 }
1701};
1702} // end anonymous namespace
1703
Sebastian Redla19a67f2010-08-03 21:58:15 +00001704/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001705///
1706/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00001707/// in an on-disk hash table indexed by the selector. The hash table also
1708/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001709void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001710 using namespace llvm;
1711
Sebastian Redla19a67f2010-08-03 21:58:15 +00001712 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00001713 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00001714 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00001715 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001716 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001717 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001718 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001719 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00001720
Sebastian Redla19a67f2010-08-03 21:58:15 +00001721 // Create the on-disk hash table representation. We walk through every
1722 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00001723 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00001724 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00001725 I = SelectorIDs.begin(), E = SelectorIDs.end();
1726 I != E; ++I) {
1727 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001728 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001729 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00001730 I->second,
1731 ObjCMethodList(),
1732 ObjCMethodList()
1733 };
1734 if (F != SemaRef.MethodPool.end()) {
1735 Data.Instance = F->second.first;
1736 Data.Factory = F->second.second;
1737 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001738 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00001739 // changed.
1740 if (Chain && I->second < FirstSelectorID) {
1741 // Selector already exists. Did it change?
1742 bool changed = false;
1743 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
1744 M = M->Next) {
1745 if (M->Method->getPCHLevel() == 0)
1746 changed = true;
1747 }
1748 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
1749 M = M->Next) {
1750 if (M->Method->getPCHLevel() == 0)
1751 changed = true;
1752 }
1753 if (!changed)
1754 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00001755 } else if (Data.Instance.Method || Data.Factory.Method) {
1756 // A new method pool entry.
1757 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00001758 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001759 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001760 }
1761
Douglas Gregorc78d3462009-04-24 21:10:55 +00001762 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001763 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001764 uint32_t BucketOffset;
1765 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001766 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001767 llvm::raw_svector_ostream Out(MethodPool);
1768 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001769 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001770 BucketOffset = Generator.Emit(Out, Trait);
1771 }
1772
1773 // Create a blob abbreviation
1774 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001775 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001776 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001777 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001778 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1779 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1780
Douglas Gregor95c13f52009-04-25 17:48:32 +00001781 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00001782 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001783 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001784 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00001785 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001786 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00001787
1788 // Create a blob abbreviation for the selector table offsets.
1789 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001790 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001791 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor95c13f52009-04-25 17:48:32 +00001792 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1793 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1794
1795 // Write the selector offsets table.
1796 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001797 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001798 Record.push_back(SelectorOffsets.size());
1799 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001800 (const char *)data(SelectorOffsets),
Douglas Gregor95c13f52009-04-25 17:48:32 +00001801 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001802 }
1803}
1804
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001805/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001806void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001807 using namespace llvm;
1808 if (SemaRef.ReferencedSelectors.empty())
1809 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00001810
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001811 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00001812
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001813 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00001814 // very tricky to fix, and given that @selector shouldn't really appear in
1815 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001816 for (DenseMap<Selector, SourceLocation>::iterator S =
1817 SemaRef.ReferencedSelectors.begin(),
1818 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
1819 Selector Sel = (*S).first;
1820 SourceLocation Loc = (*S).second;
1821 AddSelectorRef(Sel, Record);
1822 AddSourceLocation(Loc, Record);
1823 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001824 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001825}
1826
Douglas Gregorc5046832009-04-27 18:38:38 +00001827//===----------------------------------------------------------------------===//
1828// Identifier Table Serialization
1829//===----------------------------------------------------------------------===//
1830
Douglas Gregorc78d3462009-04-24 21:10:55 +00001831namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001832class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001833 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001834 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001835
Douglas Gregor1d583f22009-04-28 21:18:29 +00001836 /// \brief Determines whether this is an "interesting" identifier
1837 /// that needs a full IdentifierInfo structure written into the hash
1838 /// table.
1839 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1840 return II->isPoisoned() ||
1841 II->isExtensionToken() ||
1842 II->hasMacroDefinition() ||
1843 II->getObjCOrBuiltinID() ||
1844 II->getFETokenInfo<void>();
1845 }
1846
Douglas Gregore84a9da2009-04-20 20:36:09 +00001847public:
1848 typedef const IdentifierInfo* key_type;
1849 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001850
Sebastian Redl539c5062010-08-18 23:57:32 +00001851 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001852 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001853
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001854 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00001855 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00001856
1857 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001858 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00001859 }
Mike Stump11289f42009-09-09 15:08:12 +00001860
1861 std::pair<unsigned,unsigned>
1862 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00001863 IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001864 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001865 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1866 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00001867 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00001868 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00001869 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00001870 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001871 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1872 DEnd = IdentifierResolver::end();
1873 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00001874 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00001875 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001876 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00001877 // We emit the key length after the data length so that every
1878 // string is preceded by a 16-bit length. This matches the PTH
1879 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00001880 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001881 return std::make_pair(KeyLen, DataLen);
1882 }
Mike Stump11289f42009-09-09 15:08:12 +00001883
1884 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001885 unsigned KeyLen) {
1886 // Record the location of the key data. This is used when generating
1887 // the mapping from persistent IDs to strings.
1888 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001889 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001890 }
Mike Stump11289f42009-09-09 15:08:12 +00001891
1892 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00001893 IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00001894 if (!isInterestingIdentifier(II)) {
1895 clang::io::Emit32(Out, ID << 1);
1896 return;
1897 }
Douglas Gregorb9256522009-04-28 21:32:13 +00001898
Douglas Gregor1d583f22009-04-28 21:18:29 +00001899 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001900 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001901 bool hasMacroDefinition =
1902 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00001903 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00001904 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001905 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1906 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1907 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00001908 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001909 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00001910 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001911
Douglas Gregorc3366a52009-04-21 23:56:24 +00001912 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00001913 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001914
Douglas Gregora868bbd2009-04-21 22:25:48 +00001915 // Emit the declaration IDs in reverse order, because the
1916 // IdentifierResolver provides the declarations as they would be
1917 // visible (e.g., the function "stat" would come before the struct
1918 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1919 // adds declarations to the end of the list (so we need to see the
1920 // struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00001921 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump11289f42009-09-09 15:08:12 +00001922 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00001923 IdentifierResolver::end());
1924 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1925 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00001926 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00001927 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001928 }
1929};
1930} // end anonymous namespace
1931
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001932/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001933///
1934/// The identifier table consists of a blob containing string data
1935/// (the actual identifiers themselves) and a separate "offsets" index
1936/// that maps identifier IDs to locations within the blob.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001937void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001938 using namespace llvm;
1939
1940 // Create and write out the blob that contains the identifier
1941 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001942 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001943 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001944 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump11289f42009-09-09 15:08:12 +00001945
Douglas Gregore6648fb2009-04-28 20:33:11 +00001946 // Look for any identifiers that were named while processing the
1947 // headers, but are otherwise not needed. We add these to the hash
1948 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001949 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00001950 // file.
1951 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1952 IDEnd = PP.getIdentifierTable().end();
1953 ID != IDEnd; ++ID)
1954 getIdentifierRef(ID->second);
1955
Sebastian Redlff4a2952010-07-23 23:49:55 +00001956 // Create the on-disk hash table representation. We only store offsets
1957 // for identifiers that appear here for the first time.
1958 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00001959 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001960 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1961 ID != IDEnd; ++ID) {
1962 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001963 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001964 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001965 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001966
Douglas Gregore84a9da2009-04-20 20:36:09 +00001967 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001968 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00001969 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001970 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001971 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001972 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001973 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001974 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001975 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001976 }
1977
1978 // Create a blob abbreviation
1979 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001980 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00001981 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001982 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00001983 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001984
1985 // Write the identifier table
1986 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001987 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001988 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001989 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001990 }
1991
1992 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00001993 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001994 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00001995 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1996 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1997 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1998
1999 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002000 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00002001 Record.push_back(IdentifierOffsets.size());
2002 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00002003 (const char *)data(IdentifierOffsets),
Douglas Gregor0e149972009-04-25 19:10:14 +00002004 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002005}
2006
Douglas Gregorc5046832009-04-27 18:38:38 +00002007//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002008// DeclContext's Name Lookup Table Serialization
2009//===----------------------------------------------------------------------===//
2010
2011namespace {
2012// Trait used for the on-disk hash table used in the method pool.
2013class ASTDeclContextNameLookupTrait {
2014 ASTWriter &Writer;
2015
2016public:
2017 typedef DeclarationName key_type;
2018 typedef key_type key_type_ref;
2019
2020 typedef DeclContext::lookup_result data_type;
2021 typedef const data_type& data_type_ref;
2022
2023 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2024
2025 unsigned ComputeHash(DeclarationName Name) {
2026 llvm::FoldingSetNodeID ID;
2027 ID.AddInteger(Name.getNameKind());
2028
2029 switch (Name.getNameKind()) {
2030 case DeclarationName::Identifier:
2031 ID.AddString(Name.getAsIdentifierInfo()->getName());
2032 break;
2033 case DeclarationName::ObjCZeroArgSelector:
2034 case DeclarationName::ObjCOneArgSelector:
2035 case DeclarationName::ObjCMultiArgSelector:
2036 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2037 break;
2038 case DeclarationName::CXXConstructorName:
2039 case DeclarationName::CXXDestructorName:
2040 case DeclarationName::CXXConversionFunctionName:
2041 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
2042 break;
2043 case DeclarationName::CXXOperatorName:
2044 ID.AddInteger(Name.getCXXOverloadedOperator());
2045 break;
2046 case DeclarationName::CXXLiteralOperatorName:
2047 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2048 case DeclarationName::CXXUsingDirective:
2049 break;
2050 }
2051
2052 return ID.ComputeHash();
2053 }
2054
2055 std::pair<unsigned,unsigned>
2056 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2057 data_type_ref Lookup) {
2058 unsigned KeyLen = 1;
2059 switch (Name.getNameKind()) {
2060 case DeclarationName::Identifier:
2061 case DeclarationName::ObjCZeroArgSelector:
2062 case DeclarationName::ObjCOneArgSelector:
2063 case DeclarationName::ObjCMultiArgSelector:
2064 case DeclarationName::CXXConstructorName:
2065 case DeclarationName::CXXDestructorName:
2066 case DeclarationName::CXXConversionFunctionName:
2067 case DeclarationName::CXXLiteralOperatorName:
2068 KeyLen += 4;
2069 break;
2070 case DeclarationName::CXXOperatorName:
2071 KeyLen += 1;
2072 break;
2073 case DeclarationName::CXXUsingDirective:
2074 break;
2075 }
2076 clang::io::Emit16(Out, KeyLen);
2077
2078 // 2 bytes for num of decls and 4 for each DeclID.
2079 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2080 clang::io::Emit16(Out, DataLen);
2081
2082 return std::make_pair(KeyLen, DataLen);
2083 }
2084
2085 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2086 using namespace clang::io;
2087
2088 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2089 Emit8(Out, Name.getNameKind());
2090 switch (Name.getNameKind()) {
2091 case DeclarationName::Identifier:
2092 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2093 break;
2094 case DeclarationName::ObjCZeroArgSelector:
2095 case DeclarationName::ObjCOneArgSelector:
2096 case DeclarationName::ObjCMultiArgSelector:
2097 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2098 break;
2099 case DeclarationName::CXXConstructorName:
2100 case DeclarationName::CXXDestructorName:
2101 case DeclarationName::CXXConversionFunctionName:
2102 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2103 break;
2104 case DeclarationName::CXXOperatorName:
2105 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2106 Emit8(Out, Name.getCXXOverloadedOperator());
2107 break;
2108 case DeclarationName::CXXLiteralOperatorName:
2109 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2110 break;
2111 case DeclarationName::CXXUsingDirective:
2112 break;
2113 }
2114 }
2115
2116 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2117 data_type Lookup, unsigned DataLen) {
2118 uint64_t Start = Out.tell(); (void)Start;
2119 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2120 for (; Lookup.first != Lookup.second; ++Lookup.first)
2121 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2122
2123 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2124 }
2125};
2126} // end anonymous namespace
2127
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002128/// \brief Write the block containing all of the declaration IDs
2129/// visible from the given DeclContext.
2130///
2131/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002132/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002133uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2134 DeclContext *DC) {
2135 if (DC->getPrimaryContext() != DC)
2136 return 0;
2137
2138 // Since there is no name lookup into functions or methods, don't bother to
2139 // build a visible-declarations table for these entities.
2140 if (DC->isFunctionOrMethod())
2141 return 0;
2142
2143 // If not in C++, we perform name lookup for the translation unit via the
2144 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2145 // FIXME: In C++ we need the visible declarations in order to "see" the
2146 // friend declarations, is there a way to do this without writing the table ?
2147 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2148 return 0;
2149
2150 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00002151 if (DC->hasExternalVisibleStorage())
2152 DC->MaterializeVisibleDeclsFromExternalStorage();
2153 else
2154 DC->lookup(DeclarationName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002155
2156 // Serialize the contents of the mapping used for lookup. Note that,
2157 // although we have two very different code paths, the serialized
2158 // representation is the same for both cases: a declaration name,
2159 // followed by a size, followed by references to the visible
2160 // declarations that have that name.
2161 uint64_t Offset = Stream.GetCurrentBitNo();
2162 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2163 if (!Map || Map->empty())
2164 return 0;
2165
2166 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2167 ASTDeclContextNameLookupTrait Trait(*this);
2168
2169 // Create the on-disk hash table representation.
2170 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2171 D != DEnd; ++D) {
2172 DeclarationName Name = D->first;
2173 DeclContext::lookup_result Result = D->second.getLookupResult();
2174 Generator.insert(Name, Result, Trait);
2175 }
2176
2177 // Create the on-disk hash table in a buffer.
2178 llvm::SmallString<4096> LookupTable;
2179 uint32_t BucketOffset;
2180 {
2181 llvm::raw_svector_ostream Out(LookupTable);
2182 // Make sure that no bucket is at offset 0
2183 clang::io::Emit32(Out, 0);
2184 BucketOffset = Generator.Emit(Out, Trait);
2185 }
2186
2187 // Write the lookup table
2188 RecordData Record;
2189 Record.push_back(DECL_CONTEXT_VISIBLE);
2190 Record.push_back(BucketOffset);
2191 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2192 LookupTable.str());
2193
2194 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2195 ++NumVisibleDeclContexts;
2196 return Offset;
2197}
2198
Sebastian Redla4071b42010-08-24 00:50:09 +00002199/// \brief Write an UPDATE_VISIBLE block for the given context.
2200///
2201/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2202/// DeclContext in a dependent AST file. As such, they only exist for the TU
2203/// (in C++) and for namespaces.
2204void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00002205 // Make the context build its lookup table, but don't make it load external
2206 // decls.
2207 DC->lookup(DeclarationName());
2208
2209 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2210 if (!Map || Map->empty())
2211 return;
2212
2213 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2214 ASTDeclContextNameLookupTrait Trait(*this);
2215
2216 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002217 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2218 D != DEnd; ++D) {
2219 DeclarationName Name = D->first;
2220 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002221 // For any name that appears in this table, the results are complete, i.e.
2222 // they overwrite results from previous PCHs. Merging is always a mess.
2223 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002224 }
2225
2226 // Create the on-disk hash table in a buffer.
2227 llvm::SmallString<4096> LookupTable;
2228 uint32_t BucketOffset;
2229 {
2230 llvm::raw_svector_ostream Out(LookupTable);
2231 // Make sure that no bucket is at offset 0
2232 clang::io::Emit32(Out, 0);
2233 BucketOffset = Generator.Emit(Out, Trait);
2234 }
2235
2236 // Write the lookup table
2237 RecordData Record;
2238 Record.push_back(UPDATE_VISIBLE);
2239 Record.push_back(getDeclID(cast<Decl>(DC)));
2240 Record.push_back(BucketOffset);
2241 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2242}
2243
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002244//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00002245// General Serialization Routines
2246//===----------------------------------------------------------------------===//
2247
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002248/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002249void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00002250 Record.push_back(Attrs.size());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002251 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2252 const Attr * A = *i;
2253 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2254 AddSourceLocation(A->getLocation(), Record);
2255 Record.push_back(A->isInherited());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002256
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002257#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00002258
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002259 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002260}
2261
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002262void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002263 Record.push_back(Str.size());
2264 Record.insert(Record.end(), Str.begin(), Str.end());
2265}
2266
Douglas Gregore84a9da2009-04-20 20:36:09 +00002267/// \brief Note that the identifier II occurs at the given offset
2268/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002269void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002270 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002271 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00002272 // up earlier in the chain and thus don't need an offset.
2273 if (ID >= FirstIdentID)
2274 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002275}
2276
Douglas Gregor95c13f52009-04-25 17:48:32 +00002277/// \brief Note that the selector Sel occurs at the given offset
2278/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002279void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00002280 unsigned ID = SelectorIDs[Sel];
2281 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00002282 // Don't record offsets for selectors that are also available in a different
2283 // file.
2284 if (ID < FirstSelectorID)
2285 return;
2286 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002287}
2288
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002289ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00002290 : Stream(Stream), Chain(0), SerializationListener(0),
2291 FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00002292 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002293 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor91096292010-10-02 19:29:26 +00002294 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2295 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002296 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002297 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
2298 NextCXXBaseSpecifiersID(1)
2299{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002300}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002301
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002302void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002303 const char *isysroot) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002304 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002305 Stream.Emit((unsigned)'C', 8);
2306 Stream.Emit((unsigned)'P', 8);
2307 Stream.Emit((unsigned)'C', 8);
2308 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002309
Chris Lattner28fa4e62009-04-26 22:26:21 +00002310 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002311
Sebastian Redl143413f2010-07-12 22:02:52 +00002312 if (Chain)
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002313 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002314 else
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002315 WriteASTCore(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002316}
2317
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002318void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl143413f2010-07-12 22:02:52 +00002319 const char *isysroot) {
2320 using namespace llvm;
2321
2322 ASTContext &Context = SemaRef.Context;
2323 Preprocessor &PP = SemaRef.PP;
2324
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002325 // The translation unit is the first declaration we'll emit.
2326 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlff4a2952010-07-23 23:49:55 +00002327 ++NextDeclID;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002328 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002329
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002330 // Make sure that we emit IdentifierInfos (and any attached
2331 // declarations) for builtins.
2332 {
2333 IdentifierTable &Table = PP.getIdentifierTable();
2334 llvm::SmallVector<const char *, 32> BuiltinNames;
2335 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2336 Context.getLangOptions().NoBuiltin);
2337 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2338 getIdentifierRef(&Table.get(BuiltinNames[I]));
2339 }
2340
Chris Lattner0c797362009-09-08 18:19:27 +00002341 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002342 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002343 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002344 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00002345 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2346 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00002347 }
Douglas Gregord4df8652009-04-22 22:02:47 +00002348
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002349 // Build a record containing all of the file scoped decls in this file.
2350 RecordData UnusedFileScopedDecls;
2351 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2352 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002353
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002354 RecordData WeakUndeclaredIdentifiers;
2355 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2356 WeakUndeclaredIdentifiers.push_back(
2357 SemaRef.WeakUndeclaredIdentifiers.size());
2358 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2359 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2360 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2361 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2362 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2363 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2364 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2365 }
2366 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002367
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002368 // Build a record containing all of the locally-scoped external
2369 // declarations in this header file. Generally, this record will be
2370 // empty.
2371 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002372 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00002373 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002374 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002375 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2376 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2377 TD != TDEnd; ++TD)
2378 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2379
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002380 // Build a record containing all of the ext_vector declarations.
2381 RecordData ExtVectorDecls;
2382 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2383 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2384
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002385 // Build a record containing all of the VTable uses information.
2386 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00002387 if (!SemaRef.VTableUses.empty()) {
2388 VTableUses.push_back(SemaRef.VTableUses.size());
2389 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2390 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2391 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2392 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2393 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002394 }
2395
2396 // Build a record containing all of dynamic classes declarations.
2397 RecordData DynamicClasses;
2398 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2399 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2400
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002401 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002402 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002403 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002404 I = SemaRef.PendingInstantiations.begin(),
2405 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2406 AddDeclRef(I->first, PendingInstantiations);
2407 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002408 }
2409 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2410 "There are local ones at end of translation unit!");
2411
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002412 // Build a record containing some declaration references.
2413 RecordData SemaDeclRefs;
2414 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2415 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2416 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2417 }
2418
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002419 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002420 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002421 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002422 WriteMetadata(Context, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002423 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002424 if (StatCalls && !isysroot)
Douglas Gregor11cfd942010-07-12 23:48:14 +00002425 WriteStatCache(*StatCalls);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002426 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc277ad12009-07-18 15:33:26 +00002427 // Write the record of special types.
2428 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002429
Steve Naroffc277ad12009-07-18 15:33:26 +00002430 AddTypeRef(Context.getBuiltinVaListType(), Record);
2431 AddTypeRef(Context.getObjCIdType(), Record);
2432 AddTypeRef(Context.getObjCSelType(), Record);
2433 AddTypeRef(Context.getObjCProtoType(), Record);
2434 AddTypeRef(Context.getObjCClassType(), Record);
2435 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2436 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2437 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002438 AddTypeRef(Context.getjmp_bufType(), Record);
2439 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002440 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2441 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpd0153282009-10-20 02:12:22 +00002442 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002443 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002444 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2445 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002446 Record.push_back(Context.isInt128Installed());
Sebastian Redl539c5062010-08-18 23:57:32 +00002447 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002448
Douglas Gregor1970d882009-04-26 03:49:13 +00002449 // Keep writing types and declarations until all types and
2450 // declarations have been written.
Sebastian Redl539c5062010-08-18 23:57:32 +00002451 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002452 WriteDeclsBlockAbbrevs();
2453 while (!DeclTypesToEmit.empty()) {
2454 DeclOrType DOT = DeclTypesToEmit.front();
2455 DeclTypesToEmit.pop();
2456 if (DOT.isType())
2457 WriteType(DOT.getType());
2458 else
2459 WriteDecl(Context, DOT.getDecl());
2460 }
2461 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002462
Douglas Gregor45053152009-10-17 17:25:45 +00002463 WritePreprocessor(PP);
Sebastian Redla19a67f2010-08-03 21:58:15 +00002464 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002465 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002466 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00002467
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002468 WriteTypeDeclOffsets();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002469 WriteUserDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00002470
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002471 // Write the C++ base-specifier set offsets.
2472 if (!CXXBaseSpecifiersOffsets.empty()) {
2473 // Create a blob abbreviation for the C++ base specifiers offsets.
2474 using namespace llvm;
2475
2476 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2477 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2478 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2479 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2480 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2481
2482 // Write the selector offsets table.
2483 Record.clear();
2484 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2485 Record.push_back(CXXBaseSpecifiersOffsets.size());
2486 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
2487 (const char *)CXXBaseSpecifiersOffsets.data(),
2488 CXXBaseSpecifiersOffsets.size() * sizeof(uint32_t));
2489 }
2490
Douglas Gregord4df8652009-04-22 22:02:47 +00002491 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002492 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002493 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002494
2495 // Write the record containing tentative definitions.
2496 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002497 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002498
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002499 // Write the record containing unused file scoped decls.
2500 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002501 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002502
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002503 // Write the record containing weak undeclared identifiers.
2504 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002505 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002506 WeakUndeclaredIdentifiers);
2507
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002508 // Write the record containing locally-scoped external definitions.
2509 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002510 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002511 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002512
2513 // Write the record containing ext_vector type names.
2514 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002515 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002516
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002517 // Write the record containing VTable uses information.
2518 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002519 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002520
2521 // Write the record containing dynamic classes declarations.
2522 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002523 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002524
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002525 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002526 if (!PendingInstantiations.empty())
2527 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002528
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002529 // Write the record containing declaration references of Sema.
2530 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002531 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002532
Douglas Gregor08f01292009-04-17 22:13:46 +00002533 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002534 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002535 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002536 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002537 Record.push_back(NumLexicalDeclContexts);
2538 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00002539 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002540 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002541}
2542
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002543void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002544 const char *isysroot) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002545 using namespace llvm;
2546
2547 ASTContext &Context = SemaRef.Context;
2548 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002549
Sebastian Redl143413f2010-07-12 22:02:52 +00002550 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002551 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002552 WriteMetadata(Context, isysroot);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002553 if (StatCalls && !isysroot)
2554 WriteStatCache(*StatCalls);
2555 // FIXME: Source manager block should only write new stuff, which could be
2556 // done by tracking the largest ID in the chain
2557 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002558
2559 // The special types are in the chained PCH.
2560
2561 // We don't start with the translation unit, but with its decls that
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002562 // don't come from the chained PCH.
Sebastian Redl143413f2010-07-12 22:02:52 +00002563 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002564 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002565 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2566 E = TU->noload_decls_end();
Sebastian Redl143413f2010-07-12 22:02:52 +00002567 I != E; ++I) {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002568 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002569 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002570 else if ((*I)->isChangedSinceDeserialization())
2571 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl143413f2010-07-12 22:02:52 +00002572 }
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002573 // We also need to write a lexical updates block for the TU.
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002574 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002575 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002576 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2577 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2578 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002579 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002580 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2581 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002582 NewGlobalDecls.size() * sizeof(KindDeclIDPair));
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00002583 // And a visible updates block for the DeclContexts.
2584 Abv = new llvm::BitCodeAbbrev();
2585 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2586 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2587 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2588 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2589 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2590 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl143413f2010-07-12 22:02:52 +00002591
Sebastian Redl98912122010-07-27 23:01:28 +00002592 // Build a record containing all of the new tentative definitions in this
2593 // file, in TentativeDefinitions order.
2594 RecordData TentativeDefinitions;
2595 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2596 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2597 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2598 }
2599
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002600 // Build a record containing all of the file scoped decls in this file.
2601 RecordData UnusedFileScopedDecls;
2602 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2603 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2604 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002605 }
2606
Sebastian Redl08aca90252010-08-05 18:21:25 +00002607 // We write the entire table, overwriting the tables from the chain.
2608 RecordData WeakUndeclaredIdentifiers;
2609 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2610 WeakUndeclaredIdentifiers.push_back(
2611 SemaRef.WeakUndeclaredIdentifiers.size());
2612 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2613 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2614 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2615 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2616 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2617 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2618 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2619 }
2620 }
2621
Sebastian Redl98912122010-07-27 23:01:28 +00002622 // Build a record containing all of the locally-scoped external
2623 // declarations in this header file. Generally, this record will be
2624 // empty.
2625 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002626 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl98912122010-07-27 23:01:28 +00002627 // nondeterminstic!
2628 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2629 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2630 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2631 TD != TDEnd; ++TD) {
2632 if (TD->second->getPCHLevel() == 0)
2633 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2634 }
2635
2636 // Build a record containing all of the ext_vector declarations.
2637 RecordData ExtVectorDecls;
2638 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2639 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2640 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2641 }
2642
Sebastian Redl08aca90252010-08-05 18:21:25 +00002643 // Build a record containing all of the VTable uses information.
2644 // We write everything here, because it's too hard to determine whether
2645 // a use is new to this part.
2646 RecordData VTableUses;
2647 if (!SemaRef.VTableUses.empty()) {
2648 VTableUses.push_back(SemaRef.VTableUses.size());
2649 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2650 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2651 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2652 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2653 }
2654 }
2655
2656 // Build a record containing all of dynamic classes declarations.
2657 RecordData DynamicClasses;
2658 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2659 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
2660 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2661
2662 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002663 RecordData PendingInstantiations;
Sebastian Redl08aca90252010-08-05 18:21:25 +00002664 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002665 I = SemaRef.PendingInstantiations.begin(),
2666 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl08aca90252010-08-05 18:21:25 +00002667 if (I->first->getPCHLevel() == 0) {
Chandler Carruth54080172010-08-25 08:44:16 +00002668 AddDeclRef(I->first, PendingInstantiations);
2669 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002670 }
2671 }
2672 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2673 "There are local ones at end of translation unit!");
2674
2675 // Build a record containing some declaration references.
2676 // It's not worth the effort to avoid duplication here.
2677 RecordData SemaDeclRefs;
2678 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2679 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2680 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2681 }
2682
Sebastian Redl539c5062010-08-18 23:57:32 +00002683 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl143413f2010-07-12 22:02:52 +00002684 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00002685 for (DeclsToRewriteTy::iterator
2686 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
2687 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl143413f2010-07-12 22:02:52 +00002688 while (!DeclTypesToEmit.empty()) {
2689 DeclOrType DOT = DeclTypesToEmit.front();
2690 DeclTypesToEmit.pop();
2691 if (DOT.isType())
2692 WriteType(DOT.getType());
2693 else
2694 WriteDecl(Context, DOT.getDecl());
2695 }
2696 Stream.ExitBlock();
2697
Sebastian Redl98912122010-07-27 23:01:28 +00002698 WritePreprocessor(PP);
Sebastian Redl51c79d82010-08-04 22:21:29 +00002699 WriteSelectors(SemaRef);
2700 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlff4a2952010-07-23 23:49:55 +00002701 WriteIdentifierTable(PP);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002702 WriteTypeDeclOffsets();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002703 // FIXME: For chained PCH only write the new mappings (we currently
2704 // write all of them again).
2705 WriteUserDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl98912122010-07-27 23:01:28 +00002706
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002707 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002708 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002709 RecordData FirstLatestDeclIDs;
2710 for (FirstLatestDeclMap::iterator
2711 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
2712 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
2713 "Expected first & second to be in different PCHs");
2714 AddDeclRef(I->first, FirstLatestDeclIDs);
2715 AddDeclRef(I->second, FirstLatestDeclIDs);
2716 }
2717 if (!FirstLatestDeclIDs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002718 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002719
Sebastian Redl98912122010-07-27 23:01:28 +00002720 // Write the record containing external, unnamed definitions.
2721 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002722 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00002723
2724 // Write the record containing tentative definitions.
2725 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002726 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00002727
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002728 // Write the record containing unused file scoped decls.
2729 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002730 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002731
Sebastian Redl08aca90252010-08-05 18:21:25 +00002732 // Write the record containing weak undeclared identifiers.
2733 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002734 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl08aca90252010-08-05 18:21:25 +00002735 WeakUndeclaredIdentifiers);
2736
Sebastian Redl98912122010-07-27 23:01:28 +00002737 // Write the record containing locally-scoped external definitions.
2738 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002739 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl98912122010-07-27 23:01:28 +00002740 LocallyScopedExternalDecls);
2741
2742 // Write the record containing ext_vector type names.
2743 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002744 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002745
Sebastian Redl08aca90252010-08-05 18:21:25 +00002746 // Write the record containing VTable uses information.
2747 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002748 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002749
2750 // Write the record containing dynamic classes declarations.
2751 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002752 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002753
2754 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002755 if (!PendingInstantiations.empty())
2756 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002757
2758 // Write the record containing declaration references of Sema.
2759 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002760 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl98912122010-07-27 23:01:28 +00002761
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00002762 // Write the updates to DeclContexts.
2763 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
2764 I = UpdatedDeclContexts.begin(),
2765 E = UpdatedDeclContexts.end();
Sebastian Redla4071b42010-08-24 00:50:09 +00002766 I != E; ++I)
2767 WriteDeclContextVisibleUpdate(*I);
2768
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002769 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002770
Sebastian Redl98912122010-07-27 23:01:28 +00002771 Record.clear();
2772 Record.push_back(NumStatements);
2773 Record.push_back(NumMacros);
2774 Record.push_back(NumLexicalDeclContexts);
2775 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002776 WriteDeclReplacementsBlock();
Sebastian Redl539c5062010-08-18 23:57:32 +00002777 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl143413f2010-07-12 22:02:52 +00002778 Stream.ExitBlock();
2779}
2780
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002781void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002782 if (DeclUpdates.empty())
2783 return;
2784
2785 RecordData OffsetsRecord;
2786 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
2787 for (DeclUpdateMap::iterator
2788 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
2789 const Decl *D = I->first;
2790 UpdateRecord &URec = I->second;
2791
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00002792 if (DeclsToRewrite.count(D))
2793 continue; // The decl will be written completely,no need to store updates.
2794
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002795 uint64_t Offset = Stream.GetCurrentBitNo();
2796 Stream.EmitRecord(DECL_UPDATES, URec);
2797
2798 OffsetsRecord.push_back(GetDeclRef(D));
2799 OffsetsRecord.push_back(Offset);
2800 }
2801 Stream.ExitBlock();
2802 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
2803}
2804
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002805void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002806 if (ReplacedDecls.empty())
2807 return;
2808
2809 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002810 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002811 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
2812 Record.push_back(I->first);
2813 Record.push_back(I->second);
2814 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002815 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002816}
2817
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002818void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002819 Record.push_back(Loc.getRawEncoding());
2820}
2821
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002822void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00002823 AddSourceLocation(Range.getBegin(), Record);
2824 AddSourceLocation(Range.getEnd(), Record);
2825}
2826
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002827void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002828 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00002829 const uint64_t *Words = Value.getRawData();
2830 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002831}
2832
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002833void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00002834 Record.push_back(Value.isUnsigned());
2835 AddAPInt(Value, Record);
2836}
2837
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002838void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00002839 AddAPInt(Value.bitcastToAPInt(), Record);
2840}
2841
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002842void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002843 Record.push_back(getIdentifierRef(II));
2844}
2845
Sebastian Redl539c5062010-08-18 23:57:32 +00002846IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002847 if (II == 0)
2848 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002849
Sebastian Redl539c5062010-08-18 23:57:32 +00002850 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002851 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00002852 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002853 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002854}
2855
Sebastian Redl50e26582010-09-15 19:54:06 +00002856MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002857 if (MD == 0)
2858 return 0;
Sebastian Redl50e26582010-09-15 19:54:06 +00002859
2860 MacroID &ID = MacroDefinitions[MD];
Douglas Gregoraae92242010-03-19 21:51:54 +00002861 if (ID == 0)
Douglas Gregor91096292010-10-02 19:29:26 +00002862 ID = NextMacroID++;
Douglas Gregoraae92242010-03-19 21:51:54 +00002863 return ID;
2864}
2865
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002866void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00002867 Record.push_back(getSelectorRef(SelRef));
2868}
2869
Sebastian Redl539c5062010-08-18 23:57:32 +00002870SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00002871 if (Sel.getAsOpaquePtr() == 0) {
2872 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00002873 }
2874
Sebastian Redl539c5062010-08-18 23:57:32 +00002875 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00002876 if (SID == 0 && Chain) {
2877 // This might trigger a ReadSelector callback, which will set the ID for
2878 // this selector.
2879 Chain->LoadSelector(Sel);
2880 }
Steve Naroff2ddea052009-04-23 10:39:46 +00002881 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00002882 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00002883 }
Sebastian Redl834bb972010-08-04 17:20:04 +00002884 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00002885}
2886
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002887void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00002888 AddDeclRef(Temp->getDestructor(), Record);
2889}
2890
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002891void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
2892 CXXBaseSpecifier const *BasesEnd,
2893 RecordDataImpl &Record) {
2894 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
2895 CXXBaseSpecifiersToWrite.push_back(
2896 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
2897 Bases, BasesEnd));
2898 Record.push_back(NextCXXBaseSpecifiersID++);
2899}
2900
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002901void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002902 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002903 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002904 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00002905 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002906 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00002907 break;
2908 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002909 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00002910 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002911 case TemplateArgument::Template:
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002912 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2913 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002914 break;
2915 case TemplateArgument::TemplateExpansion:
2916 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2917 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00002918 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002919 break;
John McCall0ad16662009-10-29 08:12:44 +00002920 case TemplateArgument::Null:
2921 case TemplateArgument::Integral:
2922 case TemplateArgument::Declaration:
2923 case TemplateArgument::Pack:
2924 break;
2925 }
2926}
2927
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002928void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002929 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002930 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002931
2932 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
2933 bool InfoHasSameExpr
2934 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
2935 Record.push_back(InfoHasSameExpr);
2936 if (InfoHasSameExpr)
2937 return; // Avoid storing the same expr twice.
2938 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002939 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
2940 Record);
2941}
2942
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002943void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00002944 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00002945 AddTypeRef(QualType(), Record);
2946 return;
2947 }
2948
John McCallbcd03502009-12-07 02:54:59 +00002949 AddTypeRef(TInfo->getType(), Record);
John McCall8f115c62009-10-16 21:56:05 +00002950 TypeLocWriter TLW(*this, Record);
John McCallbcd03502009-12-07 02:54:59 +00002951 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002952 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00002953}
2954
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002955void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00002956 Record.push_back(GetOrCreateTypeID(T));
2957}
2958
2959TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00002960 return MakeTypeID(T,
2961 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
2962}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002963
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002964TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00002965 return MakeTypeID(T,
2966 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002967}
2968
2969TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
2970 if (T.isNull())
2971 return TypeIdx();
2972 assert(!T.getLocalFastQualifiers());
2973
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002974 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002975 if (Idx.getIndex() == 0) {
Douglas Gregor1970d882009-04-26 03:49:13 +00002976 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00002977 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002978 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002979 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00002980 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002981 return Idx;
2982}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002983
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002984TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002985 if (T.isNull())
2986 return TypeIdx();
2987 assert(!T.getLocalFastQualifiers());
2988
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002989 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
2990 assert(I != TypeIdxs.end() && "Type not emitted!");
2991 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002992}
2993
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002994void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002995 Record.push_back(GetDeclRef(D));
2996}
2997
Sebastian Redl539c5062010-08-18 23:57:32 +00002998DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002999 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003000 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003001 }
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003002 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00003003 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00003004 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003005 // We haven't seen this declaration before. Give it a new ID and
3006 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00003007 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003008 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003009 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3010 // We don't add it to the replacement collection here, because we don't
3011 // have the offset yet.
3012 DeclTypesToEmit.push(const_cast<Decl *>(D));
3013 // Reset the flag, so that we don't add this decl multiple times.
3014 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003015 }
3016
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003017 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003018}
3019
Sebastian Redl539c5062010-08-18 23:57:32 +00003020DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00003021 if (D == 0)
3022 return 0;
3023
3024 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3025 return DeclIDs[D];
3026}
3027
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003028void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00003029 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003030 Record.push_back(Name.getNameKind());
3031 switch (Name.getNameKind()) {
3032 case DeclarationName::Identifier:
3033 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3034 break;
3035
3036 case DeclarationName::ObjCZeroArgSelector:
3037 case DeclarationName::ObjCOneArgSelector:
3038 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00003039 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003040 break;
3041
3042 case DeclarationName::CXXConstructorName:
3043 case DeclarationName::CXXDestructorName:
3044 case DeclarationName::CXXConversionFunctionName:
3045 AddTypeRef(Name.getCXXNameType(), Record);
3046 break;
3047
3048 case DeclarationName::CXXOperatorName:
3049 Record.push_back(Name.getCXXOverloadedOperator());
3050 break;
3051
Alexis Hunt3d221f22009-11-29 07:34:05 +00003052 case DeclarationName::CXXLiteralOperatorName:
3053 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3054 break;
3055
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003056 case DeclarationName::CXXUsingDirective:
3057 // No extra data to emit
3058 break;
3059 }
3060}
Chris Lattnerca025db2010-05-07 21:43:38 +00003061
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003062void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003063 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003064 switch (Name.getNameKind()) {
3065 case DeclarationName::CXXConstructorName:
3066 case DeclarationName::CXXDestructorName:
3067 case DeclarationName::CXXConversionFunctionName:
3068 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3069 break;
3070
3071 case DeclarationName::CXXOperatorName:
3072 AddSourceLocation(
3073 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3074 Record);
3075 AddSourceLocation(
3076 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3077 Record);
3078 break;
3079
3080 case DeclarationName::CXXLiteralOperatorName:
3081 AddSourceLocation(
3082 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3083 Record);
3084 break;
3085
3086 case DeclarationName::Identifier:
3087 case DeclarationName::ObjCZeroArgSelector:
3088 case DeclarationName::ObjCOneArgSelector:
3089 case DeclarationName::ObjCMultiArgSelector:
3090 case DeclarationName::CXXUsingDirective:
3091 break;
3092 }
3093}
3094
3095void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003096 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003097 AddDeclarationName(NameInfo.getName(), Record);
3098 AddSourceLocation(NameInfo.getLoc(), Record);
3099 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3100}
3101
3102void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003103 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003104 AddNestedNameSpecifier(Info.NNS, Record);
3105 AddSourceRange(Info.NNSRange, Record);
3106 Record.push_back(Info.NumTemplParamLists);
3107 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3108 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3109}
3110
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003111void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003112 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003113 // Nested name specifiers usually aren't too long. I think that 8 would
3114 // typically accomodate the vast majority.
3115 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3116
3117 // Push each of the NNS's onto a stack for serialization in reverse order.
3118 while (NNS) {
3119 NestedNames.push_back(NNS);
3120 NNS = NNS->getPrefix();
3121 }
3122
3123 Record.push_back(NestedNames.size());
3124 while(!NestedNames.empty()) {
3125 NNS = NestedNames.pop_back_val();
3126 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3127 Record.push_back(Kind);
3128 switch (Kind) {
3129 case NestedNameSpecifier::Identifier:
3130 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3131 break;
3132
3133 case NestedNameSpecifier::Namespace:
3134 AddDeclRef(NNS->getAsNamespace(), Record);
3135 break;
3136
3137 case NestedNameSpecifier::TypeSpec:
3138 case NestedNameSpecifier::TypeSpecWithTemplate:
3139 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3140 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3141 break;
3142
3143 case NestedNameSpecifier::Global:
3144 // Don't need to write an associated value.
3145 break;
3146 }
3147 }
3148}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003149
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003150void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003151 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003152 Record.push_back(Kind);
3153 switch (Kind) {
3154 case TemplateName::Template:
3155 AddDeclRef(Name.getAsTemplateDecl(), Record);
3156 break;
3157
3158 case TemplateName::OverloadedTemplate: {
3159 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3160 Record.push_back(OvT->size());
3161 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3162 I != E; ++I)
3163 AddDeclRef(*I, Record);
3164 break;
3165 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003166
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003167 case TemplateName::QualifiedTemplate: {
3168 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3169 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3170 Record.push_back(QualT->hasTemplateKeyword());
3171 AddDeclRef(QualT->getTemplateDecl(), Record);
3172 break;
3173 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003174
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003175 case TemplateName::DependentTemplate: {
3176 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3177 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3178 Record.push_back(DepT->isIdentifier());
3179 if (DepT->isIdentifier())
3180 AddIdentifierRef(DepT->getIdentifier(), Record);
3181 else
3182 Record.push_back(DepT->getOperator());
3183 break;
3184 }
3185 }
3186}
3187
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003188void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003189 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003190 Record.push_back(Arg.getKind());
3191 switch (Arg.getKind()) {
3192 case TemplateArgument::Null:
3193 break;
3194 case TemplateArgument::Type:
3195 AddTypeRef(Arg.getAsType(), Record);
3196 break;
3197 case TemplateArgument::Declaration:
3198 AddDeclRef(Arg.getAsDecl(), Record);
3199 break;
3200 case TemplateArgument::Integral:
3201 AddAPSInt(*Arg.getAsIntegral(), Record);
3202 AddTypeRef(Arg.getIntegralType(), Record);
3203 break;
3204 case TemplateArgument::Template:
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003205 case TemplateArgument::TemplateExpansion:
3206 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003207 break;
3208 case TemplateArgument::Expression:
3209 AddStmt(Arg.getAsExpr());
3210 break;
3211 case TemplateArgument::Pack:
3212 Record.push_back(Arg.pack_size());
3213 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3214 I != E; ++I)
3215 AddTemplateArgument(*I, Record);
3216 break;
3217 }
3218}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003219
3220void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003221ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003222 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003223 assert(TemplateParams && "No TemplateParams!");
3224 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3225 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3226 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3227 Record.push_back(TemplateParams->size());
3228 for (TemplateParameterList::const_iterator
3229 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3230 P != PEnd; ++P)
3231 AddDeclRef(*P, Record);
3232}
3233
3234/// \brief Emit a template argument list.
3235void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003236ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003237 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003238 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003239 Record.push_back(TemplateArgs->size());
3240 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003241 AddTemplateArgument(TemplateArgs->get(i), Record);
3242}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003243
3244
3245void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003246ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003247 Record.push_back(Set.size());
3248 for (UnresolvedSetImpl::const_iterator
3249 I = Set.begin(), E = Set.end(); I != E; ++I) {
3250 AddDeclRef(I.getDecl(), Record);
3251 Record.push_back(I.getAccess());
3252 }
3253}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003254
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003255void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003256 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003257 Record.push_back(Base.isVirtual());
3258 Record.push_back(Base.isBaseOfClass());
3259 Record.push_back(Base.getAccessSpecifierAsWritten());
Nick Lewycky19b9f952010-07-26 16:56:01 +00003260 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003261 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00003262 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3263 : SourceLocation(),
3264 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003265}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003266
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003267void ASTWriter::FlushCXXBaseSpecifiers() {
3268 RecordData Record;
3269 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3270 Record.clear();
3271
3272 // Record the offset of this base-specifier set.
3273 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3274 if (Index == CXXBaseSpecifiersOffsets.size())
3275 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3276 else {
3277 if (Index > CXXBaseSpecifiersOffsets.size())
3278 CXXBaseSpecifiersOffsets.resize(Index + 1);
3279 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3280 }
3281
3282 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3283 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3284 Record.push_back(BEnd - B);
3285 for (; B != BEnd; ++B)
3286 AddCXXBaseSpecifier(*B, Record);
3287 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00003288
3289 // Flush any expressions that were written as part of the base specifiers.
3290 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003291 }
3292
3293 CXXBaseSpecifiersToWrite.clear();
3294}
3295
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003296void ASTWriter::AddCXXBaseOrMemberInitializers(
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003297 const CXXBaseOrMemberInitializer * const *BaseOrMembers,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003298 unsigned NumBaseOrMembers, RecordDataImpl &Record) {
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003299 Record.push_back(NumBaseOrMembers);
3300 for (unsigned i=0; i != NumBaseOrMembers; ++i) {
3301 const CXXBaseOrMemberInitializer *Init = BaseOrMembers[i];
3302
3303 Record.push_back(Init->isBaseInitializer());
3304 if (Init->isBaseInitializer()) {
3305 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3306 Record.push_back(Init->isBaseVirtual());
3307 } else {
Francois Pichetd583da02010-12-04 09:14:42 +00003308 Record.push_back(Init->isIndirectMemberInitializer());
3309 if (Init->isIndirectMemberInitializer())
3310 AddDeclRef(Init->getIndirectMember(), Record);
3311 else
3312 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003313 }
Francois Pichetd583da02010-12-04 09:14:42 +00003314
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003315 AddSourceLocation(Init->getMemberLocation(), Record);
3316 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003317 AddSourceLocation(Init->getLParenLoc(), Record);
3318 AddSourceLocation(Init->getRParenLoc(), Record);
3319 Record.push_back(Init->isWritten());
3320 if (Init->isWritten()) {
3321 Record.push_back(Init->getSourceOrder());
3322 } else {
3323 Record.push_back(Init->getNumArrayIndices());
3324 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3325 AddDeclRef(Init->getArrayIndex(i), Record);
3326 }
3327 }
3328}
3329
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003330void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3331 assert(D->DefinitionData);
3332 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3333 Record.push_back(Data.UserDeclaredConstructor);
3334 Record.push_back(Data.UserDeclaredCopyConstructor);
3335 Record.push_back(Data.UserDeclaredCopyAssignment);
3336 Record.push_back(Data.UserDeclaredDestructor);
3337 Record.push_back(Data.Aggregate);
3338 Record.push_back(Data.PlainOldData);
3339 Record.push_back(Data.Empty);
3340 Record.push_back(Data.Polymorphic);
3341 Record.push_back(Data.Abstract);
3342 Record.push_back(Data.HasTrivialConstructor);
3343 Record.push_back(Data.HasTrivialCopyConstructor);
3344 Record.push_back(Data.HasTrivialCopyAssignment);
3345 Record.push_back(Data.HasTrivialDestructor);
3346 Record.push_back(Data.ComputedVisibleConversions);
3347 Record.push_back(Data.DeclaredDefaultConstructor);
3348 Record.push_back(Data.DeclaredCopyConstructor);
3349 Record.push_back(Data.DeclaredCopyAssignment);
3350 Record.push_back(Data.DeclaredDestructor);
3351
3352 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003353 if (Data.NumBases > 0)
3354 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3355 Record);
3356
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003357 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3358 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003359 if (Data.NumVBases > 0)
3360 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3361 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003362
3363 AddUnresolvedSet(Data.Conversions, Record);
3364 AddUnresolvedSet(Data.VisibleConversions, Record);
3365 // Data.Definition is the owning decl, no need to write it.
3366 AddDeclRef(Data.FirstFriend, Record);
3367}
3368
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003369void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00003370 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003371 assert(!Chain && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00003372 assert(FirstDeclID == NextDeclID &&
3373 FirstTypeID == NextTypeID &&
3374 FirstIdentID == NextIdentID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00003375 FirstSelectorID == NextSelectorID &&
Douglas Gregor91096292010-10-02 19:29:26 +00003376 FirstMacroID == NextMacroID &&
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003377 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00003378 "Setting chain after writing has started.");
3379 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003380
3381 FirstDeclID += Chain->getTotalNumDecls();
3382 FirstTypeID += Chain->getTotalNumTypes();
3383 FirstIdentID += Chain->getTotalNumIdentifiers();
3384 FirstSelectorID += Chain->getTotalNumSelectors();
3385 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003386 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003387 NextDeclID = FirstDeclID;
3388 NextTypeID = FirstTypeID;
3389 NextIdentID = FirstIdentID;
3390 NextSelectorID = FirstSelectorID;
3391 NextMacroID = FirstMacroID;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003392 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00003393}
3394
Sebastian Redl539c5062010-08-18 23:57:32 +00003395void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00003396 IdentifierIDs[II] = ID;
3397}
3398
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003399void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003400 // Always take the highest-numbered type index. This copes with an interesting
3401 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003402 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003403 // keep the higher-numbered entry so that we can properly write it out to
3404 // the AST file.
3405 TypeIdx &StoredIdx = TypeIdxs[T];
3406 if (Idx.getIndex() >= StoredIdx.getIndex())
3407 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003408}
3409
Sebastian Redl539c5062010-08-18 23:57:32 +00003410void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003411 DeclIDs[D] = ID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003412}
Sebastian Redl834bb972010-08-04 17:20:04 +00003413
Sebastian Redl539c5062010-08-18 23:57:32 +00003414void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003415 SelectorIDs[S] = ID;
3416}
Douglas Gregor91096292010-10-02 19:29:26 +00003417
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003418void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00003419 MacroDefinition *MD) {
3420 MacroDefinitions[MD] = ID;
3421}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003422
3423void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3424 assert(D->isDefinition());
3425 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3426 // We are interested when a PCH decl is modified.
3427 if (RD->getPCHLevel() > 0) {
3428 // A forward reference was mutated into a definition. Rewrite it.
3429 // FIXME: This happens during template instantiation, should we
3430 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003431 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003432 }
3433
3434 for (CXXRecordDecl::redecl_iterator
3435 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3436 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3437 if (Redecl == RD)
3438 continue;
3439
3440 // We are interested when a PCH decl is modified.
3441 if (Redecl->getPCHLevel() > 0) {
3442 UpdateRecord &Record = DeclUpdates[Redecl];
3443 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3444 assert(Redecl->DefinitionData);
3445 assert(Redecl->DefinitionData->Definition == D);
3446 AddDeclRef(D, Record); // the DefinitionDecl
3447 }
3448 }
3449 }
3450}
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003451void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3452 // TU and namespaces are handled elsewhere.
3453 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3454 return;
3455
3456 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3457 return; // Not a source decl added to a DeclContext from PCH.
3458
3459 AddUpdatedDeclContext(DC);
3460}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003461
3462void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3463 assert(D->isImplicit());
3464 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3465 return; // Not a source member added to a class from PCH.
3466 if (!isa<CXXMethodDecl>(D))
3467 return; // We are interested in lazily declared implicit methods.
3468
3469 // A decl coming from PCH was modified.
3470 assert(RD->isDefinition());
3471 UpdateRecord &Record = DeclUpdates[RD];
3472 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3473 AddDeclRef(D, Record);
3474}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003475
3476void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3477 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00003478 // The specializations set is kept in the canonical template.
3479 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003480 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3481 return; // Not a source specialization added to a template from PCH.
3482
3483 UpdateRecord &Record = DeclUpdates[TD];
3484 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3485 AddDeclRef(D, Record);
3486}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003487
3488ASTSerializationListener::~ASTSerializationListener() { }