blob: 30b55c2591d348b1ccf03649c2b8987e6939e2a3 [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"
Chris Lattner3c304bd2009-04-11 18:40:46 +000048#include <cstdio>
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000049#include <string.h>
Douglas Gregor2cf26342009-04-09 22:27:44 +000050using namespace clang;
Sebastian Redl8538e8d2010-08-18 23:57:32 +000051using namespace clang::serialization;
Douglas Gregor2cf26342009-04-09 22:27:44 +000052
Sebastian Redlade50002010-07-30 17:03:48 +000053template <typename T, typename Allocator>
Benjamin Kramer6e089c62011-04-24 17:44:50 +000054static llvm::StringRef data(const std::vector<T, Allocator> &v) {
55 if (v.empty()) return llvm::StringRef();
56 return llvm::StringRef(reinterpret_cast<const char*>(&v[0]),
57 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000058}
Benjamin Kramer6e089c62011-04-24 17:44:50 +000059
60template <typename T>
61static llvm::StringRef data(const llvm::SmallVectorImpl<T> &v) {
62 return llvm::StringRef(reinterpret_cast<const char*>(v.data()),
63 sizeof(T) * v.size());
Sebastian Redlade50002010-07-30 17:03:48 +000064}
65
Douglas Gregor2cf26342009-04-09 22:27:44 +000066//===----------------------------------------------------------------------===//
67// Type serialization
68//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000069
Douglas Gregor2cf26342009-04-09 22:27:44 +000070namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000071 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000072 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000073 ASTWriter::RecordDataImpl &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000074
75 public:
76 /// \brief Type code that corresponds to the record generated.
Sebastian Redl8538e8d2010-08-18 23:57:32 +000077 TypeCode Code;
Douglas Gregor2cf26342009-04-09 22:27:44 +000078
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +000079 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Sebastian Redl8538e8d2010-08-18 23:57:32 +000080 : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000081
82 void VisitArrayType(const ArrayType *T);
83 void VisitFunctionType(const FunctionType *T);
84 void VisitTagType(const TagType *T);
85
86#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
87#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000088#include "clang/AST/TypeNodes.def"
89 };
90}
91
Sebastian Redl3397c552010-08-18 23:56:27 +000092void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000093 assert(false && "Built-in types are never serialized");
94}
95
Sebastian Redl3397c552010-08-18 23:56:27 +000096void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000097 Writer.AddTypeRef(T->getElementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +000098 Code = TYPE_COMPLEX;
Douglas Gregor2cf26342009-04-09 22:27:44 +000099}
100
Sebastian Redl3397c552010-08-18 23:56:27 +0000101void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000102 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000103 Code = TYPE_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000104}
105
Sebastian Redl3397c552010-08-18 23:56:27 +0000106void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000107 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000108 Code = TYPE_BLOCK_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000109}
110
Sebastian Redl3397c552010-08-18 23:56:27 +0000111void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000112 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
113 Record.push_back(T->isSpelledAsLValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000114 Code = TYPE_LVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000115}
116
Sebastian Redl3397c552010-08-18 23:56:27 +0000117void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Richard Smithdf1550f2011-04-12 10:38:03 +0000118 Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000119 Code = TYPE_RVALUE_REFERENCE;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000120}
121
Sebastian Redl3397c552010-08-18 23:56:27 +0000122void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000123 Writer.AddTypeRef(T->getPointeeType(), Record);
124 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000125 Code = TYPE_MEMBER_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000126}
127
Sebastian Redl3397c552010-08-18 23:56:27 +0000128void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000129 Writer.AddTypeRef(T->getElementType(), Record);
130 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000131 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000132}
133
Sebastian Redl3397c552010-08-18 23:56:27 +0000134void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000135 VisitArrayType(T);
136 Writer.AddAPInt(T->getSize(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000137 Code = TYPE_CONSTANT_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000138}
139
Sebastian Redl3397c552010-08-18 23:56:27 +0000140void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000141 VisitArrayType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000142 Code = TYPE_INCOMPLETE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000143}
144
Sebastian Redl3397c552010-08-18 23:56:27 +0000145void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000146 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000147 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
148 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000149 Writer.AddStmt(T->getSizeExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000150 Code = TYPE_VARIABLE_ARRAY;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000151}
152
Sebastian Redl3397c552010-08-18 23:56:27 +0000153void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000154 Writer.AddTypeRef(T->getElementType(), Record);
155 Record.push_back(T->getNumElements());
Bob Wilsone86d78c2010-11-10 21:56:12 +0000156 Record.push_back(T->getVectorKind());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000157 Code = TYPE_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000158}
159
Sebastian Redl3397c552010-08-18 23:56:27 +0000160void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000161 VisitVectorType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000162 Code = TYPE_EXT_VECTOR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000163}
164
Sebastian Redl3397c552010-08-18 23:56:27 +0000165void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000166 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000167 FunctionType::ExtInfo C = T->getExtInfo();
168 Record.push_back(C.getNoReturn());
Eli Friedmana49218e2011-04-09 08:18:08 +0000169 Record.push_back(C.getHasRegParm());
Rafael Espindola425ef722010-03-30 22:15:11 +0000170 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000171 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000172 Record.push_back(C.getCC());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000173}
174
Sebastian Redl3397c552010-08-18 23:56:27 +0000175void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000176 VisitFunctionType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000177 Code = TYPE_FUNCTION_NO_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000178}
179
Sebastian Redl3397c552010-08-18 23:56:27 +0000180void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000181 VisitFunctionType(T);
182 Record.push_back(T->getNumArgs());
183 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
184 Writer.AddTypeRef(T->getArgType(I), Record);
185 Record.push_back(T->isVariadic());
186 Record.push_back(T->getTypeQuals());
Douglas Gregorc938c162011-01-26 05:01:58 +0000187 Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
Sebastian Redl60618fa2011-03-12 11:50:43 +0000188 Record.push_back(T->getExceptionSpecType());
189 if (T->getExceptionSpecType() == EST_Dynamic) {
190 Record.push_back(T->getNumExceptions());
191 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
192 Writer.AddTypeRef(T->getExceptionType(I), Record);
193 } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
194 Writer.AddStmt(T->getNoexceptExpr());
195 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000196 Code = TYPE_FUNCTION_PROTO;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000197}
198
Sebastian Redl3397c552010-08-18 23:56:27 +0000199void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000200 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000201 Code = TYPE_UNRESOLVED_USING;
John McCalled976492009-12-04 22:46:56 +0000202}
John McCalled976492009-12-04 22:46:56 +0000203
Sebastian Redl3397c552010-08-18 23:56:27 +0000204void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000205 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000206 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
207 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000208 Code = TYPE_TYPEDEF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000209}
210
Sebastian Redl3397c552010-08-18 23:56:27 +0000211void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000212 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000213 Code = TYPE_TYPEOF_EXPR;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000214}
215
Sebastian Redl3397c552010-08-18 23:56:27 +0000216void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000217 Writer.AddTypeRef(T->getUnderlyingType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000218 Code = TYPE_TYPEOF;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000219}
220
Sebastian Redl3397c552010-08-18 23:56:27 +0000221void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson395b4752009-06-24 19:06:50 +0000222 Writer.AddStmt(T->getUnderlyingExpr());
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000223 Code = TYPE_DECLTYPE;
Anders Carlsson395b4752009-06-24 19:06:50 +0000224}
225
Richard Smith34b41d92011-02-20 03:19:35 +0000226void ASTTypeWriter::VisitAutoType(const AutoType *T) {
227 Writer.AddTypeRef(T->getDeducedType(), Record);
228 Code = TYPE_AUTO;
229}
230
Sebastian Redl3397c552010-08-18 23:56:27 +0000231void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000232 Record.push_back(T->isDependentType());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000233 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000234 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000235 "Cannot serialize in the middle of a type definition");
236}
237
Sebastian Redl3397c552010-08-18 23:56:27 +0000238void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000239 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000240 Code = TYPE_RECORD;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000241}
242
Sebastian Redl3397c552010-08-18 23:56:27 +0000243void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000244 VisitTagType(T);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000245 Code = TYPE_ENUM;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000246}
247
John McCall9d156a72011-01-06 01:58:22 +0000248void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
249 Writer.AddTypeRef(T->getModifiedType(), Record);
250 Writer.AddTypeRef(T->getEquivalentType(), Record);
251 Record.push_back(T->getAttrKind());
252 Code = TYPE_ATTRIBUTED;
253}
254
Mike Stump1eb44332009-09-09 15:08:12 +0000255void
Sebastian Redl3397c552010-08-18 23:56:27 +0000256ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000257 const SubstTemplateTypeParmType *T) {
258 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
259 Writer.AddTypeRef(T->getReplacementType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000260 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
John McCall49a832b2009-10-18 09:09:24 +0000261}
262
263void
Douglas Gregorc3069d62011-01-14 02:55:32 +0000264ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
265 const SubstTemplateTypeParmPackType *T) {
266 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
267 Writer.AddTemplateArgument(T->getArgumentPack(), Record);
268 Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
269}
270
271void
Sebastian Redl3397c552010-08-18 23:56:27 +0000272ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000273 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000274 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000275 Writer.AddTemplateName(T->getTemplateName(), Record);
276 Record.push_back(T->getNumArgs());
277 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
278 ArgI != ArgE; ++ArgI)
279 Writer.AddTemplateArgument(*ArgI, Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000280 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
281 : T->getCanonicalTypeInternal(),
282 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000283 Code = TYPE_TEMPLATE_SPECIALIZATION;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000284}
285
286void
Sebastian Redl3397c552010-08-18 23:56:27 +0000287ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000288 VisitArrayType(T);
289 Writer.AddStmt(T->getSizeExpr());
290 Writer.AddSourceRange(T->getBracketsRange(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000291 Code = TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000292}
293
294void
Sebastian Redl3397c552010-08-18 23:56:27 +0000295ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000296 const DependentSizedExtVectorType *T) {
297 // FIXME: Serialize this type (C++ only)
298 assert(false && "Cannot serialize dependent sized extended vector types");
299}
300
301void
Sebastian Redl3397c552010-08-18 23:56:27 +0000302ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000303 Record.push_back(T->getDepth());
304 Record.push_back(T->getIndex());
305 Record.push_back(T->isParameterPack());
Chandler Carruth4fb86f82011-05-01 00:51:33 +0000306 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000307 Code = TYPE_TEMPLATE_TYPE_PARM;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000308}
309
310void
Sebastian Redl3397c552010-08-18 23:56:27 +0000311ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000312 Record.push_back(T->getKeyword());
313 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
314 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000315 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
316 : T->getCanonicalTypeInternal(),
317 Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000318 Code = TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000319}
320
321void
Sebastian Redl3397c552010-08-18 23:56:27 +0000322ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000323 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000324 Record.push_back(T->getKeyword());
325 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
326 Writer.AddIdentifierRef(T->getIdentifier(), Record);
327 Record.push_back(T->getNumArgs());
328 for (DependentTemplateSpecializationType::iterator
329 I = T->begin(), E = T->end(); I != E; ++I)
330 Writer.AddTemplateArgument(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000331 Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000332}
333
Douglas Gregor7536dd52010-12-20 02:24:11 +0000334void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
335 Writer.AddTypeRef(T->getPattern(), Record);
Douglas Gregorcded4f62011-01-14 17:04:44 +0000336 if (llvm::Optional<unsigned> NumExpansions = T->getNumExpansions())
337 Record.push_back(*NumExpansions + 1);
338 else
339 Record.push_back(0);
Douglas Gregor7536dd52010-12-20 02:24:11 +0000340 Code = TYPE_PACK_EXPANSION;
341}
342
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000343void ASTTypeWriter::VisitParenType(const ParenType *T) {
344 Writer.AddTypeRef(T->getInnerType(), Record);
345 Code = TYPE_PAREN;
346}
347
Sebastian Redl3397c552010-08-18 23:56:27 +0000348void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000349 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000350 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
351 Writer.AddTypeRef(T->getNamedType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000352 Code = TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000353}
354
Sebastian Redl3397c552010-08-18 23:56:27 +0000355void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCall3cb0ebd2010-03-10 03:28:59 +0000356 Writer.AddDeclRef(T->getDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000357 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000358 Code = TYPE_INJECTED_CLASS_NAME;
John McCall3cb0ebd2010-03-10 03:28:59 +0000359}
360
Sebastian Redl3397c552010-08-18 23:56:27 +0000361void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000362 Writer.AddDeclRef(T->getDecl(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000363 Code = TYPE_OBJC_INTERFACE;
John McCallc12c5bb2010-05-15 11:32:37 +0000364}
365
Sebastian Redl3397c552010-08-18 23:56:27 +0000366void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000367 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000368 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000369 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000370 E = T->qual_end(); I != E; ++I)
371 Writer.AddDeclRef(*I, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000372 Code = TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000373}
374
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000375void
Sebastian Redl3397c552010-08-18 23:56:27 +0000376ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000377 Writer.AddTypeRef(T->getPointeeType(), Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000378 Code = TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000379}
380
John McCalla1ee0c52009-10-16 21:56:05 +0000381namespace {
382
383class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000384 ASTWriter &Writer;
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000385 ASTWriter::RecordDataImpl &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000386
387public:
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000388 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000389 : Writer(Writer), Record(Record) { }
390
John McCall51bd8032009-10-18 01:05:36 +0000391#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000392#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000393 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000394#include "clang/AST/TypeLocNodes.def"
395
John McCall51bd8032009-10-18 01:05:36 +0000396 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
397 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000398};
399
400}
401
John McCall51bd8032009-10-18 01:05:36 +0000402void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
403 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000404}
John McCall51bd8032009-10-18 01:05:36 +0000405void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000406 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
407 if (TL.needsExtraLocalData()) {
408 Record.push_back(TL.getWrittenTypeSpec());
409 Record.push_back(TL.getWrittenSignSpec());
410 Record.push_back(TL.getWrittenWidthSpec());
411 Record.push_back(TL.hasModeAttr());
412 }
John McCalla1ee0c52009-10-16 21:56:05 +0000413}
John McCall51bd8032009-10-18 01:05:36 +0000414void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
415 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000416}
John McCall51bd8032009-10-18 01:05:36 +0000417void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
418 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000419}
John McCall51bd8032009-10-18 01:05:36 +0000420void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
421 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000422}
John McCall51bd8032009-10-18 01:05:36 +0000423void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
424 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000425}
John McCall51bd8032009-10-18 01:05:36 +0000426void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
427 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000428}
John McCall51bd8032009-10-18 01:05:36 +0000429void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
430 Writer.AddSourceLocation(TL.getStarLoc(), Record);
Abramo Bagnarab6ab6c12011-03-05 14:42:21 +0000431 Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000432}
John McCall51bd8032009-10-18 01:05:36 +0000433void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
434 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
435 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
436 Record.push_back(TL.getSizeExpr() ? 1 : 0);
437 if (TL.getSizeExpr())
438 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000439}
John McCall51bd8032009-10-18 01:05:36 +0000440void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
441 VisitArrayTypeLoc(TL);
442}
443void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
444 VisitArrayTypeLoc(TL);
445}
446void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
447 VisitArrayTypeLoc(TL);
448}
449void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
450 DependentSizedArrayTypeLoc TL) {
451 VisitArrayTypeLoc(TL);
452}
453void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
454 DependentSizedExtVectorTypeLoc TL) {
455 Writer.AddSourceLocation(TL.getNameLoc(), Record);
456}
457void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
458 Writer.AddSourceLocation(TL.getNameLoc(), Record);
459}
460void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
461 Writer.AddSourceLocation(TL.getNameLoc(), Record);
462}
463void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
Abramo Bagnara796aa442011-03-12 11:17:06 +0000464 Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
465 Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
Douglas Gregordab60ad2010-10-01 18:44:50 +0000466 Record.push_back(TL.getTrailingReturn());
John McCall51bd8032009-10-18 01:05:36 +0000467 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
468 Writer.AddDeclRef(TL.getArg(i), Record);
469}
470void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
471 VisitFunctionTypeLoc(TL);
472}
473void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
474 VisitFunctionTypeLoc(TL);
475}
John McCalled976492009-12-04 22:46:56 +0000476void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
477 Writer.AddSourceLocation(TL.getNameLoc(), Record);
478}
John McCall51bd8032009-10-18 01:05:36 +0000479void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
480 Writer.AddSourceLocation(TL.getNameLoc(), Record);
481}
482void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000483 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
484 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
485 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000486}
487void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000488 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
489 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
490 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
491 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000492}
493void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
494 Writer.AddSourceLocation(TL.getNameLoc(), Record);
495}
Richard Smith34b41d92011-02-20 03:19:35 +0000496void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
497 Writer.AddSourceLocation(TL.getNameLoc(), Record);
498}
John McCall51bd8032009-10-18 01:05:36 +0000499void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
500 Writer.AddSourceLocation(TL.getNameLoc(), Record);
501}
502void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
503 Writer.AddSourceLocation(TL.getNameLoc(), Record);
504}
John McCall9d156a72011-01-06 01:58:22 +0000505void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
506 Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
507 if (TL.hasAttrOperand()) {
508 SourceRange range = TL.getAttrOperandParensRange();
509 Writer.AddSourceLocation(range.getBegin(), Record);
510 Writer.AddSourceLocation(range.getEnd(), Record);
511 }
512 if (TL.hasAttrExprOperand()) {
513 Expr *operand = TL.getAttrExprOperand();
514 Record.push_back(operand ? 1 : 0);
515 if (operand) Writer.AddStmt(operand);
516 } else if (TL.hasAttrEnumOperand()) {
517 Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
518 }
519}
John McCall51bd8032009-10-18 01:05:36 +0000520void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
521 Writer.AddSourceLocation(TL.getNameLoc(), Record);
522}
John McCall49a832b2009-10-18 09:09:24 +0000523void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
524 SubstTemplateTypeParmTypeLoc TL) {
525 Writer.AddSourceLocation(TL.getNameLoc(), Record);
526}
Douglas Gregorc3069d62011-01-14 02:55:32 +0000527void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
528 SubstTemplateTypeParmPackTypeLoc TL) {
529 Writer.AddSourceLocation(TL.getNameLoc(), Record);
530}
John McCall51bd8032009-10-18 01:05:36 +0000531void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
532 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000533 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
534 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
535 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
536 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000537 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
538 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000539}
Abramo Bagnara075f8f12010-12-10 16:29:40 +0000540void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
541 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
542 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
543}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000544void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000545 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor9e876872011-03-01 18:12:44 +0000546 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000547}
John McCall3cb0ebd2010-03-10 03:28:59 +0000548void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
549 Writer.AddSourceLocation(TL.getNameLoc(), Record);
550}
Douglas Gregor4714c122010-03-31 17:34:00 +0000551void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000552 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor2494dd02011-03-01 01:34:45 +0000553 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000554 Writer.AddSourceLocation(TL.getNameLoc(), Record);
555}
John McCall33500952010-06-11 00:33:02 +0000556void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
557 DependentTemplateSpecializationTypeLoc TL) {
558 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
Douglas Gregor94fdffa2011-03-01 20:11:18 +0000559 Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
John McCall33500952010-06-11 00:33:02 +0000560 Writer.AddSourceLocation(TL.getNameLoc(), Record);
561 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
562 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
563 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000564 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
565 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000566}
Douglas Gregor7536dd52010-12-20 02:24:11 +0000567void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
568 Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
569}
John McCall51bd8032009-10-18 01:05:36 +0000570void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
571 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000572}
573void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
574 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000575 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
576 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
577 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
578 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000579}
John McCall54e14c42009-10-22 22:37:11 +0000580void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
581 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000582}
John McCalla1ee0c52009-10-16 21:56:05 +0000583
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000584//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000585// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000586//===----------------------------------------------------------------------===//
587
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000588static void EmitBlockID(unsigned ID, const char *Name,
589 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000590 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000591 Record.clear();
592 Record.push_back(ID);
593 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
594
595 // Emit the block name if present.
596 if (Name == 0 || Name[0] == 0) return;
597 Record.clear();
598 while (*Name)
599 Record.push_back(*Name++);
600 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
601}
602
603static void EmitRecordID(unsigned ID, const char *Name,
604 llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000605 ASTWriter::RecordDataImpl &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000606 Record.clear();
607 Record.push_back(ID);
608 while (*Name)
609 Record.push_back(*Name++);
610 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000611}
612
613static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +0000614 ASTWriter::RecordDataImpl &Record) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000615#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Chris Lattner0558df22009-04-27 00:49:53 +0000616 RECORD(STMT_STOP);
617 RECORD(STMT_NULL_PTR);
618 RECORD(STMT_NULL);
619 RECORD(STMT_COMPOUND);
620 RECORD(STMT_CASE);
621 RECORD(STMT_DEFAULT);
622 RECORD(STMT_LABEL);
623 RECORD(STMT_IF);
624 RECORD(STMT_SWITCH);
625 RECORD(STMT_WHILE);
626 RECORD(STMT_DO);
627 RECORD(STMT_FOR);
628 RECORD(STMT_GOTO);
629 RECORD(STMT_INDIRECT_GOTO);
630 RECORD(STMT_CONTINUE);
631 RECORD(STMT_BREAK);
632 RECORD(STMT_RETURN);
633 RECORD(STMT_DECL);
634 RECORD(STMT_ASM);
635 RECORD(EXPR_PREDEFINED);
636 RECORD(EXPR_DECL_REF);
637 RECORD(EXPR_INTEGER_LITERAL);
638 RECORD(EXPR_FLOATING_LITERAL);
639 RECORD(EXPR_IMAGINARY_LITERAL);
640 RECORD(EXPR_STRING_LITERAL);
641 RECORD(EXPR_CHARACTER_LITERAL);
642 RECORD(EXPR_PAREN);
643 RECORD(EXPR_UNARY_OPERATOR);
644 RECORD(EXPR_SIZEOF_ALIGN_OF);
645 RECORD(EXPR_ARRAY_SUBSCRIPT);
646 RECORD(EXPR_CALL);
647 RECORD(EXPR_MEMBER);
648 RECORD(EXPR_BINARY_OPERATOR);
649 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
650 RECORD(EXPR_CONDITIONAL_OPERATOR);
651 RECORD(EXPR_IMPLICIT_CAST);
652 RECORD(EXPR_CSTYLE_CAST);
653 RECORD(EXPR_COMPOUND_LITERAL);
654 RECORD(EXPR_EXT_VECTOR_ELEMENT);
655 RECORD(EXPR_INIT_LIST);
656 RECORD(EXPR_DESIGNATED_INIT);
657 RECORD(EXPR_IMPLICIT_VALUE_INIT);
658 RECORD(EXPR_VA_ARG);
659 RECORD(EXPR_ADDR_LABEL);
660 RECORD(EXPR_STMT);
Chris Lattner0558df22009-04-27 00:49:53 +0000661 RECORD(EXPR_CHOOSE);
662 RECORD(EXPR_GNU_NULL);
663 RECORD(EXPR_SHUFFLE_VECTOR);
664 RECORD(EXPR_BLOCK);
665 RECORD(EXPR_BLOCK_DECL_REF);
Peter Collingbournef111d932011-04-15 00:35:48 +0000666 RECORD(EXPR_GENERIC_SELECTION);
Chris Lattner0558df22009-04-27 00:49:53 +0000667 RECORD(EXPR_OBJC_STRING_LITERAL);
668 RECORD(EXPR_OBJC_ENCODE);
669 RECORD(EXPR_OBJC_SELECTOR_EXPR);
670 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
671 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
672 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
673 RECORD(EXPR_OBJC_KVC_REF_EXPR);
674 RECORD(EXPR_OBJC_MESSAGE_EXPR);
Chris Lattner0558df22009-04-27 00:49:53 +0000675 RECORD(STMT_OBJC_FOR_COLLECTION);
676 RECORD(STMT_OBJC_CATCH);
677 RECORD(STMT_OBJC_FINALLY);
678 RECORD(STMT_OBJC_AT_TRY);
679 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
680 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000681 RECORD(EXPR_CXX_OPERATOR_CALL);
682 RECORD(EXPR_CXX_CONSTRUCT);
683 RECORD(EXPR_CXX_STATIC_CAST);
684 RECORD(EXPR_CXX_DYNAMIC_CAST);
685 RECORD(EXPR_CXX_REINTERPRET_CAST);
686 RECORD(EXPR_CXX_CONST_CAST);
687 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
688 RECORD(EXPR_CXX_BOOL_LITERAL);
689 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000690 RECORD(EXPR_CXX_TYPEID_EXPR);
691 RECORD(EXPR_CXX_TYPEID_TYPE);
692 RECORD(EXPR_CXX_UUIDOF_EXPR);
693 RECORD(EXPR_CXX_UUIDOF_TYPE);
694 RECORD(EXPR_CXX_THIS);
695 RECORD(EXPR_CXX_THROW);
696 RECORD(EXPR_CXX_DEFAULT_ARG);
697 RECORD(EXPR_CXX_BIND_TEMPORARY);
698 RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
699 RECORD(EXPR_CXX_NEW);
700 RECORD(EXPR_CXX_DELETE);
701 RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
702 RECORD(EXPR_EXPR_WITH_CLEANUPS);
703 RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
704 RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
705 RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
706 RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
707 RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
708 RECORD(EXPR_CXX_UNARY_TYPE_TRAIT);
709 RECORD(EXPR_CXX_NOEXCEPT);
710 RECORD(EXPR_OPAQUE_VALUE);
711 RECORD(EXPR_BINARY_TYPE_TRAIT);
712 RECORD(EXPR_PACK_EXPANSION);
713 RECORD(EXPR_SIZEOF_PACK);
714 RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
Peter Collingbournee08ce652011-02-09 21:07:24 +0000715 RECORD(EXPR_CUDA_KERNEL_CALL);
Chris Lattner0558df22009-04-27 00:49:53 +0000716#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000717}
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Sebastian Redla4232eb2010-08-18 23:56:21 +0000719void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000720 RecordData Record;
721 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000722
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000723#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
724#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Sebastian Redl3397c552010-08-18 23:56:27 +0000726 // AST Top-Level Block.
Sebastian Redlf29f0a22010-08-18 23:57:22 +0000727 BLOCK(AST_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000728 RECORD(ORIGINAL_FILE_NAME);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000729 RECORD(TYPE_OFFSET);
730 RECORD(DECL_OFFSET);
731 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000732 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000733 RECORD(IDENTIFIER_OFFSET);
734 RECORD(IDENTIFIER_TABLE);
735 RECORD(EXTERNAL_DEFINITIONS);
736 RECORD(SPECIAL_TYPES);
737 RECORD(STATISTICS);
738 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000739 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000740 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
741 RECORD(SELECTOR_OFFSETS);
742 RECORD(METHOD_POOL);
743 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000744 RECORD(SOURCE_LOCATION_OFFSETS);
745 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000746 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000747 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000748 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000749 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redla93e3b52010-07-08 22:01:51 +0000750 RECORD(CHAINED_METADATA);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000751 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000752 RECORD(TU_UPDATE_LEXICAL);
753 RECORD(REDECLS_UPDATE_LATEST);
754 RECORD(SEMA_DECL_REFS);
755 RECORD(WEAK_UNDECLARED_IDENTIFIERS);
756 RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
757 RECORD(DECL_REPLACEMENTS);
758 RECORD(UPDATE_VISIBLE);
759 RECORD(DECL_UPDATE_OFFSETS);
760 RECORD(DECL_UPDATES);
761 RECORD(CXX_BASE_SPECIFIER_OFFSETS);
762 RECORD(DIAG_PRAGMA_MAPPINGS);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000763 RECORD(CUDA_SPECIAL_DECL_REFS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000764 RECORD(HEADER_SEARCH_TABLE);
Peter Collingbourne84bccea2011-02-15 19:46:30 +0000765 RECORD(FP_PRAGMA_OPTIONS);
766 RECORD(OPENCL_EXTENSIONS);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000767
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000768 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000769 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000770 RECORD(SM_SLOC_FILE_ENTRY);
771 RECORD(SM_SLOC_BUFFER_ENTRY);
772 RECORD(SM_SLOC_BUFFER_BLOB);
773 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
774 RECORD(SM_LINE_TABLE);
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000776 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000777 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000778 RECORD(PP_MACRO_OBJECT_LIKE);
779 RECORD(PP_MACRO_FUNCTION_LIKE);
780 RECORD(PP_TOKEN);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000781
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000782 // Decls and Types block.
783 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000784 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000785 RECORD(TYPE_COMPLEX);
786 RECORD(TYPE_POINTER);
787 RECORD(TYPE_BLOCK_POINTER);
788 RECORD(TYPE_LVALUE_REFERENCE);
789 RECORD(TYPE_RVALUE_REFERENCE);
790 RECORD(TYPE_MEMBER_POINTER);
791 RECORD(TYPE_CONSTANT_ARRAY);
792 RECORD(TYPE_INCOMPLETE_ARRAY);
793 RECORD(TYPE_VARIABLE_ARRAY);
794 RECORD(TYPE_VECTOR);
795 RECORD(TYPE_EXT_VECTOR);
796 RECORD(TYPE_FUNCTION_PROTO);
797 RECORD(TYPE_FUNCTION_NO_PROTO);
798 RECORD(TYPE_TYPEDEF);
799 RECORD(TYPE_TYPEOF_EXPR);
800 RECORD(TYPE_TYPEOF);
801 RECORD(TYPE_RECORD);
802 RECORD(TYPE_ENUM);
803 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000804 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000805 RECORD(TYPE_OBJC_OBJECT_POINTER);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000806 RECORD(TYPE_DECLTYPE);
807 RECORD(TYPE_ELABORATED);
808 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
809 RECORD(TYPE_UNRESOLVED_USING);
810 RECORD(TYPE_INJECTED_CLASS_NAME);
811 RECORD(TYPE_OBJC_OBJECT);
812 RECORD(TYPE_TEMPLATE_TYPE_PARM);
813 RECORD(TYPE_TEMPLATE_SPECIALIZATION);
814 RECORD(TYPE_DEPENDENT_NAME);
815 RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
816 RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
817 RECORD(TYPE_PAREN);
818 RECORD(TYPE_PACK_EXPANSION);
819 RECORD(TYPE_ATTRIBUTED);
820 RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000821 RECORD(DECL_TRANSLATION_UNIT);
822 RECORD(DECL_TYPEDEF);
823 RECORD(DECL_ENUM);
824 RECORD(DECL_RECORD);
825 RECORD(DECL_ENUM_CONSTANT);
826 RECORD(DECL_FUNCTION);
827 RECORD(DECL_OBJC_METHOD);
828 RECORD(DECL_OBJC_INTERFACE);
829 RECORD(DECL_OBJC_PROTOCOL);
830 RECORD(DECL_OBJC_IVAR);
831 RECORD(DECL_OBJC_AT_DEFS_FIELD);
832 RECORD(DECL_OBJC_CLASS);
833 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
834 RECORD(DECL_OBJC_CATEGORY);
835 RECORD(DECL_OBJC_CATEGORY_IMPL);
836 RECORD(DECL_OBJC_IMPLEMENTATION);
837 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
838 RECORD(DECL_OBJC_PROPERTY);
839 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000840 RECORD(DECL_FIELD);
841 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000842 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000843 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000844 RECORD(DECL_FILE_SCOPE_ASM);
845 RECORD(DECL_BLOCK);
846 RECORD(DECL_CONTEXT_LEXICAL);
847 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregorb6c2b3f2011-02-08 16:34:17 +0000848 RECORD(DECL_NAMESPACE);
849 RECORD(DECL_NAMESPACE_ALIAS);
850 RECORD(DECL_USING);
851 RECORD(DECL_USING_SHADOW);
852 RECORD(DECL_USING_DIRECTIVE);
853 RECORD(DECL_UNRESOLVED_USING_VALUE);
854 RECORD(DECL_UNRESOLVED_USING_TYPENAME);
855 RECORD(DECL_LINKAGE_SPEC);
856 RECORD(DECL_CXX_RECORD);
857 RECORD(DECL_CXX_METHOD);
858 RECORD(DECL_CXX_CONSTRUCTOR);
859 RECORD(DECL_CXX_DESTRUCTOR);
860 RECORD(DECL_CXX_CONVERSION);
861 RECORD(DECL_ACCESS_SPEC);
862 RECORD(DECL_FRIEND);
863 RECORD(DECL_FRIEND_TEMPLATE);
864 RECORD(DECL_CLASS_TEMPLATE);
865 RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
866 RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
867 RECORD(DECL_FUNCTION_TEMPLATE);
868 RECORD(DECL_TEMPLATE_TYPE_PARM);
869 RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
870 RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
871 RECORD(DECL_STATIC_ASSERT);
872 RECORD(DECL_CXX_BASE_SPECIFIERS);
873 RECORD(DECL_INDIRECTFIELD);
874 RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
875
Douglas Gregor4800a5c2011-02-08 21:58:10 +0000876 BLOCK(PREPROCESSOR_DETAIL_BLOCK);
877 RECORD(PPD_MACRO_INSTANTIATION);
878 RECORD(PPD_MACRO_DEFINITION);
879 RECORD(PPD_INCLUSION_DIRECTIVE);
880
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000881 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattner0558df22009-04-27 00:49:53 +0000882 AddStmtsExprs(Stream, Record);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000883#undef RECORD
884#undef BLOCK
885 Stream.ExitBlock();
886}
887
Douglas Gregore650c8c2009-07-07 00:12:59 +0000888/// \brief Adjusts the given filename to only write out the portion of the
889/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000890///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000891/// \param Filename the file name to adjust.
892///
893/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
894/// the returned filename will be adjusted by this system root.
895///
896/// \returns either the original filename (if it needs no adjustment) or the
897/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000898static const char *
Douglas Gregore650c8c2009-07-07 00:12:59 +0000899adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
900 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000901
Douglas Gregore650c8c2009-07-07 00:12:59 +0000902 if (!isysroot)
903 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000904
Douglas Gregore650c8c2009-07-07 00:12:59 +0000905 // Verify that the filename and the system root have the same prefix.
906 unsigned Pos = 0;
907 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
908 if (Filename[Pos] != isysroot[Pos])
909 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000910
Douglas Gregore650c8c2009-07-07 00:12:59 +0000911 // We hit the end of the filename before we hit the end of the system root.
912 if (!Filename[Pos])
913 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000914
Douglas Gregore650c8c2009-07-07 00:12:59 +0000915 // If the file name has a '/' at the current position, skip over the '/'.
916 // We distinguish sysroot-based includes from absolute includes by the
917 // absence of '/' at the beginning of sysroot-based includes.
918 if (Filename[Pos] == '/')
919 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000920
Douglas Gregore650c8c2009-07-07 00:12:59 +0000921 return Filename + Pos;
922}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000923
Sebastian Redl3397c552010-08-18 23:56:27 +0000924/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000925void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot,
926 const std::string &OutputFile) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000927 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000928
Douglas Gregore650c8c2009-07-07 00:12:59 +0000929 // Metadata
930 const TargetInfo &Target = Context.Target;
931 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl77f46032010-07-09 21:00:24 +0000932 MetaAbbrev->Add(BitCodeAbbrevOp(
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000933 Chain ? CHAINED_METADATA : METADATA));
Sebastian Redl3397c552010-08-18 23:56:27 +0000934 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
935 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregore650c8c2009-07-07 00:12:59 +0000936 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
937 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
938 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl77f46032010-07-09 21:00:24 +0000939 // Target triple or chained PCH name
940 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregore650c8c2009-07-07 00:12:59 +0000941 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000942
Douglas Gregore650c8c2009-07-07 00:12:59 +0000943 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000944 Record.push_back(Chain ? CHAINED_METADATA : METADATA);
945 Record.push_back(VERSION_MAJOR);
946 Record.push_back(VERSION_MINOR);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000947 Record.push_back(CLANG_VERSION_MAJOR);
948 Record.push_back(CLANG_VERSION_MINOR);
949 Record.push_back(isysroot != 0);
Sebastian Redl77f46032010-07-09 21:00:24 +0000950 // FIXME: This writes the absolute path for chained headers.
951 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
952 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000953
Douglas Gregorb64c1932009-05-12 01:31:05 +0000954 // Original file name
955 SourceManager &SM = Context.getSourceManager();
956 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
957 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000958 FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE_NAME));
Douglas Gregorb64c1932009-05-12 01:31:05 +0000959 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
960 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
961
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000962 llvm::SmallString<128> MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000963
Michael J. Spencerfbfd1802010-12-21 16:45:57 +0000964 llvm::sys::fs::make_absolute(MainFilePath);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000965
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +0000966 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000967 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000968 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000969 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000970 Record.push_back(ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000971 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000972 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000973
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +0000974 // Original PCH directory
975 if (!OutputFile.empty() && OutputFile != "-") {
976 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
977 Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
978 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
979 unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
980
981 llvm::SmallString<128> OutputPath(OutputFile);
982
983 llvm::sys::fs::make_absolute(OutputPath);
984 StringRef origDir = llvm::sys::path::parent_path(OutputPath);
985
986 RecordData Record;
987 Record.push_back(ORIGINAL_PCH_DIR);
988 Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
989 }
990
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000991 // Repository branch/version information.
992 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000993 RepoAbbrev->Add(BitCodeAbbrevOp(VERSION_CONTROL_BRANCH_REVISION));
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000994 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
995 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000996 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +0000997 Record.push_back(VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000998 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
999 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +00001000}
1001
1002/// \brief Write the LangOptions structure.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001003void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001004 RecordData Record;
1005 Record.push_back(LangOpts.Trigraphs);
1006 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1007 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1008 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1009 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00001010 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001011 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1012 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1013 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1014 Record.push_back(LangOpts.C99); // C99 Support
Peter Collingbourne7e7fbd02011-04-15 00:35:23 +00001015 Record.push_back(LangOpts.C1X); // C1X Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001016 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
Michael J. Spencerdae4ac42010-10-21 05:21:48 +00001017 // LangOpts.MSCVersion is ignored because all it does it set a macro, which is
1018 // already saved elsewhere.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001019 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1020 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001021 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +00001022
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001023 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1024 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001025 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian412e7982010-02-09 19:31:38 +00001026 // modern abi enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001027 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian412e7982010-02-09 19:31:38 +00001028 // modern abi enabled.
Fariborz Jahanianf84109e2011-01-07 18:59:25 +00001029 Record.push_back(LangOpts.AppleKext); // Apple's kernel extensions ABI
Ted Kremenekc32647d2010-12-23 21:35:43 +00001030 Record.push_back(LangOpts.ObjCDefaultSynthProperties); // Objective-C auto-synthesized
1031 // properties enabled.
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +00001032 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump1eb44332009-09-09 15:08:12 +00001033
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001034 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001035 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1036 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +00001037 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001038 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Anders Carlssonda4b7cf2011-02-19 23:53:54 +00001039 Record.push_back(LangOpts.ObjCExceptions);
Anders Carlsson7da99b02011-02-23 03:04:54 +00001040 Record.push_back(LangOpts.CXXExceptions);
1041 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001042
Douglas Gregor6f755502011-02-01 15:15:22 +00001043 Record.push_back(LangOpts.MSBitfields); // MS-compatible structure layout
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001044 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1045 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1046 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1047
Chris Lattnerea5ce472009-04-27 07:35:58 +00001048 // Whether static initializers are protected by locks.
1049 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +00001050 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001051 Record.push_back(LangOpts.Blocks); // block extension to C
1052 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1053 // they are unused.
1054 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1055 // (modulo the platform support).
1056
Chris Lattnera4d71452010-06-26 21:25:03 +00001057 Record.push_back(LangOpts.getSignedOverflowBehavior());
1058 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001059
1060 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +00001061 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001062 // defined.
1063 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1064 // opposed to __DYNAMIC__).
1065 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1066
1067 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1068 // used (instead of C99 semantics).
1069 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Chandler Carruth0d2d1bc2011-04-23 20:05:38 +00001070 Record.push_back(LangOpts.Deprecated); // Should __DEPRECATED be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +00001071 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
1072 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +00001073 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
1074 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +00001075 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Argyrios Kyrtzidisb1bdced2011-01-15 02:56:16 +00001076 Record.push_back(LangOpts.ShortEnums); // Should the enum type be equivalent
1077 // to the smallest integer type with
1078 // enough room.
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001079 Record.push_back(LangOpts.getGCMode());
1080 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +00001081 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001082 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +00001083 Record.push_back(LangOpts.OpenCL);
Peter Collingbourne08a53262010-12-01 19:14:57 +00001084 Record.push_back(LangOpts.CUDA);
Mike Stump9c276ae2009-12-12 01:27:46 +00001085 Record.push_back(LangOpts.CatchUndefined);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00001086 Record.push_back(LangOpts.DefaultFPContract);
Anders Carlsson92f58222009-08-22 22:30:33 +00001087 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +00001088 Record.push_back(LangOpts.SpellChecking);
Roman Divackycfe9af22011-03-01 17:40:53 +00001089 Record.push_back(LangOpts.MRTD);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001090 Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +00001091}
1092
Douglas Gregor14f79002009-04-10 03:52:48 +00001093//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001094// stat cache Serialization
1095//===----------------------------------------------------------------------===//
1096
1097namespace {
1098// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +00001099class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001100public:
1101 typedef const char * key_type;
1102 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001103
Chris Lattner74e976b2010-11-23 19:28:12 +00001104 typedef struct stat data_type;
1105 typedef const data_type &data_type_ref;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001106
1107 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001108 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001109 }
Mike Stump1eb44332009-09-09 15:08:12 +00001110
1111 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001112 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1113 data_type_ref Data) {
1114 unsigned StrLen = strlen(path);
1115 clang::io::Emit16(Out, StrLen);
Chris Lattner74e976b2010-11-23 19:28:12 +00001116 unsigned DataLen = 4 + 4 + 2 + 8 + 8;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001117 clang::io::Emit8(Out, DataLen);
1118 return std::make_pair(StrLen + 1, DataLen);
1119 }
Mike Stump1eb44332009-09-09 15:08:12 +00001120
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001121 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1122 Out.write(path, KeyLen);
1123 }
Mike Stump1eb44332009-09-09 15:08:12 +00001124
Chris Lattner74e976b2010-11-23 19:28:12 +00001125 void EmitData(llvm::raw_ostream &Out, key_type_ref,
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001126 data_type_ref Data, unsigned DataLen) {
1127 using namespace clang::io;
1128 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +00001129
Chris Lattner74e976b2010-11-23 19:28:12 +00001130 Emit32(Out, (uint32_t) Data.st_ino);
1131 Emit32(Out, (uint32_t) Data.st_dev);
1132 Emit16(Out, (uint16_t) Data.st_mode);
1133 Emit64(Out, (uint64_t) Data.st_mtime);
1134 Emit64(Out, (uint64_t) Data.st_size);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001135
1136 assert(Out.tell() - Start == DataLen && "Wrong data length");
1137 }
1138};
1139} // end anonymous namespace
1140
Sebastian Redl3397c552010-08-18 23:56:27 +00001141/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001142void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001143 // Build the on-disk hash table containing information about every
1144 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +00001145 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001146 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001147 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001148 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001149 Stat != StatEnd; ++Stat, ++NumStatEntries) {
1150 const char *Filename = Stat->first();
Douglas Gregore650c8c2009-07-07 00:12:59 +00001151 Generator.insert(Filename, Stat->second);
1152 }
Mike Stump1eb44332009-09-09 15:08:12 +00001153
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001154 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001155 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001156 uint32_t BucketOffset;
1157 {
1158 llvm::raw_svector_ostream Out(StatCacheData);
1159 // Make sure that no bucket is at offset 0
1160 clang::io::Emit32(Out, 0);
1161 BucketOffset = Generator.Emit(Out);
1162 }
1163
1164 // Create a blob abbreviation
1165 using namespace llvm;
1166 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001167 Abbrev->Add(BitCodeAbbrevOp(STAT_CACHE));
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001168 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1169 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1170 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1171 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
1172
1173 // Write the stat cache
1174 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001175 Record.push_back(STAT_CACHE);
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001176 Record.push_back(BucketOffset);
1177 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001178 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001179}
1180
1181//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +00001182// Source Manager Serialization
1183//===----------------------------------------------------------------------===//
1184
1185/// \brief Create an abbreviation for the SLocEntry that refers to a
1186/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001187static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001188 using namespace llvm;
1189 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001190 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001191 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1192 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1193 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1194 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +00001195 // FileEntry fields.
1196 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1197 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor14f79002009-04-10 03:52:48 +00001198 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001199 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001200}
1201
1202/// \brief Create an abbreviation for the SLocEntry that refers to a
1203/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001204static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001205 using namespace llvm;
1206 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001207 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001208 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1209 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1210 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1211 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1212 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001213 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001214}
1215
1216/// \brief Create an abbreviation for the SLocEntry that refers to a
1217/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001218static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001219 using namespace llvm;
1220 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001221 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
Douglas Gregor14f79002009-04-10 03:52:48 +00001222 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001223 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001224}
1225
1226/// \brief Create an abbreviation for the SLocEntry that refers to an
1227/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001228static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001229 using namespace llvm;
1230 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001231 Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_INSTANTIATION_ENTRY));
Douglas Gregor14f79002009-04-10 03:52:48 +00001232 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1233 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1234 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1235 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001236 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001237 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001238}
1239
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001240namespace {
1241 // Trait used for the on-disk hash table of header search information.
1242 class HeaderFileInfoTrait {
1243 ASTWriter &Writer;
1244 HeaderSearch &HS;
1245
1246 public:
1247 HeaderFileInfoTrait(ASTWriter &Writer, HeaderSearch &HS)
1248 : Writer(Writer), HS(HS) { }
1249
1250 typedef const char *key_type;
1251 typedef key_type key_type_ref;
1252
1253 typedef HeaderFileInfo data_type;
1254 typedef const data_type &data_type_ref;
1255
1256 static unsigned ComputeHash(const char *path) {
1257 // The hash is based only on the filename portion of the key, so that the
1258 // reader can match based on filenames when symlinking or excess path
1259 // elements ("foo/../", "../") change the form of the name. However,
1260 // complete path is still the key.
1261 return llvm::HashString(llvm::sys::path::filename(path));
1262 }
1263
1264 std::pair<unsigned,unsigned>
1265 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
1266 data_type_ref Data) {
1267 unsigned StrLen = strlen(path);
1268 clang::io::Emit16(Out, StrLen);
1269 unsigned DataLen = 1 + 2 + 4;
1270 clang::io::Emit8(Out, DataLen);
1271 return std::make_pair(StrLen + 1, DataLen);
1272 }
1273
1274 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
1275 Out.write(path, KeyLen);
1276 }
1277
1278 void EmitData(llvm::raw_ostream &Out, key_type_ref,
1279 data_type_ref Data, unsigned DataLen) {
1280 using namespace clang::io;
1281 uint64_t Start = Out.tell(); (void)Start;
1282
Douglas Gregordd3e5542011-05-04 00:14:37 +00001283 unsigned char Flags = (Data.isImport << 4)
1284 | (Data.isPragmaOnce << 3)
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00001285 | (Data.DirInfo << 1)
1286 | Data.Resolved;
1287 Emit8(Out, (uint8_t)Flags);
1288 Emit16(Out, (uint16_t) Data.NumIncludes);
1289
1290 if (!Data.ControllingMacro)
1291 Emit32(Out, (uint32_t)Data.ControllingMacroID);
1292 else
1293 Emit32(Out, (uint32_t)Writer.getIdentifierRef(Data.ControllingMacro));
1294 assert(Out.tell() - Start == DataLen && "Wrong data length");
1295 }
1296 };
1297} // end anonymous namespace
1298
1299/// \brief Write the header search block for the list of files that
1300///
1301/// \param HS The header search structure to save.
1302///
1303/// \param Chain Whether we're creating a chained AST file.
1304void ASTWriter::WriteHeaderSearch(HeaderSearch &HS, const char* isysroot) {
1305 llvm::SmallVector<const FileEntry *, 16> FilesByUID;
1306 HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1307
1308 if (FilesByUID.size() > HS.header_file_size())
1309 FilesByUID.resize(HS.header_file_size());
1310
1311 HeaderFileInfoTrait GeneratorTrait(*this, HS);
1312 OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
1313 llvm::SmallVector<const char *, 4> SavedStrings;
1314 unsigned NumHeaderSearchEntries = 0;
1315 for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1316 const FileEntry *File = FilesByUID[UID];
1317 if (!File)
1318 continue;
1319
1320 const HeaderFileInfo &HFI = HS.header_file_begin()[UID];
1321 if (HFI.External && Chain)
1322 continue;
1323
1324 // Turn the file name into an absolute path, if it isn't already.
1325 const char *Filename = File->getName();
1326 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
1327
1328 // If we performed any translation on the file name at all, we need to
1329 // save this string, since the generator will refer to it later.
1330 if (Filename != File->getName()) {
1331 Filename = strdup(Filename);
1332 SavedStrings.push_back(Filename);
1333 }
1334
1335 Generator.insert(Filename, HFI, GeneratorTrait);
1336 ++NumHeaderSearchEntries;
1337 }
1338
1339 // Create the on-disk hash table in a buffer.
1340 llvm::SmallString<4096> TableData;
1341 uint32_t BucketOffset;
1342 {
1343 llvm::raw_svector_ostream Out(TableData);
1344 // Make sure that no bucket is at offset 0
1345 clang::io::Emit32(Out, 0);
1346 BucketOffset = Generator.Emit(Out, GeneratorTrait);
1347 }
1348
1349 // Create a blob abbreviation
1350 using namespace llvm;
1351 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1352 Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1353 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1354 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1355 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1356 unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1357
1358 // Write the stat cache
1359 RecordData Record;
1360 Record.push_back(HEADER_SEARCH_TABLE);
1361 Record.push_back(BucketOffset);
1362 Record.push_back(NumHeaderSearchEntries);
1363 Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData.str());
1364
1365 // Free all of the strings we had to duplicate.
1366 for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1367 free((void*)SavedStrings[I]);
1368}
1369
Douglas Gregor14f79002009-04-10 03:52:48 +00001370/// \brief Writes the block containing the serialized form of the
1371/// source manager.
1372///
1373/// TODO: We should probably use an on-disk hash table (stored in a
1374/// blob), indexed based on the file name, so that we only create
1375/// entries for files that we actually need. In the common case (no
1376/// errors), we probably won't have to create file entries for any of
1377/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001378void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001379 const Preprocessor &PP,
1380 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001381 RecordData Record;
1382
Chris Lattnerf04ad692009-04-10 17:16:57 +00001383 // Enter the source manager block.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001384 Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001385
1386 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001387 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1388 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1389 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1390 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001391
Douglas Gregorbd945002009-04-13 16:31:14 +00001392 // Write the line table.
1393 if (SourceMgr.hasLineTable()) {
1394 LineTableInfo &LineTable = SourceMgr.getLineTable();
1395
1396 // Emit the file names
1397 Record.push_back(LineTable.getNumFilenames());
1398 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1399 // Emit the file name
1400 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001401 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +00001402 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1403 Record.push_back(FilenameLen);
1404 if (FilenameLen)
1405 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1406 }
Mike Stump1eb44332009-09-09 15:08:12 +00001407
Douglas Gregorbd945002009-04-13 16:31:14 +00001408 // Emit the line entries
1409 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1410 L != LEnd; ++L) {
1411 // Emit the file ID
1412 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +00001413
Douglas Gregorbd945002009-04-13 16:31:14 +00001414 // Emit the line entries
1415 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001416 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +00001417 LEEnd = L->second.end();
1418 LE != LEEnd; ++LE) {
1419 Record.push_back(LE->FileOffset);
1420 Record.push_back(LE->LineNo);
1421 Record.push_back(LE->FilenameID);
1422 Record.push_back((unsigned)LE->FileKind);
1423 Record.push_back(LE->IncludeOffset);
1424 }
Douglas Gregorbd945002009-04-13 16:31:14 +00001425 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001426 Stream.EmitRecord(SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001427 }
1428
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001429 // Write out the source location entry table. We skip the first
1430 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001431 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001432 RecordData PreloadSLocs;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001433 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1434 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1435 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1436 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001437 // Get this source location entry.
1438 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001439
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001440 // Record the offset of this source-location entry.
1441 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1442
1443 // Figure out which record code to use.
1444 unsigned Code;
1445 if (SLoc->isFile()) {
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001446 if (SLoc->getFile().getContentCache()->OrigEntry)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001447 Code = SM_SLOC_FILE_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001448 else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001449 Code = SM_SLOC_BUFFER_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001450 } else
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001451 Code = SM_SLOC_INSTANTIATION_ENTRY;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001452 Record.clear();
1453 Record.push_back(Code);
1454
1455 Record.push_back(SLoc->getOffset());
1456 if (SLoc->isFile()) {
1457 const SrcMgr::FileInfo &File = SLoc->getFile();
1458 Record.push_back(File.getIncludeLoc().getRawEncoding());
1459 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1460 Record.push_back(File.hasLineDirectives());
1461
1462 const SrcMgr::ContentCache *Content = File.getContentCache();
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001463 if (Content->OrigEntry) {
1464 assert(Content->OrigEntry == Content->ContentsEntry &&
1465 "Writing to AST an overriden file is not supported");
1466
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001467 // The source location entry is a file. The blob associated
1468 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001469
Douglas Gregor2d52be52010-03-21 22:49:54 +00001470 // Emit size/modification time for this file.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001471 Record.push_back(Content->OrigEntry->getSize());
1472 Record.push_back(Content->OrigEntry->getModificationTime());
Douglas Gregor2d52be52010-03-21 22:49:54 +00001473
Douglas Gregore650c8c2009-07-07 00:12:59 +00001474 // Turn the file name into an absolute path, if it isn't already.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +00001475 const char *Filename = Content->OrigEntry->getName();
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001476 llvm::SmallString<128> FilePath(Filename);
Anders Carlsson2c10c802011-03-08 16:04:35 +00001477
1478 // Ask the file manager to fixup the relative path for us. This will
1479 // honor the working directory.
1480 SourceMgr.getFileManager().FixupRelativePath(FilePath);
1481
1482 // FIXME: This call to make_absolute shouldn't be necessary, the
1483 // call to FixupRelativePath should always return an absolute path.
Michael J. Spencerfbfd1802010-12-21 16:45:57 +00001484 llvm::sys::fs::make_absolute(FilePath);
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001485 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001486
Douglas Gregore650c8c2009-07-07 00:12:59 +00001487 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001488 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001489 } else {
1490 // The source location entry is a buffer. The blob associated
1491 // with this entry contains the contents of the buffer.
1492
1493 // We add one to the size so that we capture the trailing NULL
1494 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1495 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001496 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001497 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001498 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001499 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1500 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001501 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001502 Record.push_back(SM_SLOC_BUFFER_BLOB);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001503 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001504 llvm::StringRef(Buffer->getBufferStart(),
1505 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001506
1507 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001508 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001509 }
1510 } else {
1511 // The source location entry is an instantiation.
1512 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1513 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1514 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1515 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1516
1517 // Compute the token length for this macro expansion.
1518 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001519 if (I + 1 != N)
1520 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001521 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1522 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1523 }
1524 }
1525
Douglas Gregorc9490c02009-04-16 22:23:12 +00001526 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001527
1528 if (SLocEntryOffsets.empty())
1529 return;
1530
Sebastian Redl3397c552010-08-18 23:56:27 +00001531 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001532 // table is used for lazily loading source-location information.
1533 using namespace llvm;
1534 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001535 Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001536 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1537 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1538 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1539 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001540
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001541 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001542 Record.push_back(SOURCE_LOCATION_OFFSETS);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001543 Record.push_back(SLocEntryOffsets.size());
Sebastian Redl8db9fae2010-09-22 20:19:08 +00001544 unsigned BaseOffset = Chain ? Chain->getNextSLocOffset() : 0;
1545 Record.push_back(SourceMgr.getNextOffset() - BaseOffset);
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001546 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record, data(SLocEntryOffsets));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001547
Sebastian Redl3397c552010-08-18 23:56:27 +00001548 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001549 // reader which source locations entries it should load eagerly.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001550 Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001551}
1552
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001553//===----------------------------------------------------------------------===//
1554// Preprocessor Serialization
1555//===----------------------------------------------------------------------===//
1556
Douglas Gregor9c736102011-02-10 18:20:09 +00001557static int compareMacroDefinitions(const void *XPtr, const void *YPtr) {
1558 const std::pair<const IdentifierInfo *, MacroInfo *> &X =
1559 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)XPtr;
1560 const std::pair<const IdentifierInfo *, MacroInfo *> &Y =
1561 *(const std::pair<const IdentifierInfo *, MacroInfo *>*)YPtr;
1562 return X.first->getName().compare(Y.first->getName());
1563}
1564
Chris Lattner0b1fb982009-04-10 17:15:23 +00001565/// \brief Writes the block containing the serialized form of the
1566/// preprocessor.
1567///
Sebastian Redla4232eb2010-08-18 23:56:21 +00001568void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001569 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001570
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001571 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1572 if (PP.getCounterValue() != 0) {
1573 Record.push_back(PP.getCounterValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001574 Stream.EmitRecord(PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001575 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001576 }
1577
1578 // Enter the preprocessor block.
Douglas Gregorecdcb882010-10-20 22:00:55 +00001579 Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +00001580
Sebastian Redl3397c552010-08-18 23:56:27 +00001581 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001582 // FIXME: use diagnostics subsystem for localization etc.
1583 if (PP.SawDateOrTime())
1584 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001585
Douglas Gregorecdcb882010-10-20 22:00:55 +00001586
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001587 // Loop over all the macro definitions that are live at the end of the file,
1588 // emitting each to the PP section.
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001589 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001590
Douglas Gregor9c736102011-02-10 18:20:09 +00001591 // Construct the list of macro definitions that need to be serialized.
1592 llvm::SmallVector<std::pair<const IdentifierInfo *, MacroInfo *>, 2>
1593 MacrosToEmit;
1594 llvm::SmallPtrSet<const IdentifierInfo*, 4> MacroDefinitionsSeen;
Douglas Gregor040a8042011-02-11 00:26:14 +00001595 for (Preprocessor::macro_iterator I = PP.macro_begin(Chain == 0),
1596 E = PP.macro_end(Chain == 0);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001597 I != E; ++I) {
Douglas Gregor9c736102011-02-10 18:20:09 +00001598 MacroDefinitionsSeen.insert(I->first);
1599 MacrosToEmit.push_back(std::make_pair(I->first, I->second));
1600 }
1601
1602 // Sort the set of macro definitions that need to be serialized by the
1603 // name of the macro, to provide a stable ordering.
1604 llvm::array_pod_sort(MacrosToEmit.begin(), MacrosToEmit.end(),
1605 &compareMacroDefinitions);
1606
Douglas Gregor040a8042011-02-11 00:26:14 +00001607 // Resolve any identifiers that defined macros at the time they were
1608 // deserialized, adding them to the list of macros to emit (if appropriate).
1609 for (unsigned I = 0, N = DeserializedMacroNames.size(); I != N; ++I) {
1610 IdentifierInfo *Name
1611 = const_cast<IdentifierInfo *>(DeserializedMacroNames[I]);
1612 if (Name->hasMacroDefinition() && MacroDefinitionsSeen.insert(Name))
1613 MacrosToEmit.push_back(std::make_pair(Name, PP.getMacroInfo(Name)));
1614 }
1615
Douglas Gregor9c736102011-02-10 18:20:09 +00001616 for (unsigned I = 0, N = MacrosToEmit.size(); I != N; ++I) {
1617 const IdentifierInfo *Name = MacrosToEmit[I].first;
1618 MacroInfo *MI = MacrosToEmit[I].second;
Douglas Gregor040a8042011-02-11 00:26:14 +00001619 if (!MI)
1620 continue;
1621
Sebastian Redl3397c552010-08-18 23:56:27 +00001622 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001623 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl3397c552010-08-18 23:56:27 +00001624 // Also skip macros from a AST file if we're chaining.
Douglas Gregoree9b0ba2010-10-01 01:03:07 +00001625
1626 // FIXME: There is a (probably minor) optimization we could do here, if
1627 // the macro comes from the original PCH but the identifier comes from a
1628 // chained PCH, by storing the offset into the original PCH rather than
1629 // writing the macro definition a second time.
Michael J. Spencer20249a12010-10-21 03:16:25 +00001630 if (MI->isBuiltinMacro() ||
Douglas Gregor9c736102011-02-10 18:20:09 +00001631 (Chain && Name->isFromAST() && MI->isFromAST()))
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001632 continue;
1633
Douglas Gregor9c736102011-02-10 18:20:09 +00001634 AddIdentifierRef(Name, Record);
1635 MacroOffsets[Name] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001636 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1637 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001638
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001639 unsigned Code;
1640 if (MI->isObjectLike()) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001641 Code = PP_MACRO_OBJECT_LIKE;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001642 } else {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001643 Code = PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001644
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001645 Record.push_back(MI->isC99Varargs());
1646 Record.push_back(MI->isGNUVarargs());
1647 Record.push_back(MI->getNumArgs());
1648 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1649 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001650 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001651 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001652
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001653 // If we have a detailed preprocessing record, record the macro definition
1654 // ID that corresponds to this macro.
1655 if (PPRec)
1656 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
Michael J. Spencer20249a12010-10-21 03:16:25 +00001657
Douglas Gregorc9490c02009-04-16 22:23:12 +00001658 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001659 Record.clear();
1660
Chris Lattnerdf961c22009-04-10 18:08:30 +00001661 // Emit the tokens array.
1662 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1663 // Note that we know that the preprocessor does not have any annotation
1664 // tokens in it because they are created by the parser, and thus can't be
1665 // in a macro definition.
1666 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001667
Chris Lattnerdf961c22009-04-10 18:08:30 +00001668 Record.push_back(Tok.getLocation().getRawEncoding());
1669 Record.push_back(Tok.getLength());
1670
Chris Lattnerdf961c22009-04-10 18:08:30 +00001671 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1672 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001673 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001674 // FIXME: Should translate token kind to a stable encoding.
1675 Record.push_back(Tok.getKind());
1676 // FIXME: Should translate token flags to a stable encoding.
1677 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001678
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001679 Stream.EmitRecord(PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001680 Record.clear();
1681 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001682 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001683 }
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001684 Stream.ExitBlock();
1685
1686 if (PPRec)
1687 WritePreprocessorDetail(*PPRec);
1688}
1689
1690void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
1691 if (PPRec.begin(Chain) == PPRec.end(Chain))
1692 return;
1693
1694 // Enter the preprocessor block.
1695 Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001696
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001697 // If the preprocessor has a preprocessing record, emit it.
1698 unsigned NumPreprocessingRecords = 0;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001699 using namespace llvm;
1700
1701 // Set up the abbreviation for
1702 unsigned InclusionAbbrev = 0;
1703 {
1704 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1705 Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
1706 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1707 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // start location
1708 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // end location
1709 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
1710 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
1711 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
1712 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1713 InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
1714 }
1715
1716 unsigned IndexBase = Chain ? PPRec.getNumPreallocatedEntities() : 0;
1717 RecordData Record;
1718 for (PreprocessingRecord::iterator E = PPRec.begin(Chain),
1719 EEnd = PPRec.end(Chain);
1720 E != EEnd; ++E) {
1721 Record.clear();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001722
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001723 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1724 // Record this macro definition's location.
1725 MacroID ID = getMacroDefinitionID(MD);
1726
1727 // Don't write the macro definition if it is from another AST file.
1728 if (ID < FirstMacroID)
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001729 continue;
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001730
Douglas Gregor89d99802010-11-30 06:16:57 +00001731 // Notify the serialization listener that we're serializing this entity.
1732 if (SerializationListener)
1733 SerializationListener->SerializedPreprocessedEntity(*E,
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001734 Stream.GetCurrentBitNo());
Douglas Gregor89d99802010-11-30 06:16:57 +00001735
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001736 unsigned Position = ID - FirstMacroID;
1737 if (Position != MacroDefinitionOffsets.size()) {
1738 if (Position > MacroDefinitionOffsets.size())
1739 MacroDefinitionOffsets.resize(Position + 1);
1740
1741 MacroDefinitionOffsets[Position] = Stream.GetCurrentBitNo();
1742 } else
1743 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregor89d99802010-11-30 06:16:57 +00001744
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001745 Record.push_back(IndexBase + NumPreprocessingRecords++);
1746 Record.push_back(ID);
1747 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1748 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1749 AddIdentifierRef(MD->getName(), Record);
1750 AddSourceLocation(MD->getLocation(), Record);
1751 Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
1752 continue;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001753 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00001754
Douglas Gregor4800a5c2011-02-08 21:58:10 +00001755 // Notify the serialization listener that we're serializing this entity.
1756 if (SerializationListener)
1757 SerializationListener->SerializedPreprocessedEntity(*E,
1758 Stream.GetCurrentBitNo());
1759
1760 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1761 Record.push_back(IndexBase + NumPreprocessingRecords++);
1762 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1763 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1764 AddIdentifierRef(MI->getName(), Record);
1765 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1766 Stream.EmitRecord(PPD_MACRO_INSTANTIATION, Record);
1767 continue;
1768 }
1769
1770 if (InclusionDirective *ID = dyn_cast<InclusionDirective>(*E)) {
1771 Record.push_back(PPD_INCLUSION_DIRECTIVE);
1772 Record.push_back(IndexBase + NumPreprocessingRecords++);
1773 AddSourceLocation(ID->getSourceRange().getBegin(), Record);
1774 AddSourceLocation(ID->getSourceRange().getEnd(), Record);
1775 Record.push_back(ID->getFileName().size());
1776 Record.push_back(ID->wasInQuotes());
1777 Record.push_back(static_cast<unsigned>(ID->getKind()));
1778 llvm::SmallString<64> Buffer;
1779 Buffer += ID->getFileName();
1780 Buffer += ID->getFile()->getName();
1781 Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
1782 continue;
1783 }
1784
1785 llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
1786 }
Douglas Gregorc9490c02009-04-16 22:23:12 +00001787 Stream.ExitBlock();
Michael J. Spencer20249a12010-10-21 03:16:25 +00001788
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001789 // Write the offsets table for the preprocessing record.
1790 if (NumPreprocessingRecords > 0) {
1791 // Write the offsets table for identifier IDs.
1792 using namespace llvm;
1793 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001794 Abbrev->Add(BitCodeAbbrevOp(MACRO_DEFINITION_OFFSETS));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001795 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1796 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1797 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1798 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
Michael J. Spencer20249a12010-10-21 03:16:25 +00001799
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001800 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001801 Record.push_back(MACRO_DEFINITION_OFFSETS);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001802 Record.push_back(NumPreprocessingRecords);
1803 Record.push_back(MacroDefinitionOffsets.size());
1804 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001805 data(MacroDefinitionOffsets));
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001806 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001807}
1808
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001809void ASTWriter::WritePragmaDiagnosticMappings(const Diagnostic &Diag) {
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001810 RecordData Record;
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001811 for (Diagnostic::DiagStatePointsTy::const_iterator
1812 I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
1813 I != E; ++I) {
1814 const Diagnostic::DiagStatePoint &point = *I;
1815 if (point.Loc.isInvalid())
1816 continue;
1817
1818 Record.push_back(point.Loc.getRawEncoding());
1819 for (Diagnostic::DiagState::iterator
1820 I = point.State->begin(), E = point.State->end(); I != E; ++I) {
1821 unsigned diag = I->first, map = I->second;
1822 if (map & 0x10) { // mapping from a diagnostic pragma.
1823 Record.push_back(diag);
1824 Record.push_back(map & 0x7);
1825 }
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001826 }
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001827 Record.push_back(-1); // mark the end of the diag/map pairs for this
1828 // location.
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001829 }
1830
Argyrios Kyrtzidis60f76842010-11-05 22:20:49 +00001831 if (!Record.empty())
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00001832 Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00001833}
1834
Anders Carlssonc8505782011-03-06 18:41:18 +00001835void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
1836 if (CXXBaseSpecifiersOffsets.empty())
1837 return;
1838
1839 RecordData Record;
1840
1841 // Create a blob abbreviation for the C++ base specifiers offsets.
1842 using namespace llvm;
1843
1844 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1845 Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
1846 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
1847 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1848 unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1849
1850 // Write the selector offsets table.
1851 Record.clear();
1852 Record.push_back(CXX_BASE_SPECIFIER_OFFSETS);
1853 Record.push_back(CXXBaseSpecifiersOffsets.size());
1854 Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001855 data(CXXBaseSpecifiersOffsets));
Anders Carlssonc8505782011-03-06 18:41:18 +00001856}
1857
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001858//===----------------------------------------------------------------------===//
1859// Type Serialization
1860//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001861
Sebastian Redl3397c552010-08-18 23:56:27 +00001862/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001863void ASTWriter::WriteType(QualType T) {
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00001864 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001865 if (Idx.getIndex() == 0) // we haven't seen this type before.
1866 Idx = TypeIdx(NextTypeID++);
Mike Stump1eb44332009-09-09 15:08:12 +00001867
Douglas Gregor97475832010-10-05 18:37:06 +00001868 assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
Douglas Gregor55f48de2010-10-04 18:21:45 +00001869
Douglas Gregor2cf26342009-04-09 22:27:44 +00001870 // Record the offset for this type.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00001871 unsigned Index = Idx.getIndex() - FirstTypeID;
Sebastian Redl681d7232010-07-27 00:17:23 +00001872 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001873 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00001874 else if (TypeOffsets.size() < Index) {
1875 TypeOffsets.resize(Index + 1);
1876 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001877 }
1878
1879 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001880
Douglas Gregor2cf26342009-04-09 22:27:44 +00001881 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00001882 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001883
Douglas Gregora4923eb2009-11-16 21:35:15 +00001884 if (T.hasLocalNonFastQualifiers()) {
1885 Qualifiers Qs = T.getLocalQualifiers();
1886 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001887 Record.push_back(Qs.getAsOpaqueValue());
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001888 W.Code = TYPE_EXT_QUAL;
John McCall0953e762009-09-24 19:53:00 +00001889 } else {
1890 switch (T->getTypeClass()) {
1891 // For all of the concrete, non-dependent types, call the
1892 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001893#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001894 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001895#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001896#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001897 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001898 }
1899
1900 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001901 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001902
1903 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001904 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001905}
1906
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001907//===----------------------------------------------------------------------===//
1908// Declaration Serialization
1909//===----------------------------------------------------------------------===//
1910
Douglas Gregor2cf26342009-04-09 22:27:44 +00001911/// \brief Write the block containing all of the declaration IDs
1912/// lexically declared within the given DeclContext.
1913///
1914/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1915/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001916uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001917 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001918 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001919 return 0;
1920
Douglas Gregorc9490c02009-04-16 22:23:12 +00001921 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001922 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001923 Record.push_back(DECL_CONTEXT_LEXICAL);
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001924 llvm::SmallVector<KindDeclIDPair, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001925 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1926 D != DEnd; ++D)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001927 Decls.push_back(std::make_pair((*D)->getKind(), GetDeclRef(*D)));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001928
Douglas Gregor25123082009-04-22 22:34:57 +00001929 ++NumLexicalDeclContexts;
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001930 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record, data(Decls));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001931 return Offset;
1932}
1933
Sebastian Redla4232eb2010-08-18 23:56:21 +00001934void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00001935 using namespace llvm;
1936 RecordData Record;
1937
1938 // Write the type offsets array
1939 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001940 Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001941 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1942 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1943 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1944 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001945 Record.push_back(TYPE_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00001946 Record.push_back(TypeOffsets.size());
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001947 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, data(TypeOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001948
1949 // Write the declaration offsets array
1950 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001951 Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001952 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1953 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1954 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1955 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001956 Record.push_back(DECL_OFFSET);
Sebastian Redl1476ed42010-07-16 16:36:56 +00001957 Record.push_back(DeclOffsets.size());
Benjamin Kramer6e089c62011-04-24 17:44:50 +00001958 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, data(DeclOffsets));
Sebastian Redl1476ed42010-07-16 16:36:56 +00001959}
1960
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001961//===----------------------------------------------------------------------===//
1962// Global Method Pool and Selector Serialization
1963//===----------------------------------------------------------------------===//
1964
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001965namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001966// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00001967class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00001968 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001969
1970public:
1971 typedef Selector key_type;
1972 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001973
Sebastian Redl5d050072010-08-04 17:20:04 +00001974 struct data_type {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00001975 SelectorID ID;
Sebastian Redl5d050072010-08-04 17:20:04 +00001976 ObjCMethodList Instance, Factory;
1977 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001978 typedef const data_type& data_type_ref;
1979
Sebastian Redl3397c552010-08-18 23:56:27 +00001980 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001981
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001982 static unsigned ComputeHash(Selector Sel) {
Argyrios Kyrtzidis0eca89e2010-08-20 16:03:52 +00001983 return serialization::ComputeHash(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001984 }
Mike Stump1eb44332009-09-09 15:08:12 +00001985
1986 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001987 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1988 data_type_ref Methods) {
1989 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1990 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00001991 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1992 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001993 Method = Method->Next)
1994 if (Method->Method)
1995 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00001996 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001997 Method = Method->Next)
1998 if (Method->Method)
1999 DataLen += 4;
2000 clang::io::Emit16(Out, DataLen);
2001 return std::make_pair(KeyLen, DataLen);
2002 }
Mike Stump1eb44332009-09-09 15:08:12 +00002003
Douglas Gregor83941df2009-04-25 17:48:32 +00002004 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00002005 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00002006 assert((Start >> 32) == 0 && "Selector key offset too large");
2007 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002008 unsigned N = Sel.getNumArgs();
2009 clang::io::Emit16(Out, N);
2010 if (N == 0)
2011 N = 1;
2012 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00002013 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002014 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2015 }
Mike Stump1eb44332009-09-09 15:08:12 +00002016
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002017 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00002018 data_type_ref Methods, unsigned DataLen) {
2019 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00002020 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002021 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002022 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002023 Method = Method->Next)
2024 if (Method->Method)
2025 ++NumInstanceMethods;
2026
2027 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00002028 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002029 Method = Method->Next)
2030 if (Method->Method)
2031 ++NumFactoryMethods;
2032
2033 clang::io::Emit16(Out, NumInstanceMethods);
2034 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00002035 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002036 Method = Method->Next)
2037 if (Method->Method)
2038 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00002039 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002040 Method = Method->Next)
2041 if (Method->Method)
2042 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00002043
2044 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002045 }
2046};
2047} // end anonymous namespace
2048
Sebastian Redl059612d2010-08-03 21:58:15 +00002049/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002050///
2051/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00002052/// in an on-disk hash table indexed by the selector. The hash table also
2053/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002054void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002055 using namespace llvm;
2056
Sebastian Redl059612d2010-08-03 21:58:15 +00002057 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00002058 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00002059 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00002060 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00002061 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002062 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002063 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002064 ASTMethodPoolTrait Trait(*this);
Mike Stump1eb44332009-09-09 15:08:12 +00002065
Sebastian Redl059612d2010-08-03 21:58:15 +00002066 // Create the on-disk hash table representation. We walk through every
2067 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00002068 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002069 for (llvm::DenseMap<Selector, SelectorID>::iterator
Sebastian Redl5d050072010-08-04 17:20:04 +00002070 I = SelectorIDs.begin(), E = SelectorIDs.end();
2071 I != E; ++I) {
2072 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00002073 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00002074 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00002075 I->second,
2076 ObjCMethodList(),
2077 ObjCMethodList()
2078 };
2079 if (F != SemaRef.MethodPool.end()) {
2080 Data.Instance = F->second.first;
2081 Data.Factory = F->second.second;
2082 }
Sebastian Redl3397c552010-08-18 23:56:27 +00002083 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00002084 // changed.
2085 if (Chain && I->second < FirstSelectorID) {
2086 // Selector already exists. Did it change?
2087 bool changed = false;
2088 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
2089 M = M->Next) {
2090 if (M->Method->getPCHLevel() == 0)
2091 changed = true;
2092 }
2093 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
2094 M = M->Next) {
2095 if (M->Method->getPCHLevel() == 0)
2096 changed = true;
2097 }
2098 if (!changed)
2099 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00002100 } else if (Data.Instance.Method || Data.Factory.Method) {
2101 // A new method pool entry.
2102 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00002103 }
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002104 Generator.insert(S, Data, Trait);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002105 }
2106
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002107 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002108 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002109 uint32_t BucketOffset;
2110 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002111 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002112 llvm::raw_svector_ostream Out(MethodPool);
2113 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002114 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002115 BucketOffset = Generator.Emit(Out, Trait);
2116 }
2117
2118 // Create a blob abbreviation
2119 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002120 Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002121 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00002122 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002123 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2124 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
2125
Douglas Gregor83941df2009-04-25 17:48:32 +00002126 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002127 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002128 Record.push_back(METHOD_POOL);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002129 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00002130 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002131 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00002132
2133 // Create a blob abbreviation for the selector table offsets.
2134 Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002135 Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
Douglas Gregor7c789c12010-10-29 22:39:52 +00002136 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
Douglas Gregor83941df2009-04-25 17:48:32 +00002137 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2138 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2139
2140 // Write the selector offsets table.
2141 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002142 Record.push_back(SELECTOR_OFFSETS);
Douglas Gregor83941df2009-04-25 17:48:32 +00002143 Record.push_back(SelectorOffsets.size());
2144 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002145 data(SelectorOffsets));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002146 }
2147}
2148
Sebastian Redl3397c552010-08-18 23:56:27 +00002149/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002150void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00002151 using namespace llvm;
2152 if (SemaRef.ReferencedSelectors.empty())
2153 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00002154
Fariborz Jahanian32019832010-07-23 19:11:11 +00002155 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00002156
Sebastian Redl3397c552010-08-18 23:56:27 +00002157 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00002158 // very tricky to fix, and given that @selector shouldn't really appear in
2159 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00002160 for (DenseMap<Selector, SourceLocation>::iterator S =
2161 SemaRef.ReferencedSelectors.begin(),
2162 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
2163 Selector Sel = (*S).first;
2164 SourceLocation Loc = (*S).second;
2165 AddSelectorRef(Sel, Record);
2166 AddSourceLocation(Loc, Record);
2167 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002168 Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002169}
2170
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002171//===----------------------------------------------------------------------===//
2172// Identifier Table Serialization
2173//===----------------------------------------------------------------------===//
2174
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002175namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00002176class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00002177 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00002178 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002179
Douglas Gregora92193e2009-04-28 21:18:29 +00002180 /// \brief Determines whether this is an "interesting" identifier
2181 /// that needs a full IdentifierInfo structure written into the hash
2182 /// table.
2183 static bool isInterestingIdentifier(const IdentifierInfo *II) {
2184 return II->isPoisoned() ||
2185 II->isExtensionToken() ||
2186 II->hasMacroDefinition() ||
2187 II->getObjCOrBuiltinID() ||
2188 II->getFETokenInfo<void>();
2189 }
2190
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002191public:
2192 typedef const IdentifierInfo* key_type;
2193 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002194
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002195 typedef IdentID data_type;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002196 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00002197
Sebastian Redl3397c552010-08-18 23:56:27 +00002198 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00002199 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002200
2201 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00002202 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002203 }
Mike Stump1eb44332009-09-09 15:08:12 +00002204
2205 std::pair<unsigned,unsigned>
2206 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002207 IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00002208 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00002209 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
2210 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00002211 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00002212 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00002213 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00002214 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00002215 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
2216 DEnd = IdentifierResolver::end();
2217 D != DEnd; ++D)
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002218 DataLen += sizeof(DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00002219 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00002220 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00002221 // We emit the key length after the data length so that every
2222 // string is preceded by a 16-bit length. This matches the PTH
2223 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00002224 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002225 return std::make_pair(KeyLen, DataLen);
2226 }
Mike Stump1eb44332009-09-09 15:08:12 +00002227
2228 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002229 unsigned KeyLen) {
2230 // Record the location of the key data. This is used when generating
2231 // the mapping from persistent IDs to strings.
2232 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00002233 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002234 }
Mike Stump1eb44332009-09-09 15:08:12 +00002235
2236 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002237 IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00002238 if (!isInterestingIdentifier(II)) {
2239 clang::io::Emit32(Out, ID << 1);
2240 return;
2241 }
Douglas Gregor5998da52009-04-28 21:32:13 +00002242
Douglas Gregora92193e2009-04-28 21:18:29 +00002243 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002244 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00002245 bool hasMacroDefinition =
2246 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00002247 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00002248 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002249 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
2250 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
2251 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00002252 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00002253 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00002254 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002255
Douglas Gregor37e26842009-04-21 23:56:24 +00002256 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00002257 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00002258
Douglas Gregor668c1a42009-04-21 22:25:48 +00002259 // Emit the declaration IDs in reverse order, because the
2260 // IdentifierResolver provides the declarations as they would be
2261 // visible (e.g., the function "stat" would come before the struct
2262 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
2263 // adds declarations to the end of the list (so we need to see the
2264 // struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002265 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump1eb44332009-09-09 15:08:12 +00002266 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00002267 IdentifierResolver::end());
2268 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
2269 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002270 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00002271 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002272 }
2273};
2274} // end anonymous namespace
2275
Sebastian Redl3397c552010-08-18 23:56:27 +00002276/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002277///
2278/// The identifier table consists of a blob containing string data
2279/// (the actual identifiers themselves) and a separate "offsets" index
2280/// that maps identifier IDs to locations within the blob.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002281void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00002282 using namespace llvm;
2283
2284 // Create and write out the blob that contains the identifier
2285 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00002286 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002287 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002288 ASTIdentifierTableTrait Trait(*this, PP);
Mike Stump1eb44332009-09-09 15:08:12 +00002289
Douglas Gregor92b059e2009-04-28 20:33:11 +00002290 // Look for any identifiers that were named while processing the
2291 // headers, but are otherwise not needed. We add these to the hash
2292 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00002293 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00002294 // file.
2295 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
2296 IDEnd = PP.getIdentifierTable().end();
2297 ID != IDEnd; ++ID)
2298 getIdentifierRef(ID->second);
2299
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002300 // Create the on-disk hash table representation. We only store offsets
2301 // for identifiers that appear here for the first time.
2302 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002303 for (llvm::DenseMap<const IdentifierInfo *, IdentID>::iterator
Douglas Gregorafaf3082009-04-11 00:14:32 +00002304 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
2305 ID != IDEnd; ++ID) {
2306 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002307 if (!Chain || !ID->first->isFromAST())
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002308 Generator.insert(ID->first, ID->second, Trait);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002309 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00002310
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002311 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00002312 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00002313 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002314 {
Sebastian Redl3397c552010-08-18 23:56:27 +00002315 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002316 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002317 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00002318 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002319 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002320 }
2321
2322 // Create a blob abbreviation
2323 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002324 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00002325 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002326 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00002327 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00002328
2329 // Write the identifier table
2330 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002331 Record.push_back(IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00002332 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00002333 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00002334 }
2335
2336 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002337 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002338 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002339 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2340 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2341 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2342
2343 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002344 Record.push_back(IDENTIFIER_OFFSET);
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00002345 Record.push_back(IdentifierOffsets.size());
2346 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002347 data(IdentifierOffsets));
Douglas Gregorafaf3082009-04-11 00:14:32 +00002348}
2349
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002350//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002351// DeclContext's Name Lookup Table Serialization
2352//===----------------------------------------------------------------------===//
2353
2354namespace {
2355// Trait used for the on-disk hash table used in the method pool.
2356class ASTDeclContextNameLookupTrait {
2357 ASTWriter &Writer;
2358
2359public:
2360 typedef DeclarationName key_type;
2361 typedef key_type key_type_ref;
2362
2363 typedef DeclContext::lookup_result data_type;
2364 typedef const data_type& data_type_ref;
2365
2366 explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
2367
2368 unsigned ComputeHash(DeclarationName Name) {
2369 llvm::FoldingSetNodeID ID;
2370 ID.AddInteger(Name.getNameKind());
2371
2372 switch (Name.getNameKind()) {
2373 case DeclarationName::Identifier:
2374 ID.AddString(Name.getAsIdentifierInfo()->getName());
2375 break;
2376 case DeclarationName::ObjCZeroArgSelector:
2377 case DeclarationName::ObjCOneArgSelector:
2378 case DeclarationName::ObjCMultiArgSelector:
2379 ID.AddInteger(serialization::ComputeHash(Name.getObjCSelector()));
2380 break;
2381 case DeclarationName::CXXConstructorName:
2382 case DeclarationName::CXXDestructorName:
2383 case DeclarationName::CXXConversionFunctionName:
2384 ID.AddInteger(Writer.GetOrCreateTypeID(Name.getCXXNameType()));
2385 break;
2386 case DeclarationName::CXXOperatorName:
2387 ID.AddInteger(Name.getCXXOverloadedOperator());
2388 break;
2389 case DeclarationName::CXXLiteralOperatorName:
2390 ID.AddString(Name.getCXXLiteralIdentifier()->getName());
2391 case DeclarationName::CXXUsingDirective:
2392 break;
2393 }
2394
2395 return ID.ComputeHash();
2396 }
2397
2398 std::pair<unsigned,unsigned>
2399 EmitKeyDataLength(llvm::raw_ostream& Out, DeclarationName Name,
2400 data_type_ref Lookup) {
2401 unsigned KeyLen = 1;
2402 switch (Name.getNameKind()) {
2403 case DeclarationName::Identifier:
2404 case DeclarationName::ObjCZeroArgSelector:
2405 case DeclarationName::ObjCOneArgSelector:
2406 case DeclarationName::ObjCMultiArgSelector:
2407 case DeclarationName::CXXConstructorName:
2408 case DeclarationName::CXXDestructorName:
2409 case DeclarationName::CXXConversionFunctionName:
2410 case DeclarationName::CXXLiteralOperatorName:
2411 KeyLen += 4;
2412 break;
2413 case DeclarationName::CXXOperatorName:
2414 KeyLen += 1;
2415 break;
2416 case DeclarationName::CXXUsingDirective:
2417 break;
2418 }
2419 clang::io::Emit16(Out, KeyLen);
2420
2421 // 2 bytes for num of decls and 4 for each DeclID.
2422 unsigned DataLen = 2 + 4 * (Lookup.second - Lookup.first);
2423 clang::io::Emit16(Out, DataLen);
2424
2425 return std::make_pair(KeyLen, DataLen);
2426 }
2427
2428 void EmitKey(llvm::raw_ostream& Out, DeclarationName Name, unsigned) {
2429 using namespace clang::io;
2430
2431 assert(Name.getNameKind() < 0x100 && "Invalid name kind ?");
2432 Emit8(Out, Name.getNameKind());
2433 switch (Name.getNameKind()) {
2434 case DeclarationName::Identifier:
2435 Emit32(Out, Writer.getIdentifierRef(Name.getAsIdentifierInfo()));
2436 break;
2437 case DeclarationName::ObjCZeroArgSelector:
2438 case DeclarationName::ObjCOneArgSelector:
2439 case DeclarationName::ObjCMultiArgSelector:
2440 Emit32(Out, Writer.getSelectorRef(Name.getObjCSelector()));
2441 break;
2442 case DeclarationName::CXXConstructorName:
2443 case DeclarationName::CXXDestructorName:
2444 case DeclarationName::CXXConversionFunctionName:
2445 Emit32(Out, Writer.getTypeID(Name.getCXXNameType()));
2446 break;
2447 case DeclarationName::CXXOperatorName:
2448 assert(Name.getCXXOverloadedOperator() < 0x100 && "Invalid operator ?");
2449 Emit8(Out, Name.getCXXOverloadedOperator());
2450 break;
2451 case DeclarationName::CXXLiteralOperatorName:
2452 Emit32(Out, Writer.getIdentifierRef(Name.getCXXLiteralIdentifier()));
2453 break;
2454 case DeclarationName::CXXUsingDirective:
2455 break;
2456 }
2457 }
2458
2459 void EmitData(llvm::raw_ostream& Out, key_type_ref,
2460 data_type Lookup, unsigned DataLen) {
2461 uint64_t Start = Out.tell(); (void)Start;
2462 clang::io::Emit16(Out, Lookup.second - Lookup.first);
2463 for (; Lookup.first != Lookup.second; ++Lookup.first)
2464 clang::io::Emit32(Out, Writer.GetDeclRef(*Lookup.first));
2465
2466 assert(Out.tell() - Start == DataLen && "Data length is wrong");
2467 }
2468};
2469} // end anonymous namespace
2470
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002471/// \brief Write the block containing all of the declaration IDs
2472/// visible from the given DeclContext.
2473///
2474/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002475/// bitstream, or 0 if no block was written.
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002476uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
2477 DeclContext *DC) {
2478 if (DC->getPrimaryContext() != DC)
2479 return 0;
2480
2481 // Since there is no name lookup into functions or methods, don't bother to
2482 // build a visible-declarations table for these entities.
2483 if (DC->isFunctionOrMethod())
2484 return 0;
2485
2486 // If not in C++, we perform name lookup for the translation unit via the
2487 // IdentifierInfo chains, don't bother to build a visible-declarations table.
2488 // FIXME: In C++ we need the visible declarations in order to "see" the
2489 // friend declarations, is there a way to do this without writing the table ?
2490 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
2491 return 0;
2492
2493 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidisa60786b2010-08-20 23:35:55 +00002494 if (DC->hasExternalVisibleStorage())
2495 DC->MaterializeVisibleDeclsFromExternalStorage();
2496 else
2497 DC->lookup(DeclarationName());
Argyrios Kyrtzidis074dcc82010-08-20 16:04:35 +00002498
2499 // Serialize the contents of the mapping used for lookup. Note that,
2500 // although we have two very different code paths, the serialized
2501 // representation is the same for both cases: a declaration name,
2502 // followed by a size, followed by references to the visible
2503 // declarations that have that name.
2504 uint64_t Offset = Stream.GetCurrentBitNo();
2505 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2506 if (!Map || Map->empty())
2507 return 0;
2508
2509 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2510 ASTDeclContextNameLookupTrait Trait(*this);
2511
2512 // Create the on-disk hash table representation.
2513 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2514 D != DEnd; ++D) {
2515 DeclarationName Name = D->first;
2516 DeclContext::lookup_result Result = D->second.getLookupResult();
2517 Generator.insert(Name, Result, Trait);
2518 }
2519
2520 // Create the on-disk hash table in a buffer.
2521 llvm::SmallString<4096> LookupTable;
2522 uint32_t BucketOffset;
2523 {
2524 llvm::raw_svector_ostream Out(LookupTable);
2525 // Make sure that no bucket is at offset 0
2526 clang::io::Emit32(Out, 0);
2527 BucketOffset = Generator.Emit(Out, Trait);
2528 }
2529
2530 // Write the lookup table
2531 RecordData Record;
2532 Record.push_back(DECL_CONTEXT_VISIBLE);
2533 Record.push_back(BucketOffset);
2534 Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
2535 LookupTable.str());
2536
2537 Stream.EmitRecord(DECL_CONTEXT_VISIBLE, Record);
2538 ++NumVisibleDeclContexts;
2539 return Offset;
2540}
2541
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002542/// \brief Write an UPDATE_VISIBLE block for the given context.
2543///
2544/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
2545/// DeclContext in a dependent AST file. As such, they only exist for the TU
2546/// (in C++) and for namespaces.
2547void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002548 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
2549 if (!Map || Map->empty())
2550 return;
2551
2552 OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator;
2553 ASTDeclContextNameLookupTrait Trait(*this);
2554
2555 // Create the hash table.
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002556 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
2557 D != DEnd; ++D) {
2558 DeclarationName Name = D->first;
2559 DeclContext::lookup_result Result = D->second.getLookupResult();
Sebastian Redl5967d622010-08-24 00:50:16 +00002560 // For any name that appears in this table, the results are complete, i.e.
2561 // they overwrite results from previous PCHs. Merging is always a mess.
2562 Generator.insert(Name, Result, Trait);
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00002563 }
2564
2565 // Create the on-disk hash table in a buffer.
2566 llvm::SmallString<4096> LookupTable;
2567 uint32_t BucketOffset;
2568 {
2569 llvm::raw_svector_ostream Out(LookupTable);
2570 // Make sure that no bucket is at offset 0
2571 clang::io::Emit32(Out, 0);
2572 BucketOffset = Generator.Emit(Out, Trait);
2573 }
2574
2575 // Write the lookup table
2576 RecordData Record;
2577 Record.push_back(UPDATE_VISIBLE);
2578 Record.push_back(getDeclID(cast<Decl>(DC)));
2579 Record.push_back(BucketOffset);
2580 Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
2581}
2582
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002583/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
2584void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
2585 RecordData Record;
2586 Record.push_back(Opts.fp_contract);
2587 Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
2588}
2589
2590/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
2591void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
2592 if (!SemaRef.Context.getLangOptions().OpenCL)
2593 return;
2594
2595 const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
2596 RecordData Record;
2597#define OPENCLEXT(nm) Record.push_back(Opts.nm);
2598#include "clang/Basic/OpenCLExtensions.def"
2599 Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
2600}
2601
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00002602//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +00002603// General Serialization Routines
2604//===----------------------------------------------------------------------===//
2605
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002606/// \brief Write a record containing the given attributes.
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002607void ASTWriter::WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record) {
Argyrios Kyrtzidis4eb9fc02010-10-18 19:20:11 +00002608 Record.push_back(Attrs.size());
Sean Huntcf807c42010-08-18 23:23:40 +00002609 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
2610 const Attr * A = *i;
2611 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
2612 AddSourceLocation(A->getLocation(), Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002613
Sean Huntcf807c42010-08-18 23:23:40 +00002614#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00002615
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002616 }
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002617}
2618
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00002619void ASTWriter::AddString(llvm::StringRef Str, RecordDataImpl &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00002620 Record.push_back(Str.size());
2621 Record.insert(Record.end(), Str.begin(), Str.end());
2622}
2623
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002624void ASTWriter::AddVersionTuple(const VersionTuple &Version,
2625 RecordDataImpl &Record) {
2626 Record.push_back(Version.getMajor());
2627 if (llvm::Optional<unsigned> Minor = Version.getMinor())
2628 Record.push_back(*Minor + 1);
2629 else
2630 Record.push_back(0);
2631 if (llvm::Optional<unsigned> Subminor = Version.getSubminor())
2632 Record.push_back(*Subminor + 1);
2633 else
2634 Record.push_back(0);
2635}
2636
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002637/// \brief Note that the identifier II occurs at the given offset
2638/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002639void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002640 IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00002641 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002642 // up earlier in the chain and thus don't need an offset.
2643 if (ID >= FirstIdentID)
2644 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002645}
2646
Douglas Gregor83941df2009-04-25 17:48:32 +00002647/// \brief Note that the selector Sel occurs at the given offset
2648/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00002649void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00002650 unsigned ID = SelectorIDs[Sel];
2651 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00002652 // Don't record offsets for selectors that are also available in a different
2653 // file.
2654 if (ID < FirstSelectorID)
2655 return;
2656 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00002657}
2658
Sebastian Redla4232eb2010-08-18 23:56:21 +00002659ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Douglas Gregor89d99802010-11-30 06:16:57 +00002660 : Stream(Stream), Chain(0), SerializationListener(0),
2661 FirstDeclID(1), NextDeclID(FirstDeclID),
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002662 FirstTypeID(NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
Sebastian Redle58aa892010-08-04 18:21:41 +00002663 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
Douglas Gregor77424bc2010-10-02 19:29:26 +00002664 NextSelectorID(FirstSelectorID), FirstMacroID(1), NextMacroID(FirstMacroID),
2665 CollectedStmts(&StmtsToEmit),
Sebastian Redle58aa892010-08-04 18:21:41 +00002666 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
Douglas Gregor7c789c12010-10-29 22:39:52 +00002667 NumVisibleDeclContexts(0), FirstCXXBaseSpecifiersID(1),
2668 NextCXXBaseSpecifiersID(1)
2669{
Sebastian Redl30c514c2010-07-14 23:45:08 +00002670}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002671
Sebastian Redla4232eb2010-08-18 23:56:21 +00002672void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002673 const std::string &OutputFile,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002674 const char *isysroot) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002675 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002676 Stream.Emit((unsigned)'C', 8);
2677 Stream.Emit((unsigned)'P', 8);
2678 Stream.Emit((unsigned)'C', 8);
2679 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002680
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002681 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002682
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002683 if (Chain)
Sebastian Redla4232eb2010-08-18 23:56:21 +00002684 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002685 else
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002686 WriteASTCore(SemaRef, StatCalls, isysroot, OutputFile);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002687}
2688
Sebastian Redla4232eb2010-08-18 23:56:21 +00002689void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002690 const char *isysroot,
2691 const std::string &OutputFile) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002692 using namespace llvm;
2693
2694 ASTContext &Context = SemaRef.Context;
2695 Preprocessor &PP = SemaRef.PP;
2696
Douglas Gregor2cf26342009-04-09 22:27:44 +00002697 // The translation unit is the first declaration we'll emit.
2698 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002699 ++NextDeclID;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002700 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002701
Douglas Gregor2deaea32009-04-22 18:49:13 +00002702 // Make sure that we emit IdentifierInfos (and any attached
2703 // declarations) for builtins.
2704 {
2705 IdentifierTable &Table = PP.getIdentifierTable();
2706 llvm::SmallVector<const char *, 32> BuiltinNames;
2707 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2708 Context.getLangOptions().NoBuiltin);
2709 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2710 getIdentifierRef(&Table.get(BuiltinNames[I]));
2711 }
2712
Chris Lattner63d65f82009-09-08 18:19:27 +00002713 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002714 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002715 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002716 RecordData TentativeDefinitions;
Sebastian Redle9d12b62010-01-31 22:27:38 +00002717 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2718 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner63d65f82009-09-08 18:19:27 +00002719 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002720
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002721 // Build a record containing all of the file scoped decls in this file.
2722 RecordData UnusedFileScopedDecls;
2723 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2724 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00002725
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002726 RecordData WeakUndeclaredIdentifiers;
2727 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2728 WeakUndeclaredIdentifiers.push_back(
2729 SemaRef.WeakUndeclaredIdentifiers.size());
2730 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2731 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2732 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2733 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2734 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2735 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2736 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2737 }
2738 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002739
Douglas Gregor14c22f22009-04-22 22:18:58 +00002740 // Build a record containing all of the locally-scoped external
2741 // declarations in this header file. Generally, this record will be
2742 // empty.
2743 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002744 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00002745 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002746 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002747 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2748 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2749 TD != TDEnd; ++TD)
2750 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2751
Douglas Gregorb81c1702009-04-27 20:06:05 +00002752 // Build a record containing all of the ext_vector declarations.
2753 RecordData ExtVectorDecls;
2754 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2755 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2756
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002757 // Build a record containing all of the VTable uses information.
2758 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00002759 if (!SemaRef.VTableUses.empty()) {
2760 VTableUses.push_back(SemaRef.VTableUses.size());
2761 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2762 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2763 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2764 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2765 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002766 }
2767
2768 // Build a record containing all of dynamic classes declarations.
2769 RecordData DynamicClasses;
2770 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2771 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2772
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002773 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002774 RecordData PendingInstantiations;
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002775 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00002776 I = SemaRef.PendingInstantiations.begin(),
2777 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
2778 AddDeclRef(I->first, PendingInstantiations);
2779 AddSourceLocation(I->second, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002780 }
2781 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2782 "There are local ones at end of translation unit!");
2783
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002784 // Build a record containing some declaration references.
2785 RecordData SemaDeclRefs;
2786 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2787 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2788 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2789 }
2790
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002791 RecordData CUDASpecialDeclRefs;
2792 if (Context.getcudaConfigureCallDecl()) {
2793 AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
2794 }
2795
Sebastian Redl3397c552010-08-18 23:56:27 +00002796 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002797 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002798 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002799 WriteMetadata(Context, isysroot, OutputFile);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002800 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00002801 if (StatCalls && !isysroot)
Douglas Gregordd41ed52010-07-12 23:48:14 +00002802 WriteStatCache(*StatCalls);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002803 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002804 // Write the record of special types.
2805 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00002806
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002807 AddTypeRef(Context.getBuiltinVaListType(), Record);
2808 AddTypeRef(Context.getObjCIdType(), Record);
2809 AddTypeRef(Context.getObjCSelType(), Record);
2810 AddTypeRef(Context.getObjCProtoType(), Record);
2811 AddTypeRef(Context.getObjCClassType(), Record);
2812 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2813 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2814 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002815 AddTypeRef(Context.getjmp_bufType(), Record);
2816 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002817 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2818 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpadaaad32009-10-20 02:12:22 +00002819 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002820 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002821 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2822 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002823 Record.push_back(Context.isInt128Installed());
Richard Smithad762fc2011-04-14 22:09:26 +00002824 AddTypeRef(Context.AutoDeductTy, Record);
2825 AddTypeRef(Context.AutoRRefDeductTy, Record);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002826 Stream.EmitRecord(SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002827
Douglas Gregor366809a2009-04-26 03:49:13 +00002828 // Keep writing types and declarations until all types and
2829 // declarations have been written.
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002830 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002831 WriteDeclsBlockAbbrevs();
2832 while (!DeclTypesToEmit.empty()) {
2833 DeclOrType DOT = DeclTypesToEmit.front();
2834 DeclTypesToEmit.pop();
2835 if (DOT.isType())
2836 WriteType(DOT.getType());
2837 else
2838 WriteDecl(Context, DOT.getDecl());
2839 }
2840 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002841
Douglas Gregor813a97b2009-10-17 17:25:45 +00002842 WritePreprocessor(PP);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +00002843 WriteHeaderSearch(PP.getHeaderSearchInfo(), isysroot);
Sebastian Redl059612d2010-08-03 21:58:15 +00002844 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002845 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002846 WriteIdentifierTable(PP);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00002847 WriteFPPragmaOptions(SemaRef.getFPOptions());
2848 WriteOpenCLExtensions(SemaRef);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002849
Sebastian Redl1476ed42010-07-16 16:36:56 +00002850 WriteTypeDeclOffsets();
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00002851 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Douglas Gregorad1de002009-04-18 05:55:16 +00002852
Anders Carlssonc8505782011-03-06 18:41:18 +00002853 WriteCXXBaseSpecifiersOffsets();
Douglas Gregor7c789c12010-10-29 22:39:52 +00002854
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002855 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002856 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002857 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002858
2859 // Write the record containing tentative definitions.
2860 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002861 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002862
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002863 // Write the record containing unused file scoped decls.
2864 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002865 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002866
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002867 // Write the record containing weak undeclared identifiers.
2868 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002869 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002870 WeakUndeclaredIdentifiers);
2871
Douglas Gregor14c22f22009-04-22 22:18:58 +00002872 // Write the record containing locally-scoped external definitions.
2873 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002874 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002875 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002876
2877 // Write the record containing ext_vector type names.
2878 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002879 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002880
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002881 // Write the record containing VTable uses information.
2882 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002883 Stream.EmitRecord(VTABLE_USES, VTableUses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002884
2885 // Write the record containing dynamic classes declarations.
2886 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002887 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002888
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002889 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00002890 if (!PendingInstantiations.empty())
2891 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002892
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002893 // Write the record containing declaration references of Sema.
2894 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002895 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002896
Peter Collingbourne14b6ba72011-02-09 21:04:32 +00002897 // Write the record containing CUDA-specific declaration references.
2898 if (!CUDASpecialDeclRefs.empty())
2899 Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
2900
Douglas Gregor3e1af842009-04-17 22:13:46 +00002901 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002902 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002903 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002904 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002905 Record.push_back(NumLexicalDeclContexts);
2906 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002907 Stream.EmitRecord(STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002908 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002909}
2910
Sebastian Redla4232eb2010-08-18 23:56:21 +00002911void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002912 const char *isysroot) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002913 using namespace llvm;
2914
2915 ASTContext &Context = SemaRef.Context;
2916 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1476ed42010-07-16 16:36:56 +00002917
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002918 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002919 Stream.EnterSubblock(AST_BLOCK_ID, 5);
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +00002920 WriteMetadata(Context, isysroot, "");
Sebastian Redl1476ed42010-07-16 16:36:56 +00002921 if (StatCalls && !isysroot)
2922 WriteStatCache(*StatCalls);
2923 // FIXME: Source manager block should only write new stuff, which could be
2924 // done by tracking the largest ID in the chain
2925 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002926
2927 // The special types are in the chained PCH.
2928
2929 // We don't start with the translation unit, but with its decls that
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002930 // don't come from the chained PCH.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002931 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002932 llvm::SmallVector<KindDeclIDPair, 64> NewGlobalDecls;
Sebastian Redl681d7232010-07-27 00:17:23 +00002933 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2934 E = TU->noload_decls_end();
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002935 I != E; ++I) {
Sebastian Redld692af72010-07-27 18:24:41 +00002936 if ((*I)->getPCHLevel() == 0)
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002937 NewGlobalDecls.push_back(std::make_pair((*I)->getKind(), GetDeclRef(*I)));
Sebastian Redl0b17c612010-08-13 00:28:03 +00002938 else if ((*I)->isChangedSinceDeserialization())
2939 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002940 }
Sebastian Redl681d7232010-07-27 00:17:23 +00002941 // We also need to write a lexical updates block for the TU.
Sebastian Redld692af72010-07-27 18:24:41 +00002942 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002943 Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
Sebastian Redld692af72010-07-27 18:24:41 +00002944 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2945 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2946 Record.clear();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00002947 Record.push_back(TU_UPDATE_LEXICAL);
Sebastian Redld692af72010-07-27 18:24:41 +00002948 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
Benjamin Kramer6e089c62011-04-24 17:44:50 +00002949 data(NewGlobalDecls));
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00002950 // And a visible updates block for the DeclContexts.
2951 Abv = new llvm::BitCodeAbbrev();
2952 Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
2953 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
2954 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Fixed, 32));
2955 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2956 UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
2957 WriteDeclContextVisibleUpdate(TU);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002958
Sebastian Redl083abdf2010-07-27 23:01:28 +00002959 // Build a record containing all of the new tentative definitions in this
2960 // file, in TentativeDefinitions order.
2961 RecordData TentativeDefinitions;
2962 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2963 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2964 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2965 }
2966
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002967 // Build a record containing all of the file scoped decls in this file.
2968 RecordData UnusedFileScopedDecls;
2969 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2970 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2971 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002972 }
2973
Sebastian Redl40566802010-08-05 18:21:25 +00002974 // We write the entire table, overwriting the tables from the chain.
2975 RecordData WeakUndeclaredIdentifiers;
2976 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2977 WeakUndeclaredIdentifiers.push_back(
2978 SemaRef.WeakUndeclaredIdentifiers.size());
2979 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2980 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2981 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2982 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2983 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2984 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2985 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2986 }
2987 }
2988
Sebastian Redl083abdf2010-07-27 23:01:28 +00002989 // Build a record containing all of the locally-scoped external
2990 // declarations in this header file. Generally, this record will be
2991 // empty.
2992 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002993 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl083abdf2010-07-27 23:01:28 +00002994 // nondeterminstic!
2995 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2996 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2997 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2998 TD != TDEnd; ++TD) {
2999 if (TD->second->getPCHLevel() == 0)
3000 AddDeclRef(TD->second, LocallyScopedExternalDecls);
3001 }
3002
3003 // Build a record containing all of the ext_vector declarations.
3004 RecordData ExtVectorDecls;
3005 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
3006 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
3007 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
3008 }
3009
Sebastian Redl40566802010-08-05 18:21:25 +00003010 // Build a record containing all of the VTable uses information.
3011 // We write everything here, because it's too hard to determine whether
3012 // a use is new to this part.
3013 RecordData VTableUses;
3014 if (!SemaRef.VTableUses.empty()) {
3015 VTableUses.push_back(SemaRef.VTableUses.size());
3016 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
3017 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
3018 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
3019 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
3020 }
3021 }
3022
3023 // Build a record containing all of dynamic classes declarations.
3024 RecordData DynamicClasses;
3025 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
3026 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
3027 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
3028
3029 // Build a record containing all of pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003030 RecordData PendingInstantiations;
Sebastian Redl40566802010-08-05 18:21:25 +00003031 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
Chandler Carruth62c78d52010-08-25 08:44:16 +00003032 I = SemaRef.PendingInstantiations.begin(),
3033 N = SemaRef.PendingInstantiations.end(); I != N; ++I) {
Sebastian Redlc1d3ffb2011-04-24 16:27:30 +00003034 AddDeclRef(I->first, PendingInstantiations);
3035 AddSourceLocation(I->second, PendingInstantiations);
Sebastian Redl40566802010-08-05 18:21:25 +00003036 }
3037 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
3038 "There are local ones at end of translation unit!");
3039
3040 // Build a record containing some declaration references.
3041 // It's not worth the effort to avoid duplication here.
3042 RecordData SemaDeclRefs;
3043 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
3044 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
3045 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
3046 }
3047
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003048 Stream.EnterSubblock(DECLTYPES_BLOCK_ID, 3);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003049 WriteDeclsBlockAbbrevs();
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00003050 for (DeclsToRewriteTy::iterator
3051 I = DeclsToRewrite.begin(), E = DeclsToRewrite.end(); I != E; ++I)
3052 DeclTypesToEmit.push(const_cast<Decl*>(*I));
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003053 while (!DeclTypesToEmit.empty()) {
3054 DeclOrType DOT = DeclTypesToEmit.front();
3055 DeclTypesToEmit.pop();
3056 if (DOT.isType())
3057 WriteType(DOT.getType());
3058 else
3059 WriteDecl(Context, DOT.getDecl());
3060 }
3061 Stream.ExitBlock();
3062
Sebastian Redl083abdf2010-07-27 23:01:28 +00003063 WritePreprocessor(PP);
Sebastian Redla68340f2010-08-04 22:21:29 +00003064 WriteSelectors(SemaRef);
3065 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003066 WriteIdentifierTable(PP);
Peter Collingbourne84bccea2011-02-15 19:46:30 +00003067 WriteFPPragmaOptions(SemaRef.getFPOptions());
3068 WriteOpenCLExtensions(SemaRef);
3069
Sebastian Redl1476ed42010-07-16 16:36:56 +00003070 WriteTypeDeclOffsets();
Argyrios Kyrtzidisf41d3be2010-11-05 22:10:18 +00003071 // FIXME: For chained PCH only write the new mappings (we currently
3072 // write all of them again).
Argyrios Kyrtzidis3efd52c2011-01-14 20:54:07 +00003073 WritePragmaDiagnosticMappings(Context.getDiagnostics());
Sebastian Redl083abdf2010-07-27 23:01:28 +00003074
Anders Carlssonc8505782011-03-06 18:41:18 +00003075 WriteCXXBaseSpecifiersOffsets();
3076
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003077 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl3397c552010-08-18 23:56:27 +00003078 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003079 RecordData FirstLatestDeclIDs;
3080 for (FirstLatestDeclMap::iterator
3081 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
3082 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
3083 "Expected first & second to be in different PCHs");
3084 AddDeclRef(I->first, FirstLatestDeclIDs);
3085 AddDeclRef(I->second, FirstLatestDeclIDs);
3086 }
3087 if (!FirstLatestDeclIDs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003088 Stream.EmitRecord(REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00003089
Sebastian Redl083abdf2010-07-27 23:01:28 +00003090 // Write the record containing external, unnamed definitions.
3091 if (!ExternalDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003092 Stream.EmitRecord(EXTERNAL_DEFINITIONS, ExternalDefinitions);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003093
3094 // Write the record containing tentative definitions.
3095 if (!TentativeDefinitions.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003096 Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003097
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00003098 // Write the record containing unused file scoped decls.
3099 if (!UnusedFileScopedDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003100 Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003101
Sebastian Redl40566802010-08-05 18:21:25 +00003102 // Write the record containing weak undeclared identifiers.
3103 if (!WeakUndeclaredIdentifiers.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003104 Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
Sebastian Redl40566802010-08-05 18:21:25 +00003105 WeakUndeclaredIdentifiers);
3106
Sebastian Redl083abdf2010-07-27 23:01:28 +00003107 // Write the record containing locally-scoped external definitions.
3108 if (!LocallyScopedExternalDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003109 Stream.EmitRecord(LOCALLY_SCOPED_EXTERNAL_DECLS,
Sebastian Redl083abdf2010-07-27 23:01:28 +00003110 LocallyScopedExternalDecls);
3111
3112 // Write the record containing ext_vector type names.
3113 if (!ExtVectorDecls.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003114 Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003115
Sebastian Redl40566802010-08-05 18:21:25 +00003116 // Write the record containing VTable uses information.
3117 if (!VTableUses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003118 Stream.EmitRecord(VTABLE_USES, VTableUses);
Sebastian Redl40566802010-08-05 18:21:25 +00003119
3120 // Write the record containing dynamic classes declarations.
3121 if (!DynamicClasses.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003122 Stream.EmitRecord(DYNAMIC_CLASSES, DynamicClasses);
Sebastian Redl40566802010-08-05 18:21:25 +00003123
3124 // Write the record containing pending implicit instantiations.
Chandler Carruth62c78d52010-08-25 08:44:16 +00003125 if (!PendingInstantiations.empty())
3126 Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
Sebastian Redl40566802010-08-05 18:21:25 +00003127
3128 // Write the record containing declaration references of Sema.
3129 if (!SemaDeclRefs.empty())
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003130 Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl083abdf2010-07-27 23:01:28 +00003131
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00003132 // Write the updates to DeclContexts.
3133 for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator
3134 I = UpdatedDeclContexts.begin(),
3135 E = UpdatedDeclContexts.end();
Sebastian Redl1d1e42b2010-08-24 00:50:09 +00003136 I != E; ++I)
3137 WriteDeclContextVisibleUpdate(*I);
3138
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003139 WriteDeclUpdatesBlocks();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003140
Sebastian Redl083abdf2010-07-27 23:01:28 +00003141 Record.clear();
3142 Record.push_back(NumStatements);
3143 Record.push_back(NumMacros);
3144 Record.push_back(NumLexicalDeclContexts);
3145 Record.push_back(NumVisibleDeclContexts);
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003146 WriteDeclReplacementsBlock();
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003147 Stream.EmitRecord(STATISTICS, Record);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00003148 Stream.ExitBlock();
3149}
3150
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003151void ASTWriter::WriteDeclUpdatesBlocks() {
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003152 if (DeclUpdates.empty())
3153 return;
3154
3155 RecordData OffsetsRecord;
3156 Stream.EnterSubblock(DECL_UPDATES_BLOCK_ID, 3);
3157 for (DeclUpdateMap::iterator
3158 I = DeclUpdates.begin(), E = DeclUpdates.end(); I != E; ++I) {
3159 const Decl *D = I->first;
3160 UpdateRecord &URec = I->second;
3161
Argyrios Kyrtzidisba901b52010-10-24 17:26:46 +00003162 if (DeclsToRewrite.count(D))
3163 continue; // The decl will be written completely,no need to store updates.
3164
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003165 uint64_t Offset = Stream.GetCurrentBitNo();
3166 Stream.EmitRecord(DECL_UPDATES, URec);
3167
3168 OffsetsRecord.push_back(GetDeclRef(D));
3169 OffsetsRecord.push_back(Offset);
3170 }
3171 Stream.ExitBlock();
3172 Stream.EmitRecord(DECL_UPDATE_OFFSETS, OffsetsRecord);
3173}
3174
Argyrios Kyrtzidisaacdd022010-10-24 17:26:43 +00003175void ASTWriter::WriteDeclReplacementsBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00003176 if (ReplacedDecls.empty())
3177 return;
3178
3179 RecordData Record;
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003180 for (llvm::SmallVector<std::pair<DeclID, uint64_t>, 16>::iterator
Sebastian Redl0b17c612010-08-13 00:28:03 +00003181 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
3182 Record.push_back(I->first);
3183 Record.push_back(I->second);
3184 }
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003185 Stream.EmitRecord(DECL_REPLACEMENTS, Record);
Sebastian Redl0b17c612010-08-13 00:28:03 +00003186}
3187
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003188void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003189 Record.push_back(Loc.getRawEncoding());
3190}
3191
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003192void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003193 AddSourceLocation(Range.getBegin(), Record);
3194 AddSourceLocation(Range.getEnd(), Record);
3195}
3196
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003197void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003198 Record.push_back(Value.getBitWidth());
Benjamin Kramer4ea884b2010-09-06 23:43:28 +00003199 const uint64_t *Words = Value.getRawData();
3200 Record.append(Words, Words + Value.getNumWords());
Douglas Gregor2cf26342009-04-09 22:27:44 +00003201}
3202
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003203void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00003204 Record.push_back(Value.isUnsigned());
3205 AddAPInt(Value, Record);
3206}
3207
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003208void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00003209 AddAPInt(Value.bitcastToAPInt(), Record);
3210}
3211
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003212void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003213 Record.push_back(getIdentifierRef(II));
3214}
3215
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003216IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00003217 if (II == 0)
3218 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00003219
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003220 IdentID &ID = IdentifierIDs[II];
Douglas Gregorafaf3082009-04-11 00:14:32 +00003221 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003222 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00003223 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003224}
3225
Sebastian Redlf73c93f2010-09-15 19:54:06 +00003226MacroID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003227 if (MD == 0)
3228 return 0;
Sebastian Redlf73c93f2010-09-15 19:54:06 +00003229
3230 MacroID &ID = MacroDefinitions[MD];
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003231 if (ID == 0)
Douglas Gregor77424bc2010-10-02 19:29:26 +00003232 ID = NextMacroID++;
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00003233 return ID;
3234}
3235
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003236void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003237 Record.push_back(getSelectorRef(SelRef));
3238}
3239
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003240SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003241 if (Sel.getAsOpaquePtr() == 0) {
3242 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003243 }
3244
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003245 SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00003246 if (SID == 0 && Chain) {
3247 // This might trigger a ReadSelector callback, which will set the ID for
3248 // this selector.
3249 Chain->LoadSelector(Sel);
3250 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003251 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00003252 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003253 }
Sebastian Redl5d050072010-08-04 17:20:04 +00003254 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003255}
3256
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003257void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00003258 AddDeclRef(Temp->getDestructor(), Record);
3259}
3260
Douglas Gregor7c789c12010-10-29 22:39:52 +00003261void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
3262 CXXBaseSpecifier const *BasesEnd,
3263 RecordDataImpl &Record) {
3264 assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
3265 CXXBaseSpecifiersToWrite.push_back(
3266 QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
3267 Bases, BasesEnd));
3268 Record.push_back(NextCXXBaseSpecifiersID++);
3269}
3270
Sebastian Redla4232eb2010-08-18 23:56:21 +00003271void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003272 const TemplateArgumentLocInfo &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003273 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003274 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00003275 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003276 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00003277 break;
3278 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003279 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00003280 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00003281 case TemplateArgument::Template:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003282 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003283 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003284 break;
3285 case TemplateArgument::TemplateExpansion:
Douglas Gregorb6744ef2011-03-02 17:09:35 +00003286 AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
Douglas Gregora7fc9012011-01-05 18:58:31 +00003287 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregorba68eca2011-01-05 17:40:24 +00003288 AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00003289 break;
John McCall833ca992009-10-29 08:12:44 +00003290 case TemplateArgument::Null:
3291 case TemplateArgument::Integral:
3292 case TemplateArgument::Declaration:
3293 case TemplateArgument::Pack:
3294 break;
3295 }
3296}
3297
Sebastian Redla4232eb2010-08-18 23:56:21 +00003298void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003299 RecordDataImpl &Record) {
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003300 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00003301
3302 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
3303 bool InfoHasSameExpr
3304 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
3305 Record.push_back(InfoHasSameExpr);
3306 if (InfoHasSameExpr)
3307 return; // Avoid storing the same expr twice.
3308 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00003309 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
3310 Record);
3311}
3312
Douglas Gregordc355712011-02-25 00:36:19 +00003313void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
3314 RecordDataImpl &Record) {
John McCalla93c9342009-12-07 02:54:59 +00003315 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00003316 AddTypeRef(QualType(), Record);
3317 return;
3318 }
3319
Douglas Gregordc355712011-02-25 00:36:19 +00003320 AddTypeLoc(TInfo->getTypeLoc(), Record);
3321}
3322
3323void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
3324 AddTypeRef(TL.getType(), Record);
3325
John McCalla1ee0c52009-10-16 21:56:05 +00003326 TypeLocWriter TLW(*this, Record);
Douglas Gregordc355712011-02-25 00:36:19 +00003327 for (; !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00003328 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00003329}
3330
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003331void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
Argyrios Kyrtzidis7fb35182010-08-20 16:04:14 +00003332 Record.push_back(GetOrCreateTypeID(T));
3333}
3334
3335TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003336 return MakeTypeID(T,
3337 std::bind1st(std::mem_fun(&ASTWriter::GetOrCreateTypeIdx), this));
3338}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003339
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003340TypeID ASTWriter::getTypeID(QualType T) const {
Argyrios Kyrtzidiseb3f04e2010-08-20 16:04:20 +00003341 return MakeTypeID(T,
3342 std::bind1st(std::mem_fun(&ASTWriter::getTypeIdx), this));
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003343}
3344
3345TypeIdx ASTWriter::GetOrCreateTypeIdx(QualType T) {
3346 if (T.isNull())
3347 return TypeIdx();
3348 assert(!T.getLocalFastQualifiers());
3349
Argyrios Kyrtzidis01b81c42010-08-20 16:04:04 +00003350 TypeIdx &Idx = TypeIdxs[T];
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003351 if (Idx.getIndex() == 0) {
Douglas Gregor366809a2009-04-26 03:49:13 +00003352 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00003353 // into the queue of types to emit.
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003354 Idx = TypeIdx(NextTypeID++);
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003355 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00003356 }
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003357 return Idx;
3358}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003359
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003360TypeIdx ASTWriter::getTypeIdx(QualType T) const {
Argyrios Kyrtzidis26fca902010-08-20 16:04:09 +00003361 if (T.isNull())
3362 return TypeIdx();
3363 assert(!T.getLocalFastQualifiers());
3364
Argyrios Kyrtzidis5d267682010-08-20 16:04:27 +00003365 TypeIdxMap::const_iterator I = TypeIdxs.find(T);
3366 assert(I != TypeIdxs.end() && "Type not emitted!");
3367 return I->second;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003368}
3369
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003370void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003371 Record.push_back(GetDeclRef(D));
3372}
3373
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003374DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003375 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00003376 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003377 }
Douglas Gregor97475832010-10-05 18:37:06 +00003378 assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003379 DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00003380 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00003381 // We haven't seen this declaration before. Give it a new ID and
3382 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003383 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00003384 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redl0b17c612010-08-13 00:28:03 +00003385 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
3386 // We don't add it to the replacement collection here, because we don't
3387 // have the offset yet.
3388 DeclTypesToEmit.push(const_cast<Decl *>(D));
3389 // Reset the flag, so that we don't add this decl multiple times.
3390 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003391 }
3392
Sebastian Redl681d7232010-07-27 00:17:23 +00003393 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00003394}
3395
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003396DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00003397 if (D == 0)
3398 return 0;
3399
3400 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
3401 return DeclIDs[D];
3402}
3403
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003404void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00003405 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00003406 Record.push_back(Name.getNameKind());
3407 switch (Name.getNameKind()) {
3408 case DeclarationName::Identifier:
3409 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
3410 break;
3411
3412 case DeclarationName::ObjCZeroArgSelector:
3413 case DeclarationName::ObjCOneArgSelector:
3414 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00003415 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00003416 break;
3417
3418 case DeclarationName::CXXConstructorName:
3419 case DeclarationName::CXXDestructorName:
3420 case DeclarationName::CXXConversionFunctionName:
3421 AddTypeRef(Name.getCXXNameType(), Record);
3422 break;
3423
3424 case DeclarationName::CXXOperatorName:
3425 Record.push_back(Name.getCXXOverloadedOperator());
3426 break;
3427
Sean Hunt3e518bd2009-11-29 07:34:05 +00003428 case DeclarationName::CXXLiteralOperatorName:
3429 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
3430 break;
3431
Douglas Gregor2cf26342009-04-09 22:27:44 +00003432 case DeclarationName::CXXUsingDirective:
3433 // No extra data to emit
3434 break;
3435 }
3436}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003437
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003438void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003439 DeclarationName Name, RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003440 switch (Name.getNameKind()) {
3441 case DeclarationName::CXXConstructorName:
3442 case DeclarationName::CXXDestructorName:
3443 case DeclarationName::CXXConversionFunctionName:
3444 AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
3445 break;
3446
3447 case DeclarationName::CXXOperatorName:
3448 AddSourceLocation(
3449 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
3450 Record);
3451 AddSourceLocation(
3452 SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
3453 Record);
3454 break;
3455
3456 case DeclarationName::CXXLiteralOperatorName:
3457 AddSourceLocation(
3458 SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
3459 Record);
3460 break;
3461
3462 case DeclarationName::Identifier:
3463 case DeclarationName::ObjCZeroArgSelector:
3464 case DeclarationName::ObjCOneArgSelector:
3465 case DeclarationName::ObjCMultiArgSelector:
3466 case DeclarationName::CXXUsingDirective:
3467 break;
3468 }
3469}
3470
3471void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003472 RecordDataImpl &Record) {
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003473 AddDeclarationName(NameInfo.getName(), Record);
3474 AddSourceLocation(NameInfo.getLoc(), Record);
3475 AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
3476}
3477
3478void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003479 RecordDataImpl &Record) {
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003480 AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
Argyrios Kyrtzidis40451072010-10-15 18:21:24 +00003481 Record.push_back(Info.NumTemplParamLists);
3482 for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
3483 AddTemplateParameterList(Info.TemplParamLists[i], Record);
3484}
3485
Sebastian Redla4232eb2010-08-18 23:56:21 +00003486void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003487 RecordDataImpl &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003488 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003489 // typically accommodate the vast majority.
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003490 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
3491
3492 // Push each of the NNS's onto a stack for serialization in reverse order.
3493 while (NNS) {
3494 NestedNames.push_back(NNS);
3495 NNS = NNS->getPrefix();
3496 }
3497
3498 Record.push_back(NestedNames.size());
3499 while(!NestedNames.empty()) {
3500 NNS = NestedNames.pop_back_val();
3501 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
3502 Record.push_back(Kind);
3503 switch (Kind) {
3504 case NestedNameSpecifier::Identifier:
3505 AddIdentifierRef(NNS->getAsIdentifier(), Record);
3506 break;
3507
3508 case NestedNameSpecifier::Namespace:
3509 AddDeclRef(NNS->getAsNamespace(), Record);
3510 break;
3511
Douglas Gregor14aba762011-02-24 02:36:08 +00003512 case NestedNameSpecifier::NamespaceAlias:
3513 AddDeclRef(NNS->getAsNamespaceAlias(), Record);
3514 break;
3515
Chris Lattner6ad9ac02010-05-07 21:43:38 +00003516 case NestedNameSpecifier::TypeSpec:
3517 case NestedNameSpecifier::TypeSpecWithTemplate:
3518 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
3519 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3520 break;
3521
3522 case NestedNameSpecifier::Global:
3523 // Don't need to write an associated value.
3524 break;
3525 }
3526 }
3527}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003528
Douglas Gregordc355712011-02-25 00:36:19 +00003529void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
3530 RecordDataImpl &Record) {
3531 // Nested name specifiers usually aren't too long. I think that 8 would
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00003532 // typically accommodate the vast majority.
Douglas Gregordc355712011-02-25 00:36:19 +00003533 llvm::SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
3534
3535 // Push each of the nested-name-specifiers's onto a stack for
3536 // serialization in reverse order.
3537 while (NNS) {
3538 NestedNames.push_back(NNS);
3539 NNS = NNS.getPrefix();
3540 }
3541
3542 Record.push_back(NestedNames.size());
3543 while(!NestedNames.empty()) {
3544 NNS = NestedNames.pop_back_val();
3545 NestedNameSpecifier::SpecifierKind Kind
3546 = NNS.getNestedNameSpecifier()->getKind();
3547 Record.push_back(Kind);
3548 switch (Kind) {
3549 case NestedNameSpecifier::Identifier:
3550 AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
3551 AddSourceRange(NNS.getLocalSourceRange(), Record);
3552 break;
3553
3554 case NestedNameSpecifier::Namespace:
3555 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
3556 AddSourceRange(NNS.getLocalSourceRange(), Record);
3557 break;
3558
3559 case NestedNameSpecifier::NamespaceAlias:
3560 AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
3561 AddSourceRange(NNS.getLocalSourceRange(), Record);
3562 break;
3563
3564 case NestedNameSpecifier::TypeSpec:
3565 case NestedNameSpecifier::TypeSpecWithTemplate:
3566 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
3567 AddTypeLoc(NNS.getTypeLoc(), Record);
3568 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3569 break;
3570
3571 case NestedNameSpecifier::Global:
3572 AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
3573 break;
3574 }
3575 }
3576}
3577
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003578void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
Michael J. Spencer20249a12010-10-21 03:16:25 +00003579 TemplateName::NameKind Kind = Name.getKind();
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003580 Record.push_back(Kind);
3581 switch (Kind) {
3582 case TemplateName::Template:
3583 AddDeclRef(Name.getAsTemplateDecl(), Record);
3584 break;
3585
3586 case TemplateName::OverloadedTemplate: {
3587 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
3588 Record.push_back(OvT->size());
3589 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
3590 I != E; ++I)
3591 AddDeclRef(*I, Record);
3592 break;
3593 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003594
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003595 case TemplateName::QualifiedTemplate: {
3596 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
3597 AddNestedNameSpecifier(QualT->getQualifier(), Record);
3598 Record.push_back(QualT->hasTemplateKeyword());
3599 AddDeclRef(QualT->getTemplateDecl(), Record);
3600 break;
3601 }
Michael J. Spencer20249a12010-10-21 03:16:25 +00003602
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003603 case TemplateName::DependentTemplate: {
3604 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
3605 AddNestedNameSpecifier(DepT->getQualifier(), Record);
3606 Record.push_back(DepT->isIdentifier());
3607 if (DepT->isIdentifier())
3608 AddIdentifierRef(DepT->getIdentifier(), Record);
3609 else
3610 Record.push_back(DepT->getOperator());
3611 break;
3612 }
Douglas Gregor1aee05d2011-01-15 06:45:20 +00003613
3614 case TemplateName::SubstTemplateTemplateParmPack: {
3615 SubstTemplateTemplateParmPackStorage *SubstPack
3616 = Name.getAsSubstTemplateTemplateParmPack();
3617 AddDeclRef(SubstPack->getParameterPack(), Record);
3618 AddTemplateArgument(SubstPack->getArgumentPack(), Record);
3619 break;
3620 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003621 }
3622}
3623
Michael J. Spencer20249a12010-10-21 03:16:25 +00003624void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003625 RecordDataImpl &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003626 Record.push_back(Arg.getKind());
3627 switch (Arg.getKind()) {
3628 case TemplateArgument::Null:
3629 break;
3630 case TemplateArgument::Type:
3631 AddTypeRef(Arg.getAsType(), Record);
3632 break;
3633 case TemplateArgument::Declaration:
3634 AddDeclRef(Arg.getAsDecl(), Record);
3635 break;
3636 case TemplateArgument::Integral:
3637 AddAPSInt(*Arg.getAsIntegral(), Record);
3638 AddTypeRef(Arg.getIntegralType(), Record);
3639 break;
3640 case TemplateArgument::Template:
Douglas Gregor2be29f42011-01-14 23:41:42 +00003641 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
3642 break;
Douglas Gregora7fc9012011-01-05 18:58:31 +00003643 case TemplateArgument::TemplateExpansion:
3644 AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
Douglas Gregor2be29f42011-01-14 23:41:42 +00003645 if (llvm::Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
3646 Record.push_back(*NumExpansions + 1);
3647 else
3648 Record.push_back(0);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00003649 break;
3650 case TemplateArgument::Expression:
3651 AddStmt(Arg.getAsExpr());
3652 break;
3653 case TemplateArgument::Pack:
3654 Record.push_back(Arg.pack_size());
3655 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
3656 I != E; ++I)
3657 AddTemplateArgument(*I, Record);
3658 break;
3659 }
3660}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003661
3662void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003663ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003664 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003665 assert(TemplateParams && "No TemplateParams!");
3666 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
3667 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
3668 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
3669 Record.push_back(TemplateParams->size());
3670 for (TemplateParameterList::const_iterator
3671 P = TemplateParams->begin(), PEnd = TemplateParams->end();
3672 P != PEnd; ++P)
3673 AddDeclRef(*P, Record);
3674}
3675
3676/// \brief Emit a template argument list.
3677void
Sebastian Redla4232eb2010-08-18 23:56:21 +00003678ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003679 RecordDataImpl &Record) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003680 assert(TemplateArgs && "No TemplateArgs!");
Douglas Gregor910f8002010-11-07 23:05:16 +00003681 Record.push_back(TemplateArgs->size());
3682 for (int i=0, e = TemplateArgs->size(); i != e; ++i)
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00003683 AddTemplateArgument(TemplateArgs->get(i), Record);
3684}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003685
3686
3687void
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003688ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00003689 Record.push_back(Set.size());
3690 for (UnresolvedSetImpl::const_iterator
3691 I = Set.begin(), E = Set.end(); I != E; ++I) {
3692 AddDeclRef(I.getDecl(), Record);
3693 Record.push_back(I.getAccess());
3694 }
3695}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003696
Sebastian Redla4232eb2010-08-18 23:56:21 +00003697void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003698 RecordDataImpl &Record) {
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003699 Record.push_back(Base.isVirtual());
3700 Record.push_back(Base.isBaseOfClass());
3701 Record.push_back(Base.getAccessSpecifierAsWritten());
Sebastian Redlf677ea32011-02-05 19:23:19 +00003702 Record.push_back(Base.getInheritConstructors());
Nick Lewycky56062202010-07-26 16:56:01 +00003703 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003704 AddSourceRange(Base.getSourceRange(), Record);
Douglas Gregorf90b27a2011-01-03 22:36:02 +00003705 AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
3706 : SourceLocation(),
3707 Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00003708}
Sebastian Redl30c514c2010-07-14 23:45:08 +00003709
Douglas Gregor7c789c12010-10-29 22:39:52 +00003710void ASTWriter::FlushCXXBaseSpecifiers() {
3711 RecordData Record;
3712 for (unsigned I = 0, N = CXXBaseSpecifiersToWrite.size(); I != N; ++I) {
3713 Record.clear();
3714
3715 // Record the offset of this base-specifier set.
3716 unsigned Index = CXXBaseSpecifiersToWrite[I].ID - FirstCXXBaseSpecifiersID;
3717 if (Index == CXXBaseSpecifiersOffsets.size())
3718 CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
3719 else {
3720 if (Index > CXXBaseSpecifiersOffsets.size())
3721 CXXBaseSpecifiersOffsets.resize(Index + 1);
3722 CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
3723 }
3724
3725 const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
3726 *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
3727 Record.push_back(BEnd - B);
3728 for (; B != BEnd; ++B)
3729 AddCXXBaseSpecifier(*B, Record);
3730 Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
Douglas Gregoracec34b2010-10-30 04:28:16 +00003731
3732 // Flush any expressions that were written as part of the base specifiers.
3733 FlushStmts();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003734 }
3735
3736 CXXBaseSpecifiersToWrite.clear();
3737}
3738
Sean Huntcbb67482011-01-08 20:30:50 +00003739void ASTWriter::AddCXXCtorInitializers(
3740 const CXXCtorInitializer * const *CtorInitializers,
3741 unsigned NumCtorInitializers,
3742 RecordDataImpl &Record) {
3743 Record.push_back(NumCtorInitializers);
3744 for (unsigned i=0; i != NumCtorInitializers; ++i) {
3745 const CXXCtorInitializer *Init = CtorInitializers[i];
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003746
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003747 if (Init->isBaseInitializer()) {
Sean Hunt156b6402011-05-04 01:19:08 +00003748 Record.push_back(CTOR_INITIALIZER_BASE);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003749 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
3750 Record.push_back(Init->isBaseVirtual());
Sean Hunt156b6402011-05-04 01:19:08 +00003751 } else if (Init->isDelegatingInitializer()) {
3752 Record.push_back(CTOR_INITIALIZER_DELEGATING);
3753 AddDeclRef(Init->getTargetConstructor(), Record);
3754 } else if (Init->isMemberInitializer()){
3755 Record.push_back(CTOR_INITIALIZER_MEMBER);
3756 AddDeclRef(Init->getMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003757 } else {
Sean Hunt156b6402011-05-04 01:19:08 +00003758 Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
3759 AddDeclRef(Init->getIndirectMember(), Record);
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003760 }
Francois Pichet00eb3f92010-12-04 09:14:42 +00003761
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003762 AddSourceLocation(Init->getMemberLocation(), Record);
3763 AddStmt(Init->getInit());
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00003764 AddSourceLocation(Init->getLParenLoc(), Record);
3765 AddSourceLocation(Init->getRParenLoc(), Record);
3766 Record.push_back(Init->isWritten());
3767 if (Init->isWritten()) {
3768 Record.push_back(Init->getSourceOrder());
3769 } else {
3770 Record.push_back(Init->getNumArrayIndices());
3771 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
3772 AddDeclRef(Init->getArrayIndex(i), Record);
3773 }
3774 }
3775}
3776
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003777void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
3778 assert(D->DefinitionData);
3779 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
3780 Record.push_back(Data.UserDeclaredConstructor);
3781 Record.push_back(Data.UserDeclaredCopyConstructor);
3782 Record.push_back(Data.UserDeclaredCopyAssignment);
3783 Record.push_back(Data.UserDeclaredDestructor);
3784 Record.push_back(Data.Aggregate);
3785 Record.push_back(Data.PlainOldData);
3786 Record.push_back(Data.Empty);
3787 Record.push_back(Data.Polymorphic);
3788 Record.push_back(Data.Abstract);
Chandler Carruthec997dc2011-04-30 10:07:30 +00003789 Record.push_back(Data.IsStandardLayout);
Chandler Carrutha8225442011-04-30 09:17:45 +00003790 Record.push_back(Data.HasNoNonEmptyBases);
3791 Record.push_back(Data.HasPrivateFields);
3792 Record.push_back(Data.HasProtectedFields);
3793 Record.push_back(Data.HasPublicFields);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003794 Record.push_back(Data.HasTrivialConstructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00003795 Record.push_back(Data.HasConstExprNonCopyMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003796 Record.push_back(Data.HasTrivialCopyConstructor);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00003797 Record.push_back(Data.HasTrivialMoveConstructor);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003798 Record.push_back(Data.HasTrivialCopyAssignment);
Chandler Carruth4d6e5a22011-04-23 23:10:33 +00003799 Record.push_back(Data.HasTrivialMoveAssignment);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003800 Record.push_back(Data.HasTrivialDestructor);
Chandler Carruth9b6347c2011-04-24 02:49:34 +00003801 Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003802 Record.push_back(Data.ComputedVisibleConversions);
3803 Record.push_back(Data.DeclaredDefaultConstructor);
3804 Record.push_back(Data.DeclaredCopyConstructor);
3805 Record.push_back(Data.DeclaredCopyAssignment);
3806 Record.push_back(Data.DeclaredDestructor);
3807
3808 Record.push_back(Data.NumBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003809 if (Data.NumBases > 0)
3810 AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
3811 Record);
3812
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003813 // FIXME: Make VBases lazily computed when needed to avoid storing them.
3814 Record.push_back(Data.NumVBases);
Douglas Gregor7c789c12010-10-29 22:39:52 +00003815 if (Data.NumVBases > 0)
3816 AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
3817 Record);
Argyrios Kyrtzidis89eaf3a2010-10-24 17:26:40 +00003818
3819 AddUnresolvedSet(Data.Conversions, Record);
3820 AddUnresolvedSet(Data.VisibleConversions, Record);
3821 // Data.Definition is the owning decl, no need to write it.
3822 AddDeclRef(Data.FirstFriend, Record);
3823}
3824
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003825void ASTWriter::ReaderInitialized(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003826 assert(Reader && "Cannot remove chain");
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003827 assert(!Chain && "Cannot replace chain");
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003828 assert(FirstDeclID == NextDeclID &&
3829 FirstTypeID == NextTypeID &&
3830 FirstIdentID == NextIdentID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00003831 FirstSelectorID == NextSelectorID &&
Douglas Gregor77424bc2010-10-02 19:29:26 +00003832 FirstMacroID == NextMacroID &&
Douglas Gregor7c789c12010-10-29 22:39:52 +00003833 FirstCXXBaseSpecifiersID == NextCXXBaseSpecifiersID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003834 "Setting chain after writing has started.");
3835 Chain = Reader;
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003836
3837 FirstDeclID += Chain->getTotalNumDecls();
3838 FirstTypeID += Chain->getTotalNumTypes();
3839 FirstIdentID += Chain->getTotalNumIdentifiers();
3840 FirstSelectorID += Chain->getTotalNumSelectors();
3841 FirstMacroID += Chain->getTotalNumMacroDefinitions();
Douglas Gregor7c789c12010-10-29 22:39:52 +00003842 FirstCXXBaseSpecifiersID += Chain->getTotalNumCXXBaseSpecifiers();
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +00003843 NextDeclID = FirstDeclID;
3844 NextTypeID = FirstTypeID;
3845 NextIdentID = FirstIdentID;
3846 NextSelectorID = FirstSelectorID;
3847 NextMacroID = FirstMacroID;
Douglas Gregor7c789c12010-10-29 22:39:52 +00003848 NextCXXBaseSpecifiersID = FirstCXXBaseSpecifiersID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00003849}
3850
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003851void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003852 IdentifierIDs[II] = ID;
Douglas Gregor040a8042011-02-11 00:26:14 +00003853 if (II->hasMacroDefinition())
3854 DeserializedMacroNames.push_back(II);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00003855}
3856
Argyrios Kyrtzidisc8e5d512010-08-20 16:03:59 +00003857void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
Douglas Gregor97475832010-10-05 18:37:06 +00003858 // Always take the highest-numbered type index. This copes with an interesting
3859 // case for chained AST writing where we schedule writing the type and then,
Michael J. Spencer20249a12010-10-21 03:16:25 +00003860 // later, deserialize the type from another AST. In this case, we want to
Douglas Gregor97475832010-10-05 18:37:06 +00003861 // keep the higher-numbered entry so that we can properly write it out to
3862 // the AST file.
3863 TypeIdx &StoredIdx = TypeIdxs[T];
3864 if (Idx.getIndex() >= StoredIdx.getIndex())
3865 StoredIdx = Idx;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003866}
3867
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003868void ASTWriter::DeclRead(DeclID ID, const Decl *D) {
Sebastian Redl1476ed42010-07-16 16:36:56 +00003869 DeclIDs[D] = ID;
Sebastian Redl30c514c2010-07-14 23:45:08 +00003870}
Sebastian Redl5d050072010-08-04 17:20:04 +00003871
Sebastian Redl8538e8d2010-08-18 23:57:32 +00003872void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00003873 SelectorIDs[S] = ID;
3874}
Douglas Gregor77424bc2010-10-02 19:29:26 +00003875
Michael J. Spencer20249a12010-10-21 03:16:25 +00003876void ASTWriter::MacroDefinitionRead(serialization::MacroID ID,
Douglas Gregor77424bc2010-10-02 19:29:26 +00003877 MacroDefinition *MD) {
3878 MacroDefinitions[MD] = ID;
3879}
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003880
3881void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
3882 assert(D->isDefinition());
3883 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
3884 // We are interested when a PCH decl is modified.
3885 if (RD->getPCHLevel() > 0) {
3886 // A forward reference was mutated into a definition. Rewrite it.
3887 // FIXME: This happens during template instantiation, should we
3888 // have created a new definition decl instead ?
Argyrios Kyrtzidisd3d07552010-10-28 07:38:45 +00003889 RewriteDecl(RD);
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00003890 }
3891
3892 for (CXXRecordDecl::redecl_iterator
3893 I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
3894 CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
3895 if (Redecl == RD)
3896 continue;
3897
3898 // We are interested when a PCH decl is modified.
3899 if (Redecl->getPCHLevel() > 0) {
3900 UpdateRecord &Record = DeclUpdates[Redecl];
3901 Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
3902 assert(Redecl->DefinitionData);
3903 assert(Redecl->DefinitionData->Definition == D);
3904 AddDeclRef(D, Record); // the DefinitionDecl
3905 }
3906 }
3907 }
3908}
Argyrios Kyrtzidis100050b2010-10-28 07:38:51 +00003909void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
3910 // TU and namespaces are handled elsewhere.
3911 if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
3912 return;
3913
3914 if (!(D->getPCHLevel() == 0 && cast<Decl>(DC)->getPCHLevel() > 0))
3915 return; // Not a source decl added to a DeclContext from PCH.
3916
3917 AddUpdatedDeclContext(DC);
3918}
Argyrios Kyrtzidisb6cc0e12010-10-24 17:26:54 +00003919
3920void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
3921 assert(D->isImplicit());
3922 if (!(D->getPCHLevel() == 0 && RD->getPCHLevel() > 0))
3923 return; // Not a source member added to a class from PCH.
3924 if (!isa<CXXMethodDecl>(D))
3925 return; // We are interested in lazily declared implicit methods.
3926
3927 // A decl coming from PCH was modified.
3928 assert(RD->isDefinition());
3929 UpdateRecord &Record = DeclUpdates[RD];
3930 Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
3931 AddDeclRef(D, Record);
3932}
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00003933
3934void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
3935 const ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis0f04f692010-10-28 07:38:47 +00003936 // The specializations set is kept in the canonical template.
3937 TD = TD->getCanonicalDecl();
Argyrios Kyrtzidisbef1a7b2010-10-28 07:38:42 +00003938 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3939 return; // Not a source specialization added to a template from PCH.
3940
3941 UpdateRecord &Record = DeclUpdates[TD];
3942 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3943 AddDeclRef(D, Record);
3944}
Douglas Gregor89d99802010-11-30 06:16:57 +00003945
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00003946void ASTWriter::AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
3947 const FunctionDecl *D) {
3948 // The specializations set is kept in the canonical template.
3949 TD = TD->getCanonicalDecl();
3950 if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
3951 return; // Not a source specialization added to a template from PCH.
3952
3953 UpdateRecord &Record = DeclUpdates[TD];
3954 Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
3955 AddDeclRef(D, Record);
3956}
3957
Sebastian Redl58a2cd82011-04-24 16:28:06 +00003958void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
3959 if (D->getPCHLevel() == 0)
3960 return; // Declaration not imported from PCH.
3961
3962 // Implicit decl from a PCH was defined.
3963 // FIXME: Should implicit definition be a separate FunctionDecl?
3964 RewriteDecl(D);
3965}
3966
Sebastian Redlf79a7192011-04-29 08:19:30 +00003967void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
3968 if (D->getPCHLevel() == 0)
3969 return;
3970
3971 // Since the actual instantiation is delayed, this really means that we need
3972 // to update the instantiation location.
3973 UpdateRecord &Record = DeclUpdates[D];
3974 Record.push_back(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER);
3975 AddSourceLocation(
3976 D->getMemberSpecializationInfo()->getPointOfInstantiation(), Record);
3977}
3978
Douglas Gregor89d99802010-11-30 06:16:57 +00003979ASTSerializationListener::~ASTSerializationListener() { }