blob: f23a0cdce9af75ba35a024a2a0bdcc20c7ae4bfd [file] [log] [blame]
Sebastian Redl4ee2ad02010-08-18 23:56:31 +00001//===--- ASTWriter.cpp - AST File Writer ----------------------------------===//
Douglas Gregor2cf26342009-04-09 22:27:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sebastian Redla4232eb2010-08-18 23:56:21 +000010// This file defines the ASTWriter class, which writes AST files.
Douglas Gregor2cf26342009-04-09 22:27:44 +000011//
12//===----------------------------------------------------------------------===//
13
Sebastian Redl7faa2ec2010-08-18 23:56:37 +000014#include "clang/Serialization/ASTWriter.h"
Douglas Gregor89d99802010-11-30 06:16:57 +000015#include "clang/Serialization/ASTSerializationListener.h"
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +000016#include "ASTCommon.h"
Douglas Gregore737f502010-08-12 20:07:10 +000017#include "clang/Sema/Sema.h"
18#include "clang/Sema/IdentifierResolver.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000019#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclContextInternals.h"
John McCall2a7fb272010-08-25 05:32:35 +000022#include "clang/AST/DeclTemplate.h"
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000023#include "clang/AST/DeclFriend.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000024#include "clang/AST/Expr.h"
John McCall7a1fad32010-08-24 07:32:53 +000025#include "clang/AST/ExprCXX.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000026#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000027#include "clang/AST/TypeLocVisitor.h"
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000028#include "clang/Serialization/ASTReader.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000029#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000030#include "clang/Lex/PreprocessingRecord.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000031#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000032#include "clang/Lex/HeaderSearch.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000033#include "clang/Basic/FileManager.h"
Chris Lattner10e286a2010-11-23 19:19:34 +000034#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000035#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000036#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000037#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000038#include "clang/Basic/TargetInfo.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000039#include "clang/Basic/Version.h"
Douglas Gregor0a0d2b12011-03-23 00:50:03 +000040#include "clang/Basic/VersionTuple.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000041#include "llvm/ADT/APFloat.h"
42#include "llvm/ADT/APInt.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000043#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000044#include "llvm/Bitcode/BitstreamWriter.h"
Michael J. Spencerfbfd1802010-12-21 16:45:57 +000045#include "llvm/Support/FileSystem.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000046#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000047#include "llvm/Support/Path.h"
Douglas Gregorf62d43d2011-07-19 16:10:42 +000048#include <algorithm>
Chris Lattner3c304bd2009-04-11 18:40:46 +000049#include <cstdio>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000050#include <string.h>
Douglas Gregorf62d43d2011-07-19 16:10:42 +000051#include <utility>
Douglas Gregor2cf26342009-04-09 22:27:44 +000052using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000053using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000054
Sebastian Redlade50002010-07-30 17:03:48 +000055template <typename T, typename Allocator>
Chris Lattner5f9e2722011-07-23 10:55:15 +000056static StringRef data(const std::vector<T, Allocator> &v) {
57 if (v.empty()) return StringRef();
58 return StringRef(reinterpret_cast<const char*>(&v[0]),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000059 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000060}
Benjamin Kramer6e089c62011-04-24 17:44:50 +000061
62template <typename T>
Chris Lattner5f9e2722011-07-23 10:55:15 +000063static StringRef data(const SmallVectorImpl<T> &v) {
64 return StringRef(reinterpret_cast<const char*>(v.data()),
Benjamin Kramer6e089c62011-04-24 17:44:50 +000065 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000066}
67
Douglas Gregor2cf26342009-04-09 22:27:44 +000068//===----------------------------------------------------------------------===//
69// Type serialization
70//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000071
Douglas Gregor2cf26342009-04-09 22:27:44 +000072namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000073 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000074 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000075 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000076
77 public:
78 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000079 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000080
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000081 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000082 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000083
84 void VisitArrayType(const ArrayType *T);
85 void VisitFunctionType(const FunctionType *T);
86 void VisitTagType(const TagType *T);
87
88#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
89#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000090#include "clang/AST/TypeNodes.def"
91 };
92}
93
Sebastian Redl3397c552010-08-18 23:56:27 +000094void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000095 assert(false && "Built-in types are never serialized");
96}
97
Sebastian Redl3397c552010-08-18 23:56:27 +000098void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000099 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000100 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000101}
102
Sebastian Redl3397c552010-08-18 23:56:27 +0000103void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000104 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000105 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000106}
107
Sebastian Redl3397c552010-08-18 23:56:27 +0000108void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000109 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000110 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000111}
112
Sebastian Redl3397c552010-08-18 23:56:27 +0000113void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000114 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
115 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000116 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000117}
118
Sebastian Redl3397c552010-08-18 23:56:27 +0000119void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000120 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000121 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000122}
123
Sebastian Redl3397c552010-08-18 23:56:27 +0000124void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000125 Writer.AddTypeRef(T->getPointeeType(), Record);
126 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000127 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000128}
129
Sebastian Redl3397c552010-08-18 23:56:27 +0000130void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000131 Writer.AddTypeRef(T->getElementType(), Record);
132 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000133 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000134}
135
Sebastian Redl3397c552010-08-18 23:56:27 +0000136void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000137 VisitArrayType(T);
138 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000139 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000140}
141
Sebastian Redl3397c552010-08-18 23:56:27 +0000142void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000143 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000144 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000145}
146
Sebastian Redl3397c552010-08-18 23:56:27 +0000147void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000148 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000149 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
150 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000151 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000152 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000153}
154
Sebastian Redl3397c552010-08-18 23:56:27 +0000155void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000156 Writer.AddTypeRef(T->getElementType(), Record);
157 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000158 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000159 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000160}
161
Sebastian Redl3397c552010-08-18 23:56:27 +0000162void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000163 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000164 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000165}
166
Sebastian Redl3397c552010-08-18 23:56:27 +0000167void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000168 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000169 FunctionType::ExtInfo C = T->getExtInfo();
170 Record.push_back(C.getNoReturn());
Eli Friedmana49218e2011-04-09 08:18:08 +0000171 Record.push_back(C.getHasRegParm());
Rafael Espindola425ef722010-03-30 22:15:11 +0000172 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000173 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000174 Record.push_back(C.getCC());
John McCallf85e1932011-06-15 23:02:42 +0000175 Record.push_back(C.getProducesResult());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000176}
177
Sebastian Redl3397c552010-08-18 23:56:27 +0000178void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000179 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000180 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000181}
182
Sebastian Redl3397c552010-08-18 23:56:27 +0000183void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000184 VisitFunctionType(T);
185 Record.push_back(T->getNumArgs());
186 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
187 Writer.AddTypeRef(T->getArgType(I), Record);
188 Record.push_back(T->isVariadic());
189 Record.push_back(T->getTypeQuals());
Douglas Gregorc938c162011-01-26 05:01:58 +0000190 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redl60618fa2011-03-12 11:50:43 +0000191 Record.push_back(T->getExceptionSpecType());
192 if (T->getExceptionSpecType() == EST_Dynamic) {
193 Record.push_back(T->getNumExceptions());
194 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
195 Writer.AddTypeRef(T->getExceptionType(I), Record);
196 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
197 Writer.AddStmt(T->getNoexceptExpr());
198 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000199 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000200}
201
Sebastian Redl3397c552010-08-18 23:56:27 +0000202void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000203 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000204 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000205}
John McCalled976492009-12-04 22:46:56 +0000206
Sebastian Redl3397c552010-08-18 23:56:27 +0000207void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000208 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000209 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
210 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000211 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000212}
213
Sebastian Redl3397c552010-08-18 23:56:27 +0000214void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000215 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000216 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000217}
218
Sebastian Redl3397c552010-08-18 23:56:27 +0000219void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000220 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000221 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000222}
223
Sebastian Redl3397c552010-08-18 23:56:27 +0000224void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson395b4752009-06-24 19:06:50 +0000225 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000226 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000227}
228
Sean Huntca63c202011-05-24 22:41:36 +0000229void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
230 Writer.AddTypeRef(T->getBaseType(), Record);
231 Writer.AddTypeRef(T->getUnderlyingType(), Record);
232 Record.push_back(T->getUTTKind());
233 Code = TYPE_UNARY_TRANSFORM;
234}
235
Richard Smith34b41d92011-02-20 03:19:35 +0000236void ASTTypeWriter::VisitAutoType(const AutoType *T) {
237 Writer.AddTypeRef(T->getDeducedType(), Record);
238 Code = TYPE_AUTO;
239}
240
Sebastian Redl3397c552010-08-18 23:56:27 +0000241void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000242 Record.push_back(T->isDependentType());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000243 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000244 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000245 "Cannot serialize in the middle of a type definition");
246}
247
Sebastian Redl3397c552010-08-18 23:56:27 +0000248void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000249 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000250 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000251}
252
Sebastian Redl3397c552010-08-18 23:56:27 +0000253void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000254 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000255 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000256}
257
John McCall9d156a72011-01-06 01:58:22 +0000258void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
259 Writer.AddTypeRef(T->getModifiedType(), Record);
260 Writer.AddTypeRef(T->getEquivalentType(), Record);
261 Record.push_back(T->getAttrKind());
262 Code = TYPE_ATTRIBUTED;
263}
264
Mike Stump1eb44332009-09-09 15:08:12 +0000265void
Sebastian Redl3397c552010-08-18 23:56:27 +0000266ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000267 const SubstTemplateTypeParmType *T) {
268 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
269 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000270 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000271}
272
273void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000274ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
275 const SubstTemplateTypeParmPackType *T) {
276 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
277 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
278 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
279}
280
281void
Sebastian Redl3397c552010-08-18 23:56:27 +0000282ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000283 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000284 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000285 Writer.AddTemplateName(T->getTemplateName(), Record);
286 Record.push_back(T->getNumArgs());
287 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
288 ArgI != ArgE; ++ArgI)
289 Writer.AddTemplateArgument(*ArgI, Record);
Richard Smith3e4c6c42011-05-05 21:57:07 +0000290 Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
291 T->isCanonicalUnqualified() ? QualType()
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000292 : T->getCanonicalTypeInternal(),
293 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000294 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000295}
296
297void
Sebastian Redl3397c552010-08-18 23:56:27 +0000298ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000299 VisitArrayType(T);
300 Writer.AddStmt(T->getSizeExpr());
301 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000302 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000303}
304
305void
Sebastian Redl3397c552010-08-18 23:56:27 +0000306ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000307 const DependentSizedExtVectorType *T) {
308 // FIXME: Serialize this type (C++ only)
309 assert(false && "Cannot serialize dependent sized extended vector types");
310}
311
312void
Sebastian Redl3397c552010-08-18 23:56:27 +0000313ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000314 Record.push_back(T->getDepth());
315 Record.push_back(T->getIndex());
316 Record.push_back(T->isParameterPack());
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000317 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000318 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000319}
320
321void
Sebastian Redl3397c552010-08-18 23:56:27 +0000322ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000323 Record.push_back(T->getKeyword());
324 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
325 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000326 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
327 : T->getCanonicalTypeInternal(),
328 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000329 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000330}
331
332void
Sebastian Redl3397c552010-08-18 23:56:27 +0000333ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000334 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000335 Record.push_back(T->getKeyword());
336 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
337 Writer.AddIdentifierRef(T->getIdentifier(), Record);
338 Record.push_back(T->getNumArgs());
339 for (DependentTemplateSpecializationType::iterator
340 I = T->begin(), E = T->end(); I != E; ++I)
341 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000342 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000343}
344
Douglas Gregor7536dd52010-12-20 02:24:11 +0000345void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
346 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregorcded4f62011-01-14 17:04:44 +0000347 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
348 Record.push_back(*NumExpansions + 1);
349 else
350 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000351 Code = TYPE_PACK_EXPANSION;
352}
353
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000354void ASTTypeWriter::VisitParenType(const ParenType *T) {
355 Writer.AddTypeRef(T->getInnerType(), Record);
356 Code = TYPE_PAREN;
357}
358
Sebastian Redl3397c552010-08-18 23:56:27 +0000359void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000360 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000361 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
362 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000363 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000364}
365
Sebastian Redl3397c552010-08-18 23:56:27 +0000366void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCall3cb0ebd2010-03-10 03:28:59 +0000367 Writer.AddDeclRef(T->getDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000368 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000369 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000370}
371
Sebastian Redl3397c552010-08-18 23:56:27 +0000372void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000373 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000374 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000375}
376
Sebastian Redl3397c552010-08-18 23:56:27 +0000377void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000378 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000379 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000380 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000381 E = T->qual_end(); I != E; ++I)
382 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000383 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000384}
385
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000386void
Sebastian Redl3397c552010-08-18 23:56:27 +0000387ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000388 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000389 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000390}
391
John McCalla1ee0c52009-10-16 21:56:05 +0000392namespace {
393
394class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000395 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000396 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000397
398public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000399 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000400 : Writer(Writer), Record(Record) { }
401
John McCall51bd8032009-10-18 01:05:36 +0000402#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000403#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000404 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000405#include "clang/AST/TypeLocNodes.def"
406
John McCall51bd8032009-10-18 01:05:36 +0000407 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
408 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000409};
410
411}
412
John McCall51bd8032009-10-18 01:05:36 +0000413void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
414 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000415}
John McCall51bd8032009-10-18 01:05:36 +0000416void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000417 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
418 if (TL.needsExtraLocalData()) {
419 Record.push_back(TL.getWrittenTypeSpec());
420 Record.push_back(TL.getWrittenSignSpec());
421 Record.push_back(TL.getWrittenWidthSpec());
422 Record.push_back(TL.hasModeAttr());
423 }
John McCalla1ee0c52009-10-16 21:56:05 +0000424}
John McCall51bd8032009-10-18 01:05:36 +0000425void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
426 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000427}
John McCall51bd8032009-10-18 01:05:36 +0000428void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
429 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000430}
John McCall51bd8032009-10-18 01:05:36 +0000431void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
432 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000433}
John McCall51bd8032009-10-18 01:05:36 +0000434void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
435 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000436}
John McCall51bd8032009-10-18 01:05:36 +0000437void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
438 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000439}
John McCall51bd8032009-10-18 01:05:36 +0000440void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
441 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +0000442 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000443}
John McCall51bd8032009-10-18 01:05:36 +0000444void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
445 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
446 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
447 Record.push_back(TL.getSizeExpr() ? 1 : 0);
448 if (TL.getSizeExpr())
449 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000450}
John McCall51bd8032009-10-18 01:05:36 +0000451void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
452 VisitArrayTypeLoc(TL);
453}
454void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
455 VisitArrayTypeLoc(TL);
456}
457void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
458 VisitArrayTypeLoc(TL);
459}
460void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
461 DependentSizedArrayTypeLoc TL) {
462 VisitArrayTypeLoc(TL);
463}
464void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
465 DependentSizedExtVectorTypeLoc TL) {
466 Writer.AddSourceLocation(TL.getNameLoc(), Record);
467}
468void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
469 Writer.AddSourceLocation(TL.getNameLoc(), Record);
470}
471void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
472 Writer.AddSourceLocation(TL.getNameLoc(), Record);
473}
474void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +0000475 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
476 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Douglas Gregordab60ad2010-10-01 18:44:50 +0000477 Record.push_back(TL.getTrailingReturn());
John McCall51bd8032009-10-18 01:05:36 +0000478 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
479 Writer.AddDeclRef(TL.getArg(i), Record);
480}
481void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
482 VisitFunctionTypeLoc(TL);
483}
484void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
485 VisitFunctionTypeLoc(TL);
486}
John McCalled976492009-12-04 22:46:56 +0000487void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
488 Writer.AddSourceLocation(TL.getNameLoc(), Record);
489}
John McCall51bd8032009-10-18 01:05:36 +0000490void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
491 Writer.AddSourceLocation(TL.getNameLoc(), Record);
492}
493void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000494 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
495 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
496 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000497}
498void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000499 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
500 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
501 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
502 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000503}
504void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
505 Writer.AddSourceLocation(TL.getNameLoc(), Record);
506}
Sean Huntca63c202011-05-24 22:41:36 +0000507void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
508 Writer.AddSourceLocation(TL.getKWLoc(), Record);
509 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
510 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
511 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
512}
Richard Smith34b41d92011-02-20 03:19:35 +0000513void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
514 Writer.AddSourceLocation(TL.getNameLoc(), Record);
515}
John McCall51bd8032009-10-18 01:05:36 +0000516void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
517 Writer.AddSourceLocation(TL.getNameLoc(), Record);
518}
519void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
520 Writer.AddSourceLocation(TL.getNameLoc(), Record);
521}
John McCall9d156a72011-01-06 01:58:22 +0000522void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
523 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
524 if (TL.hasAttrOperand()) {
525 SourceRange range = TL.getAttrOperandParensRange();
526 Writer.AddSourceLocation(range.getBegin(), Record);
527 Writer.AddSourceLocation(range.getEnd(), Record);
528 }
529 if (TL.hasAttrExprOperand()) {
530 Expr *operand = TL.getAttrExprOperand();
531 Record.push_back(operand ? 1 : 0);
532 if (operand) Writer.AddStmt(operand);
533 } else if (TL.hasAttrEnumOperand()) {
534 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
535 }
536}
John McCall51bd8032009-10-18 01:05:36 +0000537void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
538 Writer.AddSourceLocation(TL.getNameLoc(), Record);
539}
John McCall49a832b2009-10-18 09:09:24 +0000540void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
541 SubstTemplateTypeParmTypeLoc TL) {
542 Writer.AddSourceLocation(TL.getNameLoc(), Record);
543}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000544void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
545 SubstTemplateTypeParmPackTypeLoc TL) {
546 Writer.AddSourceLocation(TL.getNameLoc(), Record);
547}
John McCall51bd8032009-10-18 01:05:36 +0000548void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
549 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000550 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
551 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
552 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
553 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000554 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
555 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000556}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000557void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
558 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
559 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
560}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000561void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000562 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor9e876872011-03-01 18:12:44 +0000563 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000564}
John McCall3cb0ebd2010-03-10 03:28:59 +0000565void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
566 Writer.AddSourceLocation(TL.getNameLoc(), Record);
567}
Douglas Gregor4714c122010-03-31 17:34:00 +0000568void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000569 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000570 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000571 Writer.AddSourceLocation(TL.getNameLoc(), Record);
572}
John McCall33500952010-06-11 00:33:02 +0000573void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
574 DependentTemplateSpecializationTypeLoc TL) {
575 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000576 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall33500952010-06-11 00:33:02 +0000577 Writer.AddSourceLocation(TL.getNameLoc(), Record);
578 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
579 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
580 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000581 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
582 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000583}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000584void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
585 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
586}
John McCall51bd8032009-10-18 01:05:36 +0000587void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
588 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000589}
590void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
591 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000592 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
593 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
594 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
595 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000596}
John McCall54e14c42009-10-22 22:37:11 +0000597void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
598 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000599}
John McCalla1ee0c52009-10-16 21:56:05 +0000600
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000601//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000602// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000603//===----------------------------------------------------------------------===//
604
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000605static void EmitBlockID(unsigned ID, const char *Name,
606 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000607 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000608 Record.clear();
609 Record.push_back(ID);
610 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
611
612 // Emit the block name if present.
613 if (Name == 0 || Name[0] == 0) return;
614 Record.clear();
615 while (*Name)
616 Record.push_back(*Name++);
617 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
618}
619
620static void EmitRecordID(unsigned ID, const char *Name,
621 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000622 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000623 Record.clear();
624 Record.push_back(ID);
625 while (*Name)
626 Record.push_back(*Name++);
627 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000628}
629
630static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000631 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000632#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000633 RECORD(STMT_STOP);
634 RECORD(STMT_NULL_PTR);
635 RECORD(STMT_NULL);
636 RECORD(STMT_COMPOUND);
637 RECORD(STMT_CASE);
638 RECORD(STMT_DEFAULT);
639 RECORD(STMT_LABEL);
640 RECORD(STMT_IF);
641 RECORD(STMT_SWITCH);
642 RECORD(STMT_WHILE);
643 RECORD(STMT_DO);
644 RECORD(STMT_FOR);
645 RECORD(STMT_GOTO);
646 RECORD(STMT_INDIRECT_GOTO);
647 RECORD(STMT_CONTINUE);
648 RECORD(STMT_BREAK);
649 RECORD(STMT_RETURN);
650 RECORD(STMT_DECL);
651 RECORD(STMT_ASM);
652 RECORD(EXPR_PREDEFINED);
653 RECORD(EXPR_DECL_REF);
654 RECORD(EXPR_INTEGER_LITERAL);
655 RECORD(EXPR_FLOATING_LITERAL);
656 RECORD(EXPR_IMAGINARY_LITERAL);
657 RECORD(EXPR_STRING_LITERAL);
658 RECORD(EXPR_CHARACTER_LITERAL);
659 RECORD(EXPR_PAREN);
660 RECORD(EXPR_UNARY_OPERATOR);
661 RECORD(EXPR_SIZEOF_ALIGN_OF);
662 RECORD(EXPR_ARRAY_SUBSCRIPT);
663 RECORD(EXPR_CALL);
664 RECORD(EXPR_MEMBER);
665 RECORD(EXPR_BINARY_OPERATOR);
666 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
667 RECORD(EXPR_CONDITIONAL_OPERATOR);
668 RECORD(EXPR_IMPLICIT_CAST);
669 RECORD(EXPR_CSTYLE_CAST);
670 RECORD(EXPR_COMPOUND_LITERAL);
671 RECORD(EXPR_EXT_VECTOR_ELEMENT);
672 RECORD(EXPR_INIT_LIST);
673 RECORD(EXPR_DESIGNATED_INIT);
674 RECORD(EXPR_IMPLICIT_VALUE_INIT);
675 RECORD(EXPR_VA_ARG);
676 RECORD(EXPR_ADDR_LABEL);
677 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000678 RECORD(EXPR_CHOOSE);
679 RECORD(EXPR_GNU_NULL);
680 RECORD(EXPR_SHUFFLE_VECTOR);
681 RECORD(EXPR_BLOCK);
682 RECORD(EXPR_BLOCK_DECL_REF);
Peter Collingbournef111d932011-04-15 00:35:48 +0000683 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattner0558df22009-04-27 00:49:53 +0000684 RECORD(EXPR_OBJC_STRING_LITERAL);
685 RECORD(EXPR_OBJC_ENCODE);
686 RECORD(EXPR_OBJC_SELECTOR_EXPR);
687 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
688 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
689 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
690 RECORD(EXPR_OBJC_KVC_REF_EXPR);
691 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000692 RECORD(STMT_OBJC_FOR_COLLECTION);
693 RECORD(STMT_OBJC_CATCH);
694 RECORD(STMT_OBJC_FINALLY);
695 RECORD(STMT_OBJC_AT_TRY);
696 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
697 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000698 RECORD(EXPR_CXX_OPERATOR_CALL);
699 RECORD(EXPR_CXX_CONSTRUCT);
700 RECORD(EXPR_CXX_STATIC_CAST);
701 RECORD(EXPR_CXX_DYNAMIC_CAST);
702 RECORD(EXPR_CXX_REINTERPRET_CAST);
703 RECORD(EXPR_CXX_CONST_CAST);
704 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
705 RECORD(EXPR_CXX_BOOL_LITERAL);
706 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000707 RECORD(EXPR_CXX_TYPEID_EXPR);
708 RECORD(EXPR_CXX_TYPEID_TYPE);
709 RECORD(EXPR_CXX_UUIDOF_EXPR);
710 RECORD(EXPR_CXX_UUIDOF_TYPE);
711 RECORD(EXPR_CXX_THIS);
712 RECORD(EXPR_CXX_THROW);
713 RECORD(EXPR_CXX_DEFAULT_ARG);
714 RECORD(EXPR_CXX_BIND_TEMPORARY);
715 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
716 RECORD(EXPR_CXX_NEW);
717 RECORD(EXPR_CXX_DELETE);
718 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
719 RECORD(EXPR_EXPR_WITH_CLEANUPS);
720 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
721 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
722 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
723 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
724 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
725 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
726 RECORD(EXPR_CXX_NOEXCEPT);
727 RECORD(EXPR_OPAQUE_VALUE);
728 RECORD(EXPR_BINARY_TYPE_TRAIT);
729 RECORD(EXPR_PACK_EXPANSION);
730 RECORD(EXPR_SIZEOF_PACK);
731 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000732 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000733#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000734}
Mike Stump1eb44332009-09-09 15:08:12 +0000735
Sebastian Redla4232eb2010-08-18 23:56:21 +0000736void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000737 RecordData Record;
738 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000740#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
741#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Sebastian Redl3397c552010-08-18 23:56:27 +0000743 // AST Top-Level Block.
Sebastian Redlf29f0a22010-08-18 23:57:22 +0000744 BLOCK(AST_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000745 RECORD(ORIGINAL_FILE_NAME);
Douglas Gregor31d375f2011-05-06 21:43:30 +0000746 RECORD(ORIGINAL_FILE_ID);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000747 RECORD(TYPE_OFFSET);
748 RECORD(DECL_OFFSET);
749 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000750 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000751 RECORD(IDENTIFIER_OFFSET);
752 RECORD(IDENTIFIER_TABLE);
753 RECORD(EXTERNAL_DEFINITIONS);
754 RECORD(SPECIAL_TYPES);
755 RECORD(STATISTICS);
756 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000757 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000758 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
759 RECORD(SELECTOR_OFFSETS);
760 RECORD(METHOD_POOL);
761 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000762 RECORD(SOURCE_LOCATION_OFFSETS);
763 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000764 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000765 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000766 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000767 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redla93e3b52010-07-08 22:01:51 +0000768 RECORD(CHAINED_METADATA);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000769 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000770 RECORD(TU_UPDATE_LEXICAL);
771 RECORD(REDECLS_UPDATE_LATEST);
772 RECORD(SEMA_DECL_REFS);
773 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
774 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
775 RECORD(DECL_REPLACEMENTS);
776 RECORD(UPDATE_VISIBLE);
777 RECORD(DECL_UPDATE_OFFSETS);
778 RECORD(DECL_UPDATES);
779 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
780 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000781 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000782 RECORD(HEADER_SEARCH_TABLE);
Douglas Gregor837593f2011-08-04 16:39:39 +0000783 RECORD(ORIGINAL_PCH_DIR);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000784 RECORD(FP_PRAGMA_OPTIONS);
785 RECORD(OPENCL_EXTENSIONS);
Sean Huntebcbe1d2011-05-04 23:29:54 +0000786 RECORD(DELEGATING_CTORS);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000787 RECORD(FILE_SOURCE_LOCATION_OFFSETS);
788 RECORD(KNOWN_NAMESPACES);
Douglas Gregor837593f2011-08-04 16:39:39 +0000789 RECORD(MODULE_OFFSET_MAP);
790 RECORD(SOURCE_MANAGER_LINE_TABLE);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000791
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000792 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000793 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000794 RECORD(SM_SLOC_FILE_ENTRY);
795 RECORD(SM_SLOC_BUFFER_ENTRY);
796 RECORD(SM_SLOC_BUFFER_BLOB);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000797 RECORD(SM_SLOC_EXPANSION_ENTRY);
Mike Stump1eb44332009-09-09 15:08:12 +0000798
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000799 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000800 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000801 RECORD(PP_MACRO_OBJECT_LIKE);
802 RECORD(PP_MACRO_FUNCTION_LIKE);
803 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000804
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000805 // Decls and Types block.
806 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000807 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000808 RECORD(TYPE_COMPLEX);
809 RECORD(TYPE_POINTER);
810 RECORD(TYPE_BLOCK_POINTER);
811 RECORD(TYPE_LVALUE_REFERENCE);
812 RECORD(TYPE_RVALUE_REFERENCE);
813 RECORD(TYPE_MEMBER_POINTER);
814 RECORD(TYPE_CONSTANT_ARRAY);
815 RECORD(TYPE_INCOMPLETE_ARRAY);
816 RECORD(TYPE_VARIABLE_ARRAY);
817 RECORD(TYPE_VECTOR);
818 RECORD(TYPE_EXT_VECTOR);
819 RECORD(TYPE_FUNCTION_PROTO);
820 RECORD(TYPE_FUNCTION_NO_PROTO);
821 RECORD(TYPE_TYPEDEF);
822 RECORD(TYPE_TYPEOF_EXPR);
823 RECORD(TYPE_TYPEOF);
824 RECORD(TYPE_RECORD);
825 RECORD(TYPE_ENUM);
826 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000827 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000828 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000829 RECORD(TYPE_DECLTYPE);
830 RECORD(TYPE_ELABORATED);
831 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
832 RECORD(TYPE_UNRESOLVED_USING);
833 RECORD(TYPE_INJECTED_CLASS_NAME);
834 RECORD(TYPE_OBJC_OBJECT);
835 RECORD(TYPE_TEMPLATE_TYPE_PARM);
836 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
837 RECORD(TYPE_DEPENDENT_NAME);
838 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
839 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
840 RECORD(TYPE_PAREN);
841 RECORD(TYPE_PACK_EXPANSION);
842 RECORD(TYPE_ATTRIBUTED);
843 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000844 RECORD(DECL_TRANSLATION_UNIT);
845 RECORD(DECL_TYPEDEF);
846 RECORD(DECL_ENUM);
847 RECORD(DECL_RECORD);
848 RECORD(DECL_ENUM_CONSTANT);
849 RECORD(DECL_FUNCTION);
850 RECORD(DECL_OBJC_METHOD);
851 RECORD(DECL_OBJC_INTERFACE);
852 RECORD(DECL_OBJC_PROTOCOL);
853 RECORD(DECL_OBJC_IVAR);
854 RECORD(DECL_OBJC_AT_DEFS_FIELD);
855 RECORD(DECL_OBJC_CLASS);
856 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
857 RECORD(DECL_OBJC_CATEGORY);
858 RECORD(DECL_OBJC_CATEGORY_IMPL);
859 RECORD(DECL_OBJC_IMPLEMENTATION);
860 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
861 RECORD(DECL_OBJC_PROPERTY);
862 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000863 RECORD(DECL_FIELD);
864 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000865 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000866 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000867 RECORD(DECL_FILE_SCOPE_ASM);
868 RECORD(DECL_BLOCK);
869 RECORD(DECL_CONTEXT_LEXICAL);
870 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000871 RECORD(DECL_NAMESPACE);
872 RECORD(DECL_NAMESPACE_ALIAS);
873 RECORD(DECL_USING);
874 RECORD(DECL_USING_SHADOW);
875 RECORD(DECL_USING_DIRECTIVE);
876 RECORD(DECL_UNRESOLVED_USING_VALUE);
877 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
878 RECORD(DECL_LINKAGE_SPEC);
879 RECORD(DECL_CXX_RECORD);
880 RECORD(DECL_CXX_METHOD);
881 RECORD(DECL_CXX_CONSTRUCTOR);
882 RECORD(DECL_CXX_DESTRUCTOR);
883 RECORD(DECL_CXX_CONVERSION);
884 RECORD(DECL_ACCESS_SPEC);
885 RECORD(DECL_FRIEND);
886 RECORD(DECL_FRIEND_TEMPLATE);
887 RECORD(DECL_CLASS_TEMPLATE);
888 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
889 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
890 RECORD(DECL_FUNCTION_TEMPLATE);
891 RECORD(DECL_TEMPLATE_TYPE_PARM);
892 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
893 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
894 RECORD(DECL_STATIC_ASSERT);
895 RECORD(DECL_CXX_BASE_SPECIFIERS);
896 RECORD(DECL_INDIRECTFIELD);
897 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
898
Douglas Gregora72d8c42011-06-03 02:27:19 +0000899 // Statements and Exprs can occur in the Decls and Types block.
900 AddStmtsExprs(Stream, Record);
901
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000902 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
Chandler Carruthf70d12d2011-07-15 07:25:21 +0000903 RECORD(PPD_MACRO_EXPANSION);
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000904 RECORD(PPD_MACRO_DEFINITION);
905 RECORD(PPD_INCLUSION_DIRECTIVE);
906
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000907#undef RECORD
908#undef BLOCK
909 Stream.ExitBlock();
910}
911
Douglas Gregore650c8c2009-07-07 00:12:59 +0000912/// \brief Adjusts the given filename to only write out the portion of the
913/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000914///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000915/// \param Filename the file name to adjust.
916///
917/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
918/// the returned filename will be adjusted by this system root.
919///
920/// \returns either the original filename (if it needs no adjustment) or the
921/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000922static const char *
Douglas Gregor832d6202011-07-22 16:35:34 +0000923adjustFilenameForRelocatablePCH(const char *Filename, StringRef isysroot) {
Douglas Gregore650c8c2009-07-07 00:12:59 +0000924 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000925
Douglas Gregor832d6202011-07-22 16:35:34 +0000926 if (isysroot.empty())
Douglas Gregore650c8c2009-07-07 00:12:59 +0000927 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000928
Douglas Gregore650c8c2009-07-07 00:12:59 +0000929 // Verify that the filename and the system root have the same prefix.
930 unsigned Pos = 0;
Douglas Gregor832d6202011-07-22 16:35:34 +0000931 for (; Filename[Pos] && Pos < isysroot.size(); ++Pos)
Douglas Gregore650c8c2009-07-07 00:12:59 +0000932 if (Filename[Pos] != isysroot[Pos])
933 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000934
Douglas Gregore650c8c2009-07-07 00:12:59 +0000935 // We hit the end of the filename before we hit the end of the system root.
936 if (!Filename[Pos])
937 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000938
Douglas Gregore650c8c2009-07-07 00:12:59 +0000939 // If the file name has a '/' at the current position, skip over the '/'.
940 // We distinguish sysroot-based includes from absolute includes by the
941 // absence of '/' at the beginning of sysroot-based includes.
942 if (Filename[Pos] == '/')
943 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000944
Douglas Gregore650c8c2009-07-07 00:12:59 +0000945 return Filename + Pos;
946}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000947
Sebastian Redl3397c552010-08-18 23:56:27 +0000948/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Douglas Gregor832d6202011-07-22 16:35:34 +0000949void ASTWriter::WriteMetadata(ASTContext &Context, StringRef isysroot,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000950 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000951 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000952
Douglas Gregore650c8c2009-07-07 00:12:59 +0000953 // Metadata
954 const TargetInfo &Target = Context.Target;
955 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl77f46032010-07-09 21:00:24 +0000956 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000957 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl3397c552010-08-18 23:56:27 +0000958 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
959 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregore650c8c2009-07-07 00:12:59 +0000960 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
961 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
962 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl77f46032010-07-09 21:00:24 +0000963 // Target triple or chained PCH name
964 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregore650c8c2009-07-07 00:12:59 +0000965 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000966
Douglas Gregore650c8c2009-07-07 00:12:59 +0000967 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000968 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
969 Record.push_back(VERSION_MAJOR);
970 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000971 Record.push_back(CLANG_VERSION_MAJOR);
972 Record.push_back(CLANG_VERSION_MINOR);
Douglas Gregor832d6202011-07-22 16:35:34 +0000973 Record.push_back(!isysroot.empty());
Sebastian Redl77f46032010-07-09 21:00:24 +0000974 // FIXME: This writes the absolute path for chained headers.
Jonathan D. Turner48d2c3f2011-07-26 18:21:30 +0000975 const std::string &BlobStr =
976 Chain ? Chain->getFileName() : Target.getTriple().getTriple();
Sebastian Redl77f46032010-07-09 21:00:24 +0000977 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Douglas Gregor31d375f2011-05-06 21:43:30 +0000979 // Original file name and file ID
Douglas Gregorb64c1932009-05-12 01:31:05 +0000980 SourceManager &SM = Context.getSourceManager();
981 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
982 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000983 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregorb64c1932009-05-12 01:31:05 +0000984 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
985 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
986
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000987 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000989 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000990
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +0000991 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000992 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000993 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000994 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000995 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000996 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregor31d375f2011-05-06 21:43:30 +0000997
998 Record.clear();
999 Record.push_back(SM.getMainFileID().getOpaqueValue());
1000 Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
Douglas Gregorb64c1932009-05-12 01:31:05 +00001001 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001002
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00001003 // Original PCH directory
1004 if (!OutputFile.empty() && OutputFile != "-") {
1005 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1006 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1007 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1008 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1009
1010 llvm::SmallString<128> OutputPath(OutputFile);
1011
1012 llvm::sys::fs::make_absolute(OutputPath);
1013 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1014
1015 RecordData Record;
1016 Record.push_back(ORIGINAL_PCH_DIR);
1017 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1018 }
1019
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001020 // Repository branch/version information.
1021 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001022 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001023 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1024 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +00001025 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001026 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +00001027 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
1028 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +00001029}
1030
1031/// \brief Write the LangOptions structure.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001032void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001033 RecordData Record;
1034 Record.push_back(LangOpts.Trigraphs);
1035 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1036 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1037 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1038 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00001039 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001040 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1041 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1042 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1043 Record.push_back(LangOpts.C99); // C99 Support
Peter Collingbourne7e7fbd02011-04-15 00:35:23 +00001044 Record.push_back(LangOpts.C1X); // C1X Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001045 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencerdae4ac42010-10-21 05:21:48 +00001046 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
1047 // already saved elsewhere.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001048 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1049 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001050 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +00001051
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001052 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1053 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001054 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian412e7982010-02-09 19:31:38 +00001055 // modern abi enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001056 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian412e7982010-02-09 19:31:38 +00001057 // modern abi enabled.
Fariborz Jahanianf84109e2011-01-07 18:59:25 +00001058 Record.push_back(LangOpts.AppleKext); // Apple's kernel extensions ABI
Ted Kremenekc32647d2010-12-23 21:35:43 +00001059 Record.push_back(LangOpts.ObjCDefaultSynthProperties); // Objective-C auto-synthesized
1060 // properties enabled.
Douglas Gregor74da19f2011-06-14 23:20:43 +00001061 Record.push_back(LangOpts.ObjCInferRelatedResultType);
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +00001062 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump1eb44332009-09-09 15:08:12 +00001063
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001064 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001065 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1066 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +00001067 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001068 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Anders Carlssonda4b7cf2011-02-19 23:53:54 +00001069 Record.push_back(LangOpts.ObjCExceptions);
Anders Carlsson7da99b02011-02-23 03:04:54 +00001070 Record.push_back(LangOpts.CXXExceptions);
1071 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001072
Douglas Gregor6f755502011-02-01 15:15:22 +00001073 Record.push_back(LangOpts.MSBitfields); // MS-compatible structure layout
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001074 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1075 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1076 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1077
Chris Lattnerea5ce472009-04-27 07:35:58 +00001078 // Whether static initializers are protected by locks.
1079 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00001080 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001081 Record.push_back(LangOpts.Blocks); // block extension to C
1082 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1083 // they are unused.
1084 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1085 // (modulo the platform support).
1086
Chris Lattnera4d71452010-06-26 21:25:03 +00001087 Record.push_back(LangOpts.getSignedOverflowBehavior());
1088 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001089
1090 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +00001091 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001092 // defined.
1093 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1094 // opposed to __DYNAMIC__).
1095 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1096
1097 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1098 // used (instead of C99 semantics).
1099 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Chandler Carruth0d2d1bc2011-04-23 20:05:38 +00001100 Record.push_back(LangOpts.Deprecated); // Should __DEPRECATED be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +00001101 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
1102 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +00001103 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
1104 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +00001105 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Argyrios Kyrtzidisb1bdced2011-01-15 02:56:16 +00001106 Record.push_back(LangOpts.ShortEnums); // Should the enum type be equivalent
1107 // to the smallest integer type with
1108 // enough room.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001109 Record.push_back(LangOpts.getGCMode());
1110 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +00001111 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001112 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00001113 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne08a53262010-12-01 19:14:57 +00001114 Record.push_back(LangOpts.CUDA);
Mike Stump9c276ae2009-12-12 01:27:46 +00001115 Record.push_back(LangOpts.CatchUndefined);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00001116 Record.push_back(LangOpts.DefaultFPContract);
Anders Carlsson92f58222009-08-22 22:30:33 +00001117 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +00001118 Record.push_back(LangOpts.SpellChecking);
Roman Divackycfe9af22011-03-01 17:40:53 +00001119 Record.push_back(LangOpts.MRTD);
John McCallf85e1932011-06-15 23:02:42 +00001120 Record.push_back(LangOpts.ObjCAutoRefCount);
1121 Record.push_back(LangOpts.ObjCInferRelatedReturnType);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001122 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001123}
1124
Douglas Gregor14f79002009-04-10 03:52:48 +00001125//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001126// stat cache Serialization
1127//===----------------------------------------------------------------------===//
1128
1129namespace {
1130// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +00001131class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001132public:
1133 typedef const char * key_type;
1134 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001135
Chris Lattner74e976b2010-11-23 19:28:12 +00001136 typedef struct stat data_type;
1137 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001138
1139 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001140 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001141 }
Mike Stump1eb44332009-09-09 15:08:12 +00001142
1143 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001144 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001145 data_type_ref Data) {
1146 unsigned StrLen = strlen(path);
1147 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +00001148 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001149 clang::io::Emit8(Out, DataLen);
1150 return std::make_pair(StrLen + 1, DataLen);
1151 }
Mike Stump1eb44332009-09-09 15:08:12 +00001152
Chris Lattner5f9e2722011-07-23 10:55:15 +00001153 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001154 Out.write(path, KeyLen);
1155 }
Mike Stump1eb44332009-09-09 15:08:12 +00001156
Chris Lattner5f9e2722011-07-23 10:55:15 +00001157 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001158 data_type_ref Data, unsigned DataLen) {
1159 using namespace clang::io;
1160 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +00001161
Chris Lattner74e976b2010-11-23 19:28:12 +00001162 Emit32(Out, (uint32_t) Data.st_ino);
1163 Emit32(Out, (uint32_t) Data.st_dev);
1164 Emit16(Out, (uint16_t) Data.st_mode);
1165 Emit64(Out, (uint64_t) Data.st_mtime);
1166 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001167
1168 assert(Out.tell() - Start == DataLen && "Wrong data length");
1169 }
1170};
1171} // end anonymous namespace
1172
Sebastian Redl3397c552010-08-18 23:56:27 +00001173/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001174void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001175 // Build the on-disk hash table containing information about every
1176 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +00001177 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001178 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001179 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001180 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001181 Stat != StatEnd; ++Stat, ++NumStatEntries) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001182 StringRef Filename = Stat->first();
Chris Lattner1e5f83b2011-07-14 18:24:21 +00001183 Generator.insert(Filename.data(), Stat->second);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001184 }
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001186 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001187 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001188 uint32_t BucketOffset;
1189 {
1190 llvm::raw_svector_ostream Out(StatCacheData);
1191 // Make sure that no bucket is at offset 0
1192 clang::io::Emit32(Out, 0);
1193 BucketOffset = Generator.Emit(Out);
1194 }
1195
1196 // Create a blob abbreviation
1197 using namespace llvm;
1198 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001199 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001200 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1201 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1202 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1203 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1204
1205 // Write the stat cache
1206 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001207 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001208 Record.push_back(BucketOffset);
1209 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001210 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001211}
1212
1213//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001214// Source Manager Serialization
1215//===----------------------------------------------------------------------===//
1216
1217/// \brief Create an abbreviation for the SLocEntry that refers to a
1218/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001219static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001220 using namespace llvm;
1221 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001222 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001223 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1224 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1225 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1226 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001227 // FileEntry fields.
1228 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1229 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor14f79002009-04-10 03:52:48 +00001230 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001231 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001232}
1233
1234/// \brief Create an abbreviation for the SLocEntry that refers to a
1235/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001236static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001237 using namespace llvm;
1238 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001239 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001240 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1241 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1242 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1243 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1244 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001245 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001246}
1247
1248/// \brief Create an abbreviation for the SLocEntry that refers to a
1249/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001250static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001251 using namespace llvm;
1252 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001253 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001254 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001255 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001256}
1257
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001258/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1259/// expansion.
1260static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001261 using namespace llvm;
1262 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001263 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001264 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1265 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1266 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1267 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001268 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001269 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001270}
1271
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001272namespace {
1273 // Trait used for the on-disk hash table of header search information.
1274 class HeaderFileInfoTrait {
1275 ASTWriter &Writer;
1276 HeaderSearch &HS;
1277
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001278 // Keep track of the framework names we've used during serialization.
1279 SmallVector<char, 128> FrameworkStringData;
1280 llvm::StringMap<unsigned> FrameworkNameOffset;
1281
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001282 public:
1283 HeaderFileInfoTrait(ASTWriter &Writer, HeaderSearch &HS)
1284 : Writer(Writer), HS(HS) { }
1285
1286 typedef const char *key_type;
1287 typedef key_type key_type_ref;
1288
1289 typedef HeaderFileInfo data_type;
1290 typedef const data_type &data_type_ref;
1291
1292 static unsigned ComputeHash(const char *path) {
1293 // The hash is based only on the filename portion of the key, so that the
1294 // reader can match based on filenames when symlinking or excess path
1295 // elements ("foo/../", "../") change the form of the name. However,
1296 // complete path is still the key.
1297 return llvm::HashString(llvm::sys::path::filename(path));
1298 }
1299
1300 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00001301 EmitKeyDataLength(raw_ostream& Out, const char *path,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001302 data_type_ref Data) {
1303 unsigned StrLen = strlen(path);
1304 clang::io::Emit16(Out, StrLen);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001305 unsigned DataLen = 1 + 2 + 4 + 4;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001306 clang::io::Emit8(Out, DataLen);
1307 return std::make_pair(StrLen + 1, DataLen);
1308 }
1309
Chris Lattner5f9e2722011-07-23 10:55:15 +00001310 void EmitKey(raw_ostream& Out, const char *path, unsigned KeyLen) {
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001311 Out.write(path, KeyLen);
1312 }
1313
Chris Lattner5f9e2722011-07-23 10:55:15 +00001314 void EmitData(raw_ostream &Out, key_type_ref,
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001315 data_type_ref Data, unsigned DataLen) {
1316 using namespace clang::io;
1317 uint64_t Start = Out.tell(); (void)Start;
1318
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001319 unsigned char Flags = (Data.isImport << 5)
1320 | (Data.isPragmaOnce << 4)
1321 | (Data.DirInfo << 2)
1322 | (Data.Resolved << 1)
1323 | Data.IndexHeaderMapHeader;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001324 Emit8(Out, (uint8_t)Flags);
1325 Emit16(Out, (uint16_t) Data.NumIncludes);
1326
1327 if (!Data.ControllingMacro)
1328 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1329 else
1330 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001331
1332 unsigned Offset = 0;
1333 if (!Data.Framework.empty()) {
1334 // If this header refers into a framework, save the framework name.
1335 llvm::StringMap<unsigned>::iterator Pos
1336 = FrameworkNameOffset.find(Data.Framework);
1337 if (Pos == FrameworkNameOffset.end()) {
1338 Offset = FrameworkStringData.size() + 1;
1339 FrameworkStringData.append(Data.Framework.begin(),
1340 Data.Framework.end());
1341 FrameworkStringData.push_back(0);
1342
1343 FrameworkNameOffset[Data.Framework] = Offset;
1344 } else
1345 Offset = Pos->second;
1346 }
1347 Emit32(Out, Offset);
1348
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001349 assert(Out.tell() - Start == DataLen && "Wrong data length");
1350 }
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001351
1352 const char *strings_begin() const { return FrameworkStringData.begin(); }
1353 const char *strings_end() const { return FrameworkStringData.end(); }
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001354 };
1355} // end anonymous namespace
1356
1357/// \brief Write the header search block for the list of files that
1358///
1359/// \param HS The header search structure to save.
1360///
1361/// \param Chain Whether we're creating a chained AST file.
Douglas Gregor832d6202011-07-22 16:35:34 +00001362void ASTWriter::WriteHeaderSearch(HeaderSearch &HS, StringRef isysroot) {
Chris Lattner5f9e2722011-07-23 10:55:15 +00001363 SmallVector<const FileEntry *, 16> FilesByUID;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001364 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1365
1366 if (FilesByUID.size() > HS.header_file_size())
1367 FilesByUID.resize(HS.header_file_size());
1368
1369 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1370 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
Chris Lattner5f9e2722011-07-23 10:55:15 +00001371 SmallVector<const char *, 4> SavedStrings;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001372 unsigned NumHeaderSearchEntries = 0;
1373 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1374 const FileEntry *File = FilesByUID[UID];
1375 if (!File)
1376 continue;
1377
1378 const HeaderFileInfo &HFI = HS.header_file_begin()[UID];
1379 if (HFI.External && Chain)
1380 continue;
1381
1382 // Turn the file name into an absolute path, if it isn't already.
1383 const char *Filename = File->getName();
1384 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1385
1386 // If we performed any translation on the file name at all, we need to
1387 // save this string, since the generator will refer to it later.
1388 if (Filename != File->getName()) {
1389 Filename = strdup(Filename);
1390 SavedStrings.push_back(Filename);
1391 }
1392
1393 Generator.insert(Filename, HFI, GeneratorTrait);
1394 ++NumHeaderSearchEntries;
1395 }
1396
1397 // Create the on-disk hash table in a buffer.
1398 llvm::SmallString<4096> TableData;
1399 uint32_t BucketOffset;
1400 {
1401 llvm::raw_svector_ostream Out(TableData);
1402 // Make sure that no bucket is at offset 0
1403 clang::io::Emit32(Out, 0);
1404 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1405 }
1406
1407 // Create a blob abbreviation
1408 using namespace llvm;
1409 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1410 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1411 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1412 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001413 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001414 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1415 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1416
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001417 // Write the header search table
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001418 RecordData Record;
1419 Record.push_back(HEADER_SEARCH_TABLE);
1420 Record.push_back(BucketOffset);
1421 Record.push_back(NumHeaderSearchEntries);
Douglas Gregorb4dc4852011-07-28 04:50:02 +00001422 Record.push_back(TableData.size());
1423 TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001424 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1425
1426 // Free all of the strings we had to duplicate.
1427 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1428 free((void*)SavedStrings[I]);
1429}
1430
Douglas Gregor14f79002009-04-10 03:52:48 +00001431/// \brief Writes the block containing the serialized form of the
1432/// source manager.
1433///
1434/// TODO: We should probably use an on-disk hash table (stored in a
1435/// blob), indexed based on the file name, so that we only create
1436/// entries for files that we actually need. In the common case (no
1437/// errors), we probably won't have to create file entries for any of
1438/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001439void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001440 const Preprocessor &PP,
Douglas Gregor832d6202011-07-22 16:35:34 +00001441 StringRef isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001442 RecordData Record;
1443
Chris Lattnerf04ad692009-04-10 17:16:57 +00001444 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001445 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001446
1447 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001448 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1449 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1450 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001451 unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001452
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001453 // Write out the source location entry table. We skip the first
1454 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001455 std::vector<uint32_t> SLocEntryOffsets;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001456 // Write out the offsets of only source location file entries.
1457 // We will go through them in ASTReader::validateFileEntries().
1458 std::vector<uint32_t> SLocFileEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001459 RecordData PreloadSLocs;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001460 SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1461 for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001462 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001463 // Get this source location entry.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001464 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001465
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001466 // Record the offset of this source-location entry.
1467 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1468
1469 // Figure out which record code to use.
1470 unsigned Code;
1471 if (SLoc->isFile()) {
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001472 if (SLoc->getFile().getContentCache()->OrigEntry) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001473 Code = SM_SLOC_FILE_ENTRY;
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001474 SLocFileEntryOffsets.push_back(Stream.GetCurrentBitNo());
1475 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001476 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001477 } else
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001478 Code = SM_SLOC_EXPANSION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001479 Record.clear();
1480 Record.push_back(Code);
1481
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001482 // Starting offset of this entry within this module, so skip the dummy.
1483 Record.push_back(SLoc->getOffset() - 2);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001484 if (SLoc->isFile()) {
1485 const SrcMgr::FileInfo &File = SLoc->getFile();
1486 Record.push_back(File.getIncludeLoc().getRawEncoding());
1487 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1488 Record.push_back(File.hasLineDirectives());
1489
1490 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001491 if (Content->OrigEntry) {
1492 assert(Content->OrigEntry == Content->ContentsEntry &&
1493 "Writing to AST an overriden file is not supported");
1494
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001495 // The source location entry is a file. The blob associated
1496 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001497
Douglas Gregor2d52be52010-03-21 22:49:54 +00001498 // Emit size/modification time for this file.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001499 Record.push_back(Content->OrigEntry->getSize());
1500 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregor2d52be52010-03-21 22:49:54 +00001501
Douglas Gregore650c8c2009-07-07 00:12:59 +00001502 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001503 const char *Filename = Content->OrigEntry->getName();
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001504 llvm::SmallString<128> FilePath(Filename);
Anders Carlsson2c10c802011-03-08 16:04:35 +00001505
1506 // Ask the file manager to fixup the relative path for us. This will
1507 // honor the working directory.
1508 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1509
1510 // FIXME: This call to make_absolute shouldn't be necessary, the
1511 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001512 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001513 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Douglas Gregore650c8c2009-07-07 00:12:59 +00001515 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001516 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001517 } else {
1518 // The source location entry is a buffer. The blob associated
1519 // with this entry contains the contents of the buffer.
1520
1521 // We add one to the size so that we capture the trailing NULL
1522 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1523 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001524 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001525 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001526 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001527 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001528 StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001529 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001530 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001531 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Chris Lattner5f9e2722011-07-23 10:55:15 +00001532 StringRef(Buffer->getBufferStart(),
Daniel Dunbarec312a12009-08-24 09:31:37 +00001533 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001534
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001535 if (strcmp(Name, "<built-in>") == 0) {
1536 PreloadSLocs.push_back(SLocEntryOffsets.size());
1537 }
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001538 }
1539 } else {
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001540 // The source location entry is a macro expansion.
Chandler Carruth17287622011-07-26 04:56:51 +00001541 const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
Chandler Carruth78df8362011-07-26 04:41:47 +00001542 Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1543 Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
1544 Record.push_back(Expansion.getExpansionLocEnd().getRawEncoding());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001545
1546 // Compute the token length for this macro expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001547 unsigned NextOffset = SourceMgr.getNextLocalOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001548 if (I + 1 != N)
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001549 NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001550 Record.push_back(NextOffset - SLoc->getOffset() - 1);
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001551 Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001552 }
1553 }
1554
Douglas Gregorc9490c02009-04-16 22:23:12 +00001555 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001556
1557 if (SLocEntryOffsets.empty())
1558 return;
1559
Sebastian Redl3397c552010-08-18 23:56:27 +00001560 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001561 // table is used for lazily loading source-location information.
1562 using namespace llvm;
1563 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001564 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001565 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001566 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001567 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1568 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001569
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001570 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001571 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001572 Record.push_back(SLocEntryOffsets.size());
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001573 Record.push_back(SourceMgr.getNextLocalOffset() - 1); // skip dummy
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001574 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001575
Argyrios Kyrtzidis4cdb0e22011-06-02 20:01:46 +00001576 Abbrev = new BitCodeAbbrev();
1577 Abbrev->Add(BitCodeAbbrevOp(FILE_SOURCE_LOCATION_OFFSETS));
1578 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1579 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1580 unsigned SLocFileOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1581
1582 Record.clear();
1583 Record.push_back(FILE_SOURCE_LOCATION_OFFSETS);
1584 Record.push_back(SLocFileEntryOffsets.size());
1585 Stream.EmitRecordWithBlob(SLocFileOffsetsAbbrev, Record,
1586 data(SLocFileEntryOffsets));
1587
Sebastian Redl3397c552010-08-18 23:56:27 +00001588 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001589 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001590 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001591
1592 // Write the line table. It depends on remapping working, so it must come
1593 // after the source location offsets.
1594 if (SourceMgr.hasLineTable()) {
1595 LineTableInfo &LineTable = SourceMgr.getLineTable();
1596
1597 Record.clear();
1598 // Emit the file names
1599 Record.push_back(LineTable.getNumFilenames());
1600 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1601 // Emit the file name
1602 const char *Filename = LineTable.getFilename(I);
1603 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1604 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1605 Record.push_back(FilenameLen);
1606 if (FilenameLen)
1607 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1608 }
1609
1610 // Emit the line entries
1611 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1612 L != LEnd; ++L) {
1613 // Only emit entries for local files.
1614 if (L->first < 0)
1615 continue;
1616
1617 // Emit the file ID
1618 Record.push_back(L->first);
1619
1620 // Emit the line entries
1621 Record.push_back(L->second.size());
1622 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1623 LEEnd = L->second.end();
1624 LE != LEEnd; ++LE) {
1625 Record.push_back(LE->FileOffset);
1626 Record.push_back(LE->LineNo);
1627 Record.push_back(LE->FilenameID);
1628 Record.push_back((unsigned)LE->FileKind);
1629 Record.push_back(LE->IncludeOffset);
1630 }
1631 }
1632 Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
1633 }
Douglas Gregor14f79002009-04-10 03:52:48 +00001634}
1635
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001636//===----------------------------------------------------------------------===//
1637// Preprocessor Serialization
1638//===----------------------------------------------------------------------===//
1639
Douglas Gregor9c736102011-02-10 18:20:09 +00001640static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1641 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1642 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1643 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1644 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1645 return X.first->getName().compare(Y.first->getName());
1646}
1647
Chris Lattner0b1fb982009-04-10 17:15:23 +00001648/// \brief Writes the block containing the serialized form of the
1649/// preprocessor.
1650///
Sebastian Redla4232eb2010-08-18 23:56:21 +00001651void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001652 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001653
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001654 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1655 if (PP.getCounterValue() != 0) {
1656 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001657 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001658 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001659 }
1660
1661 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001662 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001663
Sebastian Redl3397c552010-08-18 23:56:27 +00001664 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001665 // FIXME: use diagnostics subsystem for localization etc.
1666 if (PP.SawDateOrTime())
1667 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001668
Douglas Gregorecdcb882010-10-20 22:00:55 +00001669
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001670 // Loop over all the macro definitions that are live at the end of the file,
1671 // emitting each to the PP section.
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001672 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001673
Douglas Gregor9c736102011-02-10 18:20:09 +00001674 // Construct the list of macro definitions that need to be serialized.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001675 SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
Douglas Gregor9c736102011-02-10 18:20:09 +00001676 MacrosToEmit;
1677 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor040a8042011-02-11 00:26:14 +00001678 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1679 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001680 I != E; ++I) {
Douglas Gregor9c736102011-02-10 18:20:09 +00001681 MacroDefinitionsSeen.insert(I->first);
1682 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1683 }
1684
1685 // Sort the set of macro definitions that need to be serialized by the
1686 // name of the macro, to provide a stable ordering.
1687 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1688 &compareMacroDefinitions);
1689
Douglas Gregor040a8042011-02-11 00:26:14 +00001690 // Resolve any identifiers that defined macros at the time they were
1691 // deserialized, adding them to the list of macros to emit (if appropriate).
1692 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1693 IdentifierInfo *Name
1694 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1695 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1696 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1697 }
1698
Douglas Gregor9c736102011-02-10 18:20:09 +00001699 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1700 const IdentifierInfo *Name = MacrosToEmit[I].first;
1701 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor040a8042011-02-11 00:26:14 +00001702 if (!MI)
1703 continue;
1704
Sebastian Redl3397c552010-08-18 23:56:27 +00001705 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001706 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl3397c552010-08-18 23:56:27 +00001707 // Also skip macros from a AST file if we're chaining.
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001708
1709 // FIXME: There is a (probably minor) optimization we could do here, if
1710 // the macro comes from the original PCH but the identifier comes from a
1711 // chained PCH, by storing the offset into the original PCH rather than
1712 // writing the macro definition a second time.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001713 if (MI->isBuiltinMacro() ||
Douglas Gregor9c736102011-02-10 18:20:09 +00001714 (Chain && Name->isFromAST() && MI->isFromAST()))
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001715 continue;
1716
Douglas Gregor9c736102011-02-10 18:20:09 +00001717 AddIdentifierRef(Name, Record);
1718 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001719 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1720 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001721
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001722 unsigned Code;
1723 if (MI->isObjectLike()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001724 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001725 } else {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001726 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001727
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001728 Record.push_back(MI->isC99Varargs());
1729 Record.push_back(MI->isGNUVarargs());
1730 Record.push_back(MI->getNumArgs());
1731 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1732 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001733 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001734 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001735
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001736 // If we have a detailed preprocessing record, record the macro definition
1737 // ID that corresponds to this macro.
1738 if (PPRec)
1739 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer20249a12010-10-21 03:16:25 +00001740
Douglas Gregorc9490c02009-04-16 22:23:12 +00001741 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001742 Record.clear();
1743
Chris Lattnerdf961c22009-04-10 18:08:30 +00001744 // Emit the tokens array.
1745 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1746 // Note that we know that the preprocessor does not have any annotation
1747 // tokens in it because they are created by the parser, and thus can't be
1748 // in a macro definition.
1749 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001750
Chris Lattnerdf961c22009-04-10 18:08:30 +00001751 Record.push_back(Tok.getLocation().getRawEncoding());
1752 Record.push_back(Tok.getLength());
1753
Chris Lattnerdf961c22009-04-10 18:08:30 +00001754 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1755 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001756 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001757 // FIXME: Should translate token kind to a stable encoding.
1758 Record.push_back(Tok.getKind());
1759 // FIXME: Should translate token flags to a stable encoding.
1760 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001761
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001762 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001763 Record.clear();
1764 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001765 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001766 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001767 Stream.ExitBlock();
1768
1769 if (PPRec)
1770 WritePreprocessorDetail(*PPRec);
1771}
1772
1773void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
1774 if (PPRec.begin(Chain) == PPRec.end(Chain))
1775 return;
1776
1777 // Enter the preprocessor block.
1778 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001779
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001780 // If the preprocessor has a preprocessing record, emit it.
1781 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001782 using namespace llvm;
1783
1784 // Set up the abbreviation for
1785 unsigned InclusionAbbrev = 0;
1786 {
1787 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1788 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
1789 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1790 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1791 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1792 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1793 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1794 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1795 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1796 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1797 }
1798
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001799 unsigned FirstPreprocessorEntityID
1800 = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
1801 + NUM_PREDEF_PP_ENTITY_IDS;
1802 unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001803 RecordData Record;
Douglas Gregor8f1231b2011-07-22 06:10:01 +00001804 uint64_t BitsInChain = Chain? Chain->TotalModulesSizeInBits : 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001805 for (PreprocessingRecord::iterator E = PPRec.begin(Chain),
1806 EEnd = PPRec.end(Chain);
Douglas Gregor7338a922011-08-04 17:06:18 +00001807 E != EEnd;
1808 (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001809 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001810
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001811 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1812 // Record this macro definition's location.
1813 MacroID ID = getMacroDefinitionID(MD);
1814
1815 // Don't write the macro definition if it is from another AST file.
1816 if (ID < FirstMacroID)
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001817 continue;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001818
Douglas Gregor89d99802010-11-30 06:16:57 +00001819 // Notify the serialization listener that we're serializing this entity.
1820 if (SerializationListener)
1821 SerializationListener->SerializedPreprocessedEntity(*E,
Douglas Gregor8f1231b2011-07-22 06:10:01 +00001822 BitsInChain + Stream.GetCurrentBitNo());
Douglas Gregor89d99802010-11-30 06:16:57 +00001823
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001824 unsigned Position = ID - FirstMacroID;
1825 if (Position != MacroDefinitionOffsets.size()) {
1826 if (Position > MacroDefinitionOffsets.size())
1827 MacroDefinitionOffsets.resize(Position + 1);
1828
1829 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
1830 } else
1831 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor89d99802010-11-30 06:16:57 +00001832
Douglas Gregor7338a922011-08-04 17:06:18 +00001833 Record.push_back(NextPreprocessorEntityID);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001834 Record.push_back(ID);
1835 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1836 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1837 AddIdentifierRef(MD->getName(), Record);
1838 AddSourceLocation(MD->getLocation(), Record);
1839 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1840 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001841 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001842
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001843 // Notify the serialization listener that we're serializing this entity.
1844 if (SerializationListener)
1845 SerializationListener->SerializedPreprocessedEntity(*E,
Douglas Gregor8f1231b2011-07-22 06:10:01 +00001846 BitsInChain + Stream.GetCurrentBitNo());
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001847
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001848 if (MacroExpansion *ME = dyn_cast<MacroExpansion>(*E)) {
Douglas Gregor7338a922011-08-04 17:06:18 +00001849 Record.push_back(NextPreprocessorEntityID);
Chandler Carruth9e5bb852011-07-14 08:20:46 +00001850 AddSourceLocation(ME->getSourceRange().getBegin(), Record);
1851 AddSourceLocation(ME->getSourceRange().getEnd(), Record);
1852 AddIdentifierRef(ME->getName(), Record);
1853 Record.push_back(getMacroDefinitionID(ME->getDefinition()));
Chandler Carruthf70d12d2011-07-15 07:25:21 +00001854 Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001855 continue;
1856 }
1857
1858 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1859 Record.push_back(PPD_INCLUSION_DIRECTIVE);
Douglas Gregor7338a922011-08-04 17:06:18 +00001860 Record.push_back(NextPreprocessorEntityID);
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001861 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1862 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1863 Record.push_back(ID->getFileName().size());
1864 Record.push_back(ID->wasInQuotes());
1865 Record.push_back(static_cast<unsigned>(ID->getKind()));
1866 llvm::SmallString<64> Buffer;
1867 Buffer += ID->getFileName();
1868 Buffer += ID->getFile()->getName();
1869 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1870 continue;
1871 }
1872
1873 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1874 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001875 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001876
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001877 // Write the offsets table for the preprocessing record.
1878 if (NumPreprocessingRecords > 0) {
1879 // Write the offsets table for identifier IDs.
1880 using namespace llvm;
1881 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001882 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001883 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001884 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001885 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
Douglas Gregorfb2d9e02011-08-04 16:36:56 +00001886 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first macro def
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001887 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1888 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001889
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001890 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001891 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001892 Record.push_back(NumPreprocessingRecords);
Douglas Gregor272b6bc2011-08-04 18:56:47 +00001893 Record.push_back(FirstPreprocessorEntityID - NUM_PREDEF_PP_ENTITY_IDS);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001894 Record.push_back(MacroDefinitionOffsets.size());
Douglas Gregorfb2d9e02011-08-04 16:36:56 +00001895 Record.push_back(FirstMacroID - NUM_PREDEF_MACRO_IDS);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001896 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001897 data(MacroDefinitionOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001898 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001899}
1900
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001901void ASTWriter::WritePragmaDiagnosticMappings(const Diagnostic &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001902 RecordData Record;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001903 for (Diagnostic::DiagStatePointsTy::const_iterator
1904 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1905 I != E; ++I) {
1906 const Diagnostic::DiagStatePoint &point = *I;
1907 if (point.Loc.isInvalid())
1908 continue;
1909
1910 Record.push_back(point.Loc.getRawEncoding());
1911 for (Diagnostic::DiagState::iterator
1912 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
1913 unsigned diag = I->first, map = I->second;
1914 if (map & 0x10) { // mapping from a diagnostic pragma.
1915 Record.push_back(diag);
1916 Record.push_back(map & 0x7);
1917 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001918 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001919 Record.push_back(-1); // mark the end of the diag/map pairs for this
1920 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001921 }
1922
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00001923 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001924 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001925}
1926
Anders Carlssonc8505782011-03-06 18:41:18 +00001927void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
1928 if (CXXBaseSpecifiersOffsets.empty())
1929 return;
1930
1931 RecordData Record;
1932
1933 // Create a blob abbreviation for the C++ base specifiers offsets.
1934 using namespace llvm;
1935
1936 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1937 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
1938 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
1939 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1940 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1941
Douglas Gregore92b8a12011-08-04 00:01:48 +00001942 // Write the base specifier offsets table.
Anders Carlssonc8505782011-03-06 18:41:18 +00001943 Record.clear();
1944 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
1945 Record.push_back(CXXBaseSpecifiersOffsets.size());
1946 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001947 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00001948}
1949
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001950//===----------------------------------------------------------------------===//
1951// Type Serialization
1952//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001953
Sebastian Redl3397c552010-08-18 23:56:27 +00001954/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001955void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00001956 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001957 if (Idx.getIndex() == 0) // we haven't seen this type before.
1958 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00001959
Douglas Gregor97475832010-10-05 18:37:06 +00001960 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00001961
Douglas Gregor2cf26342009-04-09 22:27:44 +00001962 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001963 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00001964 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001965 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00001966 else if (TypeOffsets.size() < Index) {
1967 TypeOffsets.resize(Index + 1);
1968 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001969 }
1970
1971 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001972
Douglas Gregor2cf26342009-04-09 22:27:44 +00001973 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00001974 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001975
Douglas Gregora4923eb2009-11-16 21:35:15 +00001976 if (T.hasLocalNonFastQualifiers()) {
1977 Qualifiers Qs = T.getLocalQualifiers();
1978 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001979 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001980 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00001981 } else {
1982 switch (T->getTypeClass()) {
1983 // For all of the concrete, non-dependent types, call the
1984 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001985#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001986 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001987#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001988#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001989 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001990 }
1991
1992 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001993 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001994
1995 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001996 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001997}
1998
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001999//===----------------------------------------------------------------------===//
2000// Declaration Serialization
2001//===----------------------------------------------------------------------===//
2002
Douglas Gregor2cf26342009-04-09 22:27:44 +00002003/// \brief Write the block containing all of the declaration IDs
2004/// lexically declared within the given DeclContext.
2005///
2006/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2007/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002008uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00002009 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002010 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00002011 return 0;
2012
Douglas Gregorc9490c02009-04-16 22:23:12 +00002013 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002014 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002015 Record.push_back(DECL_CONTEXT_LEXICAL);
Chris Lattner5f9e2722011-07-23 10:55:15 +00002016 SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002017 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
2018 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002019 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002020
Douglas Gregor25123082009-04-22 22:34:57 +00002021 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002022 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00002023 return Offset;
2024}
2025
Sebastian Redla4232eb2010-08-18 23:56:21 +00002026void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002027 using namespace llvm;
2028 RecordData Record;
2029
2030 // Write the type offsets array
2031 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002032 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002033 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
Douglas Gregora119da02011-08-02 16:26:37 +00002034 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
Sebastian Redl1476ed42010-07-16 16:36:56 +00002035 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2036 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2037 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002038 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002039 Record.push_back(TypeOffsets.size());
Douglas Gregora119da02011-08-02 16:26:37 +00002040 Record.push_back(FirstTypeID - NUM_PREDEF_TYPE_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002041 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002042
2043 // Write the declaration offsets array
2044 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002045 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002046 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
Douglas Gregor496c7092011-08-03 15:48:04 +00002047 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
Sebastian Redl1476ed42010-07-16 16:36:56 +00002048 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2049 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2050 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002051 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002052 Record.push_back(DeclOffsets.size());
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002053 Record.push_back(FirstDeclID - NUM_PREDEF_DECL_IDS);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002054 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00002055}
2056
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002057//===----------------------------------------------------------------------===//
2058// Global Method Pool and Selector Serialization
2059//===----------------------------------------------------------------------===//
2060
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002061namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002062// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00002063class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002064 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002065
2066public:
2067 typedef Selector key_type;
2068 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002069
Sebastian Redl5d050072010-08-04 17:20:04 +00002070 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002071 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00002072 ObjCMethodList Instance, Factory;
2073 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002074 typedef const data_type& data_type_ref;
2075
Sebastian Redl3397c552010-08-18 23:56:27 +00002076 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00002077
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002078 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00002079 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002080 }
Mike Stump1eb44332009-09-09 15:08:12 +00002081
2082 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002083 EmitKeyDataLength(raw_ostream& Out, Selector Sel,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002084 data_type_ref Methods) {
2085 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2086 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00002087 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2088 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002089 Method = Method->Next)
2090 if (Method->Method)
2091 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00002092 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002093 Method = Method->Next)
2094 if (Method->Method)
2095 DataLen += 4;
2096 clang::io::Emit16(Out, DataLen);
2097 return std::make_pair(KeyLen, DataLen);
2098 }
Mike Stump1eb44332009-09-09 15:08:12 +00002099
Chris Lattner5f9e2722011-07-23 10:55:15 +00002100 void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002101 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002102 assert((Start >> 32) == 0 && "Selector key offset too large");
2103 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002104 unsigned N = Sel.getNumArgs();
2105 clang::io::Emit16(Out, N);
2106 if (N == 0)
2107 N = 1;
2108 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002109 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002110 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2111 }
Mike Stump1eb44332009-09-09 15:08:12 +00002112
Chris Lattner5f9e2722011-07-23 10:55:15 +00002113 void EmitData(raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002114 data_type_ref Methods, unsigned DataLen) {
2115 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002116 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002117 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002118 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002119 Method = Method->Next)
2120 if (Method->Method)
2121 ++NumInstanceMethods;
2122
2123 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002124 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002125 Method = Method->Next)
2126 if (Method->Method)
2127 ++NumFactoryMethods;
2128
2129 clang::io::Emit16(Out, NumInstanceMethods);
2130 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002131 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002132 Method = Method->Next)
2133 if (Method->Method)
2134 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002135 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002136 Method = Method->Next)
2137 if (Method->Method)
2138 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002139
2140 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002141 }
2142};
2143} // end anonymous namespace
2144
Sebastian Redl059612d2010-08-03 21:58:15 +00002145/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002146///
2147/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002148/// in an on-disk hash table indexed by the selector. The hash table also
2149/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002150void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002151 using namespace llvm;
2152
Sebastian Redl059612d2010-08-03 21:58:15 +00002153 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002154 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002155 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002156 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002157 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002158 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002159 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002160 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002161
Sebastian Redl059612d2010-08-03 21:58:15 +00002162 // Create the on-disk hash table representation. We walk through every
2163 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002164 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002165 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002166 I = SelectorIDs.begin(), E = SelectorIDs.end();
2167 I != E; ++I) {
2168 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002169 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002170 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002171 I->second,
2172 ObjCMethodList(),
2173 ObjCMethodList()
2174 };
2175 if (F != SemaRef.MethodPool.end()) {
2176 Data.Instance = F->second.first;
2177 Data.Factory = F->second.second;
2178 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002179 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002180 // changed.
2181 if (Chain && I->second < FirstSelectorID) {
2182 // Selector already exists. Did it change?
2183 bool changed = false;
2184 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2185 M = M->Next) {
2186 if (M->Method->getPCHLevel() == 0)
2187 changed = true;
2188 }
2189 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2190 M = M->Next) {
2191 if (M->Method->getPCHLevel() == 0)
2192 changed = true;
2193 }
2194 if (!changed)
2195 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002196 } else if (Data.Instance.Method || Data.Factory.Method) {
2197 // A new method pool entry.
2198 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002199 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002200 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002201 }
2202
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002203 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002204 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002205 uint32_t BucketOffset;
2206 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002207 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002208 llvm::raw_svector_ostream Out(MethodPool);
2209 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002210 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002211 BucketOffset = Generator.Emit(Out, Trait);
2212 }
2213
2214 // Create a blob abbreviation
2215 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002216 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002217 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002218 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002219 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2220 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2221
Douglas Gregor83941df2009-04-25 17:48:32 +00002222 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002223 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002224 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002225 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002226 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002227 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002228
2229 // Create a blob abbreviation for the selector table offsets.
2230 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002231 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002232 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002233 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor83941df2009-04-25 17:48:32 +00002234 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2235 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2236
2237 // Write the selector offsets table.
2238 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002239 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002240 Record.push_back(SelectorOffsets.size());
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002241 Record.push_back(FirstSelectorID - NUM_PREDEF_SELECTOR_IDS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002242 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002243 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002244 }
2245}
2246
Sebastian Redl3397c552010-08-18 23:56:27 +00002247/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002248void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002249 using namespace llvm;
2250 if (SemaRef.ReferencedSelectors.empty())
2251 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002252
Fariborz Jahanian32019832010-07-23 19:11:11 +00002253 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002254
Sebastian Redl3397c552010-08-18 23:56:27 +00002255 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002256 // very tricky to fix, and given that @selector shouldn't really appear in
2257 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002258 for (DenseMap<Selector, SourceLocation>::iterator S =
2259 SemaRef.ReferencedSelectors.begin(),
2260 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2261 Selector Sel = (*S).first;
2262 SourceLocation Loc = (*S).second;
2263 AddSelectorRef(Sel, Record);
2264 AddSourceLocation(Loc, Record);
2265 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002266 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002267}
2268
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002269//===----------------------------------------------------------------------===//
2270// Identifier Table Serialization
2271//===----------------------------------------------------------------------===//
2272
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002273namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002274class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002275 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002276 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002277
Douglas Gregora92193e2009-04-28 21:18:29 +00002278 /// \brief Determines whether this is an "interesting" identifier
2279 /// that needs a full IdentifierInfo structure written into the hash
2280 /// table.
2281 static bool isInterestingIdentifier(const IdentifierInfo *II) {
2282 return II->isPoisoned() ||
2283 II->isExtensionToken() ||
2284 II->hasMacroDefinition() ||
2285 II->getObjCOrBuiltinID() ||
2286 II->getFETokenInfo<void>();
2287 }
2288
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002289public:
2290 typedef const IdentifierInfo* key_type;
2291 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002292
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002293 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002294 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002295
Sebastian Redl3397c552010-08-18 23:56:27 +00002296 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00002297 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002298
2299 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002300 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002301 }
Mike Stump1eb44332009-09-09 15:08:12 +00002302
2303 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002304 EmitKeyDataLength(raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002305 IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002306 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002307 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
2308 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00002309 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00002310 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00002311 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00002312 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00002313 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2314 DEnd = IdentifierResolver::end();
2315 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002316 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002317 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002318 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002319 // We emit the key length after the data length so that every
2320 // string is preceded by a 16-bit length. This matches the PTH
2321 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002322 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002323 return std::make_pair(KeyLen, DataLen);
2324 }
Mike Stump1eb44332009-09-09 15:08:12 +00002325
Chris Lattner5f9e2722011-07-23 10:55:15 +00002326 void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002327 unsigned KeyLen) {
2328 // Record the location of the key data. This is used when generating
2329 // the mapping from persistent IDs to strings.
2330 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002331 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002332 }
Mike Stump1eb44332009-09-09 15:08:12 +00002333
Chris Lattner5f9e2722011-07-23 10:55:15 +00002334 void EmitData(raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002335 IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002336 if (!isInterestingIdentifier(II)) {
2337 clang::io::Emit32(Out, ID << 1);
2338 return;
2339 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002340
Douglas Gregora92193e2009-04-28 21:18:29 +00002341 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002342 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002343 bool hasMacroDefinition =
2344 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00002345 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00002346 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002347 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
2348 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2349 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002350 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002351 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002352 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002353
Douglas Gregor37e26842009-04-21 23:56:24 +00002354 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00002355 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00002356
Douglas Gregor668c1a42009-04-21 22:25:48 +00002357 // Emit the declaration IDs in reverse order, because the
2358 // IdentifierResolver provides the declarations as they would be
2359 // visible (e.g., the function "stat" would come before the struct
2360 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2361 // adds declarations to the end of the list (so we need to see the
2362 // struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002363 // Only emit declarations that aren't from a chained PCH, though.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002364 SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00002365 IdentifierResolver::end());
Chris Lattner5f9e2722011-07-23 10:55:15 +00002366 for (SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
Douglas Gregor668c1a42009-04-21 22:25:48 +00002367 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002368 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002369 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002370 }
2371};
2372} // end anonymous namespace
2373
Sebastian Redl3397c552010-08-18 23:56:27 +00002374/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002375///
2376/// The identifier table consists of a blob containing string data
2377/// (the actual identifiers themselves) and a separate "offsets" index
2378/// that maps identifier IDs to locations within the blob.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002379void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002380 using namespace llvm;
2381
2382 // Create and write out the blob that contains the identifier
2383 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002384 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002385 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002386 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump1eb44332009-09-09 15:08:12 +00002387
Douglas Gregor92b059e2009-04-28 20:33:11 +00002388 // Look for any identifiers that were named while processing the
2389 // headers, but are otherwise not needed. We add these to the hash
2390 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002391 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002392 // file.
2393 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2394 IDEnd = PP.getIdentifierTable().end();
2395 ID != IDEnd; ++ID)
2396 getIdentifierRef(ID->second);
2397
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002398 // Create the on-disk hash table representation. We only store offsets
2399 // for identifiers that appear here for the first time.
2400 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002401 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002402 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2403 ID != IDEnd; ++ID) {
2404 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002405 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002406 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002407 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002408
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002409 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002410 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002411 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002412 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002413 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002414 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002415 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002416 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002417 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002418 }
2419
2420 // Create a blob abbreviation
2421 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002422 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002423 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002424 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002425 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002426
2427 // Write the identifier table
2428 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002429 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002430 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002431 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002432 }
2433
2434 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002435 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002436 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002437 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002438 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002439 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2440 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2441
2442 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002443 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002444 Record.push_back(IdentifierOffsets.size());
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002445 Record.push_back(FirstIdentID - NUM_PREDEF_IDENT_IDS);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002446 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002447 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002448}
2449
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002450//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002451// DeclContext's Name Lookup Table Serialization
2452//===----------------------------------------------------------------------===//
2453
2454namespace {
2455// Trait used for the on-disk hash table used in the method pool.
2456class ASTDeclContextNameLookupTrait {
2457 ASTWriter &Writer;
2458
2459public:
2460 typedef DeclarationName key_type;
2461 typedef key_type key_type_ref;
2462
2463 typedef DeclContext::lookup_result data_type;
2464 typedef const data_type& data_type_ref;
2465
2466 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2467
2468 unsigned ComputeHash(DeclarationName Name) {
2469 llvm::FoldingSetNodeID ID;
2470 ID.AddInteger(Name.getNameKind());
2471
2472 switch (Name.getNameKind()) {
2473 case DeclarationName::Identifier:
2474 ID.AddString(Name.getAsIdentifierInfo()->getName());
2475 break;
2476 case DeclarationName::ObjCZeroArgSelector:
2477 case DeclarationName::ObjCOneArgSelector:
2478 case DeclarationName::ObjCMultiArgSelector:
2479 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2480 break;
2481 case DeclarationName::CXXConstructorName:
2482 case DeclarationName::CXXDestructorName:
2483 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002484 break;
2485 case DeclarationName::CXXOperatorName:
2486 ID.AddInteger(Name.getCXXOverloadedOperator());
2487 break;
2488 case DeclarationName::CXXLiteralOperatorName:
2489 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2490 case DeclarationName::CXXUsingDirective:
2491 break;
2492 }
2493
2494 return ID.ComputeHash();
2495 }
2496
2497 std::pair<unsigned,unsigned>
Chris Lattner5f9e2722011-07-23 10:55:15 +00002498 EmitKeyDataLength(raw_ostream& Out, DeclarationName Name,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002499 data_type_ref Lookup) {
2500 unsigned KeyLen = 1;
2501 switch (Name.getNameKind()) {
2502 case DeclarationName::Identifier:
2503 case DeclarationName::ObjCZeroArgSelector:
2504 case DeclarationName::ObjCOneArgSelector:
2505 case DeclarationName::ObjCMultiArgSelector:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002506 case DeclarationName::CXXLiteralOperatorName:
2507 KeyLen += 4;
2508 break;
2509 case DeclarationName::CXXOperatorName:
2510 KeyLen += 1;
2511 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002512 case DeclarationName::CXXConstructorName:
2513 case DeclarationName::CXXDestructorName:
2514 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002515 case DeclarationName::CXXUsingDirective:
2516 break;
2517 }
2518 clang::io::Emit16(Out, KeyLen);
2519
2520 // 2 bytes for num of decls and 4 for each DeclID.
2521 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2522 clang::io::Emit16(Out, DataLen);
2523
2524 return std::make_pair(KeyLen, DataLen);
2525 }
2526
Chris Lattner5f9e2722011-07-23 10:55:15 +00002527 void EmitKey(raw_ostream& Out, DeclarationName Name, unsigned) {
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002528 using namespace clang::io;
2529
2530 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2531 Emit8(Out, Name.getNameKind());
2532 switch (Name.getNameKind()) {
2533 case DeclarationName::Identifier:
2534 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2535 break;
2536 case DeclarationName::ObjCZeroArgSelector:
2537 case DeclarationName::ObjCOneArgSelector:
2538 case DeclarationName::ObjCMultiArgSelector:
2539 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2540 break;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002541 case DeclarationName::CXXOperatorName:
2542 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2543 Emit8(Out, Name.getCXXOverloadedOperator());
2544 break;
2545 case DeclarationName::CXXLiteralOperatorName:
2546 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2547 break;
Douglas Gregore3605012011-08-02 18:32:54 +00002548 case DeclarationName::CXXConstructorName:
2549 case DeclarationName::CXXDestructorName:
2550 case DeclarationName::CXXConversionFunctionName:
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002551 case DeclarationName::CXXUsingDirective:
2552 break;
2553 }
2554 }
2555
Chris Lattner5f9e2722011-07-23 10:55:15 +00002556 void EmitData(raw_ostream& Out, key_type_ref,
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002557 data_type Lookup, unsigned DataLen) {
2558 uint64_t Start = Out.tell(); (void)Start;
2559 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2560 for (; Lookup.first != Lookup.second; ++Lookup.first)
2561 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2562
2563 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2564 }
2565};
2566} // end anonymous namespace
2567
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002568/// \brief Write the block containing all of the declaration IDs
2569/// visible from the given DeclContext.
2570///
2571/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002572/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002573uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2574 DeclContext *DC) {
2575 if (DC->getPrimaryContext() != DC)
2576 return 0;
2577
2578 // Since there is no name lookup into functions or methods, don't bother to
2579 // build a visible-declarations table for these entities.
2580 if (DC->isFunctionOrMethod())
2581 return 0;
2582
2583 // If not in C++, we perform name lookup for the translation unit via the
2584 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2585 // FIXME: In C++ we need the visible declarations in order to "see" the
2586 // friend declarations, is there a way to do this without writing the table ?
2587 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2588 return 0;
2589
2590 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00002591 if (DC->hasExternalVisibleStorage())
2592 DC->MaterializeVisibleDeclsFromExternalStorage();
2593 else
2594 DC->lookup(DeclarationName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002595
2596 // Serialize the contents of the mapping used for lookup. Note that,
2597 // although we have two very different code paths, the serialized
2598 // representation is the same for both cases: a declaration name,
2599 // followed by a size, followed by references to the visible
2600 // declarations that have that name.
2601 uint64_t Offset = Stream.GetCurrentBitNo();
2602 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2603 if (!Map || Map->empty())
2604 return 0;
2605
2606 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2607 ASTDeclContextNameLookupTrait Trait(*this);
2608
2609 // Create the on-disk hash table representation.
2610 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2611 D != DEnd; ++D) {
2612 DeclarationName Name = D->first;
2613 DeclContext::lookup_result Result = D->second.getLookupResult();
2614 Generator.insert(Name, Result, Trait);
2615 }
2616
2617 // Create the on-disk hash table in a buffer.
2618 llvm::SmallString<4096> LookupTable;
2619 uint32_t BucketOffset;
2620 {
2621 llvm::raw_svector_ostream Out(LookupTable);
2622 // Make sure that no bucket is at offset 0
2623 clang::io::Emit32(Out, 0);
2624 BucketOffset = Generator.Emit(Out, Trait);
2625 }
2626
2627 // Write the lookup table
2628 RecordData Record;
2629 Record.push_back(DECL_CONTEXT_VISIBLE);
2630 Record.push_back(BucketOffset);
2631 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2632 LookupTable.str());
2633
2634 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2635 ++NumVisibleDeclContexts;
2636 return Offset;
2637}
2638
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002639/// \brief Write an UPDATE_VISIBLE block for the given context.
2640///
2641/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2642/// DeclContext in a dependent AST file. As such, they only exist for the TU
2643/// (in C++) and for namespaces.
2644void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002645 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2646 if (!Map || Map->empty())
2647 return;
2648
2649 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2650 ASTDeclContextNameLookupTrait Trait(*this);
2651
2652 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002653 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2654 D != DEnd; ++D) {
2655 DeclarationName Name = D->first;
2656 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002657 // For any name that appears in this table, the results are complete, i.e.
2658 // they overwrite results from previous PCHs. Merging is always a mess.
2659 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002660 }
2661
2662 // Create the on-disk hash table in a buffer.
2663 llvm::SmallString<4096> LookupTable;
2664 uint32_t BucketOffset;
2665 {
2666 llvm::raw_svector_ostream Out(LookupTable);
2667 // Make sure that no bucket is at offset 0
2668 clang::io::Emit32(Out, 0);
2669 BucketOffset = Generator.Emit(Out, Trait);
2670 }
2671
2672 // Write the lookup table
2673 RecordData Record;
2674 Record.push_back(UPDATE_VISIBLE);
2675 Record.push_back(getDeclID(cast<Decl>(DC)));
2676 Record.push_back(BucketOffset);
2677 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2678}
2679
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002680/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2681void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2682 RecordData Record;
2683 Record.push_back(Opts.fp_contract);
2684 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2685}
2686
2687/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2688void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
2689 if (!SemaRef.Context.getLangOptions().OpenCL)
2690 return;
2691
2692 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2693 RecordData Record;
2694#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2695#include "clang/Basic/OpenCLExtensions.def"
2696 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2697}
2698
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002699//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002700// General Serialization Routines
2701//===----------------------------------------------------------------------===//
2702
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002703/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002704void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00002705 Record.push_back(Attrs.size());
Sean Huntcf807c42010-08-18 23:23:40 +00002706 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2707 const Attr * A = *i;
2708 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2709 AddSourceLocation(A->getLocation(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002710
Sean Huntcf807c42010-08-18 23:23:40 +00002711#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00002712
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002713 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002714}
2715
Chris Lattner5f9e2722011-07-23 10:55:15 +00002716void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002717 Record.push_back(Str.size());
2718 Record.insert(Record.end(), Str.begin(), Str.end());
2719}
2720
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002721void ASTWriter::AddVersionTuple(const VersionTuple &Version,
2722 RecordDataImpl &Record) {
2723 Record.push_back(Version.getMajor());
2724 if (llvm::Optional<unsigned> Minor = Version.getMinor())
2725 Record.push_back(*Minor + 1);
2726 else
2727 Record.push_back(0);
2728 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
2729 Record.push_back(*Subminor + 1);
2730 else
2731 Record.push_back(0);
2732}
2733
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002734/// \brief Note that the identifier II occurs at the given offset
2735/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002736void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002737 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00002738 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002739 // up earlier in the chain and thus don't need an offset.
2740 if (ID >= FirstIdentID)
2741 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002742}
2743
Douglas Gregor83941df2009-04-25 17:48:32 +00002744/// \brief Note that the selector Sel occurs at the given offset
2745/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002746void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00002747 unsigned ID = SelectorIDs[Sel];
2748 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00002749 // Don't record offsets for selectors that are also available in a different
2750 // file.
2751 if (ID < FirstSelectorID)
2752 return;
2753 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00002754}
2755
Sebastian Redla4232eb2010-08-18 23:56:21 +00002756ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregor89d99802010-11-30 06:16:57 +00002757 : Stream(Stream), Chain(0), SerializationListener(0),
Douglas Gregor0a14e4b2011-08-03 16:05:40 +00002758 FirstDeclID(NUM_PREDEF_DECL_IDS), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002759 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Douglas Gregor6ec60e02011-08-03 21:49:18 +00002760 FirstIdentID(NUM_PREDEF_IDENT_IDS), NextIdentID(FirstIdentID),
Douglas Gregorb18b1fd2011-08-03 23:28:44 +00002761 FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
Douglas Gregorfb2d9e02011-08-04 16:36:56 +00002762 FirstMacroID(NUM_PREDEF_MACRO_IDS), NextMacroID(FirstMacroID),
Douglas Gregor77424bc2010-10-02 19:29:26 +00002763 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00002764 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00002765 NumVisibleDeclContexts(0),
Douglas Gregore92b8a12011-08-04 00:01:48 +00002766 NextCXXBaseSpecifiersID(1),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00002767 DeclParmVarAbbrev(0), DeclContextLexicalAbbrev(0),
Douglas Gregora72d8c42011-06-03 02:27:19 +00002768 DeclContextVisibleLookupAbbrev(0), UpdateVisibleAbbrev(0),
2769 DeclRefExprAbbrev(0), CharacterLiteralAbbrev(0),
2770 DeclRecordAbbrev(0), IntegerLiteralAbbrev(0),
Jonathan D. Turner953c5642011-06-03 23:11:16 +00002771 DeclTypedefAbbrev(0),
2772 DeclVarAbbrev(0), DeclFieldAbbrev(0),
2773 DeclEnumAbbrev(0), DeclObjCIvarAbbrev(0)
Douglas Gregor7c789c12010-10-29 22:39:52 +00002774{
Sebastian Redl30c514c2010-07-14 23:45:08 +00002775}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002776
Sebastian Redla4232eb2010-08-18 23:56:21 +00002777void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002778 const std::string &OutputFile,
Douglas Gregor832d6202011-07-22 16:35:34 +00002779 StringRef isysroot) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002780 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002781 Stream.Emit((unsigned)'C', 8);
2782 Stream.Emit((unsigned)'P', 8);
2783 Stream.Emit((unsigned)'C', 8);
2784 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002785
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002786 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002787
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002788 if (Chain)
Sebastian Redla4232eb2010-08-18 23:56:21 +00002789 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002790 else
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002791 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002792}
2793
Douglas Gregora2ee20a2011-07-27 21:45:57 +00002794template<typename Vector>
2795static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
2796 ASTWriter::RecordData &Record) {
2797 for (typename Vector::iterator I = Vec.begin(0, true), E = Vec.end();
2798 I != E; ++I) {
2799 Writer.AddDeclRef(*I, Record);
2800 }
2801}
2802
Sebastian Redla4232eb2010-08-18 23:56:21 +00002803void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregor832d6202011-07-22 16:35:34 +00002804 StringRef isysroot,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002805 const std::string &OutputFile) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002806 using namespace llvm;
2807
2808 ASTContext &Context = SemaRef.Context;
2809 Preprocessor &PP = SemaRef.PP;
2810
Douglas Gregor2cf26342009-04-09 22:27:44 +00002811 // The translation unit is the first declaration we'll emit.
2812 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002813 ++NextDeclID;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002814 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002815
Douglas Gregor2deaea32009-04-22 18:49:13 +00002816 // Make sure that we emit IdentifierInfos (and any attached
2817 // declarations) for builtins.
2818 {
2819 IdentifierTable &Table = PP.getIdentifierTable();
Chris Lattner5f9e2722011-07-23 10:55:15 +00002820 SmallVector<const char *, 32> BuiltinNames;
Douglas Gregor2deaea32009-04-22 18:49:13 +00002821 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2822 Context.getLangOptions().NoBuiltin);
2823 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2824 getIdentifierRef(&Table.get(BuiltinNames[I]));
2825 }
2826
Chris Lattner63d65f82009-09-08 18:19:27 +00002827 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002828 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002829 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002830 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00002831 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00002832
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002833 // Build a record containing all of the file scoped decls in this file.
2834 RecordData UnusedFileScopedDecls;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00002835 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
2836 UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00002837
Sean Huntebcbe1d2011-05-04 23:29:54 +00002838 RecordData DelegatingCtorDecls;
Douglas Gregor0129b562011-07-27 21:57:17 +00002839 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00002840
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002841 RecordData WeakUndeclaredIdentifiers;
2842 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00002843 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002844 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2845 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2846 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2847 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2848 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2849 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2850 }
2851 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002852
Douglas Gregor14c22f22009-04-22 22:18:58 +00002853 // Build a record containing all of the locally-scoped external
2854 // declarations in this header file. Generally, this record will be
2855 // empty.
2856 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002857 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00002858 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002859 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002860 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2861 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
Douglas Gregorec12ce22011-07-28 14:20:37 +00002862 TD != TDEnd; ++TD) {
2863 if (TD->second->getPCHLevel() == 0)
2864 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2865 }
2866
Douglas Gregorb81c1702009-04-27 20:06:05 +00002867 // Build a record containing all of the ext_vector declarations.
2868 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00002869 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002870
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002871 // Build a record containing all of the VTable uses information.
2872 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00002873 if (!SemaRef.VTableUses.empty()) {
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00002874 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2875 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2876 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2877 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2878 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002879 }
2880
2881 // Build a record containing all of dynamic classes declarations.
2882 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00002883 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002884
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002885 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002886 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002887 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00002888 I = SemaRef.PendingInstantiations.begin(),
2889 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2890 AddDeclRef(I->first, PendingInstantiations);
2891 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002892 }
2893 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2894 "There are local ones at end of translation unit!");
2895
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002896 // Build a record containing some declaration references.
2897 RecordData SemaDeclRefs;
2898 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2899 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2900 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2901 }
2902
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002903 RecordData CUDASpecialDeclRefs;
2904 if (Context.getcudaConfigureCallDecl()) {
2905 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
2906 }
2907
Douglas Gregord8bba9c2011-06-28 16:20:02 +00002908 // Build a record containing all of the known namespaces.
2909 RecordData KnownNamespaces;
2910 for (llvm::DenseMap<NamespaceDecl*, bool>::iterator
2911 I = SemaRef.KnownNamespaces.begin(),
2912 IEnd = SemaRef.KnownNamespaces.end();
2913 I != IEnd; ++I) {
2914 if (!I->second)
2915 AddDeclRef(I->first, KnownNamespaces);
2916 }
2917
Sebastian Redl3397c552010-08-18 23:56:27 +00002918 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002919 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002920 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002921 WriteMetadata(Context, isysroot, OutputFile);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002922 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregor832d6202011-07-22 16:35:34 +00002923 if (StatCalls && isysroot.empty())
Douglas Gregordd41ed52010-07-12 23:48:14 +00002924 WriteStatCache(*StatCalls);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002925 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Douglas Gregor69a9e012011-08-01 16:54:33 +00002926
Douglas Gregora119da02011-08-02 16:26:37 +00002927 // Form the record of special types.
2928 RecordData SpecialTypes;
2929 AddTypeRef(Context.getBuiltinVaListType(), SpecialTypes);
2930 AddTypeRef(Context.getObjCIdType(), SpecialTypes);
2931 AddTypeRef(Context.getObjCSelType(), SpecialTypes);
2932 AddTypeRef(Context.getObjCProtoType(), SpecialTypes);
2933 AddTypeRef(Context.getObjCClassType(), SpecialTypes);
2934 AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
2935 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), SpecialTypes);
2936 AddTypeRef(Context.getFILEType(), SpecialTypes);
2937 AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
2938 AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
2939 AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
2940 AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
2941 AddTypeRef(Context.getRawBlockdescriptorType(), SpecialTypes);
2942 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), SpecialTypes);
2943 AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
2944 AddTypeRef(Context.getRawNSConstantStringType(), SpecialTypes);
2945 SpecialTypes.push_back(Context.isInt128Installed());
2946 AddTypeRef(Context.AutoDeductTy, SpecialTypes);
2947 AddTypeRef(Context.AutoRRefDeductTy, SpecialTypes);
Mike Stump1eb44332009-09-09 15:08:12 +00002948
Douglas Gregor366809a2009-04-26 03:49:13 +00002949 // Keep writing types and declarations until all types and
2950 // declarations have been written.
Douglas Gregora72d8c42011-06-03 02:27:19 +00002951 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002952 WriteDeclsBlockAbbrevs();
2953 while (!DeclTypesToEmit.empty()) {
2954 DeclOrType DOT = DeclTypesToEmit.front();
2955 DeclTypesToEmit.pop();
2956 if (DOT.isType())
2957 WriteType(DOT.getType());
2958 else
2959 WriteDecl(Context, DOT.getDecl());
2960 }
2961 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002962
Douglas Gregor813a97b2009-10-17 17:25:45 +00002963 WritePreprocessor(PP);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002964 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00002965 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002966 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002967 WriteIdentifierTable(PP);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002968 WriteFPPragmaOptions(SemaRef.getFPOptions());
2969 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002970
Sebastian Redl1476ed42010-07-16 16:36:56 +00002971 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002972 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00002973
Anders Carlssonc8505782011-03-06 18:41:18 +00002974 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00002975
Douglas Gregora119da02011-08-02 16:26:37 +00002976 Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
2977
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002978 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002979 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002980 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002981
2982 // Write the record containing tentative definitions.
2983 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002984 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002985
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002986 // Write the record containing unused file scoped decls.
2987 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002988 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002989
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002990 // Write the record containing weak undeclared identifiers.
2991 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002992 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002993 WeakUndeclaredIdentifiers);
2994
Douglas Gregor14c22f22009-04-22 22:18:58 +00002995 // Write the record containing locally-scoped external definitions.
2996 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002997 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002998 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002999
3000 // Write the record containing ext_vector type names.
3001 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003002 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00003003
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003004 // Write the record containing VTable uses information.
3005 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003006 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003007
3008 // Write the record containing dynamic classes declarations.
3009 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003010 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00003011
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003012 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003013 if (!PendingInstantiations.empty())
3014 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00003015
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003016 // Write the record containing declaration references of Sema.
3017 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003018 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00003019
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003020 // Write the record containing CUDA-specific declaration references.
3021 if (!CUDASpecialDeclRefs.empty())
3022 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003023
3024 // Write the delegating constructors.
3025 if (!DelegatingCtorDecls.empty())
3026 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00003027
Douglas Gregord8bba9c2011-06-28 16:20:02 +00003028 // Write the known namespaces.
3029 if (!KnownNamespaces.empty())
3030 Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
3031
Douglas Gregor3e1af842009-04-17 22:13:46 +00003032 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00003033 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00003034 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00003035 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00003036 Record.push_back(NumLexicalDeclContexts);
3037 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003038 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00003039 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00003040}
3041
Sebastian Redla4232eb2010-08-18 23:56:21 +00003042void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Douglas Gregor832d6202011-07-22 16:35:34 +00003043 StringRef isysroot) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003044 using namespace llvm;
3045
3046 ASTContext &Context = SemaRef.Context;
3047 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1476ed42010-07-16 16:36:56 +00003048
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003049 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003050 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00003051 WriteMetadata(Context, isysroot, "");
Douglas Gregor832d6202011-07-22 16:35:34 +00003052 if (StatCalls && isysroot.empty())
Sebastian Redl1476ed42010-07-16 16:36:56 +00003053 WriteStatCache(*StatCalls);
3054 // FIXME: Source manager block should only write new stuff, which could be
3055 // done by tracking the largest ID in the chain
3056 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003057
Douglas Gregor69a9e012011-08-01 16:54:33 +00003058 // Write the mapping information describing our module dependencies and how
3059 // each of those modules were mapped into our own offset/ID space, so that
3060 // the reader can build the appropriate mapping to its own offset/ID space.
Douglas Gregor69a9e012011-08-01 16:54:33 +00003061 // The map consists solely of a blob with the following format:
Douglas Gregorf33740e2011-08-02 10:56:51 +00003062 // *(module-name-len:i16 module-name:len*i8
3063 // source-location-offset:i32
3064 // identifier-id:i32
3065 // preprocessed-entity-id:i32
3066 // macro-definition-id:i32
3067 // selector-id:i32
3068 // declaration-id:i32
3069 // c++-base-specifiers-id:i32
3070 // type-id:i32)
3071 //
Douglas Gregor69a9e012011-08-01 16:54:33 +00003072 llvm::BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
3073 Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
3074 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorf33740e2011-08-02 10:56:51 +00003075 unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor69a9e012011-08-01 16:54:33 +00003076 llvm::SmallString<2048> Buffer;
3077 {
3078 llvm::raw_svector_ostream Out(Buffer);
Douglas Gregorf33740e2011-08-02 10:56:51 +00003079 for (ModuleManager::ModuleConstIterator M = Chain->ModuleMgr.begin(),
3080 MEnd = Chain->ModuleMgr.end();
3081 M != MEnd; ++M) {
3082 StringRef FileName = (*M)->FileName;
3083 io::Emit16(Out, FileName.size());
3084 Out.write(FileName.data(), FileName.size());
3085 io::Emit32(Out, (*M)->SLocEntryBaseOffset);
3086 io::Emit32(Out, (*M)->BaseIdentifierID);
3087 io::Emit32(Out, (*M)->BasePreprocessedEntityID);
3088 io::Emit32(Out, (*M)->BaseMacroDefinitionID);
3089 io::Emit32(Out, (*M)->BaseSelectorID);
3090 io::Emit32(Out, (*M)->BaseDeclID);
Douglas Gregore3605012011-08-02 18:32:54 +00003091 io::Emit32(Out, (*M)->BaseTypeIndex);
Douglas Gregor69a9e012011-08-01 16:54:33 +00003092 }
3093 }
3094 Record.clear();
3095 Record.push_back(MODULE_OFFSET_MAP);
Douglas Gregorf33740e2011-08-02 10:56:51 +00003096 Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
Douglas Gregor69a9e012011-08-01 16:54:33 +00003097 Buffer.data(), Buffer.size());
3098
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003099 // The special types are in the chained PCH.
3100
3101 // We don't start with the translation unit, but with its decls that
Sebastian Redl3c7f4132010-08-18 23:57:06 +00003102 // don't come from the chained PCH.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003103 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Chris Lattner5f9e2722011-07-23 10:55:15 +00003104 SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl681d7232010-07-27 00:17:23 +00003105 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
3106 E = TU->noload_decls_end();
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003107 I != E; ++I) {
Sebastian Redld692af72010-07-27 18:24:41 +00003108 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00003109 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redl0b17c612010-08-13 00:28:03 +00003110 else if ((*I)->isChangedSinceDeserialization())
3111 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003112 }
Sebastian Redl681d7232010-07-27 00:17:23 +00003113 // We also need to write a lexical updates block for the TU.
Sebastian Redld692af72010-07-27 18:24:41 +00003114 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003115 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redld692af72010-07-27 18:24:41 +00003116 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3117 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
3118 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003119 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redld692af72010-07-27 18:24:41 +00003120 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00003121 data(NewGlobalDecls));
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00003122 // And a visible updates block for the DeclContexts.
3123 Abv = new llvm::BitCodeAbbrev();
3124 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
3125 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3126 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
3127 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3128 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
3129 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003130
Sebastian Redl083abdf2010-07-27 23:01:28 +00003131 // Build a record containing all of the new tentative definitions in this
3132 // file, in TentativeDefinitions order.
3133 RecordData TentativeDefinitions;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003134 AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
Douglas Gregora8623202011-07-27 20:58:46 +00003135
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003136 // Build a record containing all of the file scoped decls in this file.
3137 RecordData UnusedFileScopedDecls;
Douglas Gregora2ee20a2011-07-27 21:45:57 +00003138 AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
3139 UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003140
Sean Huntebcbe1d2011-05-04 23:29:54 +00003141 // Build a record containing all of the delegating constructor decls in this
3142 // file.
3143 RecordData DelegatingCtorDecls;
Douglas Gregor0129b562011-07-27 21:57:17 +00003144 AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003145
Sebastian Redl40566802010-08-05 18:21:25 +00003146 // We write the entire table, overwriting the tables from the chain.
3147 RecordData WeakUndeclaredIdentifiers;
3148 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
Douglas Gregor31e37b22011-07-28 18:09:57 +00003149 for (llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator
Sebastian Redl40566802010-08-05 18:21:25 +00003150 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
3151 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
3152 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
3153 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
3154 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
3155 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
3156 }
3157 }
3158
Sebastian Redl083abdf2010-07-27 23:01:28 +00003159 // Build a record containing all of the locally-scoped external
3160 // declarations in this header file. Generally, this record will be
3161 // empty.
3162 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00003163 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl083abdf2010-07-27 23:01:28 +00003164 // nondeterminstic!
3165 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
3166 TD = SemaRef.LocallyScopedExternalDecls.begin(),
3167 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
3168 TD != TDEnd; ++TD) {
3169 if (TD->second->getPCHLevel() == 0)
3170 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3171 }
3172
3173 // Build a record containing all of the ext_vector declarations.
3174 RecordData ExtVectorDecls;
Douglas Gregord58a0a52011-07-28 00:39:29 +00003175 AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003176
Sebastian Redl40566802010-08-05 18:21:25 +00003177 // Build a record containing all of the VTable uses information.
3178 // We write everything here, because it's too hard to determine whether
3179 // a use is new to this part.
3180 RecordData VTableUses;
3181 if (!SemaRef.VTableUses.empty()) {
Sebastian Redl40566802010-08-05 18:21:25 +00003182 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3183 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3184 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3185 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3186 }
3187 }
3188
3189 // Build a record containing all of dynamic classes declarations.
3190 RecordData DynamicClasses;
Douglas Gregora126f172011-07-28 00:53:40 +00003191 AddLazyVectorDecls(*this, SemaRef.DynamicClasses, DynamicClasses);
Sebastian Redl40566802010-08-05 18:21:25 +00003192
3193 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003194 RecordData PendingInstantiations;
Sebastian Redl40566802010-08-05 18:21:25 +00003195 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003196 I = SemaRef.PendingInstantiations.begin(),
3197 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redlc1d3ffb2011-04-24 16:27:30 +00003198 AddDeclRef(I->first, PendingInstantiations);
3199 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl40566802010-08-05 18:21:25 +00003200 }
3201 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3202 "There are local ones at end of translation unit!");
3203
3204 // Build a record containing some declaration references.
3205 // It's not worth the effort to avoid duplication here.
3206 RecordData SemaDeclRefs;
3207 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3208 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3209 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3210 }
3211
Douglas Gregora72d8c42011-06-03 02:27:19 +00003212 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003213 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00003214 for (DeclsToRewriteTy::iterator
3215 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
3216 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003217 while (!DeclTypesToEmit.empty()) {
3218 DeclOrType DOT = DeclTypesToEmit.front();
3219 DeclTypesToEmit.pop();
3220 if (DOT.isType())
3221 WriteType(DOT.getType());
3222 else
3223 WriteDecl(Context, DOT.getDecl());
3224 }
3225 Stream.ExitBlock();
3226
Sebastian Redl083abdf2010-07-27 23:01:28 +00003227 WritePreprocessor(PP);
Sebastian Redla68340f2010-08-04 22:21:29 +00003228 WriteSelectors(SemaRef);
3229 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003230 WriteIdentifierTable(PP);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003231 WriteFPPragmaOptions(SemaRef.getFPOptions());
3232 WriteOpenCLExtensions(SemaRef);
3233
Sebastian Redl1476ed42010-07-16 16:36:56 +00003234 WriteTypeDeclOffsets();
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00003235 // FIXME: For chained PCH only write the new mappings (we currently
3236 // write all of them again).
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003237 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl083abdf2010-07-27 23:01:28 +00003238
Anders Carlssonc8505782011-03-06 18:41:18 +00003239 WriteCXXBaseSpecifiersOffsets();
3240
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003241 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl3397c552010-08-18 23:56:27 +00003242 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003243 RecordData FirstLatestDeclIDs;
3244 for (FirstLatestDeclMap::iterator
3245 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
3246 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
3247 "Expected first & second to be in different PCHs");
3248 AddDeclRef(I->first, FirstLatestDeclIDs);
3249 AddDeclRef(I->second, FirstLatestDeclIDs);
3250 }
3251 if (!FirstLatestDeclIDs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003252 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003253
Sebastian Redl083abdf2010-07-27 23:01:28 +00003254 // Write the record containing external, unnamed definitions.
3255 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003256 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003257
3258 // Write the record containing tentative definitions.
3259 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003260 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003261
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003262 // Write the record containing unused file scoped decls.
3263 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003264 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003265
Sebastian Redl40566802010-08-05 18:21:25 +00003266 // Write the record containing weak undeclared identifiers.
3267 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003268 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl40566802010-08-05 18:21:25 +00003269 WeakUndeclaredIdentifiers);
3270
Sebastian Redl083abdf2010-07-27 23:01:28 +00003271 // Write the record containing locally-scoped external definitions.
3272 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003273 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl083abdf2010-07-27 23:01:28 +00003274 LocallyScopedExternalDecls);
3275
3276 // Write the record containing ext_vector type names.
3277 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003278 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003279
Sebastian Redl40566802010-08-05 18:21:25 +00003280 // Write the record containing VTable uses information.
3281 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003282 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl40566802010-08-05 18:21:25 +00003283
3284 // Write the record containing dynamic classes declarations.
3285 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003286 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl40566802010-08-05 18:21:25 +00003287
3288 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003289 if (!PendingInstantiations.empty())
3290 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl40566802010-08-05 18:21:25 +00003291
3292 // Write the record containing declaration references of Sema.
3293 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003294 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sean Huntebcbe1d2011-05-04 23:29:54 +00003295
3296 // Write the delegating constructors.
3297 if (!DelegatingCtorDecls.empty())
3298 Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003299
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00003300 // Write the updates to DeclContexts.
3301 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3302 I = UpdatedDeclContexts.begin(),
3303 E = UpdatedDeclContexts.end();
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003304 I != E; ++I)
3305 WriteDeclContextVisibleUpdate(*I);
3306
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003307 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003308
Sebastian Redl083abdf2010-07-27 23:01:28 +00003309 Record.clear();
3310 Record.push_back(NumStatements);
3311 Record.push_back(NumMacros);
3312 Record.push_back(NumLexicalDeclContexts);
3313 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003314 WriteDeclReplacementsBlock();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003315 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003316 Stream.ExitBlock();
3317}
3318
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003319void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003320 if (DeclUpdates.empty())
3321 return;
3322
3323 RecordData OffsetsRecord;
Douglas Gregora72d8c42011-06-03 02:27:19 +00003324 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, NUM_ALLOWED_ABBREVS_SIZE);
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003325 for (DeclUpdateMap::iterator
3326 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3327 const Decl *D = I->first;
3328 UpdateRecord &URec = I->second;
3329
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003330 if (DeclsToRewrite.count(D))
3331 continue; // The decl will be written completely,no need to store updates.
3332
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003333 uint64_t Offset = Stream.GetCurrentBitNo();
3334 Stream.EmitRecord(DECL_UPDATES, URec);
3335
3336 OffsetsRecord.push_back(GetDeclRef(D));
3337 OffsetsRecord.push_back(Offset);
3338 }
3339 Stream.ExitBlock();
3340 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3341}
3342
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003343void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003344 if (ReplacedDecls.empty())
3345 return;
3346
3347 RecordData Record;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003348 for (SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003349 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
3350 Record.push_back(I->first);
3351 Record.push_back(I->second);
3352 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003353 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003354}
3355
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003356void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003357 Record.push_back(Loc.getRawEncoding());
3358}
3359
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003360void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003361 AddSourceLocation(Range.getBegin(), Record);
3362 AddSourceLocation(Range.getEnd(), Record);
3363}
3364
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003365void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003366 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003367 const uint64_t *Words = Value.getRawData();
3368 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003369}
3370
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003371void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003372 Record.push_back(Value.isUnsigned());
3373 AddAPInt(Value, Record);
3374}
3375
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003376void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003377 AddAPInt(Value.bitcastToAPInt(), Record);
3378}
3379
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003380void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003381 Record.push_back(getIdentifierRef(II));
3382}
3383
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003384IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003385 if (II == 0)
3386 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003387
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003388 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003389 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003390 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003391 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003392}
3393
Sebastian Redlf73c93f2010-09-15 19:54:06 +00003394MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003395 if (MD == 0)
3396 return 0;
Sebastian Redlf73c93f2010-09-15 19:54:06 +00003397
3398 MacroID &ID = MacroDefinitions[MD];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003399 if (ID == 0)
Douglas Gregor77424bc2010-10-02 19:29:26 +00003400 ID = NextMacroID++;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003401 return ID;
3402}
3403
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003404void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003405 Record.push_back(getSelectorRef(SelRef));
3406}
3407
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003408SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003409 if (Sel.getAsOpaquePtr() == 0) {
3410 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003411 }
3412
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003413 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003414 if (SID == 0 && Chain) {
3415 // This might trigger a ReadSelector callback, which will set the ID for
3416 // this selector.
3417 Chain->LoadSelector(Sel);
3418 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003419 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003420 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003421 }
Sebastian Redl5d050072010-08-04 17:20:04 +00003422 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003423}
3424
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003425void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00003426 AddDeclRef(Temp->getDestructor(), Record);
3427}
3428
Douglas Gregor7c789c12010-10-29 22:39:52 +00003429void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3430 CXXBaseSpecifier const *BasesEnd,
3431 RecordDataImpl &Record) {
3432 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3433 CXXBaseSpecifiersToWrite.push_back(
3434 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3435 Bases, BasesEnd));
3436 Record.push_back(NextCXXBaseSpecifiersID++);
3437}
3438
Sebastian Redla4232eb2010-08-18 23:56:21 +00003439void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003440 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003441 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003442 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00003443 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003444 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00003445 break;
3446 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003447 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00003448 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00003449 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003450 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003451 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003452 break;
3453 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003454 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003455 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003456 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00003457 break;
John McCall833ca992009-10-29 08:12:44 +00003458 case TemplateArgument::Null:
3459 case TemplateArgument::Integral:
3460 case TemplateArgument::Declaration:
3461 case TemplateArgument::Pack:
3462 break;
3463 }
3464}
3465
Sebastian Redla4232eb2010-08-18 23:56:21 +00003466void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003467 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003468 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003469
3470 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3471 bool InfoHasSameExpr
3472 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3473 Record.push_back(InfoHasSameExpr);
3474 if (InfoHasSameExpr)
3475 return; // Avoid storing the same expr twice.
3476 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003477 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3478 Record);
3479}
3480
Douglas Gregordc355712011-02-25 00:36:19 +00003481void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3482 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00003483 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00003484 AddTypeRef(QualType(), Record);
3485 return;
3486 }
3487
Douglas Gregordc355712011-02-25 00:36:19 +00003488 AddTypeLoc(TInfo->getTypeLoc(), Record);
3489}
3490
3491void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3492 AddTypeRef(TL.getType(), Record);
3493
John McCalla1ee0c52009-10-16 21:56:05 +00003494 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00003495 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003496 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00003497}
3498
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003499void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00003500 Record.push_back(GetOrCreateTypeID(T));
3501}
3502
3503TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003504 return MakeTypeID(T,
3505 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3506}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003507
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003508TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003509 return MakeTypeID(T,
3510 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003511}
3512
3513TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3514 if (T.isNull())
3515 return TypeIdx();
3516 assert(!T.getLocalFastQualifiers());
3517
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00003518 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003519 if (Idx.getIndex() == 0) {
Douglas Gregor366809a2009-04-26 03:49:13 +00003520 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00003521 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003522 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003523 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00003524 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003525 return Idx;
3526}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003527
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003528TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003529 if (T.isNull())
3530 return TypeIdx();
3531 assert(!T.getLocalFastQualifiers());
3532
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003533 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3534 assert(I != TypeIdxs.end() && "Type not emitted!");
3535 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003536}
3537
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003538void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003539 Record.push_back(GetDeclRef(D));
3540}
3541
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003542DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003543 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003544 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003545 }
Douglas Gregor97475832010-10-05 18:37:06 +00003546 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003547 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00003548 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003549 // We haven't seen this declaration before. Give it a new ID and
3550 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003551 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003552 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redl0b17c612010-08-13 00:28:03 +00003553 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3554 // We don't add it to the replacement collection here, because we don't
3555 // have the offset yet.
3556 DeclTypesToEmit.push(const_cast<Decl *>(D));
3557 // Reset the flag, so that we don't add this decl multiple times.
3558 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003559 }
3560
Sebastian Redl681d7232010-07-27 00:17:23 +00003561 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003562}
3563
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003564DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003565 if (D == 0)
3566 return 0;
3567
3568 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3569 return DeclIDs[D];
3570}
3571
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003572void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00003573 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00003574 Record.push_back(Name.getNameKind());
3575 switch (Name.getNameKind()) {
3576 case DeclarationName::Identifier:
3577 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3578 break;
3579
3580 case DeclarationName::ObjCZeroArgSelector:
3581 case DeclarationName::ObjCOneArgSelector:
3582 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003583 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003584 break;
3585
3586 case DeclarationName::CXXConstructorName:
3587 case DeclarationName::CXXDestructorName:
3588 case DeclarationName::CXXConversionFunctionName:
3589 AddTypeRef(Name.getCXXNameType(), Record);
3590 break;
3591
3592 case DeclarationName::CXXOperatorName:
3593 Record.push_back(Name.getCXXOverloadedOperator());
3594 break;
3595
Sean Hunt3e518bd2009-11-29 07:34:05 +00003596 case DeclarationName::CXXLiteralOperatorName:
3597 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3598 break;
3599
Douglas Gregor2cf26342009-04-09 22:27:44 +00003600 case DeclarationName::CXXUsingDirective:
3601 // No extra data to emit
3602 break;
3603 }
3604}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003605
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003606void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003607 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003608 switch (Name.getNameKind()) {
3609 case DeclarationName::CXXConstructorName:
3610 case DeclarationName::CXXDestructorName:
3611 case DeclarationName::CXXConversionFunctionName:
3612 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3613 break;
3614
3615 case DeclarationName::CXXOperatorName:
3616 AddSourceLocation(
3617 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3618 Record);
3619 AddSourceLocation(
3620 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3621 Record);
3622 break;
3623
3624 case DeclarationName::CXXLiteralOperatorName:
3625 AddSourceLocation(
3626 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3627 Record);
3628 break;
3629
3630 case DeclarationName::Identifier:
3631 case DeclarationName::ObjCZeroArgSelector:
3632 case DeclarationName::ObjCOneArgSelector:
3633 case DeclarationName::ObjCMultiArgSelector:
3634 case DeclarationName::CXXUsingDirective:
3635 break;
3636 }
3637}
3638
3639void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003640 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003641 AddDeclarationName(NameInfo.getName(), Record);
3642 AddSourceLocation(NameInfo.getLoc(), Record);
3643 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3644}
3645
3646void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003647 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003648 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003649 Record.push_back(Info.NumTemplParamLists);
3650 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3651 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3652}
3653
Sebastian Redla4232eb2010-08-18 23:56:21 +00003654void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003655 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003656 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003657 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003658 SmallVector<NestedNameSpecifier *, 8> NestedNames;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003659
3660 // Push each of the NNS's onto a stack for serialization in reverse order.
3661 while (NNS) {
3662 NestedNames.push_back(NNS);
3663 NNS = NNS->getPrefix();
3664 }
3665
3666 Record.push_back(NestedNames.size());
3667 while(!NestedNames.empty()) {
3668 NNS = NestedNames.pop_back_val();
3669 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3670 Record.push_back(Kind);
3671 switch (Kind) {
3672 case NestedNameSpecifier::Identifier:
3673 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3674 break;
3675
3676 case NestedNameSpecifier::Namespace:
3677 AddDeclRef(NNS->getAsNamespace(), Record);
3678 break;
3679
Douglas Gregor14aba762011-02-24 02:36:08 +00003680 case NestedNameSpecifier::NamespaceAlias:
3681 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
3682 break;
3683
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003684 case NestedNameSpecifier::TypeSpec:
3685 case NestedNameSpecifier::TypeSpecWithTemplate:
3686 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3687 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3688 break;
3689
3690 case NestedNameSpecifier::Global:
3691 // Don't need to write an associated value.
3692 break;
3693 }
3694 }
3695}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003696
Douglas Gregordc355712011-02-25 00:36:19 +00003697void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
3698 RecordDataImpl &Record) {
3699 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003700 // typically accommodate the vast majority.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003701 SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
Douglas Gregordc355712011-02-25 00:36:19 +00003702
3703 // Push each of the nested-name-specifiers's onto a stack for
3704 // serialization in reverse order.
3705 while (NNS) {
3706 NestedNames.push_back(NNS);
3707 NNS = NNS.getPrefix();
3708 }
3709
3710 Record.push_back(NestedNames.size());
3711 while(!NestedNames.empty()) {
3712 NNS = NestedNames.pop_back_val();
3713 NestedNameSpecifier::SpecifierKind Kind
3714 = NNS.getNestedNameSpecifier()->getKind();
3715 Record.push_back(Kind);
3716 switch (Kind) {
3717 case NestedNameSpecifier::Identifier:
3718 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
3719 AddSourceRange(NNS.getLocalSourceRange(), Record);
3720 break;
3721
3722 case NestedNameSpecifier::Namespace:
3723 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
3724 AddSourceRange(NNS.getLocalSourceRange(), Record);
3725 break;
3726
3727 case NestedNameSpecifier::NamespaceAlias:
3728 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
3729 AddSourceRange(NNS.getLocalSourceRange(), Record);
3730 break;
3731
3732 case NestedNameSpecifier::TypeSpec:
3733 case NestedNameSpecifier::TypeSpecWithTemplate:
3734 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3735 AddTypeLoc(NNS.getTypeLoc(), Record);
3736 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3737 break;
3738
3739 case NestedNameSpecifier::Global:
3740 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3741 break;
3742 }
3743 }
3744}
3745
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003746void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00003747 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003748 Record.push_back(Kind);
3749 switch (Kind) {
3750 case TemplateName::Template:
3751 AddDeclRef(Name.getAsTemplateDecl(), Record);
3752 break;
3753
3754 case TemplateName::OverloadedTemplate: {
3755 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3756 Record.push_back(OvT->size());
3757 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3758 I != E; ++I)
3759 AddDeclRef(*I, Record);
3760 break;
3761 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003762
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003763 case TemplateName::QualifiedTemplate: {
3764 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3765 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3766 Record.push_back(QualT->hasTemplateKeyword());
3767 AddDeclRef(QualT->getTemplateDecl(), Record);
3768 break;
3769 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003770
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003771 case TemplateName::DependentTemplate: {
3772 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3773 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3774 Record.push_back(DepT->isIdentifier());
3775 if (DepT->isIdentifier())
3776 AddIdentifierRef(DepT->getIdentifier(), Record);
3777 else
3778 Record.push_back(DepT->getOperator());
3779 break;
3780 }
John McCall14606042011-06-30 08:33:18 +00003781
3782 case TemplateName::SubstTemplateTemplateParm: {
3783 SubstTemplateTemplateParmStorage *subst
3784 = Name.getAsSubstTemplateTemplateParm();
3785 AddDeclRef(subst->getParameter(), Record);
3786 AddTemplateName(subst->getReplacement(), Record);
3787 break;
3788 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00003789
3790 case TemplateName::SubstTemplateTemplateParmPack: {
3791 SubstTemplateTemplateParmPackStorage *SubstPack
3792 = Name.getAsSubstTemplateTemplateParmPack();
3793 AddDeclRef(SubstPack->getParameterPack(), Record);
3794 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
3795 break;
3796 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003797 }
3798}
3799
Michael J. Spencer20249a12010-10-21 03:16:25 +00003800void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003801 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003802 Record.push_back(Arg.getKind());
3803 switch (Arg.getKind()) {
3804 case TemplateArgument::Null:
3805 break;
3806 case TemplateArgument::Type:
3807 AddTypeRef(Arg.getAsType(), Record);
3808 break;
3809 case TemplateArgument::Declaration:
3810 AddDeclRef(Arg.getAsDecl(), Record);
3811 break;
3812 case TemplateArgument::Integral:
3813 AddAPSInt(*Arg.getAsIntegral(), Record);
3814 AddTypeRef(Arg.getIntegralType(), Record);
3815 break;
3816 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00003817 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3818 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00003819 case TemplateArgument::TemplateExpansion:
3820 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00003821 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3822 Record.push_back(*NumExpansions + 1);
3823 else
3824 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003825 break;
3826 case TemplateArgument::Expression:
3827 AddStmt(Arg.getAsExpr());
3828 break;
3829 case TemplateArgument::Pack:
3830 Record.push_back(Arg.pack_size());
3831 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3832 I != E; ++I)
3833 AddTemplateArgument(*I, Record);
3834 break;
3835 }
3836}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003837
3838void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003839ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003840 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003841 assert(TemplateParams && "No TemplateParams!");
3842 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3843 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3844 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3845 Record.push_back(TemplateParams->size());
3846 for (TemplateParameterList::const_iterator
3847 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3848 P != PEnd; ++P)
3849 AddDeclRef(*P, Record);
3850}
3851
3852/// \brief Emit a template argument list.
3853void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003854ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003855 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003856 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00003857 Record.push_back(TemplateArgs->size());
3858 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003859 AddTemplateArgument(TemplateArgs->get(i), Record);
3860}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003861
3862
3863void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003864ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003865 Record.push_back(Set.size());
3866 for (UnresolvedSetImpl::const_iterator
3867 I = Set.begin(), E = Set.end(); I != E; ++I) {
3868 AddDeclRef(I.getDecl(), Record);
3869 Record.push_back(I.getAccess());
3870 }
3871}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003872
Sebastian Redla4232eb2010-08-18 23:56:21 +00003873void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003874 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003875 Record.push_back(Base.isVirtual());
3876 Record.push_back(Base.isBaseOfClass());
3877 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00003878 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00003879 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003880 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00003881 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3882 : SourceLocation(),
3883 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003884}
Sebastian Redl30c514c2010-07-14 23:45:08 +00003885
Douglas Gregor7c789c12010-10-29 22:39:52 +00003886void ASTWriter::FlushCXXBaseSpecifiers() {
3887 RecordData Record;
3888 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3889 Record.clear();
3890
3891 // Record the offset of this base-specifier set.
Douglas Gregore92b8a12011-08-04 00:01:48 +00003892 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
Douglas Gregor7c789c12010-10-29 22:39:52 +00003893 if (Index == CXXBaseSpecifiersOffsets.size())
3894 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3895 else {
3896 if (Index > CXXBaseSpecifiersOffsets.size())
3897 CXXBaseSpecifiersOffsets.resize(Index + 1);
3898 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3899 }
3900
3901 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3902 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3903 Record.push_back(BEnd - B);
3904 for (; B != BEnd; ++B)
3905 AddCXXBaseSpecifier(*B, Record);
3906 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00003907
3908 // Flush any expressions that were written as part of the base specifiers.
3909 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003910 }
3911
3912 CXXBaseSpecifiersToWrite.clear();
3913}
3914
Sean Huntcbb67482011-01-08 20:30:50 +00003915void ASTWriter::AddCXXCtorInitializers(
3916 const CXXCtorInitializer * const *CtorInitializers,
3917 unsigned NumCtorInitializers,
3918 RecordDataImpl &Record) {
3919 Record.push_back(NumCtorInitializers);
3920 for (unsigned i=0; i != NumCtorInitializers; ++i) {
3921 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003922
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003923 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00003924 Record.push_back(CTOR_INITIALIZER_BASE);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003925 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3926 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00003927 } else if (Init->isDelegatingInitializer()) {
3928 Record.push_back(CTOR_INITIALIZER_DELEGATING);
3929 AddDeclRef(Init->getTargetConstructor(), Record);
3930 } else if (Init->isMemberInitializer()){
3931 Record.push_back(CTOR_INITIALIZER_MEMBER);
3932 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003933 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00003934 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
3935 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003936 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00003937
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003938 AddSourceLocation(Init->getMemberLocation(), Record);
3939 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003940 AddSourceLocation(Init->getLParenLoc(), Record);
3941 AddSourceLocation(Init->getRParenLoc(), Record);
3942 Record.push_back(Init->isWritten());
3943 if (Init->isWritten()) {
3944 Record.push_back(Init->getSourceOrder());
3945 } else {
3946 Record.push_back(Init->getNumArrayIndices());
3947 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3948 AddDeclRef(Init->getArrayIndex(i), Record);
3949 }
3950 }
3951}
3952
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003953void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3954 assert(D->DefinitionData);
3955 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3956 Record.push_back(Data.UserDeclaredConstructor);
3957 Record.push_back(Data.UserDeclaredCopyConstructor);
3958 Record.push_back(Data.UserDeclaredCopyAssignment);
3959 Record.push_back(Data.UserDeclaredDestructor);
3960 Record.push_back(Data.Aggregate);
3961 Record.push_back(Data.PlainOldData);
3962 Record.push_back(Data.Empty);
3963 Record.push_back(Data.Polymorphic);
3964 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00003965 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00003966 Record.push_back(Data.HasNoNonEmptyBases);
3967 Record.push_back(Data.HasPrivateFields);
3968 Record.push_back(Data.HasProtectedFields);
3969 Record.push_back(Data.HasPublicFields);
Douglas Gregor2bb11012011-05-13 01:05:07 +00003970 Record.push_back(Data.HasMutableFields);
Sean Hunt023df372011-05-09 18:22:59 +00003971 Record.push_back(Data.HasTrivialDefaultConstructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00003972 Record.push_back(Data.HasConstExprNonCopyMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003973 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00003974 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003975 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00003976 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003977 Record.push_back(Data.HasTrivialDestructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00003978 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003979 Record.push_back(Data.ComputedVisibleConversions);
Sean Huntcdee3fe2011-05-11 22:34:38 +00003980 Record.push_back(Data.UserProvidedDefaultConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003981 Record.push_back(Data.DeclaredDefaultConstructor);
3982 Record.push_back(Data.DeclaredCopyConstructor);
3983 Record.push_back(Data.DeclaredCopyAssignment);
3984 Record.push_back(Data.DeclaredDestructor);
3985
3986 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003987 if (Data.NumBases > 0)
3988 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3989 Record);
3990
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003991 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3992 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003993 if (Data.NumVBases > 0)
3994 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3995 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003996
3997 AddUnresolvedSet(Data.Conversions, Record);
3998 AddUnresolvedSet(Data.VisibleConversions, Record);
3999 // Data.Definition is the owning decl, no need to write it.
4000 AddDeclRef(Data.FirstFriend, Record);
4001}
4002
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004003void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004004 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004005 assert(!Chain && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004006 assert(FirstDeclID == NextDeclID &&
4007 FirstTypeID == NextTypeID &&
4008 FirstIdentID == NextIdentID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00004009 FirstSelectorID == NextSelectorID &&
Douglas Gregor77424bc2010-10-02 19:29:26 +00004010 FirstMacroID == NextMacroID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004011 "Setting chain after writing has started.");
Douglas Gregorf62d43d2011-07-19 16:10:42 +00004012
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004013 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00004014
4015 FirstDeclID += Chain->getTotalNumDecls();
4016 FirstTypeID += Chain->getTotalNumTypes();
4017 FirstIdentID += Chain->getTotalNumIdentifiers();
4018 FirstSelectorID += Chain->getTotalNumSelectors();
4019 FirstMacroID += Chain->getTotalNumMacroDefinitions();
4020 NextDeclID = FirstDeclID;
4021 NextTypeID = FirstTypeID;
4022 NextIdentID = FirstIdentID;
4023 NextSelectorID = FirstSelectorID;
4024 NextMacroID = FirstMacroID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00004025}
4026
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004027void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004028 IdentifierIDs[II] = ID;
Douglas Gregor040a8042011-02-11 00:26:14 +00004029 if (II->hasMacroDefinition())
4030 DeserializedMacroNames.push_back(II);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00004031}
4032
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00004033void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00004034 // Always take the highest-numbered type index. This copes with an interesting
4035 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00004036 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00004037 // keep the higher-numbered entry so that we can properly write it out to
4038 // the AST file.
4039 TypeIdx &StoredIdx = TypeIdxs[T];
4040 if (Idx.getIndex() >= StoredIdx.getIndex())
4041 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00004042}
4043
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004044void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1476ed42010-07-16 16:36:56 +00004045 DeclIDs[D] = ID;
Sebastian Redl30c514c2010-07-14 23:45:08 +00004046}
Sebastian Redl5d050072010-08-04 17:20:04 +00004047
Sebastian Redl8538e8d2010-08-18 23:57:32 +00004048void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00004049 SelectorIDs[S] = ID;
4050}
Douglas Gregor77424bc2010-10-02 19:29:26 +00004051
Michael J. Spencer20249a12010-10-21 03:16:25 +00004052void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00004053 MacroDefinition *MD) {
4054 MacroDefinitions[MD] = ID;
4055}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004056
4057void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
4058 assert(D->isDefinition());
4059 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
4060 // We are interested when a PCH decl is modified.
4061 if (RD->getPCHLevel() > 0) {
4062 // A forward reference was mutated into a definition. Rewrite it.
4063 // FIXME: This happens during template instantiation, should we
4064 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00004065 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00004066 }
4067
4068 for (CXXRecordDecl::redecl_iterator
4069 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
4070 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
4071 if (Redecl == RD)
4072 continue;
4073
4074 // We are interested when a PCH decl is modified.
4075 if (Redecl->getPCHLevel() > 0) {
4076 UpdateRecord &Record = DeclUpdates[Redecl];
4077 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
4078 assert(Redecl->DefinitionData);
4079 assert(Redecl->DefinitionData->Definition == D);
4080 AddDeclRef(D, Record); // the DefinitionDecl
4081 }
4082 }
4083 }
4084}
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00004085void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
4086 // TU and namespaces are handled elsewhere.
4087 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
4088 return;
4089
4090 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
4091 return; // Not a source decl added to a DeclContext from PCH.
4092
4093 AddUpdatedDeclContext(DC);
4094}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00004095
4096void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
4097 assert(D->isImplicit());
4098 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
4099 return; // Not a source member added to a class from PCH.
4100 if (!isa<CXXMethodDecl>(D))
4101 return; // We are interested in lazily declared implicit methods.
4102
4103 // A decl coming from PCH was modified.
4104 assert(RD->isDefinition());
4105 UpdateRecord &Record = DeclUpdates[RD];
4106 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
4107 AddDeclRef(D, Record);
4108}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004109
4110void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
4111 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00004112 // The specializations set is kept in the canonical template.
4113 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00004114 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
4115 return; // Not a source specialization added to a template from PCH.
4116
4117 UpdateRecord &Record = DeclUpdates[TD];
4118 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
4119 AddDeclRef(D, Record);
4120}
Douglas Gregor89d99802010-11-30 06:16:57 +00004121
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00004122void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
4123 const FunctionDecl *D) {
4124 // The specializations set is kept in the canonical template.
4125 TD = TD->getCanonicalDecl();
4126 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
4127 return; // Not a source specialization added to a template from PCH.
4128
4129 UpdateRecord &Record = DeclUpdates[TD];
4130 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
4131 AddDeclRef(D, Record);
4132}
4133
Sebastian Redl58a2cd82011-04-24 16:28:06 +00004134void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
4135 if (D->getPCHLevel() == 0)
4136 return; // Declaration not imported from PCH.
4137
4138 // Implicit decl from a PCH was defined.
4139 // FIXME: Should implicit definition be a separate FunctionDecl?
4140 RewriteDecl(D);
4141}
4142
Sebastian Redlf79a7192011-04-29 08:19:30 +00004143void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
4144 if (D->getPCHLevel() == 0)
4145 return;
4146
4147 // Since the actual instantiation is delayed, this really means that we need
4148 // to update the instantiation location.
4149 UpdateRecord &Record = DeclUpdates[D];
4150 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
4151 AddSourceLocation(
4152 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
4153}
4154
Douglas Gregor89d99802010-11-30 06:16:57 +00004155ASTSerializationListener::~ASTSerializationListener() { }