blob: 52415b0d516a610770845bb6220609d75935b5bd [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.
Fariborz Jahanian13f3b2f2011-01-07 18:59:25 +0000885 Record.push_back(LangOpts.AppleKext); // Apple's kernel extensions ABI
Ted Kremenek1d56c9e2010-12-23 21:35:43 +0000886 Record.push_back(LangOpts.ObjCDefaultSynthProperties); // Objective-C auto-synthesized
887 // properties enabled.
Fariborz Jahanian62c56022010-04-22 21:01:59 +0000888 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump11289f42009-09-09 15:08:12 +0000889
Douglas Gregor55abb232009-04-10 20:39:37 +0000890 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor55abb232009-04-10 20:39:37 +0000891 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
892 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanf2911662009-06-25 23:01:11 +0000893 Record.push_back(LangOpts.AltiVec);
Douglas Gregor55abb232009-04-10 20:39:37 +0000894 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar925152c2010-02-10 18:48:44 +0000895 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor55abb232009-04-10 20:39:37 +0000896
897 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
898 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
899 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
900
Chris Lattner258172e2009-04-27 07:35:58 +0000901 // Whether static initializers are protected by locks.
902 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregorb3286fe2009-09-03 14:36:33 +0000903 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor55abb232009-04-10 20:39:37 +0000904 Record.push_back(LangOpts.Blocks); // block extension to C
905 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
906 // they are unused.
907 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
908 // (modulo the platform support).
909
Chris Lattner51924e512010-06-26 21:25:03 +0000910 Record.push_back(LangOpts.getSignedOverflowBehavior());
911 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor55abb232009-04-10 20:39:37 +0000912
913 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump11289f42009-09-09 15:08:12 +0000914 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor55abb232009-04-10 20:39:37 +0000915 // defined.
916 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
917 // opposed to __DYNAMIC__).
918 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
919
920 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
921 // used (instead of C99 semantics).
922 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlsson5879fbd2009-05-13 19:49:53 +0000923 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
924 // be enabled.
Eli Friedman9ffd4a92009-06-05 07:05:05 +0000925 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
926 // unsigned type
John Thompsoned4e2952009-11-05 20:14:16 +0000927 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor55abb232009-04-10 20:39:37 +0000928 Record.push_back(LangOpts.getGCMode());
929 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbar143021e2009-09-21 04:16:19 +0000930 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor55abb232009-04-10 20:39:37 +0000931 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanf2911662009-06-25 23:01:11 +0000932 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne546d0792010-12-01 19:14:57 +0000933 Record.push_back(LangOpts.CUDA);
Mike Stumpd9546382009-12-12 01:27:46 +0000934 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson9cedbef2009-08-22 22:30:33 +0000935 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregor8ed0c0b2010-07-09 17:35:33 +0000936 Record.push_back(LangOpts.SpellChecking);
Sebastian Redl539c5062010-08-18 23:57:32 +0000937 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor55abb232009-04-10 20:39:37 +0000938}
939
Douglas Gregora7f71a92009-04-10 03:52:48 +0000940//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +0000941// stat cache Serialization
942//===----------------------------------------------------------------------===//
943
944namespace {
945// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000946class ASTStatCacheTrait {
Douglas Gregorc5046832009-04-27 18:38:38 +0000947public:
948 typedef const char * key_type;
949 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +0000950
Chris Lattner2a6fa472010-11-23 19:28:12 +0000951 typedef struct stat data_type;
952 typedef const data_type &data_type_ref;
Douglas Gregorc5046832009-04-27 18:38:38 +0000953
954 static unsigned ComputeHash(const char *path) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +0000955 return llvm::HashString(path);
Douglas Gregorc5046832009-04-27 18:38:38 +0000956 }
Mike Stump11289f42009-09-09 15:08:12 +0000957
958 std::pair<unsigned,unsigned>
Douglas Gregorc5046832009-04-27 18:38:38 +0000959 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
960 data_type_ref Data) {
961 unsigned StrLen = strlen(path);
962 clang::io::Emit16(Out, StrLen);
Chris Lattner2a6fa472010-11-23 19:28:12 +0000963 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregorc5046832009-04-27 18:38:38 +0000964 clang::io::Emit8(Out, DataLen);
965 return std::make_pair(StrLen + 1, DataLen);
966 }
Mike Stump11289f42009-09-09 15:08:12 +0000967
Douglas Gregorc5046832009-04-27 18:38:38 +0000968 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
969 Out.write(path, KeyLen);
970 }
Mike Stump11289f42009-09-09 15:08:12 +0000971
Chris Lattner2a6fa472010-11-23 19:28:12 +0000972 void EmitData(llvm::raw_ostream &Out, key_type_ref,
Douglas Gregorc5046832009-04-27 18:38:38 +0000973 data_type_ref Data, unsigned DataLen) {
974 using namespace clang::io;
975 uint64_t Start = Out.tell(); (void)Start;
Mike Stump11289f42009-09-09 15:08:12 +0000976
Chris Lattner2a6fa472010-11-23 19:28:12 +0000977 Emit32(Out, (uint32_t) Data.st_ino);
978 Emit32(Out, (uint32_t) Data.st_dev);
979 Emit16(Out, (uint16_t) Data.st_mode);
980 Emit64(Out, (uint64_t) Data.st_mtime);
981 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregorc5046832009-04-27 18:38:38 +0000982
983 assert(Out.tell() - Start == DataLen && "Wrong data length");
984 }
985};
986} // end anonymous namespace
987
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000988/// \brief Write the stat() system call cache to the AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +0000989void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregorc5046832009-04-27 18:38:38 +0000990 // Build the on-disk hash table containing information about every
991 // stat() call.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +0000992 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregorc5046832009-04-27 18:38:38 +0000993 unsigned NumStatEntries = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000994 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregorc5046832009-04-27 18:38:38 +0000995 StatEnd = StatCalls.end();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000996 Stat != StatEnd; ++Stat, ++NumStatEntries) {
997 const char *Filename = Stat->first();
Douglas Gregor0086a5a2009-07-07 00:12:59 +0000998 Generator.insert(Filename, Stat->second);
999 }
Mike Stump11289f42009-09-09 15:08:12 +00001000
Douglas Gregorc5046832009-04-27 18:38:38 +00001001 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001002 llvm::SmallString<4096> StatCacheData;
Douglas Gregorc5046832009-04-27 18:38:38 +00001003 uint32_t BucketOffset;
1004 {
1005 llvm::raw_svector_ostream Out(StatCacheData);
1006 // Make sure that no bucket is at offset 0
1007 clang::io::Emit32(Out, 0);
1008 BucketOffset = Generator.Emit(Out);
1009 }
1010
1011 // Create a blob abbreviation
1012 using namespace llvm;
1013 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001014 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregorc5046832009-04-27 18:38:38 +00001015 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1016 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1017 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1018 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1019
1020 // Write the stat cache
1021 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001022 Record.push_back(STAT_CACHE);
Douglas Gregorc5046832009-04-27 18:38:38 +00001023 Record.push_back(BucketOffset);
1024 Record.push_back(NumStatEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001025 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregorc5046832009-04-27 18:38:38 +00001026}
1027
1028//===----------------------------------------------------------------------===//
Douglas Gregora7f71a92009-04-10 03:52:48 +00001029// Source Manager Serialization
1030//===----------------------------------------------------------------------===//
1031
1032/// \brief Create an abbreviation for the SLocEntry that refers to a
1033/// file.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001034static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001035 using namespace llvm;
1036 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001037 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001038 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1039 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1040 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1041 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001042 // FileEntry fields.
1043 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1044 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001045 // HeaderFileInfo fields.
1046 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
1047 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
1048 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
1049 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregora7f71a92009-04-10 03:52:48 +00001050 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregor8f45df52009-04-16 22:23:12 +00001051 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001052}
1053
1054/// \brief Create an abbreviation for the SLocEntry that refers to a
1055/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001056static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001057 using namespace llvm;
1058 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001059 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001060 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1061 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1062 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1063 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1064 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001065 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001066}
1067
1068/// \brief Create an abbreviation for the SLocEntry that refers to a
1069/// buffer's blob.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001070static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001071 using namespace llvm;
1072 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001073 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001074 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregor8f45df52009-04-16 22:23:12 +00001075 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001076}
1077
1078/// \brief Create an abbreviation for the SLocEntry that refers to an
1079/// buffer.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001080static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregora7f71a92009-04-10 03:52:48 +00001081 using namespace llvm;
1082 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001083 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregora7f71a92009-04-10 03:52:48 +00001084 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1085 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1086 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1087 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor83243272009-04-15 18:05:10 +00001088 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregor8f45df52009-04-16 22:23:12 +00001089 return Stream.EmitAbbrev(Abbrev);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001090}
1091
1092/// \brief Writes the block containing the serialized form of the
1093/// source manager.
1094///
1095/// TODO: We should probably use an on-disk hash table (stored in a
1096/// blob), indexed based on the file name, so that we only create
1097/// entries for files that we actually need. In the common case (no
1098/// errors), we probably won't have to create file entries for any of
1099/// the files in the AST.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001100void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001101 const Preprocessor &PP,
1102 const char *isysroot) {
Douglas Gregor258ae542009-04-27 06:38:32 +00001103 RecordData Record;
1104
Chris Lattner0910e3b2009-04-10 17:16:57 +00001105 // Enter the source manager block.
Sebastian Redl539c5062010-08-18 23:57:32 +00001106 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001107
1108 // Abbreviations for the various kinds of source-location entries.
Chris Lattnerc4976c732009-04-27 19:03:22 +00001109 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1110 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1111 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1112 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001113
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001114 // Write the line table.
1115 if (SourceMgr.hasLineTable()) {
1116 LineTableInfo &LineTable = SourceMgr.getLineTable();
1117
1118 // Emit the file names
1119 Record.push_back(LineTable.getNumFilenames());
1120 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1121 // Emit the file name
1122 const char *Filename = LineTable.getFilename(I);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001123 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001124 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1125 Record.push_back(FilenameLen);
1126 if (FilenameLen)
1127 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1128 }
Mike Stump11289f42009-09-09 15:08:12 +00001129
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001130 // Emit the line entries
1131 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1132 L != LEnd; ++L) {
1133 // Emit the file ID
1134 Record.push_back(L->first);
Mike Stump11289f42009-09-09 15:08:12 +00001135
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001136 // Emit the line entries
1137 Record.push_back(L->second.size());
Mike Stump11289f42009-09-09 15:08:12 +00001138 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001139 LEEnd = L->second.end();
1140 LE != LEEnd; ++LE) {
1141 Record.push_back(LE->FileOffset);
1142 Record.push_back(LE->LineNo);
1143 Record.push_back(LE->FilenameID);
1144 Record.push_back((unsigned)LE->FileKind);
1145 Record.push_back(LE->IncludeOffset);
1146 }
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001147 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001148 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregor4c7626e2009-04-13 16:31:14 +00001149 }
1150
Douglas Gregor258ae542009-04-27 06:38:32 +00001151 // Write out the source location entry table. We skip the first
1152 // entry, which is always the same dummy entry.
Chris Lattner12d61d32009-04-27 19:01:47 +00001153 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor258ae542009-04-27 06:38:32 +00001154 RecordData PreloadSLocs;
Sebastian Redl5c415f32010-07-22 17:01:13 +00001155 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1156 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1157 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1158 I != N; ++I) {
Douglas Gregor8655e882009-10-16 22:46:09 +00001159 // Get this source location entry.
1160 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00001161
Douglas Gregor258ae542009-04-27 06:38:32 +00001162 // Record the offset of this source-location entry.
1163 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1164
1165 // Figure out which record code to use.
1166 unsigned Code;
1167 if (SLoc->isFile()) {
1168 if (SLoc->getFile().getContentCache()->Entry)
Sebastian Redl539c5062010-08-18 23:57:32 +00001169 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001170 else
Sebastian Redl539c5062010-08-18 23:57:32 +00001171 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001172 } else
Sebastian Redl539c5062010-08-18 23:57:32 +00001173 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor258ae542009-04-27 06:38:32 +00001174 Record.clear();
1175 Record.push_back(Code);
1176
1177 Record.push_back(SLoc->getOffset());
1178 if (SLoc->isFile()) {
1179 const SrcMgr::FileInfo &File = SLoc->getFile();
1180 Record.push_back(File.getIncludeLoc().getRawEncoding());
1181 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1182 Record.push_back(File.hasLineDirectives());
1183
1184 const SrcMgr::ContentCache *Content = File.getContentCache();
1185 if (Content->Entry) {
1186 // The source location entry is a file. The blob associated
1187 // with this entry is the file name.
Mike Stump11289f42009-09-09 15:08:12 +00001188
Douglas Gregorb41ca8f2010-03-21 22:49:54 +00001189 // Emit size/modification time for this file.
1190 Record.push_back(Content->Entry->getSize());
1191 Record.push_back(Content->Entry->getModificationTime());
1192
Douglas Gregor5712ebc2010-03-16 16:35:32 +00001193 // Emit header-search information associated with this file.
1194 HeaderFileInfo HFI;
1195 HeaderSearch &HS = PP.getHeaderSearchInfo();
1196 if (Content->Entry->getUID() < HS.header_file_size())
1197 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1198 Record.push_back(HFI.isImport);
1199 Record.push_back(HFI.DirInfo);
1200 Record.push_back(HFI.NumIncludes);
1201 AddIdentifierRef(HFI.ControllingMacro, Record);
1202
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001203 // Turn the file name into an absolute path, if it isn't already.
1204 const char *Filename = Content->Entry->getName();
Michael J. Spencer740857f2010-12-21 16:45:57 +00001205 llvm::SmallString<128> FilePath(Filename);
1206 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamd16d38c2010-03-14 07:15:57 +00001207 Filename = FilePath.c_str();
Mike Stump11289f42009-09-09 15:08:12 +00001208
Douglas Gregor0086a5a2009-07-07 00:12:59 +00001209 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001210 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor258ae542009-04-27 06:38:32 +00001211
1212 // FIXME: For now, preload all file source locations, so that
1213 // we get the appropriate File entries in the reader. This is
1214 // a temporary measure.
Sebastian Redl5c415f32010-07-22 17:01:13 +00001215 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001216 } else {
1217 // The source location entry is a buffer. The blob associated
1218 // with this entry contains the contents of the buffer.
1219
1220 // We add one to the size so that we capture the trailing NULL
1221 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1222 // the reader side).
Douglas Gregor874cc622010-03-16 00:35:39 +00001223 const llvm::MemoryBuffer *Buffer
Chris Lattnerfb24a3a2010-04-20 20:35:58 +00001224 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor258ae542009-04-27 06:38:32 +00001225 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbar8100d012009-08-24 09:31:37 +00001226 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1227 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001228 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001229 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor258ae542009-04-27 06:38:32 +00001230 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbar8100d012009-08-24 09:31:37 +00001231 llvm::StringRef(Buffer->getBufferStart(),
1232 Buffer->getBufferSize() + 1));
Douglas Gregor258ae542009-04-27 06:38:32 +00001233
1234 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl5c415f32010-07-22 17:01:13 +00001235 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor258ae542009-04-27 06:38:32 +00001236 }
1237 } else {
1238 // The source location entry is an instantiation.
1239 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1240 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1241 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1242 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1243
1244 // Compute the token length for this macro expansion.
1245 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregor8655e882009-10-16 22:46:09 +00001246 if (I + 1 != N)
1247 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor258ae542009-04-27 06:38:32 +00001248 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1249 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1250 }
1251 }
1252
Douglas Gregor8f45df52009-04-16 22:23:12 +00001253 Stream.ExitBlock();
Douglas Gregor258ae542009-04-27 06:38:32 +00001254
1255 if (SLocEntryOffsets.empty())
1256 return;
1257
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001258 // Write the source-location offsets table into the AST block. This
Douglas Gregor258ae542009-04-27 06:38:32 +00001259 // table is used for lazily loading source-location information.
1260 using namespace llvm;
1261 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001262 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor258ae542009-04-27 06:38:32 +00001263 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1264 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1265 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1266 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump11289f42009-09-09 15:08:12 +00001267
Douglas Gregor258ae542009-04-27 06:38:32 +00001268 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001269 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor258ae542009-04-27 06:38:32 +00001270 Record.push_back(SLocEntryOffsets.size());
Sebastian Redlc1d035f2010-09-22 20:19:08 +00001271 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1272 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Douglas Gregor258ae542009-04-27 06:38:32 +00001273 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001274 (const char *)data(SLocEntryOffsets),
Chris Lattner12d61d32009-04-27 19:01:47 +00001275 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor258ae542009-04-27 06:38:32 +00001276
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001277 // Write the source location entry preloads array, telling the AST
Douglas Gregor258ae542009-04-27 06:38:32 +00001278 // reader which source locations entries it should load eagerly.
Sebastian Redl539c5062010-08-18 23:57:32 +00001279 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregora7f71a92009-04-10 03:52:48 +00001280}
1281
Douglas Gregorc5046832009-04-27 18:38:38 +00001282//===----------------------------------------------------------------------===//
1283// Preprocessor Serialization
1284//===----------------------------------------------------------------------===//
1285
Chris Lattnereeffaef2009-04-10 17:15:23 +00001286/// \brief Writes the block containing the serialized form of the
1287/// preprocessor.
1288///
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001289void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001290 RecordData Record;
Chris Lattner0910e3b2009-04-10 17:16:57 +00001291
Chris Lattner0af3ba12009-04-13 01:29:17 +00001292 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1293 if (PP.getCounterValue() != 0) {
1294 Record.push_back(PP.getCounterValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001295 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattner0af3ba12009-04-13 01:29:17 +00001296 Record.clear();
Douglas Gregoreda6a892009-04-26 00:07:37 +00001297 }
1298
1299 // Enter the preprocessor block.
Douglas Gregor796d76a2010-10-20 22:00:55 +00001300 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump11289f42009-09-09 15:08:12 +00001301
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001302 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregoreda6a892009-04-26 00:07:37 +00001303 // FIXME: use diagnostics subsystem for localization etc.
1304 if (PP.SawDateOrTime())
1305 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump11289f42009-09-09 15:08:12 +00001306
Douglas Gregor796d76a2010-10-20 22:00:55 +00001307
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001308 // Loop over all the macro definitions that are live at the end of the file,
1309 // emitting each to the PP section.
Douglas Gregoraae92242010-03-19 21:51:54 +00001310 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Douglas Gregor796d76a2010-10-20 22:00:55 +00001311 unsigned InclusionAbbrev = 0;
1312 if (PPRec) {
1313 using namespace llvm;
1314 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1315 Abbrev->Add(BitCodeAbbrevOp(PP_INCLUSION_DIRECTIVE));
1316 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1317 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1318 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1319 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1320 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1321 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1322 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001323 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor796d76a2010-10-20 22:00:55 +00001324 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001325
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001326 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1327 I != E; ++I) {
Chris Lattner34321bc2009-04-10 21:41:48 +00001328 // FIXME: This emits macros in hash table order, we should do it in a stable
1329 // order so that output is reproducible.
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001330 MacroInfo *MI = I->second;
1331
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001332 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001333 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001334 // Also skip macros from a AST file if we're chaining.
Douglas Gregoreb114da2010-10-01 01:03:07 +00001335
1336 // FIXME: There is a (probably minor) optimization we could do here, if
1337 // the macro comes from the original PCH but the identifier comes from a
1338 // chained PCH, by storing the offset into the original PCH rather than
1339 // writing the macro definition a second time.
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001340 if (MI->isBuiltinMacro() ||
Douglas Gregoreb114da2010-10-01 01:03:07 +00001341 (Chain && I->first->isFromAST() && MI->isFromAST()))
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001342 continue;
1343
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001344 AddIdentifierRef(I->first, Record);
Douglas Gregorc3366a52009-04-21 23:56:24 +00001345 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001346 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1347 Record.push_back(MI->isUsed());
Mike Stump11289f42009-09-09 15:08:12 +00001348
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001349 unsigned Code;
1350 if (MI->isObjectLike()) {
Sebastian Redl539c5062010-08-18 23:57:32 +00001351 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001352 } else {
Sebastian Redl539c5062010-08-18 23:57:32 +00001353 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump11289f42009-09-09 15:08:12 +00001354
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001355 Record.push_back(MI->isC99Varargs());
1356 Record.push_back(MI->isGNUVarargs());
1357 Record.push_back(MI->getNumArgs());
1358 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1359 I != E; ++I)
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001360 AddIdentifierRef(*I, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001361 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001362
Douglas Gregoraae92242010-03-19 21:51:54 +00001363 // If we have a detailed preprocessing record, record the macro definition
1364 // ID that corresponds to this macro.
1365 if (PPRec)
1366 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001367
Douglas Gregor8f45df52009-04-16 22:23:12 +00001368 Stream.EmitRecord(Code, Record);
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001369 Record.clear();
1370
Chris Lattner2199f5b2009-04-10 18:08:30 +00001371 // Emit the tokens array.
1372 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1373 // Note that we know that the preprocessor does not have any annotation
1374 // tokens in it because they are created by the parser, and thus can't be
1375 // in a macro definition.
1376 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump11289f42009-09-09 15:08:12 +00001377
Chris Lattner2199f5b2009-04-10 18:08:30 +00001378 Record.push_back(Tok.getLocation().getRawEncoding());
1379 Record.push_back(Tok.getLength());
1380
Chris Lattner2199f5b2009-04-10 18:08:30 +00001381 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1382 // it is needed.
Chris Lattnerc523d8e2009-04-11 21:15:38 +00001383 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump11289f42009-09-09 15:08:12 +00001384
Chris Lattner2199f5b2009-04-10 18:08:30 +00001385 // FIXME: Should translate token kind to a stable encoding.
1386 Record.push_back(Tok.getKind());
1387 // FIXME: Should translate token flags to a stable encoding.
1388 Record.push_back(Tok.getFlags());
Mike Stump11289f42009-09-09 15:08:12 +00001389
Sebastian Redl539c5062010-08-18 23:57:32 +00001390 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattner2199f5b2009-04-10 18:08:30 +00001391 Record.clear();
1392 }
Douglas Gregorc3366a52009-04-21 23:56:24 +00001393 ++NumMacros;
Chris Lattnerbaa52f42009-04-10 18:00:12 +00001394 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001395
Douglas Gregoraae92242010-03-19 21:51:54 +00001396 // If the preprocessor has a preprocessing record, emit it.
1397 unsigned NumPreprocessingRecords = 0;
1398 if (PPRec) {
Sebastian Redl7abd8d52010-09-27 23:20:01 +00001399 unsigned IndexBase = Chain ? PPRec->getNumPreallocatedEntities() : 0;
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001400 for (PreprocessingRecord::iterator E = PPRec->begin(Chain),
1401 EEnd = PPRec->end(Chain);
Douglas Gregoraae92242010-03-19 21:51:54 +00001402 E != EEnd; ++E) {
1403 Record.clear();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001404
Douglas Gregoraae92242010-03-19 21:51:54 +00001405 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1406 // Record this macro definition's location.
Sebastian Redl50e26582010-09-15 19:54:06 +00001407 MacroID ID = getMacroDefinitionID(MD);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001408
Douglas Gregor91096292010-10-02 19:29:26 +00001409 // Don't write the macro definition if it is from another AST file.
1410 if (ID < FirstMacroID)
1411 continue;
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001412
1413 // Notify the serialization listener that we're serializing this entity.
1414 if (SerializationListener)
1415 SerializationListener->SerializedPreprocessedEntity(*E,
1416 Stream.GetCurrentBitNo());
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001417
Douglas Gregor91096292010-10-02 19:29:26 +00001418 unsigned Position = ID - FirstMacroID;
1419 if (Position != MacroDefinitionOffsets.size()) {
1420 if (Position > MacroDefinitionOffsets.size())
1421 MacroDefinitionOffsets.resize(Position + 1);
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001422
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001423 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
Douglas Gregoraae92242010-03-19 21:51:54 +00001424 } else
1425 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001426
Sebastian Redl9609b4f2010-09-27 22:18:47 +00001427 Record.push_back(IndexBase + NumPreprocessingRecords++);
Douglas Gregoraae92242010-03-19 21:51:54 +00001428 Record.push_back(ID);
1429 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1430 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1431 AddIdentifierRef(MD->getName(), Record);
1432 AddSourceLocation(MD->getLocation(), Record);
Sebastian Redl539c5062010-08-18 23:57:32 +00001433 Stream.EmitRecord(PP_MACRO_DEFINITION, Record);
Douglas Gregoraae92242010-03-19 21:51:54 +00001434 continue;
1435 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001436
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001437 // Notify the serialization listener that we're serializing this entity.
1438 if (SerializationListener)
1439 SerializationListener->SerializedPreprocessedEntity(*E,
1440 Stream.GetCurrentBitNo());
1441
1442 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1443 Record.push_back(IndexBase + NumPreprocessingRecords++);
1444 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1445 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1446 AddIdentifierRef(MI->getName(), Record);
1447 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1448 Stream.EmitRecord(PP_MACRO_INSTANTIATION, Record);
1449 continue;
1450 }
1451
Douglas Gregor796d76a2010-10-20 22:00:55 +00001452 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1453 Record.push_back(PP_INCLUSION_DIRECTIVE);
1454 Record.push_back(IndexBase + NumPreprocessingRecords++);
1455 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1456 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1457 Record.push_back(ID->getFileName().size());
1458 Record.push_back(ID->wasInQuotes());
1459 Record.push_back(static_cast<unsigned>(ID->getKind()));
1460 llvm::SmallString<64> Buffer;
1461 Buffer += ID->getFileName();
1462 Buffer += ID->getFile()->getName();
1463 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1464 continue;
1465 }
Douglas Gregorf88e35b2010-11-30 06:16:57 +00001466
1467 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
Douglas Gregoraae92242010-03-19 21:51:54 +00001468 }
1469 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001470
Douglas Gregor8f45df52009-04-16 22:23:12 +00001471 Stream.ExitBlock();
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001472
Douglas Gregoraae92242010-03-19 21:51:54 +00001473 // Write the offsets table for the preprocessing record.
1474 if (NumPreprocessingRecords > 0) {
1475 // Write the offsets table for identifier IDs.
1476 using namespace llvm;
1477 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001478 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregoraae92242010-03-19 21:51:54 +00001479 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1480 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1481 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1482 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00001483
Douglas Gregoraae92242010-03-19 21:51:54 +00001484 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001485 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregoraae92242010-03-19 21:51:54 +00001486 Record.push_back(NumPreprocessingRecords);
1487 Record.push_back(MacroDefinitionOffsets.size());
1488 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001489 (const char *)data(MacroDefinitionOffsets),
Douglas Gregoraae92242010-03-19 21:51:54 +00001490 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1491 }
Chris Lattnereeffaef2009-04-10 17:15:23 +00001492}
1493
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001494void ASTWriter::WriteUserDiagnosticMappings(const Diagnostic &Diag) {
1495 RecordData Record;
1496 for (unsigned i = 0; i != diag::DIAG_UPPER_LIMIT; ++i) {
Argyrios Kyrtzidis1cb0de12010-12-15 18:44:22 +00001497 diag::Mapping Map = Diag.getDiagnosticMappingInfo(i,Diag.GetCurDiagState());
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001498 if (Map & 0x8) { // user mapping.
1499 Record.push_back(i);
1500 Record.push_back(Map & 0x7);
1501 }
1502 }
1503
Argyrios Kyrtzidisb0ca9eb2010-11-05 22:20:49 +00001504 if (!Record.empty())
1505 Stream.EmitRecord(DIAG_USER_MAPPINGS, Record);
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00001506}
1507
Douglas Gregorc5046832009-04-27 18:38:38 +00001508//===----------------------------------------------------------------------===//
1509// Type Serialization
1510//===----------------------------------------------------------------------===//
Chris Lattnereeffaef2009-04-10 17:15:23 +00001511
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001512/// \brief Write the representation of a type to the AST stream.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001513void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00001514 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001515 if (Idx.getIndex() == 0) // we haven't seen this type before.
1516 Idx = TypeIdx(NextTypeID++);
Mike Stump11289f42009-09-09 15:08:12 +00001517
Douglas Gregor9b3932c2010-10-05 18:37:06 +00001518 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregordc72caa2010-10-04 18:21:45 +00001519
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001520 // Record the offset for this type.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00001521 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001522 if (TypeOffsets.size() == Index)
Douglas Gregor8f45df52009-04-16 22:23:12 +00001523 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001524 else if (TypeOffsets.size() < Index) {
1525 TypeOffsets.resize(Index + 1);
1526 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001527 }
1528
1529 RecordData Record;
Mike Stump11289f42009-09-09 15:08:12 +00001530
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001531 // Emit the type's representation.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001532 ASTTypeWriter W(*this, Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001533
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001534 if (T.hasLocalNonFastQualifiers()) {
1535 Qualifiers Qs = T.getLocalQualifiers();
1536 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall8ccfcb52009-09-24 19:53:00 +00001537 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl539c5062010-08-18 23:57:32 +00001538 W.Code = TYPE_EXT_QUAL;
John McCall8ccfcb52009-09-24 19:53:00 +00001539 } else {
1540 switch (T->getTypeClass()) {
1541 // For all of the concrete, non-dependent types, call the
1542 // appropriate visitor function.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001543#define TYPE(Class, Base) \
Mike Stump281d6d72010-01-20 02:03:14 +00001544 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001545#define ABSTRACT_TYPE(Class, Base)
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001546#include "clang/AST/TypeNodes.def"
John McCall8ccfcb52009-09-24 19:53:00 +00001547 }
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001548 }
1549
1550 // Emit the serialized record.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001551 Stream.EmitRecord(W.Code, Record);
Douglas Gregorfeb84b02009-04-14 21:18:50 +00001552
1553 // Flush any expressions that were written as part of this type.
Douglas Gregor8f45df52009-04-16 22:23:12 +00001554 FlushStmts();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001555}
1556
Douglas Gregorc5046832009-04-27 18:38:38 +00001557//===----------------------------------------------------------------------===//
1558// Declaration Serialization
1559//===----------------------------------------------------------------------===//
1560
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001561/// \brief Write the block containing all of the declaration IDs
1562/// lexically declared within the given DeclContext.
1563///
1564/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1565/// bistream, or 0 if no block was written.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001566uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001567 DeclContext *DC) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001568 if (DC->decls_empty())
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001569 return 0;
1570
Douglas Gregor8f45df52009-04-16 22:23:12 +00001571 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001572 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001573 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001574 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001575 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1576 D != DEnd; ++D)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001577 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001578
Douglas Gregora57c3ab2009-04-22 22:34:57 +00001579 ++NumLexicalDeclContexts;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001580 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001581 reinterpret_cast<char*>(Decls.data()),
1582 Decls.size() * sizeof(KindDeclIDPair));
Douglas Gregoref84c4b2009-04-09 22:27:44 +00001583 return Offset;
1584}
1585
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001586void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001587 using namespace llvm;
1588 RecordData Record;
1589
1590 // Write the type offsets array
1591 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001592 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001593 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1594 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1595 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1596 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001597 Record.push_back(TYPE_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001598 Record.push_back(TypeOffsets.size());
1599 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001600 (const char *)data(TypeOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001601 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1602
1603 // Write the declaration offsets array
1604 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001605 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001606 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1607 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1608 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1609 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001610 Record.push_back(DECL_OFFSET);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001611 Record.push_back(DeclOffsets.size());
1612 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001613 (const char *)data(DeclOffsets),
Sebastian Redl1ea025b2010-07-16 16:36:56 +00001614 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1615}
1616
Douglas Gregorc5046832009-04-27 18:38:38 +00001617//===----------------------------------------------------------------------===//
1618// Global Method Pool and Selector Serialization
1619//===----------------------------------------------------------------------===//
1620
Douglas Gregore84a9da2009-04-20 20:36:09 +00001621namespace {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001622// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001623class ASTMethodPoolTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001624 ASTWriter &Writer;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001625
1626public:
1627 typedef Selector key_type;
1628 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001629
Sebastian Redl834bb972010-08-04 17:20:04 +00001630 struct data_type {
Sebastian Redl539c5062010-08-18 23:57:32 +00001631 SelectorID ID;
Sebastian Redl834bb972010-08-04 17:20:04 +00001632 ObjCMethodList Instance, Factory;
1633 };
Douglas Gregorc78d3462009-04-24 21:10:55 +00001634 typedef const data_type& data_type_ref;
1635
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001636 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump11289f42009-09-09 15:08:12 +00001637
Douglas Gregorc78d3462009-04-24 21:10:55 +00001638 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis4bd97102010-08-20 16:03:52 +00001639 return serialization::ComputeHash(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001640 }
Mike Stump11289f42009-09-09 15:08:12 +00001641
1642 std::pair<unsigned,unsigned>
Douglas Gregorc78d3462009-04-24 21:10:55 +00001643 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1644 data_type_ref Methods) {
1645 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1646 clang::io::Emit16(Out, KeyLen);
Sebastian Redl834bb972010-08-04 17:20:04 +00001647 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1648 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001649 Method = Method->Next)
1650 if (Method->Method)
1651 DataLen += 4;
Sebastian Redl834bb972010-08-04 17:20:04 +00001652 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001653 Method = Method->Next)
1654 if (Method->Method)
1655 DataLen += 4;
1656 clang::io::Emit16(Out, DataLen);
1657 return std::make_pair(KeyLen, DataLen);
1658 }
Mike Stump11289f42009-09-09 15:08:12 +00001659
Douglas Gregor95c13f52009-04-25 17:48:32 +00001660 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump11289f42009-09-09 15:08:12 +00001661 uint64_t Start = Out.tell();
Douglas Gregor95c13f52009-04-25 17:48:32 +00001662 assert((Start >> 32) == 0 && "Selector key offset too large");
1663 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001664 unsigned N = Sel.getNumArgs();
1665 clang::io::Emit16(Out, N);
1666 if (N == 0)
1667 N = 1;
1668 for (unsigned I = 0; I != N; ++I)
Mike Stump11289f42009-09-09 15:08:12 +00001669 clang::io::Emit32(Out,
Douglas Gregorc78d3462009-04-24 21:10:55 +00001670 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1671 }
Mike Stump11289f42009-09-09 15:08:12 +00001672
Douglas Gregorc78d3462009-04-24 21:10:55 +00001673 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001674 data_type_ref Methods, unsigned DataLen) {
1675 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl834bb972010-08-04 17:20:04 +00001676 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001677 unsigned NumInstanceMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001678 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001679 Method = Method->Next)
1680 if (Method->Method)
1681 ++NumInstanceMethods;
1682
1683 unsigned NumFactoryMethods = 0;
Sebastian Redl834bb972010-08-04 17:20:04 +00001684 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001685 Method = Method->Next)
1686 if (Method->Method)
1687 ++NumFactoryMethods;
1688
1689 clang::io::Emit16(Out, NumInstanceMethods);
1690 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl834bb972010-08-04 17:20:04 +00001691 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001692 Method = Method->Next)
1693 if (Method->Method)
1694 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl834bb972010-08-04 17:20:04 +00001695 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001696 Method = Method->Next)
1697 if (Method->Method)
1698 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001699
1700 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc78d3462009-04-24 21:10:55 +00001701 }
1702};
1703} // end anonymous namespace
1704
Sebastian Redla19a67f2010-08-03 21:58:15 +00001705/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001706///
1707/// The method pool contains both instance and factory methods, stored
Sebastian Redla19a67f2010-08-03 21:58:15 +00001708/// in an on-disk hash table indexed by the selector. The hash table also
1709/// contains an empty entry for every other selector known to Sema.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001710void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00001711 using namespace llvm;
1712
Sebastian Redla19a67f2010-08-03 21:58:15 +00001713 // Do we have to do anything at all?
Sebastian Redl834bb972010-08-04 17:20:04 +00001714 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redla19a67f2010-08-03 21:58:15 +00001715 return;
Sebastian Redld95a56e2010-08-04 18:21:41 +00001716 unsigned NumTableEntries = 0;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001717 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorc78d3462009-04-24 21:10:55 +00001718 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001719 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001720 ASTMethodPoolTrait Trait(*this);
Mike Stump11289f42009-09-09 15:08:12 +00001721
Sebastian Redla19a67f2010-08-03 21:58:15 +00001722 // Create the on-disk hash table representation. We walk through every
1723 // selector we've seen and look it up in the method pool.
Sebastian Redld95a56e2010-08-04 18:21:41 +00001724 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl539c5062010-08-18 23:57:32 +00001725 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl834bb972010-08-04 17:20:04 +00001726 I = SelectorIDs.begin(), E = SelectorIDs.end();
1727 I != E; ++I) {
1728 Selector S = I->first;
Sebastian Redla19a67f2010-08-03 21:58:15 +00001729 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001730 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl834bb972010-08-04 17:20:04 +00001731 I->second,
1732 ObjCMethodList(),
1733 ObjCMethodList()
1734 };
1735 if (F != SemaRef.MethodPool.end()) {
1736 Data.Instance = F->second.first;
1737 Data.Factory = F->second.second;
1738 }
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001739 // Only write this selector if it's not in an existing AST or something
Sebastian Redld95a56e2010-08-04 18:21:41 +00001740 // changed.
1741 if (Chain && I->second < FirstSelectorID) {
1742 // Selector already exists. Did it change?
1743 bool changed = false;
1744 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
1745 M = M->Next) {
1746 if (M->Method->getPCHLevel() == 0)
1747 changed = true;
1748 }
1749 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
1750 M = M->Next) {
1751 if (M->Method->getPCHLevel() == 0)
1752 changed = true;
1753 }
1754 if (!changed)
1755 continue;
Sebastian Redl6e1a2a02010-08-04 21:22:45 +00001756 } else if (Data.Instance.Method || Data.Factory.Method) {
1757 // A new method pool entry.
1758 ++NumTableEntries;
Sebastian Redld95a56e2010-08-04 18:21:41 +00001759 }
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001760 Generator.insert(S, Data, Trait);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001761 }
1762
Douglas Gregorc78d3462009-04-24 21:10:55 +00001763 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001764 llvm::SmallString<4096> MethodPool;
Douglas Gregorc78d3462009-04-24 21:10:55 +00001765 uint32_t BucketOffset;
1766 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001767 ASTMethodPoolTrait Trait(*this);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001768 llvm::raw_svector_ostream Out(MethodPool);
1769 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001770 clang::io::Emit32(Out, 0);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001771 BucketOffset = Generator.Emit(Out, Trait);
1772 }
1773
1774 // Create a blob abbreviation
1775 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001776 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001777 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor95c13f52009-04-25 17:48:32 +00001778 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc78d3462009-04-24 21:10:55 +00001779 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1780 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1781
Douglas Gregor95c13f52009-04-25 17:48:32 +00001782 // Write the method pool
Douglas Gregorc78d3462009-04-24 21:10:55 +00001783 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001784 Record.push_back(METHOD_POOL);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001785 Record.push_back(BucketOffset);
Sebastian Redld95a56e2010-08-04 18:21:41 +00001786 Record.push_back(NumTableEntries);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001787 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor95c13f52009-04-25 17:48:32 +00001788
1789 // Create a blob abbreviation for the selector table offsets.
1790 Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001791 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregord4c5ed02010-10-29 22:39:52 +00001792 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor95c13f52009-04-25 17:48:32 +00001793 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1794 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1795
1796 // Write the selector offsets table.
1797 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00001798 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor95c13f52009-04-25 17:48:32 +00001799 Record.push_back(SelectorOffsets.size());
1800 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00001801 (const char *)data(SelectorOffsets),
Douglas Gregor95c13f52009-04-25 17:48:32 +00001802 SelectorOffsets.size() * 4);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001803 }
1804}
1805
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001806/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001807void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001808 using namespace llvm;
1809 if (SemaRef.ReferencedSelectors.empty())
1810 return;
Sebastian Redlada023c2010-08-04 20:40:17 +00001811
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001812 RecordData Record;
Sebastian Redlada023c2010-08-04 20:40:17 +00001813
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001814 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redl51c79d82010-08-04 22:21:29 +00001815 // very tricky to fix, and given that @selector shouldn't really appear in
1816 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001817 for (DenseMap<Selector, SourceLocation>::iterator S =
1818 SemaRef.ReferencedSelectors.begin(),
1819 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
1820 Selector Sel = (*S).first;
1821 SourceLocation Loc = (*S).second;
1822 AddSelectorRef(Sel, Record);
1823 AddSourceLocation(Loc, Record);
1824 }
Sebastian Redl539c5062010-08-18 23:57:32 +00001825 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00001826}
1827
Douglas Gregorc5046832009-04-27 18:38:38 +00001828//===----------------------------------------------------------------------===//
1829// Identifier Table Serialization
1830//===----------------------------------------------------------------------===//
1831
Douglas Gregorc78d3462009-04-24 21:10:55 +00001832namespace {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001833class ASTIdentifierTableTrait {
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001834 ASTWriter &Writer;
Douglas Gregorc3366a52009-04-21 23:56:24 +00001835 Preprocessor &PP;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001836
Douglas Gregor1d583f22009-04-28 21:18:29 +00001837 /// \brief Determines whether this is an "interesting" identifier
1838 /// that needs a full IdentifierInfo structure written into the hash
1839 /// table.
1840 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1841 return II->isPoisoned() ||
1842 II->isExtensionToken() ||
1843 II->hasMacroDefinition() ||
1844 II->getObjCOrBuiltinID() ||
1845 II->getFETokenInfo<void>();
1846 }
1847
Douglas Gregore84a9da2009-04-20 20:36:09 +00001848public:
1849 typedef const IdentifierInfo* key_type;
1850 typedef key_type key_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001851
Sebastian Redl539c5062010-08-18 23:57:32 +00001852 typedef IdentID data_type;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001853 typedef data_type data_type_ref;
Mike Stump11289f42009-09-09 15:08:12 +00001854
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001855 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregorc3366a52009-04-21 23:56:24 +00001856 : Writer(Writer), PP(PP) { }
Douglas Gregore84a9da2009-04-20 20:36:09 +00001857
1858 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbarf8502d52009-10-17 23:52:28 +00001859 return llvm::HashString(II->getName());
Douglas Gregore84a9da2009-04-20 20:36:09 +00001860 }
Mike Stump11289f42009-09-09 15:08:12 +00001861
1862 std::pair<unsigned,unsigned>
1863 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00001864 IdentID ID) {
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001865 unsigned KeyLen = II->getLength() + 1;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001866 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1867 if (isInterestingIdentifier(II)) {
Douglas Gregorb9256522009-04-28 21:32:13 +00001868 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump11289f42009-09-09 15:08:12 +00001869 if (II->hasMacroDefinition() &&
Douglas Gregor1d583f22009-04-28 21:18:29 +00001870 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregorb9256522009-04-28 21:32:13 +00001871 DataLen += 4;
Douglas Gregor1d583f22009-04-28 21:18:29 +00001872 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1873 DEnd = IdentifierResolver::end();
1874 D != DEnd; ++D)
Sebastian Redl539c5062010-08-18 23:57:32 +00001875 DataLen += sizeof(DeclID);
Douglas Gregor1d583f22009-04-28 21:18:29 +00001876 }
Douglas Gregora868bbd2009-04-21 22:25:48 +00001877 clang::io::Emit16(Out, DataLen);
Douglas Gregorab4df582009-04-28 20:01:51 +00001878 // We emit the key length after the data length so that every
1879 // string is preceded by a 16-bit length. This matches the PTH
1880 // format for storing identifiers.
Douglas Gregor5287b4e2009-04-25 21:04:17 +00001881 clang::io::Emit16(Out, KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001882 return std::make_pair(KeyLen, DataLen);
1883 }
Mike Stump11289f42009-09-09 15:08:12 +00001884
1885 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregore84a9da2009-04-20 20:36:09 +00001886 unsigned KeyLen) {
1887 // Record the location of the key data. This is used when generating
1888 // the mapping from persistent IDs to strings.
1889 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001890 Out.write(II->getNameStart(), KeyLen);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001891 }
Mike Stump11289f42009-09-09 15:08:12 +00001892
1893 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl539c5062010-08-18 23:57:32 +00001894 IdentID ID, unsigned) {
Douglas Gregor1d583f22009-04-28 21:18:29 +00001895 if (!isInterestingIdentifier(II)) {
1896 clang::io::Emit32(Out, ID << 1);
1897 return;
1898 }
Douglas Gregorb9256522009-04-28 21:32:13 +00001899
Douglas Gregor1d583f22009-04-28 21:18:29 +00001900 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001901 uint32_t Bits = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001902 bool hasMacroDefinition =
1903 II->hasMacroDefinition() &&
Douglas Gregorc3366a52009-04-21 23:56:24 +00001904 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorb9256522009-04-28 21:32:13 +00001905 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001906 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1907 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1908 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis3084a612010-08-11 22:55:12 +00001909 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbar91b640a2009-12-18 20:58:47 +00001910 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregorb9256522009-04-28 21:32:13 +00001911 clang::io::Emit16(Out, Bits);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001912
Douglas Gregorc3366a52009-04-21 23:56:24 +00001913 if (hasMacroDefinition)
Douglas Gregorb9256522009-04-28 21:32:13 +00001914 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregorc3366a52009-04-21 23:56:24 +00001915
Douglas Gregora868bbd2009-04-21 22:25:48 +00001916 // Emit the declaration IDs in reverse order, because the
1917 // IdentifierResolver provides the declarations as they would be
1918 // visible (e.g., the function "stat" would come before the struct
1919 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1920 // adds declarations to the end of the list (so we need to see the
1921 // struct "status" before the function "status").
Sebastian Redlff4a2952010-07-23 23:49:55 +00001922 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump11289f42009-09-09 15:08:12 +00001923 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregora868bbd2009-04-21 22:25:48 +00001924 IdentifierResolver::end());
1925 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1926 DEnd = Decls.rend();
Douglas Gregore84a9da2009-04-20 20:36:09 +00001927 D != DEnd; ++D)
Sebastian Redl78f51772010-08-02 18:30:12 +00001928 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001929 }
1930};
1931} // end anonymous namespace
1932
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001933/// \brief Write the identifier table into the AST file.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001934///
1935/// The identifier table consists of a blob containing string data
1936/// (the actual identifiers themselves) and a separate "offsets" index
1937/// that maps identifier IDs to locations within the blob.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00001938void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001939 using namespace llvm;
1940
1941 // Create and write out the blob that contains the identifier
1942 // strings.
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001943 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001944 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001945 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump11289f42009-09-09 15:08:12 +00001946
Douglas Gregore6648fb2009-04-28 20:33:11 +00001947 // Look for any identifiers that were named while processing the
1948 // headers, but are otherwise not needed. We add these to the hash
1949 // table to enable checking of the predefines buffer in the case
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001950 // where the user adds new macro definitions when building the AST
Douglas Gregore6648fb2009-04-28 20:33:11 +00001951 // file.
1952 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1953 IDEnd = PP.getIdentifierTable().end();
1954 ID != IDEnd; ++ID)
1955 getIdentifierRef(ID->second);
1956
Sebastian Redlff4a2952010-07-23 23:49:55 +00001957 // Create the on-disk hash table representation. We only store offsets
1958 // for identifiers that appear here for the first time.
1959 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl539c5062010-08-18 23:57:32 +00001960 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001961 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1962 ID != IDEnd; ++ID) {
1963 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redld44cd6a2010-08-18 23:57:06 +00001964 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00001965 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001966 }
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001967
Douglas Gregore84a9da2009-04-20 20:36:09 +00001968 // Create the on-disk hash table in a buffer.
Mike Stump11289f42009-09-09 15:08:12 +00001969 llvm::SmallString<4096> IdentifierTable;
Douglas Gregora868bbd2009-04-21 22:25:48 +00001970 uint32_t BucketOffset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00001971 {
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00001972 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregore84a9da2009-04-20 20:36:09 +00001973 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc78d3462009-04-24 21:10:55 +00001974 // Make sure that no bucket is at offset 0
Douglas Gregor4647cfa2009-04-24 21:49:02 +00001975 clang::io::Emit32(Out, 0);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001976 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001977 }
1978
1979 // Create a blob abbreviation
1980 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001981 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregora868bbd2009-04-21 22:25:48 +00001982 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregore84a9da2009-04-20 20:36:09 +00001983 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregor8f45df52009-04-16 22:23:12 +00001984 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001985
1986 // Write the identifier table
1987 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00001988 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregora868bbd2009-04-21 22:25:48 +00001989 Record.push_back(BucketOffset);
Daniel Dunbar8100d012009-08-24 09:31:37 +00001990 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00001991 }
1992
1993 // Write the offsets table for identifier IDs.
Douglas Gregor0e149972009-04-25 19:10:14 +00001994 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00001995 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor0e149972009-04-25 19:10:14 +00001996 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1997 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1998 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1999
2000 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002001 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor0e149972009-04-25 19:10:14 +00002002 Record.push_back(IdentifierOffsets.size());
2003 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redl3df5a082010-07-30 17:03:48 +00002004 (const char *)data(IdentifierOffsets),
Douglas Gregor0e149972009-04-25 19:10:14 +00002005 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002006}
2007
Douglas Gregorc5046832009-04-27 18:38:38 +00002008//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002009// DeclContext's Name Lookup Table Serialization
2010//===----------------------------------------------------------------------===//
2011
2012namespace {
2013// Trait used for the on-disk hash table used in the method pool.
2014class ASTDeclContextNameLookupTrait {
2015 ASTWriter &Writer;
2016
2017public:
2018 typedef DeclarationName key_type;
2019 typedef key_type key_type_ref;
2020
2021 typedef DeclContext::lookup_result data_type;
2022 typedef const data_type& data_type_ref;
2023
2024 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2025
2026 unsigned ComputeHash(DeclarationName Name) {
2027 llvm::FoldingSetNodeID ID;
2028 ID.AddInteger(Name.getNameKind());
2029
2030 switch (Name.getNameKind()) {
2031 case DeclarationName::Identifier:
2032 ID.AddString(Name.getAsIdentifierInfo()->getName());
2033 break;
2034 case DeclarationName::ObjCZeroArgSelector:
2035 case DeclarationName::ObjCOneArgSelector:
2036 case DeclarationName::ObjCMultiArgSelector:
2037 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2038 break;
2039 case DeclarationName::CXXConstructorName:
2040 case DeclarationName::CXXDestructorName:
2041 case DeclarationName::CXXConversionFunctionName:
2042 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
2043 break;
2044 case DeclarationName::CXXOperatorName:
2045 ID.AddInteger(Name.getCXXOverloadedOperator());
2046 break;
2047 case DeclarationName::CXXLiteralOperatorName:
2048 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2049 case DeclarationName::CXXUsingDirective:
2050 break;
2051 }
2052
2053 return ID.ComputeHash();
2054 }
2055
2056 std::pair<unsigned,unsigned>
2057 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2058 data_type_ref Lookup) {
2059 unsigned KeyLen = 1;
2060 switch (Name.getNameKind()) {
2061 case DeclarationName::Identifier:
2062 case DeclarationName::ObjCZeroArgSelector:
2063 case DeclarationName::ObjCOneArgSelector:
2064 case DeclarationName::ObjCMultiArgSelector:
2065 case DeclarationName::CXXConstructorName:
2066 case DeclarationName::CXXDestructorName:
2067 case DeclarationName::CXXConversionFunctionName:
2068 case DeclarationName::CXXLiteralOperatorName:
2069 KeyLen += 4;
2070 break;
2071 case DeclarationName::CXXOperatorName:
2072 KeyLen += 1;
2073 break;
2074 case DeclarationName::CXXUsingDirective:
2075 break;
2076 }
2077 clang::io::Emit16(Out, KeyLen);
2078
2079 // 2 bytes for num of decls and 4 for each DeclID.
2080 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2081 clang::io::Emit16(Out, DataLen);
2082
2083 return std::make_pair(KeyLen, DataLen);
2084 }
2085
2086 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2087 using namespace clang::io;
2088
2089 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2090 Emit8(Out, Name.getNameKind());
2091 switch (Name.getNameKind()) {
2092 case DeclarationName::Identifier:
2093 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2094 break;
2095 case DeclarationName::ObjCZeroArgSelector:
2096 case DeclarationName::ObjCOneArgSelector:
2097 case DeclarationName::ObjCMultiArgSelector:
2098 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2099 break;
2100 case DeclarationName::CXXConstructorName:
2101 case DeclarationName::CXXDestructorName:
2102 case DeclarationName::CXXConversionFunctionName:
2103 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2104 break;
2105 case DeclarationName::CXXOperatorName:
2106 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2107 Emit8(Out, Name.getCXXOverloadedOperator());
2108 break;
2109 case DeclarationName::CXXLiteralOperatorName:
2110 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2111 break;
2112 case DeclarationName::CXXUsingDirective:
2113 break;
2114 }
2115 }
2116
2117 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2118 data_type Lookup, unsigned DataLen) {
2119 uint64_t Start = Out.tell(); (void)Start;
2120 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2121 for (; Lookup.first != Lookup.second; ++Lookup.first)
2122 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2123
2124 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2125 }
2126};
2127} // end anonymous namespace
2128
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002129/// \brief Write the block containing all of the declaration IDs
2130/// visible from the given DeclContext.
2131///
2132/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redla4071b42010-08-24 00:50:09 +00002133/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002134uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2135 DeclContext *DC) {
2136 if (DC->getPrimaryContext() != DC)
2137 return 0;
2138
2139 // Since there is no name lookup into functions or methods, don't bother to
2140 // build a visible-declarations table for these entities.
2141 if (DC->isFunctionOrMethod())
2142 return 0;
2143
2144 // If not in C++, we perform name lookup for the translation unit via the
2145 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2146 // FIXME: In C++ we need the visible declarations in order to "see" the
2147 // friend declarations, is there a way to do this without writing the table ?
2148 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2149 return 0;
2150
2151 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisd32ee892010-08-20 23:35:55 +00002152 if (DC->hasExternalVisibleStorage())
2153 DC->MaterializeVisibleDeclsFromExternalStorage();
2154 else
2155 DC->lookup(DeclarationName());
Argyrios Kyrtzidisba88bfa2010-08-20 16:04:35 +00002156
2157 // Serialize the contents of the mapping used for lookup. Note that,
2158 // although we have two very different code paths, the serialized
2159 // representation is the same for both cases: a declaration name,
2160 // followed by a size, followed by references to the visible
2161 // declarations that have that name.
2162 uint64_t Offset = Stream.GetCurrentBitNo();
2163 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2164 if (!Map || Map->empty())
2165 return 0;
2166
2167 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2168 ASTDeclContextNameLookupTrait Trait(*this);
2169
2170 // Create the on-disk hash table representation.
2171 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2172 D != DEnd; ++D) {
2173 DeclarationName Name = D->first;
2174 DeclContext::lookup_result Result = D->second.getLookupResult();
2175 Generator.insert(Name, Result, Trait);
2176 }
2177
2178 // Create the on-disk hash table in a buffer.
2179 llvm::SmallString<4096> LookupTable;
2180 uint32_t BucketOffset;
2181 {
2182 llvm::raw_svector_ostream Out(LookupTable);
2183 // Make sure that no bucket is at offset 0
2184 clang::io::Emit32(Out, 0);
2185 BucketOffset = Generator.Emit(Out, Trait);
2186 }
2187
2188 // Write the lookup table
2189 RecordData Record;
2190 Record.push_back(DECL_CONTEXT_VISIBLE);
2191 Record.push_back(BucketOffset);
2192 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2193 LookupTable.str());
2194
2195 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2196 ++NumVisibleDeclContexts;
2197 return Offset;
2198}
2199
Sebastian Redla4071b42010-08-24 00:50:09 +00002200/// \brief Write an UPDATE_VISIBLE block for the given context.
2201///
2202/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2203/// DeclContext in a dependent AST file. As such, they only exist for the TU
2204/// (in C++) and for namespaces.
2205void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redla4071b42010-08-24 00:50:09 +00002206 // Make the context build its lookup table, but don't make it load external
2207 // decls.
2208 DC->lookup(DeclarationName());
2209
2210 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2211 if (!Map || Map->empty())
2212 return;
2213
2214 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2215 ASTDeclContextNameLookupTrait Trait(*this);
2216
2217 // Create the hash table.
Sebastian Redla4071b42010-08-24 00:50:09 +00002218 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2219 D != DEnd; ++D) {
2220 DeclarationName Name = D->first;
2221 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl9617e7e2010-08-24 00:50:16 +00002222 // For any name that appears in this table, the results are complete, i.e.
2223 // they overwrite results from previous PCHs. Merging is always a mess.
2224 Generator.insert(Name, Result, Trait);
Sebastian Redla4071b42010-08-24 00:50:09 +00002225 }
2226
2227 // Create the on-disk hash table in a buffer.
2228 llvm::SmallString<4096> LookupTable;
2229 uint32_t BucketOffset;
2230 {
2231 llvm::raw_svector_ostream Out(LookupTable);
2232 // Make sure that no bucket is at offset 0
2233 clang::io::Emit32(Out, 0);
2234 BucketOffset = Generator.Emit(Out, Trait);
2235 }
2236
2237 // Write the lookup table
2238 RecordData Record;
2239 Record.push_back(UPDATE_VISIBLE);
2240 Record.push_back(getDeclID(cast<Decl>(DC)));
2241 Record.push_back(BucketOffset);
2242 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2243}
2244
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002245//===----------------------------------------------------------------------===//
Douglas Gregorc5046832009-04-27 18:38:38 +00002246// General Serialization Routines
2247//===----------------------------------------------------------------------===//
2248
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002249/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002250void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis9beef8e2010-10-18 19:20:11 +00002251 Record.push_back(Attrs.size());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002252 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2253 const Attr * A = *i;
2254 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2255 AddSourceLocation(A->getLocation(), Record);
2256 Record.push_back(A->isInherited());
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002257
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002258#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00002259
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002260 }
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002261}
2262
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002263void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregorbc8a78d52009-04-15 21:30:51 +00002264 Record.push_back(Str.size());
2265 Record.insert(Record.end(), Str.begin(), Str.end());
2266}
2267
Douglas Gregore84a9da2009-04-20 20:36:09 +00002268/// \brief Note that the identifier II occurs at the given offset
2269/// within the identifier table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002270void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl539c5062010-08-18 23:57:32 +00002271 IdentID ID = IdentifierIDs[II];
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002272 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlff4a2952010-07-23 23:49:55 +00002273 // up earlier in the chain and thus don't need an offset.
2274 if (ID >= FirstIdentID)
2275 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregore84a9da2009-04-20 20:36:09 +00002276}
2277
Douglas Gregor95c13f52009-04-25 17:48:32 +00002278/// \brief Note that the selector Sel occurs at the given offset
2279/// within the method pool/selector table.
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002280void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor95c13f52009-04-25 17:48:32 +00002281 unsigned ID = SelectorIDs[Sel];
2282 assert(ID && "Unknown selector");
Sebastian Redld95a56e2010-08-04 18:21:41 +00002283 // Don't record offsets for selectors that are also available in a different
2284 // file.
2285 if (ID < FirstSelectorID)
2286 return;
2287 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor95c13f52009-04-25 17:48:32 +00002288}
2289
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002290ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregorf88e35b2010-11-30 06:16:57 +00002291 : Stream(Stream), Chain(0), SerializationListener(0),
2292 FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl539c5062010-08-18 23:57:32 +00002293 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002294 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor91096292010-10-02 19:29:26 +00002295 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2296 CollectedStmts(&StmtsToEmit),
Sebastian Redld95a56e2010-08-04 18:21:41 +00002297 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002298 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
2299 NextCXXBaseSpecifiersID(1)
2300{
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002301}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002302
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002303void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002304 const char *isysroot) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002305 // Emit the file header.
Douglas Gregor8f45df52009-04-16 22:23:12 +00002306 Stream.Emit((unsigned)'C', 8);
2307 Stream.Emit((unsigned)'P', 8);
2308 Stream.Emit((unsigned)'C', 8);
2309 Stream.Emit((unsigned)'H', 8);
Mike Stump11289f42009-09-09 15:08:12 +00002310
Chris Lattner28fa4e62009-04-26 22:26:21 +00002311 WriteBlockInfoBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002312
Sebastian Redl143413f2010-07-12 22:02:52 +00002313 if (Chain)
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002314 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002315 else
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002316 WriteASTCore(SemaRef, StatCalls, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002317}
2318
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002319void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl143413f2010-07-12 22:02:52 +00002320 const char *isysroot) {
2321 using namespace llvm;
2322
2323 ASTContext &Context = SemaRef.Context;
2324 Preprocessor &PP = SemaRef.PP;
2325
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002326 // The translation unit is the first declaration we'll emit.
2327 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlff4a2952010-07-23 23:49:55 +00002328 ++NextDeclID;
Douglas Gregor12bfa382009-10-17 00:13:19 +00002329 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002330
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002331 // Make sure that we emit IdentifierInfos (and any attached
2332 // declarations) for builtins.
2333 {
2334 IdentifierTable &Table = PP.getIdentifierTable();
2335 llvm::SmallVector<const char *, 32> BuiltinNames;
2336 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2337 Context.getLangOptions().NoBuiltin);
2338 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2339 getIdentifierRef(&Table.get(BuiltinNames[I]));
2340 }
2341
Chris Lattner0c797362009-09-08 18:19:27 +00002342 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redl35351a92010-01-31 22:27:38 +00002343 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner0c797362009-09-08 18:19:27 +00002344 // headers.
Douglas Gregord4df8652009-04-22 22:02:47 +00002345 RecordData TentativeDefinitions;
Sebastian Redl35351a92010-01-31 22:27:38 +00002346 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2347 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner0c797362009-09-08 18:19:27 +00002348 }
Douglas Gregord4df8652009-04-22 22:02:47 +00002349
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002350 // Build a record containing all of the file scoped decls in this file.
2351 RecordData UnusedFileScopedDecls;
2352 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2353 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002354
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002355 RecordData WeakUndeclaredIdentifiers;
2356 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2357 WeakUndeclaredIdentifiers.push_back(
2358 SemaRef.WeakUndeclaredIdentifiers.size());
2359 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2360 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2361 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2362 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2363 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2364 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2365 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2366 }
2367 }
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002368
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002369 // Build a record containing all of the locally-scoped external
2370 // declarations in this header file. Generally, this record will be
2371 // empty.
2372 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002373 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner0c797362009-09-08 18:19:27 +00002374 // nondeterminstic!
Mike Stump11289f42009-09-09 15:08:12 +00002375 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002376 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2377 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2378 TD != TDEnd; ++TD)
2379 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2380
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002381 // Build a record containing all of the ext_vector declarations.
2382 RecordData ExtVectorDecls;
2383 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2384 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2385
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002386 // Build a record containing all of the VTable uses information.
2387 RecordData VTableUses;
Argyrios Kyrtzidisedee67f2010-08-03 17:29:52 +00002388 if (!SemaRef.VTableUses.empty()) {
2389 VTableUses.push_back(SemaRef.VTableUses.size());
2390 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2391 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2392 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2393 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2394 }
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002395 }
2396
2397 // Build a record containing all of dynamic classes declarations.
2398 RecordData DynamicClasses;
2399 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2400 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2401
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002402 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002403 RecordData PendingInstantiations;
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002404 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002405 I = SemaRef.PendingInstantiations.begin(),
2406 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2407 AddDeclRef(I->first, PendingInstantiations);
2408 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002409 }
2410 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2411 "There are local ones at end of translation unit!");
2412
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002413 // Build a record containing some declaration references.
2414 RecordData SemaDeclRefs;
2415 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2416 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2417 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2418 }
2419
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002420 // Write the remaining AST contents.
Douglas Gregor652d82a2009-04-18 05:55:16 +00002421 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002422 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002423 WriteMetadata(Context, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002424 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002425 if (StatCalls && !isysroot)
Douglas Gregor11cfd942010-07-12 23:48:14 +00002426 WriteStatCache(*StatCalls);
Douglas Gregor0086a5a2009-07-07 00:12:59 +00002427 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc277ad12009-07-18 15:33:26 +00002428 // Write the record of special types.
2429 Record.clear();
Mike Stump11289f42009-09-09 15:08:12 +00002430
Steve Naroffc277ad12009-07-18 15:33:26 +00002431 AddTypeRef(Context.getBuiltinVaListType(), Record);
2432 AddTypeRef(Context.getObjCIdType(), Record);
2433 AddTypeRef(Context.getObjCSelType(), Record);
2434 AddTypeRef(Context.getObjCProtoType(), Record);
2435 AddTypeRef(Context.getObjCClassType(), Record);
2436 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2437 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2438 AddTypeRef(Context.getFILEType(), Record);
Mike Stumpa4de80b2009-07-28 02:25:19 +00002439 AddTypeRef(Context.getjmp_bufType(), Record);
2440 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregora8eed7d2009-08-21 00:27:50 +00002441 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2442 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpd0153282009-10-20 02:12:22 +00002443 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stumpe1b19ba2009-10-22 00:49:09 +00002444 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahaniane804c282010-04-23 17:41:07 +00002445 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2446 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidise862cbc2010-07-04 21:44:19 +00002447 Record.push_back(Context.isInt128Installed());
Sebastian Redl539c5062010-08-18 23:57:32 +00002448 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump11289f42009-09-09 15:08:12 +00002449
Douglas Gregor1970d882009-04-26 03:49:13 +00002450 // Keep writing types and declarations until all types and
2451 // declarations have been written.
Sebastian Redl539c5062010-08-18 23:57:32 +00002452 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002453 WriteDeclsBlockAbbrevs();
2454 while (!DeclTypesToEmit.empty()) {
2455 DeclOrType DOT = DeclTypesToEmit.front();
2456 DeclTypesToEmit.pop();
2457 if (DOT.isType())
2458 WriteType(DOT.getType());
2459 else
2460 WriteDecl(Context, DOT.getDecl());
2461 }
2462 Stream.ExitBlock();
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002463
Douglas Gregor45053152009-10-17 17:25:45 +00002464 WritePreprocessor(PP);
Sebastian Redla19a67f2010-08-03 21:58:15 +00002465 WriteSelectors(SemaRef);
Fariborz Jahanianc51609a2010-07-23 19:11:11 +00002466 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002467 WriteIdentifierTable(PP);
Douglas Gregor745ed142009-04-25 18:35:21 +00002468
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002469 WriteTypeDeclOffsets();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002470 WriteUserDiagnosticMappings(Context.getDiagnostics());
Douglas Gregor652d82a2009-04-18 05:55:16 +00002471
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002472 // Write the C++ base-specifier set offsets.
2473 if (!CXXBaseSpecifiersOffsets.empty()) {
2474 // Create a blob abbreviation for the C++ base specifiers offsets.
2475 using namespace llvm;
2476
2477 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2478 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2479 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2480 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2481 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2482
2483 // Write the selector offsets table.
2484 Record.clear();
2485 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
2486 Record.push_back(CXXBaseSpecifiersOffsets.size());
2487 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
2488 (const char *)CXXBaseSpecifiersOffsets.data(),
2489 CXXBaseSpecifiersOffsets.size() * sizeof(uint32_t));
2490 }
2491
Douglas Gregord4df8652009-04-22 22:02:47 +00002492 // Write the record containing external, unnamed definitions.
Douglas Gregor1a0d0b92009-04-14 00:24:19 +00002493 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002494 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregord4df8652009-04-22 22:02:47 +00002495
2496 // Write the record containing tentative definitions.
2497 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002498 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002499
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002500 // Write the record containing unused file scoped decls.
2501 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002502 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002503
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002504 // Write the record containing weak undeclared identifiers.
2505 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002506 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidisee1afa32010-08-05 09:48:08 +00002507 WeakUndeclaredIdentifiers);
2508
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002509 // Write the record containing locally-scoped external definitions.
2510 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002511 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregoracfc76c2009-04-22 22:18:58 +00002512 LocallyScopedExternalDecls);
Douglas Gregor61cac2b2009-04-27 20:06:05 +00002513
2514 // Write the record containing ext_vector type names.
2515 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002516 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump11289f42009-09-09 15:08:12 +00002517
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002518 // Write the record containing VTable uses information.
2519 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002520 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002521
2522 // Write the record containing dynamic classes declarations.
2523 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002524 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisaf2eac22010-07-06 15:37:04 +00002525
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002526 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002527 if (!PendingInstantiations.empty())
2528 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis7f76d112010-08-05 09:48:16 +00002529
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002530 // Write the record containing declaration references of Sema.
2531 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002532 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis2d688102010-08-02 07:14:54 +00002533
Douglas Gregor08f01292009-04-17 22:13:46 +00002534 // Some simple statistics
Douglas Gregor652d82a2009-04-18 05:55:16 +00002535 Record.clear();
Douglas Gregor08f01292009-04-17 22:13:46 +00002536 Record.push_back(NumStatements);
Douglas Gregorc3366a52009-04-21 23:56:24 +00002537 Record.push_back(NumMacros);
Douglas Gregora57c3ab2009-04-22 22:34:57 +00002538 Record.push_back(NumLexicalDeclContexts);
2539 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl539c5062010-08-18 23:57:32 +00002540 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregor8f45df52009-04-16 22:23:12 +00002541 Stream.ExitBlock();
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002542}
2543
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002544void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002545 const char *isysroot) {
Sebastian Redl143413f2010-07-12 22:02:52 +00002546 using namespace llvm;
2547
2548 ASTContext &Context = SemaRef.Context;
2549 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002550
Sebastian Redl143413f2010-07-12 22:02:52 +00002551 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002552 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00002553 WriteMetadata(Context, isysroot);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002554 if (StatCalls && !isysroot)
2555 WriteStatCache(*StatCalls);
2556 // FIXME: Source manager block should only write new stuff, which could be
2557 // done by tracking the largest ID in the chain
2558 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl143413f2010-07-12 22:02:52 +00002559
2560 // The special types are in the chained PCH.
2561
2562 // We don't start with the translation unit, but with its decls that
Sebastian Redld44cd6a2010-08-18 23:57:06 +00002563 // don't come from the chained PCH.
Sebastian Redl143413f2010-07-12 22:02:52 +00002564 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002565 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002566 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2567 E = TU->noload_decls_end();
Sebastian Redl143413f2010-07-12 22:02:52 +00002568 I != E; ++I) {
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002569 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002570 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002571 else if ((*I)->isChangedSinceDeserialization())
2572 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl143413f2010-07-12 22:02:52 +00002573 }
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002574 // We also need to write a lexical updates block for the TU.
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002575 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl539c5062010-08-18 23:57:32 +00002576 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002577 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2578 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2579 Record.clear();
Sebastian Redl539c5062010-08-18 23:57:32 +00002580 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redl4b1f4902010-07-27 18:24:41 +00002581 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2582 reinterpret_cast<const char*>(NewGlobalDecls.data()),
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002583 NewGlobalDecls.size() * sizeof(KindDeclIDPair));
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00002584 // And a visible updates block for the DeclContexts.
2585 Abv = new llvm::BitCodeAbbrev();
2586 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2587 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2588 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2589 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2590 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2591 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl143413f2010-07-12 22:02:52 +00002592
Sebastian Redl98912122010-07-27 23:01:28 +00002593 // Build a record containing all of the new tentative definitions in this
2594 // file, in TentativeDefinitions order.
2595 RecordData TentativeDefinitions;
2596 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2597 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2598 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2599 }
2600
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002601 // Build a record containing all of the file scoped decls in this file.
2602 RecordData UnusedFileScopedDecls;
2603 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2604 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2605 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002606 }
2607
Sebastian Redl08aca90252010-08-05 18:21:25 +00002608 // We write the entire table, overwriting the tables from the chain.
2609 RecordData WeakUndeclaredIdentifiers;
2610 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2611 WeakUndeclaredIdentifiers.push_back(
2612 SemaRef.WeakUndeclaredIdentifiers.size());
2613 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2614 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2615 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2616 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2617 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2618 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2619 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2620 }
2621 }
2622
Sebastian Redl98912122010-07-27 23:01:28 +00002623 // Build a record containing all of the locally-scoped external
2624 // declarations in this header file. Generally, this record will be
2625 // empty.
2626 RecordData LocallyScopedExternalDecls;
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002627 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl98912122010-07-27 23:01:28 +00002628 // nondeterminstic!
2629 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2630 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2631 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2632 TD != TDEnd; ++TD) {
2633 if (TD->second->getPCHLevel() == 0)
2634 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2635 }
2636
2637 // Build a record containing all of the ext_vector declarations.
2638 RecordData ExtVectorDecls;
2639 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2640 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2641 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2642 }
2643
Sebastian Redl08aca90252010-08-05 18:21:25 +00002644 // Build a record containing all of the VTable uses information.
2645 // We write everything here, because it's too hard to determine whether
2646 // a use is new to this part.
2647 RecordData VTableUses;
2648 if (!SemaRef.VTableUses.empty()) {
2649 VTableUses.push_back(SemaRef.VTableUses.size());
2650 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2651 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2652 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2653 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2654 }
2655 }
2656
2657 // Build a record containing all of dynamic classes declarations.
2658 RecordData DynamicClasses;
2659 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2660 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
2661 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2662
2663 // Build a record containing all of pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002664 RecordData PendingInstantiations;
Sebastian Redl08aca90252010-08-05 18:21:25 +00002665 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth54080172010-08-25 08:44:16 +00002666 I = SemaRef.PendingInstantiations.begin(),
2667 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redl08aca90252010-08-05 18:21:25 +00002668 if (I->first->getPCHLevel() == 0) {
Chandler Carruth54080172010-08-25 08:44:16 +00002669 AddDeclRef(I->first, PendingInstantiations);
2670 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002671 }
2672 }
2673 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2674 "There are local ones at end of translation unit!");
2675
2676 // Build a record containing some declaration references.
2677 // It's not worth the effort to avoid duplication here.
2678 RecordData SemaDeclRefs;
2679 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2680 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2681 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2682 }
2683
Sebastian Redl539c5062010-08-18 23:57:32 +00002684 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl143413f2010-07-12 22:02:52 +00002685 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00002686 for (DeclsToRewriteTy::iterator
2687 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
2688 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl143413f2010-07-12 22:02:52 +00002689 while (!DeclTypesToEmit.empty()) {
2690 DeclOrType DOT = DeclTypesToEmit.front();
2691 DeclTypesToEmit.pop();
2692 if (DOT.isType())
2693 WriteType(DOT.getType());
2694 else
2695 WriteDecl(Context, DOT.getDecl());
2696 }
2697 Stream.ExitBlock();
2698
Sebastian Redl98912122010-07-27 23:01:28 +00002699 WritePreprocessor(PP);
Sebastian Redl51c79d82010-08-04 22:21:29 +00002700 WriteSelectors(SemaRef);
2701 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlff4a2952010-07-23 23:49:55 +00002702 WriteIdentifierTable(PP);
Sebastian Redl1ea025b2010-07-16 16:36:56 +00002703 WriteTypeDeclOffsets();
Argyrios Kyrtzidis452707c2010-11-05 22:10:18 +00002704 // FIXME: For chained PCH only write the new mappings (we currently
2705 // write all of them again).
2706 WriteUserDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl98912122010-07-27 23:01:28 +00002707
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002708 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl42a0f6a2010-08-18 23:56:27 +00002709 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002710 RecordData FirstLatestDeclIDs;
2711 for (FirstLatestDeclMap::iterator
2712 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
2713 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
2714 "Expected first & second to be in different PCHs");
2715 AddDeclRef(I->first, FirstLatestDeclIDs);
2716 AddDeclRef(I->second, FirstLatestDeclIDs);
2717 }
2718 if (!FirstLatestDeclIDs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002719 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00002720
Sebastian Redl98912122010-07-27 23:01:28 +00002721 // Write the record containing external, unnamed definitions.
2722 if (!ExternalDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002723 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00002724
2725 // Write the record containing tentative definitions.
2726 if (!TentativeDefinitions.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002727 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl98912122010-07-27 23:01:28 +00002728
Argyrios Kyrtzidis35672e72010-08-13 18:42:17 +00002729 // Write the record containing unused file scoped decls.
2730 if (!UnusedFileScopedDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002731 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002732
Sebastian Redl08aca90252010-08-05 18:21:25 +00002733 // Write the record containing weak undeclared identifiers.
2734 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002735 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl08aca90252010-08-05 18:21:25 +00002736 WeakUndeclaredIdentifiers);
2737
Sebastian Redl98912122010-07-27 23:01:28 +00002738 // Write the record containing locally-scoped external definitions.
2739 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002740 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl98912122010-07-27 23:01:28 +00002741 LocallyScopedExternalDecls);
2742
2743 // Write the record containing ext_vector type names.
2744 if (!ExtVectorDecls.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002745 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl98912122010-07-27 23:01:28 +00002746
Sebastian Redl08aca90252010-08-05 18:21:25 +00002747 // Write the record containing VTable uses information.
2748 if (!VTableUses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002749 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002750
2751 // Write the record containing dynamic classes declarations.
2752 if (!DynamicClasses.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002753 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002754
2755 // Write the record containing pending implicit instantiations.
Chandler Carruth54080172010-08-25 08:44:16 +00002756 if (!PendingInstantiations.empty())
2757 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl08aca90252010-08-05 18:21:25 +00002758
2759 // Write the record containing declaration references of Sema.
2760 if (!SemaDeclRefs.empty())
Sebastian Redl539c5062010-08-18 23:57:32 +00002761 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl98912122010-07-27 23:01:28 +00002762
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00002763 // Write the updates to DeclContexts.
2764 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
2765 I = UpdatedDeclContexts.begin(),
2766 E = UpdatedDeclContexts.end();
Sebastian Redla4071b42010-08-24 00:50:09 +00002767 I != E; ++I)
2768 WriteDeclContextVisibleUpdate(*I);
2769
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002770 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002771
Sebastian Redl98912122010-07-27 23:01:28 +00002772 Record.clear();
2773 Record.push_back(NumStatements);
2774 Record.push_back(NumMacros);
2775 Record.push_back(NumLexicalDeclContexts);
2776 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002777 WriteDeclReplacementsBlock();
Sebastian Redl539c5062010-08-18 23:57:32 +00002778 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl143413f2010-07-12 22:02:52 +00002779 Stream.ExitBlock();
2780}
2781
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002782void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002783 if (DeclUpdates.empty())
2784 return;
2785
2786 RecordData OffsetsRecord;
2787 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
2788 for (DeclUpdateMap::iterator
2789 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
2790 const Decl *D = I->first;
2791 UpdateRecord &URec = I->second;
2792
Argyrios Kyrtzidis3ba70b82010-10-24 17:26:46 +00002793 if (DeclsToRewrite.count(D))
2794 continue; // The decl will be written completely,no need to store updates.
2795
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002796 uint64_t Offset = Stream.GetCurrentBitNo();
2797 Stream.EmitRecord(DECL_UPDATES, URec);
2798
2799 OffsetsRecord.push_back(GetDeclRef(D));
2800 OffsetsRecord.push_back(Offset);
2801 }
2802 Stream.ExitBlock();
2803 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
2804}
2805
Argyrios Kyrtzidis97bfda92010-10-24 17:26:43 +00002806void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002807 if (ReplacedDecls.empty())
2808 return;
2809
2810 RecordData Record;
Sebastian Redl539c5062010-08-18 23:57:32 +00002811 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002812 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
2813 Record.push_back(I->first);
2814 Record.push_back(I->second);
2815 }
Sebastian Redl539c5062010-08-18 23:57:32 +00002816 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redle7c1fe62010-08-13 00:28:03 +00002817}
2818
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002819void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002820 Record.push_back(Loc.getRawEncoding());
2821}
2822
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002823void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00002824 AddSourceLocation(Range.getBegin(), Record);
2825 AddSourceLocation(Range.getEnd(), Record);
2826}
2827
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002828void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002829 Record.push_back(Value.getBitWidth());
Benjamin Kramer25f9ea62010-09-06 23:43:28 +00002830 const uint64_t *Words = Value.getRawData();
2831 Record.append(Words, Words + Value.getNumWords());
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002832}
2833
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002834void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor1daeb692009-04-13 18:14:40 +00002835 Record.push_back(Value.isUnsigned());
2836 AddAPInt(Value, Record);
2837}
2838
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002839void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregore0a3a512009-04-14 21:55:33 +00002840 AddAPInt(Value.bitcastToAPInt(), Record);
2841}
2842
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002843void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002844 Record.push_back(getIdentifierRef(II));
2845}
2846
Sebastian Redl539c5062010-08-18 23:57:32 +00002847IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002848 if (II == 0)
2849 return 0;
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002850
Sebastian Redl539c5062010-08-18 23:57:32 +00002851 IdentID &ID = IdentifierIDs[II];
Douglas Gregor3ed42cb2009-04-11 00:14:32 +00002852 if (ID == 0)
Sebastian Redlff4a2952010-07-23 23:49:55 +00002853 ID = NextIdentID++;
Douglas Gregor4621c6a2009-04-22 18:49:13 +00002854 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002855}
2856
Sebastian Redl50e26582010-09-15 19:54:06 +00002857MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregoraae92242010-03-19 21:51:54 +00002858 if (MD == 0)
2859 return 0;
Sebastian Redl50e26582010-09-15 19:54:06 +00002860
2861 MacroID &ID = MacroDefinitions[MD];
Douglas Gregoraae92242010-03-19 21:51:54 +00002862 if (ID == 0)
Douglas Gregor91096292010-10-02 19:29:26 +00002863 ID = NextMacroID++;
Douglas Gregoraae92242010-03-19 21:51:54 +00002864 return ID;
2865}
2866
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002867void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl834bb972010-08-04 17:20:04 +00002868 Record.push_back(getSelectorRef(SelRef));
2869}
2870
Sebastian Redl539c5062010-08-18 23:57:32 +00002871SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl834bb972010-08-04 17:20:04 +00002872 if (Sel.getAsOpaquePtr() == 0) {
2873 return 0;
Steve Naroff2ddea052009-04-23 10:39:46 +00002874 }
2875
Sebastian Redl539c5062010-08-18 23:57:32 +00002876 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redld95a56e2010-08-04 18:21:41 +00002877 if (SID == 0 && Chain) {
2878 // This might trigger a ReadSelector callback, which will set the ID for
2879 // this selector.
2880 Chain->LoadSelector(Sel);
2881 }
Steve Naroff2ddea052009-04-23 10:39:46 +00002882 if (SID == 0) {
Sebastian Redld95a56e2010-08-04 18:21:41 +00002883 SID = NextSelectorID++;
Steve Naroff2ddea052009-04-23 10:39:46 +00002884 }
Sebastian Redl834bb972010-08-04 17:20:04 +00002885 return SID;
Steve Naroff2ddea052009-04-23 10:39:46 +00002886}
2887
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002888void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnercba86142010-05-10 00:25:06 +00002889 AddDeclRef(Temp->getDestructor(), Record);
2890}
2891
Douglas Gregord4c5ed02010-10-29 22:39:52 +00002892void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
2893 CXXBaseSpecifier const *BasesEnd,
2894 RecordDataImpl &Record) {
2895 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
2896 CXXBaseSpecifiersToWrite.push_back(
2897 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
2898 Bases, BasesEnd));
2899 Record.push_back(NextCXXBaseSpecifiersID++);
2900}
2901
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002902void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002903 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002904 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002905 switch (Kind) {
John McCall0ad16662009-10-29 08:12:44 +00002906 case TemplateArgument::Expression:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002907 AddStmt(Arg.getAsExpr());
John McCall0ad16662009-10-29 08:12:44 +00002908 break;
2909 case TemplateArgument::Type:
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002910 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall0ad16662009-10-29 08:12:44 +00002911 break;
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002912 case TemplateArgument::Template:
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002913 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2914 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregore4ff4b52011-01-05 18:58:31 +00002915 break;
2916 case TemplateArgument::TemplateExpansion:
2917 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2918 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregoreb29d182011-01-05 17:40:24 +00002919 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor9167f8b2009-11-11 01:00:40 +00002920 break;
John McCall0ad16662009-10-29 08:12:44 +00002921 case TemplateArgument::Null:
2922 case TemplateArgument::Integral:
2923 case TemplateArgument::Declaration:
2924 case TemplateArgument::Pack:
2925 break;
2926 }
2927}
2928
Sebastian Redl55c0ad52010-08-18 23:56:21 +00002929void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002930 RecordDataImpl &Record) {
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002931 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidisddf5f212010-06-28 09:31:42 +00002932
2933 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
2934 bool InfoHasSameExpr
2935 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
2936 Record.push_back(InfoHasSameExpr);
2937 if (InfoHasSameExpr)
2938 return; // Avoid storing the same expr twice.
2939 }
Argyrios Kyrtzidisae85e242010-06-22 09:54:59 +00002940 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
2941 Record);
2942}
2943
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002944void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordDataImpl &Record) {
John McCallbcd03502009-12-07 02:54:59 +00002945 if (TInfo == 0) {
John McCall8f115c62009-10-16 21:56:05 +00002946 AddTypeRef(QualType(), Record);
2947 return;
2948 }
2949
John McCallbcd03502009-12-07 02:54:59 +00002950 AddTypeRef(TInfo->getType(), Record);
John McCall8f115c62009-10-16 21:56:05 +00002951 TypeLocWriter TLW(*this, Record);
John McCallbcd03502009-12-07 02:54:59 +00002952 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnama9c81a82010-03-14 07:06:50 +00002953 TLW.Visit(TL);
John McCall8f115c62009-10-16 21:56:05 +00002954}
2955
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00002956void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis9ab44ea2010-08-20 16:04:14 +00002957 Record.push_back(GetOrCreateTypeID(T));
2958}
2959
2960TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00002961 return MakeTypeID(T,
2962 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
2963}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002964
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002965TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidis082e4612010-08-20 16:04:20 +00002966 return MakeTypeID(T,
2967 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002968}
2969
2970TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
2971 if (T.isNull())
2972 return TypeIdx();
2973 assert(!T.getLocalFastQualifiers());
2974
Argyrios Kyrtzidisa7fbbb02010-08-20 16:04:04 +00002975 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002976 if (Idx.getIndex() == 0) {
Douglas Gregor1970d882009-04-26 03:49:13 +00002977 // We haven't seen this type before. Assign it a new ID and put it
John McCall8ccfcb52009-09-24 19:53:00 +00002978 // into the queue of types to emit.
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00002979 Idx = TypeIdx(NextTypeID++);
Douglas Gregor12bfa382009-10-17 00:13:19 +00002980 DeclTypesToEmit.push(T);
Douglas Gregor1970d882009-04-26 03:49:13 +00002981 }
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002982 return Idx;
2983}
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002984
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002985TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidise394f2c2010-08-20 16:04:09 +00002986 if (T.isNull())
2987 return TypeIdx();
2988 assert(!T.getLocalFastQualifiers());
2989
Argyrios Kyrtzidis07347322010-08-20 16:04:27 +00002990 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
2991 assert(I != TypeIdxs.end() && "Type not emitted!");
2992 return I->second;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00002993}
2994
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00002995void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00002996 Record.push_back(GetDeclRef(D));
2997}
2998
Sebastian Redl539c5062010-08-18 23:57:32 +00002999DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003000 if (D == 0) {
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003001 return 0;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003002 }
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003003 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl539c5062010-08-18 23:57:32 +00003004 DeclID &ID = DeclIDs[D];
Mike Stump11289f42009-09-09 15:08:12 +00003005 if (ID == 0) {
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003006 // We haven't seen this declaration before. Give it a new ID and
3007 // enqueue it in the list of declarations to emit.
Sebastian Redlff4a2952010-07-23 23:49:55 +00003008 ID = NextDeclID++;
Douglas Gregor12bfa382009-10-17 00:13:19 +00003009 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redle7c1fe62010-08-13 00:28:03 +00003010 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3011 // We don't add it to the replacement collection here, because we don't
3012 // have the offset yet.
3013 DeclTypesToEmit.push(const_cast<Decl *>(D));
3014 // Reset the flag, so that we don't add this decl multiple times.
3015 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003016 }
3017
Sebastian Redl66c5eef2010-07-27 00:17:23 +00003018 return ID;
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003019}
3020
Sebastian Redl539c5062010-08-18 23:57:32 +00003021DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregore84a9da2009-04-20 20:36:09 +00003022 if (D == 0)
3023 return 0;
3024
3025 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3026 return DeclIDs[D];
3027}
3028
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003029void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattner258172e2009-04-27 07:35:58 +00003030 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003031 Record.push_back(Name.getNameKind());
3032 switch (Name.getNameKind()) {
3033 case DeclarationName::Identifier:
3034 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3035 break;
3036
3037 case DeclarationName::ObjCZeroArgSelector:
3038 case DeclarationName::ObjCOneArgSelector:
3039 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff2ddea052009-04-23 10:39:46 +00003040 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003041 break;
3042
3043 case DeclarationName::CXXConstructorName:
3044 case DeclarationName::CXXDestructorName:
3045 case DeclarationName::CXXConversionFunctionName:
3046 AddTypeRef(Name.getCXXNameType(), Record);
3047 break;
3048
3049 case DeclarationName::CXXOperatorName:
3050 Record.push_back(Name.getCXXOverloadedOperator());
3051 break;
3052
Alexis Hunt3d221f22009-11-29 07:34:05 +00003053 case DeclarationName::CXXLiteralOperatorName:
3054 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3055 break;
3056
Douglas Gregoref84c4b2009-04-09 22:27:44 +00003057 case DeclarationName::CXXUsingDirective:
3058 // No extra data to emit
3059 break;
3060 }
3061}
Chris Lattnerca025db2010-05-07 21:43:38 +00003062
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003063void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003064 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003065 switch (Name.getNameKind()) {
3066 case DeclarationName::CXXConstructorName:
3067 case DeclarationName::CXXDestructorName:
3068 case DeclarationName::CXXConversionFunctionName:
3069 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3070 break;
3071
3072 case DeclarationName::CXXOperatorName:
3073 AddSourceLocation(
3074 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3075 Record);
3076 AddSourceLocation(
3077 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3078 Record);
3079 break;
3080
3081 case DeclarationName::CXXLiteralOperatorName:
3082 AddSourceLocation(
3083 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3084 Record);
3085 break;
3086
3087 case DeclarationName::Identifier:
3088 case DeclarationName::ObjCZeroArgSelector:
3089 case DeclarationName::ObjCOneArgSelector:
3090 case DeclarationName::ObjCMultiArgSelector:
3091 case DeclarationName::CXXUsingDirective:
3092 break;
3093 }
3094}
3095
3096void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003097 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003098 AddDeclarationName(NameInfo.getName(), Record);
3099 AddSourceLocation(NameInfo.getLoc(), Record);
3100 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3101}
3102
3103void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003104 RecordDataImpl &Record) {
Argyrios Kyrtzidis434383d2010-10-15 18:21:24 +00003105 AddNestedNameSpecifier(Info.NNS, Record);
3106 AddSourceRange(Info.NNSRange, Record);
3107 Record.push_back(Info.NumTemplParamLists);
3108 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3109 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3110}
3111
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003112void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003113 RecordDataImpl &Record) {
Chris Lattnerca025db2010-05-07 21:43:38 +00003114 // Nested name specifiers usually aren't too long. I think that 8 would
3115 // typically accomodate the vast majority.
3116 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3117
3118 // Push each of the NNS's onto a stack for serialization in reverse order.
3119 while (NNS) {
3120 NestedNames.push_back(NNS);
3121 NNS = NNS->getPrefix();
3122 }
3123
3124 Record.push_back(NestedNames.size());
3125 while(!NestedNames.empty()) {
3126 NNS = NestedNames.pop_back_val();
3127 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3128 Record.push_back(Kind);
3129 switch (Kind) {
3130 case NestedNameSpecifier::Identifier:
3131 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3132 break;
3133
3134 case NestedNameSpecifier::Namespace:
3135 AddDeclRef(NNS->getAsNamespace(), Record);
3136 break;
3137
3138 case NestedNameSpecifier::TypeSpec:
3139 case NestedNameSpecifier::TypeSpecWithTemplate:
3140 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3141 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3142 break;
3143
3144 case NestedNameSpecifier::Global:
3145 // Don't need to write an associated value.
3146 break;
3147 }
3148 }
3149}
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003150
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003151void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003152 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003153 Record.push_back(Kind);
3154 switch (Kind) {
3155 case TemplateName::Template:
3156 AddDeclRef(Name.getAsTemplateDecl(), Record);
3157 break;
3158
3159 case TemplateName::OverloadedTemplate: {
3160 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3161 Record.push_back(OvT->size());
3162 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3163 I != E; ++I)
3164 AddDeclRef(*I, Record);
3165 break;
3166 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003167
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003168 case TemplateName::QualifiedTemplate: {
3169 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3170 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3171 Record.push_back(QualT->hasTemplateKeyword());
3172 AddDeclRef(QualT->getTemplateDecl(), Record);
3173 break;
3174 }
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003175
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003176 case TemplateName::DependentTemplate: {
3177 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3178 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3179 Record.push_back(DepT->isIdentifier());
3180 if (DepT->isIdentifier())
3181 AddIdentifierRef(DepT->getIdentifier(), Record);
3182 else
3183 Record.push_back(DepT->getOperator());
3184 break;
3185 }
3186 }
3187}
3188
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003189void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003190 RecordDataImpl &Record) {
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003191 Record.push_back(Arg.getKind());
3192 switch (Arg.getKind()) {
3193 case TemplateArgument::Null:
3194 break;
3195 case TemplateArgument::Type:
3196 AddTypeRef(Arg.getAsType(), Record);
3197 break;
3198 case TemplateArgument::Declaration:
3199 AddDeclRef(Arg.getAsDecl(), Record);
3200 break;
3201 case TemplateArgument::Integral:
3202 AddAPSInt(*Arg.getAsIntegral(), Record);
3203 AddTypeRef(Arg.getIntegralType(), Record);
3204 break;
3205 case TemplateArgument::Template:
Douglas Gregore4ff4b52011-01-05 18:58:31 +00003206 case TemplateArgument::TemplateExpansion:
3207 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Argyrios Kyrtzidis106caf922010-06-19 19:28:53 +00003208 break;
3209 case TemplateArgument::Expression:
3210 AddStmt(Arg.getAsExpr());
3211 break;
3212 case TemplateArgument::Pack:
3213 Record.push_back(Arg.pack_size());
3214 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3215 I != E; ++I)
3216 AddTemplateArgument(*I, Record);
3217 break;
3218 }
3219}
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003220
3221void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003222ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003223 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003224 assert(TemplateParams && "No TemplateParams!");
3225 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3226 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3227 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3228 Record.push_back(TemplateParams->size());
3229 for (TemplateParameterList::const_iterator
3230 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3231 P != PEnd; ++P)
3232 AddDeclRef(*P, Record);
3233}
3234
3235/// \brief Emit a template argument list.
3236void
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003237ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003238 RecordDataImpl &Record) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003239 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor1ccc8412010-11-07 23:05:16 +00003240 Record.push_back(TemplateArgs->size());
3241 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00003242 AddTemplateArgument(TemplateArgs->get(i), Record);
3243}
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003244
3245
3246void
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003247ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +00003248 Record.push_back(Set.size());
3249 for (UnresolvedSetImpl::const_iterator
3250 I = Set.begin(), E = Set.end(); I != E; ++I) {
3251 AddDeclRef(I.getDecl(), Record);
3252 Record.push_back(I.getAccess());
3253 }
3254}
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003255
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003256void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003257 RecordDataImpl &Record) {
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003258 Record.push_back(Base.isVirtual());
3259 Record.push_back(Base.isBaseOfClass());
3260 Record.push_back(Base.getAccessSpecifierAsWritten());
Nick Lewycky19b9f952010-07-26 16:56:01 +00003261 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003262 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregor752a5952011-01-03 22:36:02 +00003263 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3264 : SourceLocation(),
3265 Record);
Argyrios Kyrtzidis3701fcd2010-07-02 23:30:27 +00003266}
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003267
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003268void ASTWriter::FlushCXXBaseSpecifiers() {
3269 RecordData Record;
3270 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3271 Record.clear();
3272
3273 // Record the offset of this base-specifier set.
3274 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3275 if (Index == CXXBaseSpecifiersOffsets.size())
3276 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3277 else {
3278 if (Index > CXXBaseSpecifiersOffsets.size())
3279 CXXBaseSpecifiersOffsets.resize(Index + 1);
3280 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3281 }
3282
3283 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3284 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3285 Record.push_back(BEnd - B);
3286 for (; B != BEnd; ++B)
3287 AddCXXBaseSpecifier(*B, Record);
3288 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregord5853042010-10-30 04:28:16 +00003289
3290 // Flush any expressions that were written as part of the base specifiers.
3291 FlushStmts();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003292 }
3293
3294 CXXBaseSpecifiersToWrite.clear();
3295}
3296
Sebastian Redl55c0ad52010-08-18 23:56:21 +00003297void ASTWriter::AddCXXBaseOrMemberInitializers(
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003298 const CXXBaseOrMemberInitializer * const *BaseOrMembers,
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003299 unsigned NumBaseOrMembers, RecordDataImpl &Record) {
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003300 Record.push_back(NumBaseOrMembers);
3301 for (unsigned i=0; i != NumBaseOrMembers; ++i) {
3302 const CXXBaseOrMemberInitializer *Init = BaseOrMembers[i];
3303
3304 Record.push_back(Init->isBaseInitializer());
3305 if (Init->isBaseInitializer()) {
3306 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3307 Record.push_back(Init->isBaseVirtual());
3308 } else {
Francois Pichetd583da02010-12-04 09:14:42 +00003309 Record.push_back(Init->isIndirectMemberInitializer());
3310 if (Init->isIndirectMemberInitializer())
3311 AddDeclRef(Init->getIndirectMember(), Record);
3312 else
3313 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003314 }
Francois Pichetd583da02010-12-04 09:14:42 +00003315
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003316 AddSourceLocation(Init->getMemberLocation(), Record);
3317 AddStmt(Init->getInit());
Argyrios Kyrtzidis5b6a03f2010-08-09 10:54:12 +00003318 AddSourceLocation(Init->getLParenLoc(), Record);
3319 AddSourceLocation(Init->getRParenLoc(), Record);
3320 Record.push_back(Init->isWritten());
3321 if (Init->isWritten()) {
3322 Record.push_back(Init->getSourceOrder());
3323 } else {
3324 Record.push_back(Init->getNumArrayIndices());
3325 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3326 AddDeclRef(Init->getArrayIndex(i), Record);
3327 }
3328 }
3329}
3330
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003331void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3332 assert(D->DefinitionData);
3333 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3334 Record.push_back(Data.UserDeclaredConstructor);
3335 Record.push_back(Data.UserDeclaredCopyConstructor);
3336 Record.push_back(Data.UserDeclaredCopyAssignment);
3337 Record.push_back(Data.UserDeclaredDestructor);
3338 Record.push_back(Data.Aggregate);
3339 Record.push_back(Data.PlainOldData);
3340 Record.push_back(Data.Empty);
3341 Record.push_back(Data.Polymorphic);
3342 Record.push_back(Data.Abstract);
3343 Record.push_back(Data.HasTrivialConstructor);
3344 Record.push_back(Data.HasTrivialCopyConstructor);
3345 Record.push_back(Data.HasTrivialCopyAssignment);
3346 Record.push_back(Data.HasTrivialDestructor);
3347 Record.push_back(Data.ComputedVisibleConversions);
3348 Record.push_back(Data.DeclaredDefaultConstructor);
3349 Record.push_back(Data.DeclaredCopyConstructor);
3350 Record.push_back(Data.DeclaredCopyAssignment);
3351 Record.push_back(Data.DeclaredDestructor);
3352
3353 Record.push_back(Data.NumBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003354 if (Data.NumBases > 0)
3355 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3356 Record);
3357
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003358 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3359 Record.push_back(Data.NumVBases);
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003360 if (Data.NumVBases > 0)
3361 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3362 Record);
Argyrios Kyrtzidiseb39d9a2010-10-24 17:26:40 +00003363
3364 AddUnresolvedSet(Data.Conversions, Record);
3365 AddUnresolvedSet(Data.VisibleConversions, Record);
3366 // Data.Definition is the owning decl, no need to write it.
3367 AddDeclRef(Data.FirstFriend, Record);
3368}
3369
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003370void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redl07a89a82010-07-30 00:29:29 +00003371 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003372 assert(!Chain && "Cannot replace chain");
Sebastian Redl07a89a82010-07-30 00:29:29 +00003373 assert(FirstDeclID == NextDeclID &&
3374 FirstTypeID == NextTypeID &&
3375 FirstIdentID == NextIdentID &&
Sebastian Redld95a56e2010-08-04 18:21:41 +00003376 FirstSelectorID == NextSelectorID &&
Douglas Gregor91096292010-10-02 19:29:26 +00003377 FirstMacroID == NextMacroID &&
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003378 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redl07a89a82010-07-30 00:29:29 +00003379 "Setting chain after writing has started.");
3380 Chain = Reader;
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003381
3382 FirstDeclID += Chain->getTotalNumDecls();
3383 FirstTypeID += Chain->getTotalNumTypes();
3384 FirstIdentID += Chain->getTotalNumIdentifiers();
3385 FirstSelectorID += Chain->getTotalNumSelectors();
3386 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003387 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +00003388 NextDeclID = FirstDeclID;
3389 NextTypeID = FirstTypeID;
3390 NextIdentID = FirstIdentID;
3391 NextSelectorID = FirstSelectorID;
3392 NextMacroID = FirstMacroID;
Douglas Gregord4c5ed02010-10-29 22:39:52 +00003393 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redl07a89a82010-07-30 00:29:29 +00003394}
3395
Sebastian Redl539c5062010-08-18 23:57:32 +00003396void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlff4a2952010-07-23 23:49:55 +00003397 IdentifierIDs[II] = ID;
3398}
3399
Argyrios Kyrtzidisbb5c7eae2010-08-20 16:03:59 +00003400void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003401 // Always take the highest-numbered type index. This copes with an interesting
3402 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003403 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor9b3932c2010-10-05 18:37:06 +00003404 // keep the higher-numbered entry so that we can properly write it out to
3405 // the AST file.
3406 TypeIdx &StoredIdx = TypeIdxs[T];
3407 if (Idx.getIndex() >= StoredIdx.getIndex())
3408 StoredIdx = Idx;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003409}
3410
Sebastian Redl539c5062010-08-18 23:57:32 +00003411void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1ea025b2010-07-16 16:36:56 +00003412 DeclIDs[D] = ID;
Sebastian Redl85b2a6a2010-07-14 23:45:08 +00003413}
Sebastian Redl834bb972010-08-04 17:20:04 +00003414
Sebastian Redl539c5062010-08-18 23:57:32 +00003415void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl834bb972010-08-04 17:20:04 +00003416 SelectorIDs[S] = ID;
3417}
Douglas Gregor91096292010-10-02 19:29:26 +00003418
Michael J. Spencer4c0ffa82010-10-21 03:16:25 +00003419void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor91096292010-10-02 19:29:26 +00003420 MacroDefinition *MD) {
3421 MacroDefinitions[MD] = ID;
3422}
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003423
3424void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3425 assert(D->isDefinition());
3426 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3427 // We are interested when a PCH decl is modified.
3428 if (RD->getPCHLevel() > 0) {
3429 // A forward reference was mutated into a definition. Rewrite it.
3430 // FIXME: This happens during template instantiation, should we
3431 // have created a new definition decl instead ?
Argyrios Kyrtzidis47299722010-10-28 07:38:45 +00003432 RewriteDecl(RD);
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00003433 }
3434
3435 for (CXXRecordDecl::redecl_iterator
3436 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3437 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3438 if (Redecl == RD)
3439 continue;
3440
3441 // We are interested when a PCH decl is modified.
3442 if (Redecl->getPCHLevel() > 0) {
3443 UpdateRecord &Record = DeclUpdates[Redecl];
3444 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3445 assert(Redecl->DefinitionData);
3446 assert(Redecl->DefinitionData->Definition == D);
3447 AddDeclRef(D, Record); // the DefinitionDecl
3448 }
3449 }
3450 }
3451}
Argyrios Kyrtzidis01c2df42010-10-28 07:38:51 +00003452void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3453 // TU and namespaces are handled elsewhere.
3454 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3455 return;
3456
3457 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3458 return; // Not a source decl added to a DeclContext from PCH.
3459
3460 AddUpdatedDeclContext(DC);
3461}
Argyrios Kyrtzidise16a5302010-10-24 17:26:54 +00003462
3463void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3464 assert(D->isImplicit());
3465 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3466 return; // Not a source member added to a class from PCH.
3467 if (!isa<CXXMethodDecl>(D))
3468 return; // We are interested in lazily declared implicit methods.
3469
3470 // A decl coming from PCH was modified.
3471 assert(RD->isDefinition());
3472 UpdateRecord &Record = DeclUpdates[RD];
3473 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3474 AddDeclRef(D, Record);
3475}
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003476
3477void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3478 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisef80a012010-10-28 07:38:47 +00003479 // The specializations set is kept in the canonical template.
3480 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidis402dbbb2010-10-28 07:38:42 +00003481 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3482 return; // Not a source specialization added to a template from PCH.
3483
3484 UpdateRecord &Record = DeclUpdates[TD];
3485 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3486 AddDeclRef(D, Record);
3487}
Douglas Gregorf88e35b2010-11-30 06:16:57 +00003488
3489ASTSerializationListener::~ASTSerializationListener() { }