blob: af954c842f8f9db43e383b068ce3fbc318190822 [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 Gregore737f502010-08-12 20:07:10 +000015#include "clang/Sema/Sema.h"
16#include "clang/Sema/IdentifierResolver.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclContextInternals.h"
Douglas Gregor0b748912009-04-14 21:18:50 +000020#include "clang/AST/Expr.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000021#include "clang/AST/Type.h"
John McCalla1ee0c52009-10-16 21:56:05 +000022#include "clang/AST/TypeLocVisitor.h"
Sebastian Redl6ab7cd82010-08-18 23:57:17 +000023#include "clang/Serialization/ASTReader.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000024#include "clang/Lex/MacroInfo.h"
Douglas Gregor6a5a23f2010-03-19 21:51:54 +000025#include "clang/Lex/PreprocessingRecord.h"
Chris Lattner7c5d24e2009-04-10 18:00:12 +000026#include "clang/Lex/Preprocessor.h"
Steve Naroff83d63c72009-04-24 20:03:17 +000027#include "clang/Lex/HeaderSearch.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000028#include "clang/Basic/FileManager.h"
Douglas Gregor3251ceb2009-04-20 20:36:09 +000029#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000030#include "clang/Basic/SourceManager.h"
Douglas Gregorbd945002009-04-13 16:31:14 +000031#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregor2bec0412009-04-10 21:16:55 +000032#include "clang/Basic/TargetInfo.h"
Douglas Gregorab41e632009-04-27 22:23:34 +000033#include "clang/Basic/Version.h"
Douglas Gregor17fc2232009-04-14 21:55:33 +000034#include "llvm/ADT/APFloat.h"
35#include "llvm/ADT/APInt.h"
Daniel Dunbar2596e422009-10-17 23:52:28 +000036#include "llvm/ADT/StringExtras.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000037#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregor14f79002009-04-10 03:52:48 +000038#include "llvm/Support/MemoryBuffer.h"
Douglas Gregorb64c1932009-05-12 01:31:05 +000039#include "llvm/System/Path.h"
Chris Lattner3c304bd2009-04-11 18:40:46 +000040#include <cstdio>
Douglas Gregor2cf26342009-04-09 22:27:44 +000041using namespace clang;
42
Sebastian Redlade50002010-07-30 17:03:48 +000043template <typename T, typename Allocator>
44T *data(std::vector<T, Allocator> &v) {
45 return v.empty() ? 0 : &v.front();
46}
47template <typename T, typename Allocator>
48const T *data(const std::vector<T, Allocator> &v) {
49 return v.empty() ? 0 : &v.front();
50}
51
Douglas Gregor2cf26342009-04-09 22:27:44 +000052//===----------------------------------------------------------------------===//
53// Type serialization
54//===----------------------------------------------------------------------===//
Chris Lattner12b1c762009-04-27 06:16:06 +000055
Douglas Gregor2cf26342009-04-09 22:27:44 +000056namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +000057 class ASTTypeWriter {
Sebastian Redla4232eb2010-08-18 23:56:21 +000058 ASTWriter &Writer;
59 ASTWriter::RecordData &Record;
Douglas Gregor2cf26342009-04-09 22:27:44 +000060
61 public:
62 /// \brief Type code that corresponds to the record generated.
63 pch::TypeCode Code;
64
Sebastian Redl3397c552010-08-18 23:56:27 +000065 ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordData &Record)
Douglas Gregor4fed3f42009-04-27 18:38:38 +000066 : Writer(Writer), Record(Record), Code(pch::TYPE_EXT_QUAL) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000067
68 void VisitArrayType(const ArrayType *T);
69 void VisitFunctionType(const FunctionType *T);
70 void VisitTagType(const TagType *T);
71
72#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
73#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +000074#include "clang/AST/TypeNodes.def"
75 };
76}
77
Sebastian Redl3397c552010-08-18 23:56:27 +000078void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000079 assert(false && "Built-in types are never serialized");
80}
81
Sebastian Redl3397c552010-08-18 23:56:27 +000082void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000083 Writer.AddTypeRef(T->getElementType(), Record);
84 Code = pch::TYPE_COMPLEX;
85}
86
Sebastian Redl3397c552010-08-18 23:56:27 +000087void ASTTypeWriter::VisitPointerType(const PointerType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000088 Writer.AddTypeRef(T->getPointeeType(), Record);
89 Code = pch::TYPE_POINTER;
90}
91
Sebastian Redl3397c552010-08-18 23:56:27 +000092void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +000093 Writer.AddTypeRef(T->getPointeeType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +000094 Code = pch::TYPE_BLOCK_POINTER;
95}
96
Sebastian Redl3397c552010-08-18 23:56:27 +000097void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +000098 Writer.AddTypeRef(T->getPointeeType(), Record);
99 Code = pch::TYPE_LVALUE_REFERENCE;
100}
101
Sebastian Redl3397c552010-08-18 23:56:27 +0000102void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000103 Writer.AddTypeRef(T->getPointeeType(), Record);
104 Code = pch::TYPE_RVALUE_REFERENCE;
105}
106
Sebastian Redl3397c552010-08-18 23:56:27 +0000107void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000108 Writer.AddTypeRef(T->getPointeeType(), Record);
109 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000110 Code = pch::TYPE_MEMBER_POINTER;
111}
112
Sebastian Redl3397c552010-08-18 23:56:27 +0000113void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000114 Writer.AddTypeRef(T->getElementType(), Record);
115 Record.push_back(T->getSizeModifier()); // FIXME: stable values
John McCall0953e762009-09-24 19:53:00 +0000116 Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
Douglas Gregor2cf26342009-04-09 22:27:44 +0000117}
118
Sebastian Redl3397c552010-08-18 23:56:27 +0000119void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000120 VisitArrayType(T);
121 Writer.AddAPInt(T->getSize(), Record);
122 Code = pch::TYPE_CONSTANT_ARRAY;
123}
124
Sebastian Redl3397c552010-08-18 23:56:27 +0000125void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000126 VisitArrayType(T);
127 Code = pch::TYPE_INCOMPLETE_ARRAY;
128}
129
Sebastian Redl3397c552010-08-18 23:56:27 +0000130void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000131 VisitArrayType(T);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000132 Writer.AddSourceLocation(T->getLBracketLoc(), Record);
133 Writer.AddSourceLocation(T->getRBracketLoc(), Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000134 Writer.AddStmt(T->getSizeExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000135 Code = pch::TYPE_VARIABLE_ARRAY;
136}
137
Sebastian Redl3397c552010-08-18 23:56:27 +0000138void ASTTypeWriter::VisitVectorType(const VectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000139 Writer.AddTypeRef(T->getElementType(), Record);
140 Record.push_back(T->getNumElements());
Chris Lattner788b0fd2010-06-23 06:00:24 +0000141 Record.push_back(T->getAltiVecSpecific());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000142 Code = pch::TYPE_VECTOR;
143}
144
Sebastian Redl3397c552010-08-18 23:56:27 +0000145void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000146 VisitVectorType(T);
147 Code = pch::TYPE_EXT_VECTOR;
148}
149
Sebastian Redl3397c552010-08-18 23:56:27 +0000150void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000151 Writer.AddTypeRef(T->getResultType(), Record);
Rafael Espindola264ba482010-03-30 20:24:48 +0000152 FunctionType::ExtInfo C = T->getExtInfo();
153 Record.push_back(C.getNoReturn());
Rafael Espindola425ef722010-03-30 22:15:11 +0000154 Record.push_back(C.getRegParm());
Douglas Gregorab8bbf42010-01-18 17:14:39 +0000155 // FIXME: need to stabilize encoding of calling convention...
Rafael Espindola264ba482010-03-30 20:24:48 +0000156 Record.push_back(C.getCC());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000157}
158
Sebastian Redl3397c552010-08-18 23:56:27 +0000159void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000160 VisitFunctionType(T);
161 Code = pch::TYPE_FUNCTION_NO_PROTO;
162}
163
Sebastian Redl3397c552010-08-18 23:56:27 +0000164void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000165 VisitFunctionType(T);
166 Record.push_back(T->getNumArgs());
167 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
168 Writer.AddTypeRef(T->getArgType(I), Record);
169 Record.push_back(T->isVariadic());
170 Record.push_back(T->getTypeQuals());
Sebastian Redl465226e2009-05-27 22:11:52 +0000171 Record.push_back(T->hasExceptionSpec());
172 Record.push_back(T->hasAnyExceptionSpec());
173 Record.push_back(T->getNumExceptions());
174 for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
175 Writer.AddTypeRef(T->getExceptionType(I), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000176 Code = pch::TYPE_FUNCTION_PROTO;
177}
178
Sebastian Redl3397c552010-08-18 23:56:27 +0000179void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
John McCalled976492009-12-04 22:46:56 +0000180 Writer.AddDeclRef(T->getDecl(), Record);
181 Code = pch::TYPE_UNRESOLVED_USING;
182}
John McCalled976492009-12-04 22:46:56 +0000183
Sebastian Redl3397c552010-08-18 23:56:27 +0000184void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000185 Writer.AddDeclRef(T->getDecl(), Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000186 assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
187 Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000188 Code = pch::TYPE_TYPEDEF;
189}
190
Sebastian Redl3397c552010-08-18 23:56:27 +0000191void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc9490c02009-04-16 22:23:12 +0000192 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000193 Code = pch::TYPE_TYPEOF_EXPR;
194}
195
Sebastian Redl3397c552010-08-18 23:56:27 +0000196void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000197 Writer.AddTypeRef(T->getUnderlyingType(), Record);
198 Code = pch::TYPE_TYPEOF;
199}
200
Sebastian Redl3397c552010-08-18 23:56:27 +0000201void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
Anders Carlsson395b4752009-06-24 19:06:50 +0000202 Writer.AddStmt(T->getUnderlyingExpr());
203 Code = pch::TYPE_DECLTYPE;
204}
205
Sebastian Redl3397c552010-08-18 23:56:27 +0000206void ASTTypeWriter::VisitTagType(const TagType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000207 Record.push_back(T->isDependentType());
Douglas Gregor2cf26342009-04-09 22:27:44 +0000208 Writer.AddDeclRef(T->getDecl(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +0000209 assert(!T->isBeingDefined() &&
Douglas Gregor2cf26342009-04-09 22:27:44 +0000210 "Cannot serialize in the middle of a type definition");
211}
212
Sebastian Redl3397c552010-08-18 23:56:27 +0000213void ASTTypeWriter::VisitRecordType(const RecordType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000214 VisitTagType(T);
215 Code = pch::TYPE_RECORD;
216}
217
Sebastian Redl3397c552010-08-18 23:56:27 +0000218void ASTTypeWriter::VisitEnumType(const EnumType *T) {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000219 VisitTagType(T);
220 Code = pch::TYPE_ENUM;
221}
222
Mike Stump1eb44332009-09-09 15:08:12 +0000223void
Sebastian Redl3397c552010-08-18 23:56:27 +0000224ASTTypeWriter::VisitSubstTemplateTypeParmType(
John McCall49a832b2009-10-18 09:09:24 +0000225 const SubstTemplateTypeParmType *T) {
226 Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
227 Writer.AddTypeRef(T->getReplacementType(), Record);
228 Code = pch::TYPE_SUBST_TEMPLATE_TYPE_PARM;
229}
230
231void
Sebastian Redl3397c552010-08-18 23:56:27 +0000232ASTTypeWriter::VisitTemplateSpecializationType(
Douglas Gregor2cf26342009-04-09 22:27:44 +0000233 const TemplateSpecializationType *T) {
Argyrios Kyrtzidisbe191102010-07-08 13:09:53 +0000234 Record.push_back(T->isDependentType());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000235 Writer.AddTemplateName(T->getTemplateName(), Record);
236 Record.push_back(T->getNumArgs());
237 for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end();
238 ArgI != ArgE; ++ArgI)
239 Writer.AddTemplateArgument(*ArgI, Record);
Argyrios Kyrtzidis9763e222010-07-02 11:55:11 +0000240 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
241 : T->getCanonicalTypeInternal(),
242 Record);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000243 Code = pch::TYPE_TEMPLATE_SPECIALIZATION;
244}
245
246void
Sebastian Redl3397c552010-08-18 23:56:27 +0000247ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
Argyrios Kyrtzidisae8b17f2010-06-30 08:49:25 +0000248 VisitArrayType(T);
249 Writer.AddStmt(T->getSizeExpr());
250 Writer.AddSourceRange(T->getBracketsRange(), Record);
251 Code = pch::TYPE_DEPENDENT_SIZED_ARRAY;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000252}
253
254void
Sebastian Redl3397c552010-08-18 23:56:27 +0000255ASTTypeWriter::VisitDependentSizedExtVectorType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000256 const DependentSizedExtVectorType *T) {
257 // FIXME: Serialize this type (C++ only)
258 assert(false && "Cannot serialize dependent sized extended vector types");
259}
260
261void
Sebastian Redl3397c552010-08-18 23:56:27 +0000262ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000263 Record.push_back(T->getDepth());
264 Record.push_back(T->getIndex());
265 Record.push_back(T->isParameterPack());
266 Writer.AddIdentifierRef(T->getName(), Record);
267 Code = pch::TYPE_TEMPLATE_TYPE_PARM;
268}
269
270void
Sebastian Redl3397c552010-08-18 23:56:27 +0000271ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000272 Record.push_back(T->getKeyword());
273 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
274 Writer.AddIdentifierRef(T->getIdentifier(), Record);
Argyrios Kyrtzidisf48d45e2010-07-02 11:55:24 +0000275 Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
276 : T->getCanonicalTypeInternal(),
277 Record);
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000278 Code = pch::TYPE_DEPENDENT_NAME;
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000279}
280
281void
Sebastian Redl3397c552010-08-18 23:56:27 +0000282ASTTypeWriter::VisitDependentTemplateSpecializationType(
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000283 const DependentTemplateSpecializationType *T) {
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000284 Record.push_back(T->getKeyword());
285 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
286 Writer.AddIdentifierRef(T->getIdentifier(), Record);
287 Record.push_back(T->getNumArgs());
288 for (DependentTemplateSpecializationType::iterator
289 I = T->begin(), E = T->end(); I != E; ++I)
290 Writer.AddTemplateArgument(*I, Record);
291 Code = pch::TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000292}
293
Sebastian Redl3397c552010-08-18 23:56:27 +0000294void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000295 Record.push_back(T->getKeyword());
Argyrios Kyrtzidis3acad622010-06-25 16:24:58 +0000296 Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
297 Writer.AddTypeRef(T->getNamedType(), Record);
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000298 Code = pch::TYPE_ELABORATED;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000299}
300
Sebastian Redl3397c552010-08-18 23:56:27 +0000301void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
John McCall3cb0ebd2010-03-10 03:28:59 +0000302 Writer.AddDeclRef(T->getDecl(), Record);
John McCall31f17ec2010-04-27 00:57:59 +0000303 Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
John McCall3cb0ebd2010-03-10 03:28:59 +0000304 Code = pch::TYPE_INJECTED_CLASS_NAME;
305}
306
Sebastian Redl3397c552010-08-18 23:56:27 +0000307void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000308 Writer.AddDeclRef(T->getDecl(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000309 Code = pch::TYPE_OBJC_INTERFACE;
310}
311
Sebastian Redl3397c552010-08-18 23:56:27 +0000312void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
John McCallc12c5bb2010-05-15 11:32:37 +0000313 Writer.AddTypeRef(T->getBaseType(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +0000314 Record.push_back(T->getNumProtocols());
John McCallc12c5bb2010-05-15 11:32:37 +0000315 for (ObjCObjectType::qual_iterator I = T->qual_begin(),
Steve Naroff446ee4e2009-05-27 16:21:00 +0000316 E = T->qual_end(); I != E; ++I)
317 Writer.AddDeclRef(*I, Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000318 Code = pch::TYPE_OBJC_OBJECT;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000319}
320
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000321void
Sebastian Redl3397c552010-08-18 23:56:27 +0000322ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
Mike Stump1eb44332009-09-09 15:08:12 +0000323 Writer.AddTypeRef(T->getPointeeType(), Record);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000324 Code = pch::TYPE_OBJC_OBJECT_POINTER;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000325}
326
John McCalla1ee0c52009-10-16 21:56:05 +0000327namespace {
328
329class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
Sebastian Redla4232eb2010-08-18 23:56:21 +0000330 ASTWriter &Writer;
331 ASTWriter::RecordData &Record;
John McCalla1ee0c52009-10-16 21:56:05 +0000332
333public:
Sebastian Redla4232eb2010-08-18 23:56:21 +0000334 TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordData &Record)
John McCalla1ee0c52009-10-16 21:56:05 +0000335 : Writer(Writer), Record(Record) { }
336
John McCall51bd8032009-10-18 01:05:36 +0000337#define ABSTRACT_TYPELOC(CLASS, PARENT)
John McCalla1ee0c52009-10-16 21:56:05 +0000338#define TYPELOC(CLASS, PARENT) \
John McCall51bd8032009-10-18 01:05:36 +0000339 void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000340#include "clang/AST/TypeLocNodes.def"
341
John McCall51bd8032009-10-18 01:05:36 +0000342 void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
343 void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
John McCalla1ee0c52009-10-16 21:56:05 +0000344};
345
346}
347
John McCall51bd8032009-10-18 01:05:36 +0000348void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
349 // nothing to do
John McCalla1ee0c52009-10-16 21:56:05 +0000350}
John McCall51bd8032009-10-18 01:05:36 +0000351void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
Douglas Gregorddf889a2010-01-18 18:04:31 +0000352 Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
353 if (TL.needsExtraLocalData()) {
354 Record.push_back(TL.getWrittenTypeSpec());
355 Record.push_back(TL.getWrittenSignSpec());
356 Record.push_back(TL.getWrittenWidthSpec());
357 Record.push_back(TL.hasModeAttr());
358 }
John McCalla1ee0c52009-10-16 21:56:05 +0000359}
John McCall51bd8032009-10-18 01:05:36 +0000360void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
361 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000362}
John McCall51bd8032009-10-18 01:05:36 +0000363void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
364 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000365}
John McCall51bd8032009-10-18 01:05:36 +0000366void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
367 Writer.AddSourceLocation(TL.getCaretLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000368}
John McCall51bd8032009-10-18 01:05:36 +0000369void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
370 Writer.AddSourceLocation(TL.getAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000371}
John McCall51bd8032009-10-18 01:05:36 +0000372void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
373 Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000374}
John McCall51bd8032009-10-18 01:05:36 +0000375void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
376 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000377}
John McCall51bd8032009-10-18 01:05:36 +0000378void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
379 Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
380 Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
381 Record.push_back(TL.getSizeExpr() ? 1 : 0);
382 if (TL.getSizeExpr())
383 Writer.AddStmt(TL.getSizeExpr());
John McCalla1ee0c52009-10-16 21:56:05 +0000384}
John McCall51bd8032009-10-18 01:05:36 +0000385void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
386 VisitArrayTypeLoc(TL);
387}
388void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
389 VisitArrayTypeLoc(TL);
390}
391void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
392 VisitArrayTypeLoc(TL);
393}
394void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
395 DependentSizedArrayTypeLoc TL) {
396 VisitArrayTypeLoc(TL);
397}
398void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
399 DependentSizedExtVectorTypeLoc TL) {
400 Writer.AddSourceLocation(TL.getNameLoc(), Record);
401}
402void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
403 Writer.AddSourceLocation(TL.getNameLoc(), Record);
404}
405void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
406 Writer.AddSourceLocation(TL.getNameLoc(), Record);
407}
408void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
409 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
410 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
411 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
412 Writer.AddDeclRef(TL.getArg(i), Record);
413}
414void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
415 VisitFunctionTypeLoc(TL);
416}
417void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
418 VisitFunctionTypeLoc(TL);
419}
John McCalled976492009-12-04 22:46:56 +0000420void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
421 Writer.AddSourceLocation(TL.getNameLoc(), Record);
422}
John McCall51bd8032009-10-18 01:05:36 +0000423void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
424 Writer.AddSourceLocation(TL.getNameLoc(), Record);
425}
426void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000427 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
428 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
429 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000430}
431void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
John McCallcfb708c2010-01-13 20:03:27 +0000432 Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
433 Writer.AddSourceLocation(TL.getLParenLoc(), Record);
434 Writer.AddSourceLocation(TL.getRParenLoc(), Record);
435 Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000436}
437void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
438 Writer.AddSourceLocation(TL.getNameLoc(), Record);
439}
440void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
441 Writer.AddSourceLocation(TL.getNameLoc(), Record);
442}
443void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
444 Writer.AddSourceLocation(TL.getNameLoc(), Record);
445}
John McCall51bd8032009-10-18 01:05:36 +0000446void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
447 Writer.AddSourceLocation(TL.getNameLoc(), Record);
448}
John McCall49a832b2009-10-18 09:09:24 +0000449void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
450 SubstTemplateTypeParmTypeLoc TL) {
451 Writer.AddSourceLocation(TL.getNameLoc(), Record);
452}
John McCall51bd8032009-10-18 01:05:36 +0000453void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
454 TemplateSpecializationTypeLoc TL) {
John McCall833ca992009-10-29 08:12:44 +0000455 Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
456 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
457 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
458 for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000459 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
460 TL.getArgLoc(i).getLocInfo(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000461}
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000462void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000463 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
464 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000465}
John McCall3cb0ebd2010-03-10 03:28:59 +0000466void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
467 Writer.AddSourceLocation(TL.getNameLoc(), Record);
468}
Douglas Gregor4714c122010-03-31 17:34:00 +0000469void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
Abramo Bagnarae4da7a02010-05-19 21:37:53 +0000470 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
471 Writer.AddSourceRange(TL.getQualifierRange(), Record);
John McCall51bd8032009-10-18 01:05:36 +0000472 Writer.AddSourceLocation(TL.getNameLoc(), Record);
473}
John McCall33500952010-06-11 00:33:02 +0000474void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
475 DependentTemplateSpecializationTypeLoc TL) {
476 Writer.AddSourceLocation(TL.getKeywordLoc(), Record);
477 Writer.AddSourceRange(TL.getQualifierRange(), Record);
478 Writer.AddSourceLocation(TL.getNameLoc(), Record);
479 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
480 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
481 for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +0000482 Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
483 TL.getArgLoc(I).getLocInfo(), Record);
John McCall33500952010-06-11 00:33:02 +0000484}
John McCall51bd8032009-10-18 01:05:36 +0000485void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
486 Writer.AddSourceLocation(TL.getNameLoc(), Record);
John McCallc12c5bb2010-05-15 11:32:37 +0000487}
488void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
489 Record.push_back(TL.hasBaseTypeAsWritten());
John McCall51bd8032009-10-18 01:05:36 +0000490 Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
491 Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
492 for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
493 Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
John McCalla1ee0c52009-10-16 21:56:05 +0000494}
John McCall54e14c42009-10-22 22:37:11 +0000495void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
496 Writer.AddSourceLocation(TL.getStarLoc(), Record);
John McCall54e14c42009-10-22 22:37:11 +0000497}
John McCalla1ee0c52009-10-16 21:56:05 +0000498
Chris Lattner4dcf151a2009-04-22 05:57:30 +0000499//===----------------------------------------------------------------------===//
Sebastian Redla4232eb2010-08-18 23:56:21 +0000500// ASTWriter Implementation
Douglas Gregor2cf26342009-04-09 22:27:44 +0000501//===----------------------------------------------------------------------===//
502
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000503static void EmitBlockID(unsigned ID, const char *Name,
504 llvm::BitstreamWriter &Stream,
Sebastian Redla4232eb2010-08-18 23:56:21 +0000505 ASTWriter::RecordData &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000506 Record.clear();
507 Record.push_back(ID);
508 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
509
510 // Emit the block name if present.
511 if (Name == 0 || Name[0] == 0) return;
512 Record.clear();
513 while (*Name)
514 Record.push_back(*Name++);
515 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
516}
517
518static void EmitRecordID(unsigned ID, const char *Name,
519 llvm::BitstreamWriter &Stream,
Sebastian Redla4232eb2010-08-18 23:56:21 +0000520 ASTWriter::RecordData &Record) {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000521 Record.clear();
522 Record.push_back(ID);
523 while (*Name)
524 Record.push_back(*Name++);
525 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattner0558df22009-04-27 00:49:53 +0000526}
527
528static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
Sebastian Redla4232eb2010-08-18 23:56:21 +0000529 ASTWriter::RecordData &Record) {
Chris Lattner0558df22009-04-27 00:49:53 +0000530#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
531 RECORD(STMT_STOP);
532 RECORD(STMT_NULL_PTR);
533 RECORD(STMT_NULL);
534 RECORD(STMT_COMPOUND);
535 RECORD(STMT_CASE);
536 RECORD(STMT_DEFAULT);
537 RECORD(STMT_LABEL);
538 RECORD(STMT_IF);
539 RECORD(STMT_SWITCH);
540 RECORD(STMT_WHILE);
541 RECORD(STMT_DO);
542 RECORD(STMT_FOR);
543 RECORD(STMT_GOTO);
544 RECORD(STMT_INDIRECT_GOTO);
545 RECORD(STMT_CONTINUE);
546 RECORD(STMT_BREAK);
547 RECORD(STMT_RETURN);
548 RECORD(STMT_DECL);
549 RECORD(STMT_ASM);
550 RECORD(EXPR_PREDEFINED);
551 RECORD(EXPR_DECL_REF);
552 RECORD(EXPR_INTEGER_LITERAL);
553 RECORD(EXPR_FLOATING_LITERAL);
554 RECORD(EXPR_IMAGINARY_LITERAL);
555 RECORD(EXPR_STRING_LITERAL);
556 RECORD(EXPR_CHARACTER_LITERAL);
557 RECORD(EXPR_PAREN);
558 RECORD(EXPR_UNARY_OPERATOR);
559 RECORD(EXPR_SIZEOF_ALIGN_OF);
560 RECORD(EXPR_ARRAY_SUBSCRIPT);
561 RECORD(EXPR_CALL);
562 RECORD(EXPR_MEMBER);
563 RECORD(EXPR_BINARY_OPERATOR);
564 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
565 RECORD(EXPR_CONDITIONAL_OPERATOR);
566 RECORD(EXPR_IMPLICIT_CAST);
567 RECORD(EXPR_CSTYLE_CAST);
568 RECORD(EXPR_COMPOUND_LITERAL);
569 RECORD(EXPR_EXT_VECTOR_ELEMENT);
570 RECORD(EXPR_INIT_LIST);
571 RECORD(EXPR_DESIGNATED_INIT);
572 RECORD(EXPR_IMPLICIT_VALUE_INIT);
573 RECORD(EXPR_VA_ARG);
574 RECORD(EXPR_ADDR_LABEL);
575 RECORD(EXPR_STMT);
576 RECORD(EXPR_TYPES_COMPATIBLE);
577 RECORD(EXPR_CHOOSE);
578 RECORD(EXPR_GNU_NULL);
579 RECORD(EXPR_SHUFFLE_VECTOR);
580 RECORD(EXPR_BLOCK);
581 RECORD(EXPR_BLOCK_DECL_REF);
582 RECORD(EXPR_OBJC_STRING_LITERAL);
583 RECORD(EXPR_OBJC_ENCODE);
584 RECORD(EXPR_OBJC_SELECTOR_EXPR);
585 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
586 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
587 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
588 RECORD(EXPR_OBJC_KVC_REF_EXPR);
589 RECORD(EXPR_OBJC_MESSAGE_EXPR);
590 RECORD(EXPR_OBJC_SUPER_EXPR);
591 RECORD(STMT_OBJC_FOR_COLLECTION);
592 RECORD(STMT_OBJC_CATCH);
593 RECORD(STMT_OBJC_FINALLY);
594 RECORD(STMT_OBJC_AT_TRY);
595 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
596 RECORD(STMT_OBJC_AT_THROW);
Sam Weinigeb7f9612010-02-07 06:32:43 +0000597 RECORD(EXPR_CXX_OPERATOR_CALL);
598 RECORD(EXPR_CXX_CONSTRUCT);
599 RECORD(EXPR_CXX_STATIC_CAST);
600 RECORD(EXPR_CXX_DYNAMIC_CAST);
601 RECORD(EXPR_CXX_REINTERPRET_CAST);
602 RECORD(EXPR_CXX_CONST_CAST);
603 RECORD(EXPR_CXX_FUNCTIONAL_CAST);
604 RECORD(EXPR_CXX_BOOL_LITERAL);
605 RECORD(EXPR_CXX_NULL_PTR_LITERAL);
Chris Lattner0558df22009-04-27 00:49:53 +0000606#undef RECORD
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000607}
Mike Stump1eb44332009-09-09 15:08:12 +0000608
Sebastian Redla4232eb2010-08-18 23:56:21 +0000609void ASTWriter::WriteBlockInfoBlock() {
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000610 RecordData Record;
611 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
Mike Stump1eb44332009-09-09 15:08:12 +0000612
Chris Lattner2f4efd12009-04-27 00:40:25 +0000613#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000614#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Sebastian Redl3397c552010-08-18 23:56:27 +0000616 // AST Top-Level Block.
Sebastian Redlf29f0a22010-08-18 23:57:22 +0000617 BLOCK(AST_BLOCK);
Zhongxing Xu51e774d2009-06-03 09:23:28 +0000618 RECORD(ORIGINAL_FILE_NAME);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000619 RECORD(TYPE_OFFSET);
620 RECORD(DECL_OFFSET);
621 RECORD(LANGUAGE_OPTIONS);
Douglas Gregorab41e632009-04-27 22:23:34 +0000622 RECORD(METADATA);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000623 RECORD(IDENTIFIER_OFFSET);
624 RECORD(IDENTIFIER_TABLE);
625 RECORD(EXTERNAL_DEFINITIONS);
626 RECORD(SPECIAL_TYPES);
627 RECORD(STATISTICS);
628 RECORD(TENTATIVE_DEFINITIONS);
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +0000629 RECORD(UNUSED_FILESCOPED_DECLS);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000630 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
631 RECORD(SELECTOR_OFFSETS);
632 RECORD(METHOD_POOL);
633 RECORD(PP_COUNTER_VALUE);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000634 RECORD(SOURCE_LOCATION_OFFSETS);
635 RECORD(SOURCE_LOCATION_PRELOADS);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000636 RECORD(STAT_CACHE);
Douglas Gregorb81c1702009-04-27 20:06:05 +0000637 RECORD(EXT_VECTOR_DECLS);
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000638 RECORD(VERSION_CONTROL_BRANCH_REVISION);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000639 RECORD(MACRO_DEFINITION_OFFSETS);
Sebastian Redla93e3b52010-07-08 22:01:51 +0000640 RECORD(CHAINED_METADATA);
Fariborz Jahanian32019832010-07-23 19:11:11 +0000641 RECORD(REFERENCED_SELECTOR_POOL);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000642
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000643 // SourceManager Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000644 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000645 RECORD(SM_SLOC_FILE_ENTRY);
646 RECORD(SM_SLOC_BUFFER_ENTRY);
647 RECORD(SM_SLOC_BUFFER_BLOB);
648 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
649 RECORD(SM_LINE_TABLE);
Mike Stump1eb44332009-09-09 15:08:12 +0000650
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000651 // Preprocessor Block.
Chris Lattner2f4efd12009-04-27 00:40:25 +0000652 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000653 RECORD(PP_MACRO_OBJECT_LIKE);
654 RECORD(PP_MACRO_FUNCTION_LIKE);
655 RECORD(PP_TOKEN);
Douglas Gregor6a5a23f2010-03-19 21:51:54 +0000656 RECORD(PP_MACRO_INSTANTIATION);
657 RECORD(PP_MACRO_DEFINITION);
658
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000659 // Decls and Types block.
660 BLOCK(DECLTYPES_BLOCK);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000661 RECORD(TYPE_EXT_QUAL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000662 RECORD(TYPE_COMPLEX);
663 RECORD(TYPE_POINTER);
664 RECORD(TYPE_BLOCK_POINTER);
665 RECORD(TYPE_LVALUE_REFERENCE);
666 RECORD(TYPE_RVALUE_REFERENCE);
667 RECORD(TYPE_MEMBER_POINTER);
668 RECORD(TYPE_CONSTANT_ARRAY);
669 RECORD(TYPE_INCOMPLETE_ARRAY);
670 RECORD(TYPE_VARIABLE_ARRAY);
671 RECORD(TYPE_VECTOR);
672 RECORD(TYPE_EXT_VECTOR);
673 RECORD(TYPE_FUNCTION_PROTO);
674 RECORD(TYPE_FUNCTION_NO_PROTO);
675 RECORD(TYPE_TYPEDEF);
676 RECORD(TYPE_TYPEOF_EXPR);
677 RECORD(TYPE_TYPEOF);
678 RECORD(TYPE_RECORD);
679 RECORD(TYPE_ENUM);
680 RECORD(TYPE_OBJC_INTERFACE);
John McCalla53d2cb2010-05-16 02:12:35 +0000681 RECORD(TYPE_OBJC_OBJECT);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000682 RECORD(TYPE_OBJC_OBJECT_POINTER);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000683 RECORD(DECL_ATTR);
684 RECORD(DECL_TRANSLATION_UNIT);
685 RECORD(DECL_TYPEDEF);
686 RECORD(DECL_ENUM);
687 RECORD(DECL_RECORD);
688 RECORD(DECL_ENUM_CONSTANT);
689 RECORD(DECL_FUNCTION);
690 RECORD(DECL_OBJC_METHOD);
691 RECORD(DECL_OBJC_INTERFACE);
692 RECORD(DECL_OBJC_PROTOCOL);
693 RECORD(DECL_OBJC_IVAR);
694 RECORD(DECL_OBJC_AT_DEFS_FIELD);
695 RECORD(DECL_OBJC_CLASS);
696 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
697 RECORD(DECL_OBJC_CATEGORY);
698 RECORD(DECL_OBJC_CATEGORY_IMPL);
699 RECORD(DECL_OBJC_IMPLEMENTATION);
700 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
701 RECORD(DECL_OBJC_PROPERTY);
702 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000703 RECORD(DECL_FIELD);
704 RECORD(DECL_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000705 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000706 RECORD(DECL_PARM_VAR);
Chris Lattner0ff8cda2009-04-26 22:32:16 +0000707 RECORD(DECL_FILE_SCOPE_ASM);
708 RECORD(DECL_BLOCK);
709 RECORD(DECL_CONTEXT_LEXICAL);
710 RECORD(DECL_CONTEXT_VISIBLE);
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000711 // Statements and Exprs can occur in the Decls and Types block.
Chris Lattner0558df22009-04-27 00:49:53 +0000712 AddStmtsExprs(Stream, Record);
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000713#undef RECORD
714#undef BLOCK
715 Stream.ExitBlock();
716}
717
Douglas Gregore650c8c2009-07-07 00:12:59 +0000718/// \brief Adjusts the given filename to only write out the portion of the
719/// filename that is not part of the system root directory.
Mike Stump1eb44332009-09-09 15:08:12 +0000720///
Douglas Gregore650c8c2009-07-07 00:12:59 +0000721/// \param Filename the file name to adjust.
722///
723/// \param isysroot When non-NULL, the PCH file is a relocatable PCH file and
724/// the returned filename will be adjusted by this system root.
725///
726/// \returns either the original filename (if it needs no adjustment) or the
727/// adjusted filename (which points into the @p Filename parameter).
Mike Stump1eb44332009-09-09 15:08:12 +0000728static const char *
Douglas Gregore650c8c2009-07-07 00:12:59 +0000729adjustFilenameForRelocatablePCH(const char *Filename, const char *isysroot) {
730 assert(Filename && "No file name to adjust?");
Mike Stump1eb44332009-09-09 15:08:12 +0000731
Douglas Gregore650c8c2009-07-07 00:12:59 +0000732 if (!isysroot)
733 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000734
Douglas Gregore650c8c2009-07-07 00:12:59 +0000735 // Verify that the filename and the system root have the same prefix.
736 unsigned Pos = 0;
737 for (; Filename[Pos] && isysroot[Pos]; ++Pos)
738 if (Filename[Pos] != isysroot[Pos])
739 return Filename; // Prefixes don't match.
Mike Stump1eb44332009-09-09 15:08:12 +0000740
Douglas Gregore650c8c2009-07-07 00:12:59 +0000741 // We hit the end of the filename before we hit the end of the system root.
742 if (!Filename[Pos])
743 return Filename;
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Douglas Gregore650c8c2009-07-07 00:12:59 +0000745 // If the file name has a '/' at the current position, skip over the '/'.
746 // We distinguish sysroot-based includes from absolute includes by the
747 // absence of '/' at the beginning of sysroot-based includes.
748 if (Filename[Pos] == '/')
749 ++Pos;
Mike Stump1eb44332009-09-09 15:08:12 +0000750
Douglas Gregore650c8c2009-07-07 00:12:59 +0000751 return Filename + Pos;
752}
Chris Lattnerb145b1e2009-04-26 22:26:21 +0000753
Sebastian Redl3397c552010-08-18 23:56:27 +0000754/// \brief Write the AST metadata (e.g., i686-apple-darwin9).
Sebastian Redla4232eb2010-08-18 23:56:21 +0000755void ASTWriter::WriteMetadata(ASTContext &Context, const char *isysroot) {
Douglas Gregor2bec0412009-04-10 21:16:55 +0000756 using namespace llvm;
Douglas Gregorb64c1932009-05-12 01:31:05 +0000757
Douglas Gregore650c8c2009-07-07 00:12:59 +0000758 // Metadata
759 const TargetInfo &Target = Context.Target;
760 BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev();
Sebastian Redl77f46032010-07-09 21:00:24 +0000761 MetaAbbrev->Add(BitCodeAbbrevOp(
762 Chain ? pch::CHAINED_METADATA : pch::METADATA));
Sebastian Redl3397c552010-08-18 23:56:27 +0000763 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST major
764 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // AST minor
Douglas Gregore650c8c2009-07-07 00:12:59 +0000765 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major
766 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor
767 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
Sebastian Redl77f46032010-07-09 21:00:24 +0000768 // Target triple or chained PCH name
769 MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregore650c8c2009-07-07 00:12:59 +0000770 unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev);
Mike Stump1eb44332009-09-09 15:08:12 +0000771
Douglas Gregore650c8c2009-07-07 00:12:59 +0000772 RecordData Record;
Sebastian Redl77f46032010-07-09 21:00:24 +0000773 Record.push_back(Chain ? pch::CHAINED_METADATA : pch::METADATA);
Douglas Gregore650c8c2009-07-07 00:12:59 +0000774 Record.push_back(pch::VERSION_MAJOR);
775 Record.push_back(pch::VERSION_MINOR);
776 Record.push_back(CLANG_VERSION_MAJOR);
777 Record.push_back(CLANG_VERSION_MINOR);
778 Record.push_back(isysroot != 0);
Sebastian Redl77f46032010-07-09 21:00:24 +0000779 // FIXME: This writes the absolute path for chained headers.
780 const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple();
781 Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr);
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Douglas Gregorb64c1932009-05-12 01:31:05 +0000783 // Original file name
784 SourceManager &SM = Context.getSourceManager();
785 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
786 BitCodeAbbrev *FileAbbrev = new BitCodeAbbrev();
787 FileAbbrev->Add(BitCodeAbbrevOp(pch::ORIGINAL_FILE_NAME));
788 FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
789 unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
790
791 llvm::sys::Path MainFilePath(MainFile->getName());
Mike Stump1eb44332009-09-09 15:08:12 +0000792
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000793 MainFilePath.makeAbsolute();
Douglas Gregorb64c1932009-05-12 01:31:05 +0000794
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +0000795 const char *MainFileNameStr = MainFilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +0000796 MainFileNameStr = adjustFilenameForRelocatablePCH(MainFileNameStr,
Douglas Gregore650c8c2009-07-07 00:12:59 +0000797 isysroot);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000798 RecordData Record;
799 Record.push_back(pch::ORIGINAL_FILE_NAME);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000800 Stream.EmitRecordWithBlob(FileAbbrevCode, Record, MainFileNameStr);
Douglas Gregorb64c1932009-05-12 01:31:05 +0000801 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000802
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000803 // Repository branch/version information.
804 BitCodeAbbrev *RepoAbbrev = new BitCodeAbbrev();
805 RepoAbbrev->Add(BitCodeAbbrevOp(pch::VERSION_CONTROL_BRANCH_REVISION));
806 RepoAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
807 unsigned RepoAbbrevCode = Stream.EmitAbbrev(RepoAbbrev);
Douglas Gregor445e23e2009-10-05 21:07:28 +0000808 Record.clear();
Ted Kremenek5b4ec632010-01-22 20:59:36 +0000809 Record.push_back(pch::VERSION_CONTROL_BRANCH_REVISION);
Ted Kremenekf7a96a32010-01-22 22:12:47 +0000810 Stream.EmitRecordWithBlob(RepoAbbrevCode, Record,
811 getClangFullRepositoryVersion());
Douglas Gregor2bec0412009-04-10 21:16:55 +0000812}
813
814/// \brief Write the LangOptions structure.
Sebastian Redla4232eb2010-08-18 23:56:21 +0000815void ASTWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000816 RecordData Record;
817 Record.push_back(LangOpts.Trigraphs);
818 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
819 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
820 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
821 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
Chandler Carrutheb5d7b72010-04-17 20:17:31 +0000822 Record.push_back(LangOpts.GNUKeywords); // Allow GNU-extension keywords
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000823 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
824 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
825 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
826 Record.push_back(LangOpts.C99); // C99 Support
827 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
828 Record.push_back(LangOpts.CPlusPlus); // C++ Support
829 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000830 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000832 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
833 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000834 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000835 // modern abi enabled.
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +0000836 Record.push_back(LangOpts.ObjCNonFragileABI2); // Objective-C enhanced
Fariborz Jahanian412e7982010-02-09 19:31:38 +0000837 // modern abi enabled.
Fariborz Jahanian4c9d8d02010-04-22 21:01:59 +0000838 Record.push_back(LangOpts.NoConstantCFStrings); // non cfstring generation enabled..
Mike Stump1eb44332009-09-09 15:08:12 +0000839
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000840 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000841 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
842 Record.push_back(LangOpts.LaxVectorConversions);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000843 Record.push_back(LangOpts.AltiVec);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000844 Record.push_back(LangOpts.Exceptions); // Support exception handling.
Daniel Dunbar73482882010-02-10 18:48:44 +0000845 Record.push_back(LangOpts.SjLjExceptions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000846
847 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
848 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
849 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
850
Chris Lattnerea5ce472009-04-27 07:35:58 +0000851 // Whether static initializers are protected by locks.
852 Record.push_back(LangOpts.ThreadsafeStatics);
Douglas Gregor972d9542009-09-03 14:36:33 +0000853 Record.push_back(LangOpts.POSIXThreads);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000854 Record.push_back(LangOpts.Blocks); // block extension to C
855 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
856 // they are unused.
857 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
858 // (modulo the platform support).
859
Chris Lattnera4d71452010-06-26 21:25:03 +0000860 Record.push_back(LangOpts.getSignedOverflowBehavior());
861 Record.push_back(LangOpts.HeinousExtensions);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000862
863 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
Mike Stump1eb44332009-09-09 15:08:12 +0000864 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000865 // defined.
866 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
867 // opposed to __DYNAMIC__).
868 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
869
870 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
871 // used (instead of C99 semantics).
872 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
Anders Carlssona33d9b42009-05-13 19:49:53 +0000873 Record.push_back(LangOpts.AccessControl); // Whether C++ access control should
874 // be enabled.
Eli Friedman15b91762009-06-05 07:05:05 +0000875 Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or
876 // unsigned type
John Thompsona6fda122009-11-05 20:14:16 +0000877 Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000878 Record.push_back(LangOpts.getGCMode());
879 Record.push_back(LangOpts.getVisibilityMode());
Daniel Dunbarab8e2812009-09-21 04:16:19 +0000880 Record.push_back(LangOpts.getStackProtectorMode());
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000881 Record.push_back(LangOpts.InstantiationDepth);
Nate Begemanb9e7e632009-06-25 23:01:11 +0000882 Record.push_back(LangOpts.OpenCL);
Mike Stump9c276ae2009-12-12 01:27:46 +0000883 Record.push_back(LangOpts.CatchUndefined);
Anders Carlsson92f58222009-08-22 22:30:33 +0000884 Record.push_back(LangOpts.ElideConstructors);
Douglas Gregora0068fc2010-07-09 17:35:33 +0000885 Record.push_back(LangOpts.SpellChecking);
Douglas Gregorc9490c02009-04-16 22:23:12 +0000886 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor0a0428e2009-04-10 20:39:37 +0000887}
888
Douglas Gregor14f79002009-04-10 03:52:48 +0000889//===----------------------------------------------------------------------===//
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000890// stat cache Serialization
891//===----------------------------------------------------------------------===//
892
893namespace {
894// Trait used for the on-disk hash table of stat cache results.
Sebastian Redl3397c552010-08-18 23:56:27 +0000895class ASTStatCacheTrait {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000896public:
897 typedef const char * key_type;
898 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +0000899
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000900 typedef std::pair<int, struct stat> data_type;
901 typedef const data_type& data_type_ref;
902
903 static unsigned ComputeHash(const char *path) {
Daniel Dunbar2596e422009-10-17 23:52:28 +0000904 return llvm::HashString(path);
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000905 }
Mike Stump1eb44332009-09-09 15:08:12 +0000906
907 std::pair<unsigned,unsigned>
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000908 EmitKeyDataLength(llvm::raw_ostream& Out, const char *path,
909 data_type_ref Data) {
910 unsigned StrLen = strlen(path);
911 clang::io::Emit16(Out, StrLen);
912 unsigned DataLen = 1; // result value
913 if (Data.first == 0)
914 DataLen += 4 + 4 + 2 + 8 + 8;
915 clang::io::Emit8(Out, DataLen);
916 return std::make_pair(StrLen + 1, DataLen);
917 }
Mike Stump1eb44332009-09-09 15:08:12 +0000918
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000919 void EmitKey(llvm::raw_ostream& Out, const char *path, unsigned KeyLen) {
920 Out.write(path, KeyLen);
921 }
Mike Stump1eb44332009-09-09 15:08:12 +0000922
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000923 void EmitData(llvm::raw_ostream& Out, key_type_ref,
924 data_type_ref Data, unsigned DataLen) {
925 using namespace clang::io;
926 uint64_t Start = Out.tell(); (void)Start;
Mike Stump1eb44332009-09-09 15:08:12 +0000927
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000928 // Result of stat()
929 Emit8(Out, Data.first? 1 : 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000930
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000931 if (Data.first == 0) {
932 Emit32(Out, (uint32_t) Data.second.st_ino);
933 Emit32(Out, (uint32_t) Data.second.st_dev);
934 Emit16(Out, (uint16_t) Data.second.st_mode);
935 Emit64(Out, (uint64_t) Data.second.st_mtime);
936 Emit64(Out, (uint64_t) Data.second.st_size);
937 }
938
939 assert(Out.tell() - Start == DataLen && "Wrong data length");
940 }
941};
942} // end anonymous namespace
943
Sebastian Redl3397c552010-08-18 23:56:27 +0000944/// \brief Write the stat() system call cache to the AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +0000945void ASTWriter::WriteStatCache(MemorizeStatCalls &StatCalls) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000946 // Build the on-disk hash table containing information about every
947 // stat() call.
Sebastian Redl3397c552010-08-18 23:56:27 +0000948 OnDiskChainedHashTableGenerator<ASTStatCacheTrait> Generator;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000949 unsigned NumStatEntries = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000950 for (MemorizeStatCalls::iterator Stat = StatCalls.begin(),
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000951 StatEnd = StatCalls.end();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000952 Stat != StatEnd; ++Stat, ++NumStatEntries) {
953 const char *Filename = Stat->first();
Douglas Gregore650c8c2009-07-07 00:12:59 +0000954 Generator.insert(Filename, Stat->second);
955 }
Mike Stump1eb44332009-09-09 15:08:12 +0000956
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000957 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000958 llvm::SmallString<4096> StatCacheData;
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000959 uint32_t BucketOffset;
960 {
961 llvm::raw_svector_ostream Out(StatCacheData);
962 // Make sure that no bucket is at offset 0
963 clang::io::Emit32(Out, 0);
964 BucketOffset = Generator.Emit(Out);
965 }
966
967 // Create a blob abbreviation
968 using namespace llvm;
969 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
970 Abbrev->Add(BitCodeAbbrevOp(pch::STAT_CACHE));
971 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
972 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
973 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
974 unsigned StatCacheAbbrev = Stream.EmitAbbrev(Abbrev);
975
976 // Write the stat cache
977 RecordData Record;
978 Record.push_back(pch::STAT_CACHE);
979 Record.push_back(BucketOffset);
980 Record.push_back(NumStatEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +0000981 Stream.EmitRecordWithBlob(StatCacheAbbrev, Record, StatCacheData.str());
Douglas Gregor4fed3f42009-04-27 18:38:38 +0000982}
983
984//===----------------------------------------------------------------------===//
Douglas Gregor14f79002009-04-10 03:52:48 +0000985// Source Manager Serialization
986//===----------------------------------------------------------------------===//
987
988/// \brief Create an abbreviation for the SLocEntry that refers to a
989/// file.
Douglas Gregorc9490c02009-04-16 22:23:12 +0000990static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +0000991 using namespace llvm;
992 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
993 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
994 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
995 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
996 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
997 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregor2d52be52010-03-21 22:49:54 +0000998 // FileEntry fields.
999 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1000 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
Douglas Gregor12fab312010-03-16 16:35:32 +00001001 // HeaderFileInfo fields.
1002 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isImport
1003 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // DirInfo
1004 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumIncludes
1005 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // ControllingMacro
Douglas Gregor14f79002009-04-10 03:52:48 +00001006 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc9490c02009-04-16 22:23:12 +00001007 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001008}
1009
1010/// \brief Create an abbreviation for the SLocEntry that refers to a
1011/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001012static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001013 using namespace llvm;
1014 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1015 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1016 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1017 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1018 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1019 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1020 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001021 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001022}
1023
1024/// \brief Create an abbreviation for the SLocEntry that refers to a
1025/// buffer's blob.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001026static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001027 using namespace llvm;
1028 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1029 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1030 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc9490c02009-04-16 22:23:12 +00001031 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001032}
1033
1034/// \brief Create an abbreviation for the SLocEntry that refers to an
1035/// buffer.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001036static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregor14f79002009-04-10 03:52:48 +00001037 using namespace llvm;
1038 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1039 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1040 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1041 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1042 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1043 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregorf60e9912009-04-15 18:05:10 +00001044 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc9490c02009-04-16 22:23:12 +00001045 return Stream.EmitAbbrev(Abbrev);
Douglas Gregor14f79002009-04-10 03:52:48 +00001046}
1047
1048/// \brief Writes the block containing the serialized form of the
1049/// source manager.
1050///
1051/// TODO: We should probably use an on-disk hash table (stored in a
1052/// blob), indexed based on the file name, so that we only create
1053/// entries for files that we actually need. In the common case (no
1054/// errors), we probably won't have to create file entries for any of
1055/// the files in the AST.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001056void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
Douglas Gregore650c8c2009-07-07 00:12:59 +00001057 const Preprocessor &PP,
1058 const char *isysroot) {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001059 RecordData Record;
1060
Chris Lattnerf04ad692009-04-10 17:16:57 +00001061 // Enter the source manager block.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001062 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregor14f79002009-04-10 03:52:48 +00001063
1064 // Abbreviations for the various kinds of source-location entries.
Chris Lattner828e18c2009-04-27 19:03:22 +00001065 unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1066 unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1067 unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1068 unsigned SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
Douglas Gregor14f79002009-04-10 03:52:48 +00001069
Douglas Gregorbd945002009-04-13 16:31:14 +00001070 // Write the line table.
1071 if (SourceMgr.hasLineTable()) {
1072 LineTableInfo &LineTable = SourceMgr.getLineTable();
1073
1074 // Emit the file names
1075 Record.push_back(LineTable.getNumFilenames());
1076 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1077 // Emit the file name
1078 const char *Filename = LineTable.getFilename(I);
Douglas Gregore650c8c2009-07-07 00:12:59 +00001079 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Douglas Gregorbd945002009-04-13 16:31:14 +00001080 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1081 Record.push_back(FilenameLen);
1082 if (FilenameLen)
1083 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1084 }
Mike Stump1eb44332009-09-09 15:08:12 +00001085
Douglas Gregorbd945002009-04-13 16:31:14 +00001086 // Emit the line entries
1087 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1088 L != LEnd; ++L) {
1089 // Emit the file ID
1090 Record.push_back(L->first);
Mike Stump1eb44332009-09-09 15:08:12 +00001091
Douglas Gregorbd945002009-04-13 16:31:14 +00001092 // Emit the line entries
1093 Record.push_back(L->second.size());
Mike Stump1eb44332009-09-09 15:08:12 +00001094 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
Douglas Gregorbd945002009-04-13 16:31:14 +00001095 LEEnd = L->second.end();
1096 LE != LEEnd; ++LE) {
1097 Record.push_back(LE->FileOffset);
1098 Record.push_back(LE->LineNo);
1099 Record.push_back(LE->FilenameID);
1100 Record.push_back((unsigned)LE->FileKind);
1101 Record.push_back(LE->IncludeOffset);
1102 }
Douglas Gregorbd945002009-04-13 16:31:14 +00001103 }
Zhongxing Xu3d8216a2009-05-22 08:38:27 +00001104 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregorbd945002009-04-13 16:31:14 +00001105 }
1106
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001107 // Write out the source location entry table. We skip the first
1108 // entry, which is always the same dummy entry.
Chris Lattner090d9b52009-04-27 19:01:47 +00001109 std::vector<uint32_t> SLocEntryOffsets;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001110 RecordData PreloadSLocs;
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001111 unsigned BaseSLocID = Chain ? Chain->getTotalNumSLocs() : 0;
1112 SLocEntryOffsets.reserve(SourceMgr.sloc_entry_size() - 1 - BaseSLocID);
1113 for (unsigned I = BaseSLocID + 1, N = SourceMgr.sloc_entry_size();
1114 I != N; ++I) {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001115 // Get this source location entry.
1116 const SrcMgr::SLocEntry *SLoc = &SourceMgr.getSLocEntry(I);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001117
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001118 // Record the offset of this source-location entry.
1119 SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1120
1121 // Figure out which record code to use.
1122 unsigned Code;
1123 if (SLoc->isFile()) {
1124 if (SLoc->getFile().getContentCache()->Entry)
1125 Code = pch::SM_SLOC_FILE_ENTRY;
1126 else
1127 Code = pch::SM_SLOC_BUFFER_ENTRY;
1128 } else
1129 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1130 Record.clear();
1131 Record.push_back(Code);
1132
1133 Record.push_back(SLoc->getOffset());
1134 if (SLoc->isFile()) {
1135 const SrcMgr::FileInfo &File = SLoc->getFile();
1136 Record.push_back(File.getIncludeLoc().getRawEncoding());
1137 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1138 Record.push_back(File.hasLineDirectives());
1139
1140 const SrcMgr::ContentCache *Content = File.getContentCache();
1141 if (Content->Entry) {
1142 // The source location entry is a file. The blob associated
1143 // with this entry is the file name.
Mike Stump1eb44332009-09-09 15:08:12 +00001144
Douglas Gregor2d52be52010-03-21 22:49:54 +00001145 // Emit size/modification time for this file.
1146 Record.push_back(Content->Entry->getSize());
1147 Record.push_back(Content->Entry->getModificationTime());
1148
Douglas Gregor12fab312010-03-16 16:35:32 +00001149 // Emit header-search information associated with this file.
1150 HeaderFileInfo HFI;
1151 HeaderSearch &HS = PP.getHeaderSearchInfo();
1152 if (Content->Entry->getUID() < HS.header_file_size())
1153 HFI = HS.header_file_begin()[Content->Entry->getUID()];
1154 Record.push_back(HFI.isImport);
1155 Record.push_back(HFI.DirInfo);
1156 Record.push_back(HFI.NumIncludes);
1157 AddIdentifierRef(HFI.ControllingMacro, Record);
1158
Douglas Gregore650c8c2009-07-07 00:12:59 +00001159 // Turn the file name into an absolute path, if it isn't already.
1160 const char *Filename = Content->Entry->getName();
1161 llvm::sys::Path FilePath(Filename, strlen(Filename));
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00001162 FilePath.makeAbsolute();
Kovarththanan Rajaratnamaba54a92010-03-14 07:15:57 +00001163 Filename = FilePath.c_str();
Mike Stump1eb44332009-09-09 15:08:12 +00001164
Douglas Gregore650c8c2009-07-07 00:12:59 +00001165 Filename = adjustFilenameForRelocatablePCH(Filename, isysroot);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001166 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record, Filename);
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001167
1168 // FIXME: For now, preload all file source locations, so that
1169 // we get the appropriate File entries in the reader. This is
1170 // a temporary measure.
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001171 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001172 } else {
1173 // The source location entry is a buffer. The blob associated
1174 // with this entry contains the contents of the buffer.
1175
1176 // We add one to the size so that we capture the trailing NULL
1177 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1178 // the reader side).
Douglas Gregor36c35ba2010-03-16 00:35:39 +00001179 const llvm::MemoryBuffer *Buffer
Chris Lattnere127a0d2010-04-20 20:35:58 +00001180 = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001181 const char *Name = Buffer->getBufferIdentifier();
Daniel Dunbarec312a12009-08-24 09:31:37 +00001182 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1183 llvm::StringRef(Name, strlen(Name) + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001184 Record.clear();
1185 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
1186 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Daniel Dunbarec312a12009-08-24 09:31:37 +00001187 llvm::StringRef(Buffer->getBufferStart(),
1188 Buffer->getBufferSize() + 1));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001189
1190 if (strcmp(Name, "<built-in>") == 0)
Sebastian Redl0fa7d0b2010-07-22 17:01:13 +00001191 PreloadSLocs.push_back(BaseSLocID + SLocEntryOffsets.size());
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001192 }
1193 } else {
1194 // The source location entry is an instantiation.
1195 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1196 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1197 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1198 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1199
1200 // Compute the token length for this macro expansion.
1201 unsigned NextOffset = SourceMgr.getNextOffset();
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001202 if (I + 1 != N)
1203 NextOffset = SourceMgr.getSLocEntry(I + 1).getOffset();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001204 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1205 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
1206 }
1207 }
1208
Douglas Gregorc9490c02009-04-16 22:23:12 +00001209 Stream.ExitBlock();
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001210
1211 if (SLocEntryOffsets.empty())
1212 return;
1213
Sebastian Redl3397c552010-08-18 23:56:27 +00001214 // Write the source-location offsets table into the AST block. This
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001215 // table is used for lazily loading source-location information.
1216 using namespace llvm;
1217 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1218 Abbrev->Add(BitCodeAbbrevOp(pch::SOURCE_LOCATION_OFFSETS));
1219 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1220 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // next offset
1221 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1222 unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
Mike Stump1eb44332009-09-09 15:08:12 +00001223
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001224 Record.clear();
1225 Record.push_back(pch::SOURCE_LOCATION_OFFSETS);
1226 Record.push_back(SLocEntryOffsets.size());
1227 Record.push_back(SourceMgr.getNextOffset());
1228 Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001229 (const char *)data(SLocEntryOffsets),
Chris Lattner090d9b52009-04-27 19:01:47 +00001230 SLocEntryOffsets.size()*sizeof(SLocEntryOffsets[0]));
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001231
Sebastian Redl3397c552010-08-18 23:56:27 +00001232 // Write the source location entry preloads array, telling the AST
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001233 // reader which source locations entries it should load eagerly.
1234 Stream.EmitRecord(pch::SOURCE_LOCATION_PRELOADS, PreloadSLocs);
Douglas Gregor14f79002009-04-10 03:52:48 +00001235}
1236
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001237//===----------------------------------------------------------------------===//
1238// Preprocessor Serialization
1239//===----------------------------------------------------------------------===//
1240
Chris Lattner0b1fb982009-04-10 17:15:23 +00001241/// \brief Writes the block containing the serialized form of the
1242/// preprocessor.
1243///
Sebastian Redla4232eb2010-08-18 23:56:21 +00001244void ASTWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001245 RecordData Record;
Chris Lattnerf04ad692009-04-10 17:16:57 +00001246
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001247 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1248 if (PP.getCounterValue() != 0) {
1249 Record.push_back(PP.getCounterValue());
Douglas Gregorc9490c02009-04-16 22:23:12 +00001250 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattnerc1f9d822009-04-13 01:29:17 +00001251 Record.clear();
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001252 }
1253
1254 // Enter the preprocessor block.
1255 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Mike Stump1eb44332009-09-09 15:08:12 +00001256
Sebastian Redl3397c552010-08-18 23:56:27 +00001257 // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
Douglas Gregor2eafc1b2009-04-26 00:07:37 +00001258 // FIXME: use diagnostics subsystem for localization etc.
1259 if (PP.SawDateOrTime())
1260 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001261
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001262 // Loop over all the macro definitions that are live at the end of the file,
1263 // emitting each to the PP section.
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001264 PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001265 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1266 I != E; ++I) {
Chris Lattner42d42b52009-04-10 21:41:48 +00001267 // FIXME: This emits macros in hash table order, we should do it in a stable
1268 // order so that output is reproducible.
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001269 MacroInfo *MI = I->second;
1270
Sebastian Redl3397c552010-08-18 23:56:27 +00001271 // Don't emit builtin macros like __LINE__ to the AST file unless they have
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001272 // been redefined by the header (in which case they are not isBuiltinMacro).
Sebastian Redl3397c552010-08-18 23:56:27 +00001273 // Also skip macros from a AST file if we're chaining.
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001274 if (MI->isBuiltinMacro() || (Chain && MI->isFromAST()))
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001275 continue;
1276
Chris Lattner7356a312009-04-11 21:15:38 +00001277 AddIdentifierRef(I->first, Record);
Douglas Gregor37e26842009-04-21 23:56:24 +00001278 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001279 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1280 Record.push_back(MI->isUsed());
Mike Stump1eb44332009-09-09 15:08:12 +00001281
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001282 unsigned Code;
1283 if (MI->isObjectLike()) {
1284 Code = pch::PP_MACRO_OBJECT_LIKE;
1285 } else {
1286 Code = pch::PP_MACRO_FUNCTION_LIKE;
Mike Stump1eb44332009-09-09 15:08:12 +00001287
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001288 Record.push_back(MI->isC99Varargs());
1289 Record.push_back(MI->isGNUVarargs());
1290 Record.push_back(MI->getNumArgs());
1291 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1292 I != E; ++I)
Chris Lattner7356a312009-04-11 21:15:38 +00001293 AddIdentifierRef(*I, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001294 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001295
1296 // If we have a detailed preprocessing record, record the macro definition
1297 // ID that corresponds to this macro.
1298 if (PPRec)
1299 Record.push_back(getMacroDefinitionID(PPRec->findMacroDefinition(MI)));
1300
Douglas Gregorc9490c02009-04-16 22:23:12 +00001301 Stream.EmitRecord(Code, Record);
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001302 Record.clear();
1303
Chris Lattnerdf961c22009-04-10 18:08:30 +00001304 // Emit the tokens array.
1305 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1306 // Note that we know that the preprocessor does not have any annotation
1307 // tokens in it because they are created by the parser, and thus can't be
1308 // in a macro definition.
1309 const Token &Tok = MI->getReplacementToken(TokNo);
Mike Stump1eb44332009-09-09 15:08:12 +00001310
Chris Lattnerdf961c22009-04-10 18:08:30 +00001311 Record.push_back(Tok.getLocation().getRawEncoding());
1312 Record.push_back(Tok.getLength());
1313
Chris Lattnerdf961c22009-04-10 18:08:30 +00001314 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1315 // it is needed.
Chris Lattner7356a312009-04-11 21:15:38 +00001316 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001317
Chris Lattnerdf961c22009-04-10 18:08:30 +00001318 // FIXME: Should translate token kind to a stable encoding.
1319 Record.push_back(Tok.getKind());
1320 // FIXME: Should translate token flags to a stable encoding.
1321 Record.push_back(Tok.getFlags());
Mike Stump1eb44332009-09-09 15:08:12 +00001322
Douglas Gregorc9490c02009-04-16 22:23:12 +00001323 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattnerdf961c22009-04-10 18:08:30 +00001324 Record.clear();
1325 }
Douglas Gregor37e26842009-04-21 23:56:24 +00001326 ++NumMacros;
Chris Lattner7c5d24e2009-04-10 18:00:12 +00001327 }
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001328
1329 // If the preprocessor has a preprocessing record, emit it.
1330 unsigned NumPreprocessingRecords = 0;
1331 if (PPRec) {
1332 for (PreprocessingRecord::iterator E = PPRec->begin(), EEnd = PPRec->end();
1333 E != EEnd; ++E) {
1334 Record.clear();
1335
1336 if (MacroInstantiation *MI = dyn_cast<MacroInstantiation>(*E)) {
1337 Record.push_back(NumPreprocessingRecords++);
1338 AddSourceLocation(MI->getSourceRange().getBegin(), Record);
1339 AddSourceLocation(MI->getSourceRange().getEnd(), Record);
1340 AddIdentifierRef(MI->getName(), Record);
1341 Record.push_back(getMacroDefinitionID(MI->getDefinition()));
1342 Stream.EmitRecord(pch::PP_MACRO_INSTANTIATION, Record);
1343 continue;
1344 }
1345
1346 if (MacroDefinition *MD = dyn_cast<MacroDefinition>(*E)) {
1347 // Record this macro definition's location.
1348 pch::IdentID ID = getMacroDefinitionID(MD);
1349 if (ID != MacroDefinitionOffsets.size()) {
1350 if (ID > MacroDefinitionOffsets.size())
1351 MacroDefinitionOffsets.resize(ID + 1);
1352
1353 MacroDefinitionOffsets[ID] = Stream.GetCurrentBitNo();
1354 } else
1355 MacroDefinitionOffsets.push_back(Stream.GetCurrentBitNo());
1356
1357 Record.push_back(NumPreprocessingRecords++);
1358 Record.push_back(ID);
1359 AddSourceLocation(MD->getSourceRange().getBegin(), Record);
1360 AddSourceLocation(MD->getSourceRange().getEnd(), Record);
1361 AddIdentifierRef(MD->getName(), Record);
1362 AddSourceLocation(MD->getLocation(), Record);
1363 Stream.EmitRecord(pch::PP_MACRO_DEFINITION, Record);
1364 continue;
1365 }
1366 }
1367 }
1368
Douglas Gregorc9490c02009-04-16 22:23:12 +00001369 Stream.ExitBlock();
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001370
1371 // Write the offsets table for the preprocessing record.
1372 if (NumPreprocessingRecords > 0) {
1373 // Write the offsets table for identifier IDs.
1374 using namespace llvm;
1375 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1376 Abbrev->Add(BitCodeAbbrevOp(pch::MACRO_DEFINITION_OFFSETS));
1377 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of records
1378 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macro defs
1379 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1380 unsigned MacroDefOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1381
1382 Record.clear();
1383 Record.push_back(pch::MACRO_DEFINITION_OFFSETS);
1384 Record.push_back(NumPreprocessingRecords);
1385 Record.push_back(MacroDefinitionOffsets.size());
1386 Stream.EmitRecordWithBlob(MacroDefOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001387 (const char *)data(MacroDefinitionOffsets),
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00001388 MacroDefinitionOffsets.size() * sizeof(uint32_t));
1389 }
Chris Lattner0b1fb982009-04-10 17:15:23 +00001390}
1391
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001392//===----------------------------------------------------------------------===//
1393// Type Serialization
1394//===----------------------------------------------------------------------===//
Chris Lattner0b1fb982009-04-10 17:15:23 +00001395
Sebastian Redl3397c552010-08-18 23:56:27 +00001396/// \brief Write the representation of a type to the AST stream.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001397void ASTWriter::WriteType(QualType T) {
Douglas Gregor8038d512009-04-10 17:25:41 +00001398 pch::TypeID &ID = TypeIDs[T];
Chris Lattnerf04ad692009-04-10 17:16:57 +00001399 if (ID == 0) // we haven't seen this type before.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001400 ID = NextTypeID++;
Mike Stump1eb44332009-09-09 15:08:12 +00001401
Douglas Gregor2cf26342009-04-09 22:27:44 +00001402 // Record the offset for this type.
Sebastian Redl681d7232010-07-27 00:17:23 +00001403 unsigned Index = ID - FirstTypeID;
1404 if (TypeOffsets.size() == Index)
Douglas Gregorc9490c02009-04-16 22:23:12 +00001405 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Sebastian Redl681d7232010-07-27 00:17:23 +00001406 else if (TypeOffsets.size() < Index) {
1407 TypeOffsets.resize(Index + 1);
1408 TypeOffsets[Index] = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001409 }
1410
1411 RecordData Record;
Mike Stump1eb44332009-09-09 15:08:12 +00001412
Douglas Gregor2cf26342009-04-09 22:27:44 +00001413 // Emit the type's representation.
Sebastian Redl3397c552010-08-18 23:56:27 +00001414 ASTTypeWriter W(*this, Record);
John McCall0953e762009-09-24 19:53:00 +00001415
Douglas Gregora4923eb2009-11-16 21:35:15 +00001416 if (T.hasLocalNonFastQualifiers()) {
1417 Qualifiers Qs = T.getLocalQualifiers();
1418 AddTypeRef(T.getLocalUnqualifiedType(), Record);
John McCall0953e762009-09-24 19:53:00 +00001419 Record.push_back(Qs.getAsOpaqueValue());
1420 W.Code = pch::TYPE_EXT_QUAL;
1421 } else {
1422 switch (T->getTypeClass()) {
1423 // For all of the concrete, non-dependent types, call the
1424 // appropriate visitor function.
Douglas Gregor2cf26342009-04-09 22:27:44 +00001425#define TYPE(Class, Base) \
Mike Stumpb7166332010-01-20 02:03:14 +00001426 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001427#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001428#include "clang/AST/TypeNodes.def"
John McCall0953e762009-09-24 19:53:00 +00001429 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00001430 }
1431
1432 // Emit the serialized record.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001433 Stream.EmitRecord(W.Code, Record);
Douglas Gregor0b748912009-04-14 21:18:50 +00001434
1435 // Flush any expressions that were written as part of this type.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001436 FlushStmts();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001437}
1438
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001439//===----------------------------------------------------------------------===//
1440// Declaration Serialization
1441//===----------------------------------------------------------------------===//
1442
Douglas Gregor2cf26342009-04-09 22:27:44 +00001443/// \brief Write the block containing all of the declaration IDs
1444/// lexically declared within the given DeclContext.
1445///
1446/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1447/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001448uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001449 DeclContext *DC) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001450 if (DC->decls_empty())
Douglas Gregor2cf26342009-04-09 22:27:44 +00001451 return 0;
1452
Douglas Gregorc9490c02009-04-16 22:23:12 +00001453 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001454 RecordData Record;
Sebastian Redl681d7232010-07-27 00:17:23 +00001455 Record.push_back(pch::DECL_CONTEXT_LEXICAL);
1456 llvm::SmallVector<pch::DeclID, 64> Decls;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001457 for (DeclContext::decl_iterator D = DC->decls_begin(), DEnd = DC->decls_end();
1458 D != DEnd; ++D)
Sebastian Redl681d7232010-07-27 00:17:23 +00001459 Decls.push_back(GetDeclRef(*D));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001460
Douglas Gregor25123082009-04-22 22:34:57 +00001461 ++NumLexicalDeclContexts;
Sebastian Redl681d7232010-07-27 00:17:23 +00001462 Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
1463 reinterpret_cast<char*>(Decls.data()), Decls.size() * sizeof(pch::DeclID));
Douglas Gregor2cf26342009-04-09 22:27:44 +00001464 return Offset;
1465}
1466
1467/// \brief Write the block containing all of the declaration IDs
1468/// visible from the given DeclContext.
1469///
1470/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1471/// bistream, or 0 if no block was written.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001472uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
Douglas Gregor2cf26342009-04-09 22:27:44 +00001473 DeclContext *DC) {
1474 if (DC->getPrimaryContext() != DC)
1475 return 0;
1476
Argyrios Kyrtzidis67643342010-06-29 22:47:00 +00001477 // Since there is no name lookup into functions or methods, don't bother to
1478 // build a visible-declarations table for these entities.
1479 if (DC->isFunctionOrMethod())
1480 return 0;
1481
1482 // If not in C++, we perform name lookup for the translation unit via the
1483 // IdentifierInfo chains, don't bother to build a visible-declarations table.
1484 // FIXME: In C++ we need the visible declarations in order to "see" the
1485 // friend declarations, is there a way to do this without writing the table ?
1486 if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus)
Douglas Gregor58f06992009-04-18 15:49:20 +00001487 return 0;
1488
Douglas Gregor2cf26342009-04-09 22:27:44 +00001489 // Force the DeclContext to build a its name-lookup table.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001490 DC->lookup(DeclarationName());
Douglas Gregor2cf26342009-04-09 22:27:44 +00001491
1492 // Serialize the contents of the mapping used for lookup. Note that,
1493 // although we have two very different code paths, the serialized
1494 // representation is the same for both cases: a declaration name,
1495 // followed by a size, followed by references to the visible
1496 // declarations that have that name.
Douglas Gregorc9490c02009-04-16 22:23:12 +00001497 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregor2cf26342009-04-09 22:27:44 +00001498 RecordData Record;
1499 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor8c700062009-04-13 21:20:57 +00001500 if (!Map)
1501 return 0;
1502
Douglas Gregor2cf26342009-04-09 22:27:44 +00001503 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1504 D != DEnd; ++D) {
1505 AddDeclarationName(D->first, Record);
1506 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1507 Record.push_back(Result.second - Result.first);
Mike Stump1eb44332009-09-09 15:08:12 +00001508 for (; Result.first != Result.second; ++Result.first)
Douglas Gregor2cf26342009-04-09 22:27:44 +00001509 AddDeclRef(*Result.first, Record);
1510 }
1511
1512 if (Record.size() == 0)
1513 return 0;
1514
Douglas Gregorc9490c02009-04-16 22:23:12 +00001515 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregor25123082009-04-22 22:34:57 +00001516 ++NumVisibleDeclContexts;
Douglas Gregor2cf26342009-04-09 22:27:44 +00001517 return Offset;
1518}
1519
Sebastian Redla4232eb2010-08-18 23:56:21 +00001520void ASTWriter::WriteTypeDeclOffsets() {
Sebastian Redl1476ed42010-07-16 16:36:56 +00001521 using namespace llvm;
1522 RecordData Record;
1523
1524 // Write the type offsets array
1525 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1526 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
1527 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
1528 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
1529 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1530 Record.clear();
1531 Record.push_back(pch::TYPE_OFFSET);
1532 Record.push_back(TypeOffsets.size());
1533 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001534 (const char *)data(TypeOffsets),
Sebastian Redl1476ed42010-07-16 16:36:56 +00001535 TypeOffsets.size() * sizeof(TypeOffsets[0]));
1536
1537 // Write the declaration offsets array
1538 Abbrev = new BitCodeAbbrev();
1539 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
1540 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
1541 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
1542 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1543 Record.clear();
1544 Record.push_back(pch::DECL_OFFSET);
1545 Record.push_back(DeclOffsets.size());
1546 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001547 (const char *)data(DeclOffsets),
Sebastian Redl1476ed42010-07-16 16:36:56 +00001548 DeclOffsets.size() * sizeof(DeclOffsets[0]));
1549}
1550
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001551//===----------------------------------------------------------------------===//
1552// Global Method Pool and Selector Serialization
1553//===----------------------------------------------------------------------===//
1554
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001555namespace {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001556// Trait used for the on-disk hash table used in the method pool.
Sebastian Redl3397c552010-08-18 23:56:27 +00001557class ASTMethodPoolTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00001558 ASTWriter &Writer;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001559
1560public:
1561 typedef Selector key_type;
1562 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001563
Sebastian Redl5d050072010-08-04 17:20:04 +00001564 struct data_type {
1565 pch::SelectorID ID;
1566 ObjCMethodList Instance, Factory;
1567 };
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001568 typedef const data_type& data_type_ref;
1569
Sebastian Redl3397c552010-08-18 23:56:27 +00001570 explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
Mike Stump1eb44332009-09-09 15:08:12 +00001571
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001572 static unsigned ComputeHash(Selector Sel) {
1573 unsigned N = Sel.getNumArgs();
1574 if (N == 0)
1575 ++N;
1576 unsigned R = 5381;
1577 for (unsigned I = 0; I != N; ++I)
1578 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
Daniel Dunbar2596e422009-10-17 23:52:28 +00001579 R = llvm::HashString(II->getName(), R);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001580 return R;
1581 }
Mike Stump1eb44332009-09-09 15:08:12 +00001582
1583 std::pair<unsigned,unsigned>
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001584 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1585 data_type_ref Methods) {
1586 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1587 clang::io::Emit16(Out, KeyLen);
Sebastian Redl5d050072010-08-04 17:20:04 +00001588 unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
1589 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001590 Method = Method->Next)
1591 if (Method->Method)
1592 DataLen += 4;
Sebastian Redl5d050072010-08-04 17:20:04 +00001593 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001594 Method = Method->Next)
1595 if (Method->Method)
1596 DataLen += 4;
1597 clang::io::Emit16(Out, DataLen);
1598 return std::make_pair(KeyLen, DataLen);
1599 }
Mike Stump1eb44332009-09-09 15:08:12 +00001600
Douglas Gregor83941df2009-04-25 17:48:32 +00001601 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
Mike Stump1eb44332009-09-09 15:08:12 +00001602 uint64_t Start = Out.tell();
Douglas Gregor83941df2009-04-25 17:48:32 +00001603 assert((Start >> 32) == 0 && "Selector key offset too large");
1604 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001605 unsigned N = Sel.getNumArgs();
1606 clang::io::Emit16(Out, N);
1607 if (N == 0)
1608 N = 1;
1609 for (unsigned I = 0; I != N; ++I)
Mike Stump1eb44332009-09-09 15:08:12 +00001610 clang::io::Emit32(Out,
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001611 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1612 }
Mike Stump1eb44332009-09-09 15:08:12 +00001613
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001614 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregora67e58c2009-04-24 21:49:02 +00001615 data_type_ref Methods, unsigned DataLen) {
1616 uint64_t Start = Out.tell(); (void)Start;
Sebastian Redl5d050072010-08-04 17:20:04 +00001617 clang::io::Emit32(Out, Methods.ID);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001618 unsigned NumInstanceMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00001619 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001620 Method = Method->Next)
1621 if (Method->Method)
1622 ++NumInstanceMethods;
1623
1624 unsigned NumFactoryMethods = 0;
Sebastian Redl5d050072010-08-04 17:20:04 +00001625 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001626 Method = Method->Next)
1627 if (Method->Method)
1628 ++NumFactoryMethods;
1629
1630 clang::io::Emit16(Out, NumInstanceMethods);
1631 clang::io::Emit16(Out, NumFactoryMethods);
Sebastian Redl5d050072010-08-04 17:20:04 +00001632 for (const ObjCMethodList *Method = &Methods.Instance; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001633 Method = Method->Next)
1634 if (Method->Method)
1635 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Sebastian Redl5d050072010-08-04 17:20:04 +00001636 for (const ObjCMethodList *Method = &Methods.Factory; Method;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001637 Method = Method->Next)
1638 if (Method->Method)
1639 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregora67e58c2009-04-24 21:49:02 +00001640
1641 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001642 }
1643};
1644} // end anonymous namespace
1645
Sebastian Redl059612d2010-08-03 21:58:15 +00001646/// \brief Write ObjC data: selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001647///
1648/// The method pool contains both instance and factory methods, stored
Sebastian Redl059612d2010-08-03 21:58:15 +00001649/// in an on-disk hash table indexed by the selector. The hash table also
1650/// contains an empty entry for every other selector known to Sema.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001651void ASTWriter::WriteSelectors(Sema &SemaRef) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001652 using namespace llvm;
1653
Sebastian Redl059612d2010-08-03 21:58:15 +00001654 // Do we have to do anything at all?
Sebastian Redl5d050072010-08-04 17:20:04 +00001655 if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
Sebastian Redl059612d2010-08-03 21:58:15 +00001656 return;
Sebastian Redle58aa892010-08-04 18:21:41 +00001657 unsigned NumTableEntries = 0;
Sebastian Redl059612d2010-08-03 21:58:15 +00001658 // Create and write out the blob that contains selectors and the method pool.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001659 {
Sebastian Redl3397c552010-08-18 23:56:27 +00001660 OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001661
Sebastian Redl059612d2010-08-03 21:58:15 +00001662 // Create the on-disk hash table representation. We walk through every
1663 // selector we've seen and look it up in the method pool.
Sebastian Redle58aa892010-08-04 18:21:41 +00001664 SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
Sebastian Redl5d050072010-08-04 17:20:04 +00001665 for (llvm::DenseMap<Selector, pch::SelectorID>::iterator
1666 I = SelectorIDs.begin(), E = SelectorIDs.end();
1667 I != E; ++I) {
1668 Selector S = I->first;
Sebastian Redl059612d2010-08-03 21:58:15 +00001669 Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
Sebastian Redl3397c552010-08-18 23:56:27 +00001670 ASTMethodPoolTrait::data_type Data = {
Sebastian Redl5d050072010-08-04 17:20:04 +00001671 I->second,
1672 ObjCMethodList(),
1673 ObjCMethodList()
1674 };
1675 if (F != SemaRef.MethodPool.end()) {
1676 Data.Instance = F->second.first;
1677 Data.Factory = F->second.second;
1678 }
Sebastian Redl3397c552010-08-18 23:56:27 +00001679 // Only write this selector if it's not in an existing AST or something
Sebastian Redle58aa892010-08-04 18:21:41 +00001680 // changed.
1681 if (Chain && I->second < FirstSelectorID) {
1682 // Selector already exists. Did it change?
1683 bool changed = false;
1684 for (ObjCMethodList *M = &Data.Instance; !changed && M && M->Method;
1685 M = M->Next) {
1686 if (M->Method->getPCHLevel() == 0)
1687 changed = true;
1688 }
1689 for (ObjCMethodList *M = &Data.Factory; !changed && M && M->Method;
1690 M = M->Next) {
1691 if (M->Method->getPCHLevel() == 0)
1692 changed = true;
1693 }
1694 if (!changed)
1695 continue;
Sebastian Redlfa78dec2010-08-04 21:22:45 +00001696 } else if (Data.Instance.Method || Data.Factory.Method) {
1697 // A new method pool entry.
1698 ++NumTableEntries;
Sebastian Redle58aa892010-08-04 18:21:41 +00001699 }
Sebastian Redl5d050072010-08-04 17:20:04 +00001700 Generator.insert(S, Data);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001701 }
1702
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001703 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001704 llvm::SmallString<4096> MethodPool;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001705 uint32_t BucketOffset;
1706 {
Sebastian Redl3397c552010-08-18 23:56:27 +00001707 ASTMethodPoolTrait Trait(*this);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001708 llvm::raw_svector_ostream Out(MethodPool);
1709 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001710 clang::io::Emit32(Out, 0);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001711 BucketOffset = Generator.Emit(Out, Trait);
1712 }
1713
1714 // Create a blob abbreviation
1715 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1716 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1717 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor83941df2009-04-25 17:48:32 +00001718 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001719 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1720 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1721
Douglas Gregor83941df2009-04-25 17:48:32 +00001722 // Write the method pool
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001723 RecordData Record;
1724 Record.push_back(pch::METHOD_POOL);
1725 Record.push_back(BucketOffset);
Sebastian Redle58aa892010-08-04 18:21:41 +00001726 Record.push_back(NumTableEntries);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001727 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool.str());
Douglas Gregor83941df2009-04-25 17:48:32 +00001728
1729 // Create a blob abbreviation for the selector table offsets.
1730 Abbrev = new BitCodeAbbrev();
1731 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1732 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1733 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1734 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1735
1736 // Write the selector offsets table.
1737 Record.clear();
1738 Record.push_back(pch::SELECTOR_OFFSETS);
1739 Record.push_back(SelectorOffsets.size());
1740 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001741 (const char *)data(SelectorOffsets),
Douglas Gregor83941df2009-04-25 17:48:32 +00001742 SelectorOffsets.size() * 4);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001743 }
1744}
1745
Sebastian Redl3397c552010-08-18 23:56:27 +00001746/// \brief Write the selectors referenced in @selector expression into AST file.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001747void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
Fariborz Jahanian32019832010-07-23 19:11:11 +00001748 using namespace llvm;
1749 if (SemaRef.ReferencedSelectors.empty())
1750 return;
Sebastian Redl725cd962010-08-04 20:40:17 +00001751
Fariborz Jahanian32019832010-07-23 19:11:11 +00001752 RecordData Record;
Sebastian Redl725cd962010-08-04 20:40:17 +00001753
Sebastian Redl3397c552010-08-18 23:56:27 +00001754 // Note: this writes out all references even for a dependent AST. But it is
Sebastian Redla68340f2010-08-04 22:21:29 +00001755 // very tricky to fix, and given that @selector shouldn't really appear in
1756 // headers, probably not worth it. It's not a correctness issue.
Fariborz Jahanian32019832010-07-23 19:11:11 +00001757 for (DenseMap<Selector, SourceLocation>::iterator S =
1758 SemaRef.ReferencedSelectors.begin(),
1759 E = SemaRef.ReferencedSelectors.end(); S != E; ++S) {
1760 Selector Sel = (*S).first;
1761 SourceLocation Loc = (*S).second;
1762 AddSelectorRef(Sel, Record);
1763 AddSourceLocation(Loc, Record);
1764 }
1765 Stream.EmitRecord(pch::REFERENCED_SELECTOR_POOL, Record);
1766}
1767
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001768//===----------------------------------------------------------------------===//
1769// Identifier Table Serialization
1770//===----------------------------------------------------------------------===//
1771
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001772namespace {
Sebastian Redl3397c552010-08-18 23:56:27 +00001773class ASTIdentifierTableTrait {
Sebastian Redla4232eb2010-08-18 23:56:21 +00001774 ASTWriter &Writer;
Douglas Gregor37e26842009-04-21 23:56:24 +00001775 Preprocessor &PP;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001776
Douglas Gregora92193e2009-04-28 21:18:29 +00001777 /// \brief Determines whether this is an "interesting" identifier
1778 /// that needs a full IdentifierInfo structure written into the hash
1779 /// table.
1780 static bool isInterestingIdentifier(const IdentifierInfo *II) {
1781 return II->isPoisoned() ||
1782 II->isExtensionToken() ||
1783 II->hasMacroDefinition() ||
1784 II->getObjCOrBuiltinID() ||
1785 II->getFETokenInfo<void>();
1786 }
1787
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001788public:
1789 typedef const IdentifierInfo* key_type;
1790 typedef key_type key_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001791
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001792 typedef pch::IdentID data_type;
1793 typedef data_type data_type_ref;
Mike Stump1eb44332009-09-09 15:08:12 +00001794
Sebastian Redl3397c552010-08-18 23:56:27 +00001795 ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP)
Douglas Gregor37e26842009-04-21 23:56:24 +00001796 : Writer(Writer), PP(PP) { }
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001797
1798 static unsigned ComputeHash(const IdentifierInfo* II) {
Daniel Dunbar2596e422009-10-17 23:52:28 +00001799 return llvm::HashString(II->getName());
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001800 }
Mike Stump1eb44332009-09-09 15:08:12 +00001801
1802 std::pair<unsigned,unsigned>
1803 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001804 pch::IdentID ID) {
Daniel Dunbare013d682009-10-18 20:26:12 +00001805 unsigned KeyLen = II->getLength() + 1;
Douglas Gregora92193e2009-04-28 21:18:29 +00001806 unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
1807 if (isInterestingIdentifier(II)) {
Douglas Gregor5998da52009-04-28 21:32:13 +00001808 DataLen += 2; // 2 bytes for builtin ID, flags
Mike Stump1eb44332009-09-09 15:08:12 +00001809 if (II->hasMacroDefinition() &&
Douglas Gregora92193e2009-04-28 21:18:29 +00001810 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
Douglas Gregor5998da52009-04-28 21:32:13 +00001811 DataLen += 4;
Douglas Gregora92193e2009-04-28 21:18:29 +00001812 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1813 DEnd = IdentifierResolver::end();
1814 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00001815 DataLen += sizeof(pch::DeclID);
Douglas Gregora92193e2009-04-28 21:18:29 +00001816 }
Douglas Gregor668c1a42009-04-21 22:25:48 +00001817 clang::io::Emit16(Out, DataLen);
Douglas Gregor02fc7512009-04-28 20:01:51 +00001818 // We emit the key length after the data length so that every
1819 // string is preceded by a 16-bit length. This matches the PTH
1820 // format for storing identifiers.
Douglas Gregord6595a42009-04-25 21:04:17 +00001821 clang::io::Emit16(Out, KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001822 return std::make_pair(KeyLen, DataLen);
1823 }
Mike Stump1eb44332009-09-09 15:08:12 +00001824
1825 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001826 unsigned KeyLen) {
1827 // Record the location of the key data. This is used when generating
1828 // the mapping from persistent IDs to strings.
1829 Writer.SetIdentifierOffset(II, Out.tell());
Daniel Dunbare013d682009-10-18 20:26:12 +00001830 Out.write(II->getNameStart(), KeyLen);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001831 }
Mike Stump1eb44332009-09-09 15:08:12 +00001832
1833 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001834 pch::IdentID ID, unsigned) {
Douglas Gregora92193e2009-04-28 21:18:29 +00001835 if (!isInterestingIdentifier(II)) {
1836 clang::io::Emit32(Out, ID << 1);
1837 return;
1838 }
Douglas Gregor5998da52009-04-28 21:32:13 +00001839
Douglas Gregora92193e2009-04-28 21:18:29 +00001840 clang::io::Emit32(Out, (ID << 1) | 0x01);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001841 uint32_t Bits = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001842 bool hasMacroDefinition =
1843 II->hasMacroDefinition() &&
Douglas Gregor37e26842009-04-21 23:56:24 +00001844 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregor5998da52009-04-28 21:32:13 +00001845 Bits = (uint32_t)II->getObjCOrBuiltinID();
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001846 Bits = (Bits << 1) | unsigned(hasMacroDefinition);
1847 Bits = (Bits << 1) | unsigned(II->isExtensionToken());
1848 Bits = (Bits << 1) | unsigned(II->isPoisoned());
Argyrios Kyrtzidis646395b2010-08-11 22:55:12 +00001849 Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
Daniel Dunbarb0b84382009-12-18 20:58:47 +00001850 Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
Douglas Gregor5998da52009-04-28 21:32:13 +00001851 clang::io::Emit16(Out, Bits);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001852
Douglas Gregor37e26842009-04-21 23:56:24 +00001853 if (hasMacroDefinition)
Douglas Gregor5998da52009-04-28 21:32:13 +00001854 clang::io::Emit32(Out, Writer.getMacroOffset(II));
Douglas Gregor37e26842009-04-21 23:56:24 +00001855
Douglas Gregor668c1a42009-04-21 22:25:48 +00001856 // Emit the declaration IDs in reverse order, because the
1857 // IdentifierResolver provides the declarations as they would be
1858 // visible (e.g., the function "stat" would come before the struct
1859 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1860 // adds declarations to the end of the list (so we need to see the
1861 // struct "status" before the function "status").
Sebastian Redlf2f0f032010-07-23 23:49:55 +00001862 // Only emit declarations that aren't from a chained PCH, though.
Mike Stump1eb44332009-09-09 15:08:12 +00001863 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
Douglas Gregor668c1a42009-04-21 22:25:48 +00001864 IdentifierResolver::end());
1865 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1866 DEnd = Decls.rend();
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001867 D != DEnd; ++D)
Sebastian Redld8c5abb2010-08-02 18:30:12 +00001868 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001869 }
1870};
1871} // end anonymous namespace
1872
Sebastian Redl3397c552010-08-18 23:56:27 +00001873/// \brief Write the identifier table into the AST file.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001874///
1875/// The identifier table consists of a blob containing string data
1876/// (the actual identifiers themselves) and a separate "offsets" index
1877/// that maps identifier IDs to locations within the blob.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001878void ASTWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregorafaf3082009-04-11 00:14:32 +00001879 using namespace llvm;
1880
1881 // Create and write out the blob that contains the identifier
1882 // strings.
Douglas Gregorafaf3082009-04-11 00:14:32 +00001883 {
Sebastian Redl3397c552010-08-18 23:56:27 +00001884 OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
Mike Stump1eb44332009-09-09 15:08:12 +00001885
Douglas Gregor92b059e2009-04-28 20:33:11 +00001886 // Look for any identifiers that were named while processing the
1887 // headers, but are otherwise not needed. We add these to the hash
1888 // table to enable checking of the predefines buffer in the case
Sebastian Redl3397c552010-08-18 23:56:27 +00001889 // where the user adds new macro definitions when building the AST
Douglas Gregor92b059e2009-04-28 20:33:11 +00001890 // file.
1891 for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
1892 IDEnd = PP.getIdentifierTable().end();
1893 ID != IDEnd; ++ID)
1894 getIdentifierRef(ID->second);
1895
Sebastian Redlf2f0f032010-07-23 23:49:55 +00001896 // Create the on-disk hash table representation. We only store offsets
1897 // for identifiers that appear here for the first time.
1898 IdentifierOffsets.resize(NextIdentID - FirstIdentID);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001899 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1900 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1901 ID != IDEnd; ++ID) {
1902 assert(ID->first && "NULL identifier in identifier table");
Sebastian Redl3c7f4132010-08-18 23:57:06 +00001903 if (!Chain || !ID->first->isFromAST())
Sebastian Redlf2f0f032010-07-23 23:49:55 +00001904 Generator.insert(ID->first, ID->second);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001905 }
Douglas Gregorafaf3082009-04-11 00:14:32 +00001906
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001907 // Create the on-disk hash table in a buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001908 llvm::SmallString<4096> IdentifierTable;
Douglas Gregor668c1a42009-04-21 22:25:48 +00001909 uint32_t BucketOffset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001910 {
Sebastian Redl3397c552010-08-18 23:56:27 +00001911 ASTIdentifierTableTrait Trait(*this, PP);
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001912 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001913 // Make sure that no bucket is at offset 0
Douglas Gregora67e58c2009-04-24 21:49:02 +00001914 clang::io::Emit32(Out, 0);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001915 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001916 }
1917
1918 // Create a blob abbreviation
1919 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1920 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregor668c1a42009-04-21 22:25:48 +00001921 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001922 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc9490c02009-04-16 22:23:12 +00001923 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorafaf3082009-04-11 00:14:32 +00001924
1925 // Write the identifier table
1926 RecordData Record;
1927 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregor668c1a42009-04-21 22:25:48 +00001928 Record.push_back(BucketOffset);
Daniel Dunbarec312a12009-08-24 09:31:37 +00001929 Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable.str());
Douglas Gregorafaf3082009-04-11 00:14:32 +00001930 }
1931
1932 // Write the offsets table for identifier IDs.
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001933 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1934 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
1935 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
1936 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1937 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1938
1939 RecordData Record;
1940 Record.push_back(pch::IDENTIFIER_OFFSET);
1941 Record.push_back(IdentifierOffsets.size());
1942 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
Sebastian Redlade50002010-07-30 17:03:48 +00001943 (const char *)data(IdentifierOffsets),
Douglas Gregor2b3a5a82009-04-25 19:10:14 +00001944 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregorafaf3082009-04-11 00:14:32 +00001945}
1946
Douglas Gregor4fed3f42009-04-27 18:38:38 +00001947//===----------------------------------------------------------------------===//
1948// General Serialization Routines
1949//===----------------------------------------------------------------------===//
1950
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001951/// \brief Write a record containing the given attributes.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001952void ASTWriter::WriteAttributeRecord(const AttrVec &Attrs) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001953 RecordData Record;
Sean Huntcf807c42010-08-18 23:23:40 +00001954 for (AttrVec::const_iterator i = Attrs.begin(), e = Attrs.end(); i != e; ++i){
1955 const Attr * A = *i;
1956 Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
1957 AddSourceLocation(A->getLocation(), Record);
1958 Record.push_back(A->isInherited());
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001959
Sean Huntcf807c42010-08-18 23:23:40 +00001960#include "clang/Serialization/AttrPCHWrite.inc"
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00001961
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001962 }
1963
Douglas Gregorc9490c02009-04-16 22:23:12 +00001964 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001965}
1966
Sebastian Redla4232eb2010-08-18 23:56:21 +00001967void ASTWriter::AddString(const std::string &Str, RecordData &Record) {
Douglas Gregor68a2eb02009-04-15 21:30:51 +00001968 Record.push_back(Str.size());
1969 Record.insert(Record.end(), Str.begin(), Str.end());
1970}
1971
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001972/// \brief Note that the identifier II occurs at the given offset
1973/// within the identifier table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001974void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00001975 pch::IdentID ID = IdentifierIDs[II];
Sebastian Redl3397c552010-08-18 23:56:27 +00001976 // Only store offsets new to this AST file. Other identifier names are looked
Sebastian Redlf2f0f032010-07-23 23:49:55 +00001977 // up earlier in the chain and thus don't need an offset.
1978 if (ID >= FirstIdentID)
1979 IdentifierOffsets[ID - FirstIdentID] = Offset;
Douglas Gregor3251ceb2009-04-20 20:36:09 +00001980}
1981
Douglas Gregor83941df2009-04-25 17:48:32 +00001982/// \brief Note that the selector Sel occurs at the given offset
1983/// within the method pool/selector table.
Sebastian Redla4232eb2010-08-18 23:56:21 +00001984void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
Douglas Gregor83941df2009-04-25 17:48:32 +00001985 unsigned ID = SelectorIDs[Sel];
1986 assert(ID && "Unknown selector");
Sebastian Redle58aa892010-08-04 18:21:41 +00001987 // Don't record offsets for selectors that are also available in a different
1988 // file.
1989 if (ID < FirstSelectorID)
1990 return;
1991 SelectorOffsets[ID - FirstSelectorID] = Offset;
Douglas Gregor83941df2009-04-25 17:48:32 +00001992}
1993
Sebastian Redla4232eb2010-08-18 23:56:21 +00001994ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream)
Sebastian Redle58aa892010-08-04 18:21:41 +00001995 : Stream(Stream), Chain(0), FirstDeclID(1), NextDeclID(FirstDeclID),
1996 FirstTypeID(pch::NUM_PREDEF_TYPE_IDS), NextTypeID(FirstTypeID),
1997 FirstIdentID(1), NextIdentID(FirstIdentID), FirstSelectorID(1),
1998 NextSelectorID(FirstSelectorID), CollectedStmts(&StmtsToEmit),
1999 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2000 NumVisibleDeclContexts(0) {
Sebastian Redl30c514c2010-07-14 23:45:08 +00002001}
Douglas Gregor2cf26342009-04-09 22:27:44 +00002002
Sebastian Redla4232eb2010-08-18 23:56:21 +00002003void ASTWriter::WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002004 const char *isysroot) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002005 // Emit the file header.
Douglas Gregorc9490c02009-04-16 22:23:12 +00002006 Stream.Emit((unsigned)'C', 8);
2007 Stream.Emit((unsigned)'P', 8);
2008 Stream.Emit((unsigned)'C', 8);
2009 Stream.Emit((unsigned)'H', 8);
Mike Stump1eb44332009-09-09 15:08:12 +00002010
Chris Lattnerb145b1e2009-04-26 22:26:21 +00002011 WriteBlockInfoBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002012
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002013 if (Chain)
Sebastian Redla4232eb2010-08-18 23:56:21 +00002014 WriteASTChain(SemaRef, StatCalls, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002015 else
Sebastian Redla4232eb2010-08-18 23:56:21 +00002016 WriteASTCore(SemaRef, StatCalls, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002017}
2018
Sebastian Redla4232eb2010-08-18 23:56:21 +00002019void ASTWriter::WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002020 const char *isysroot) {
2021 using namespace llvm;
2022
2023 ASTContext &Context = SemaRef.Context;
2024 Preprocessor &PP = SemaRef.PP;
2025
Douglas Gregor2cf26342009-04-09 22:27:44 +00002026 // The translation unit is the first declaration we'll emit.
2027 DeclIDs[Context.getTranslationUnitDecl()] = 1;
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002028 ++NextDeclID;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002029 DeclTypesToEmit.push(Context.getTranslationUnitDecl());
Douglas Gregor2cf26342009-04-09 22:27:44 +00002030
Douglas Gregor2deaea32009-04-22 18:49:13 +00002031 // Make sure that we emit IdentifierInfos (and any attached
2032 // declarations) for builtins.
2033 {
2034 IdentifierTable &Table = PP.getIdentifierTable();
2035 llvm::SmallVector<const char *, 32> BuiltinNames;
2036 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2037 Context.getLangOptions().NoBuiltin);
2038 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2039 getIdentifierRef(&Table.get(BuiltinNames[I]));
2040 }
2041
Chris Lattner63d65f82009-09-08 18:19:27 +00002042 // Build a record containing all of the tentative definitions in this file, in
Sebastian Redle9d12b62010-01-31 22:27:38 +00002043 // TentativeDefinitions order. Generally, this record will be empty for
Chris Lattner63d65f82009-09-08 18:19:27 +00002044 // headers.
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002045 RecordData TentativeDefinitions;
Sebastian Redle9d12b62010-01-31 22:27:38 +00002046 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2047 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
Chris Lattner63d65f82009-09-08 18:19:27 +00002048 }
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002049
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002050 // Build a record containing all of the file scoped decls in this file.
2051 RecordData UnusedFileScopedDecls;
2052 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i)
2053 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl40566802010-08-05 18:21:25 +00002054
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002055 RecordData WeakUndeclaredIdentifiers;
2056 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2057 WeakUndeclaredIdentifiers.push_back(
2058 SemaRef.WeakUndeclaredIdentifiers.size());
2059 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2060 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2061 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2062 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2063 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2064 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2065 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2066 }
2067 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002068
Douglas Gregor14c22f22009-04-22 22:18:58 +00002069 // Build a record containing all of the locally-scoped external
2070 // declarations in this header file. Generally, this record will be
2071 // empty.
2072 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002073 // FIXME: This is filling in the AST file in densemap order which is
Chris Lattner63d65f82009-09-08 18:19:27 +00002074 // nondeterminstic!
Mike Stump1eb44332009-09-09 15:08:12 +00002075 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
Douglas Gregor14c22f22009-04-22 22:18:58 +00002076 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2077 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2078 TD != TDEnd; ++TD)
2079 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2080
Douglas Gregorb81c1702009-04-27 20:06:05 +00002081 // Build a record containing all of the ext_vector declarations.
2082 RecordData ExtVectorDecls;
2083 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I)
2084 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2085
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002086 // Build a record containing all of the VTable uses information.
2087 RecordData VTableUses;
Argyrios Kyrtzidisbe4ebcd2010-08-03 17:29:52 +00002088 if (!SemaRef.VTableUses.empty()) {
2089 VTableUses.push_back(SemaRef.VTableUses.size());
2090 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2091 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2092 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2093 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2094 }
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002095 }
2096
2097 // Build a record containing all of dynamic classes declarations.
2098 RecordData DynamicClasses;
2099 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2100 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2101
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002102 // Build a record containing all of pending implicit instantiations.
2103 RecordData PendingImplicitInstantiations;
2104 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
2105 I = SemaRef.PendingImplicitInstantiations.begin(),
2106 N = SemaRef.PendingImplicitInstantiations.end(); I != N; ++I) {
2107 AddDeclRef(I->first, PendingImplicitInstantiations);
2108 AddSourceLocation(I->second, PendingImplicitInstantiations);
2109 }
2110 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2111 "There are local ones at end of translation unit!");
2112
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002113 // Build a record containing some declaration references.
2114 RecordData SemaDeclRefs;
2115 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2116 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2117 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2118 }
2119
Sebastian Redl3397c552010-08-18 23:56:27 +00002120 // Write the remaining AST contents.
Douglas Gregorad1de002009-04-18 05:55:16 +00002121 RecordData Record;
Sebastian Redlf29f0a22010-08-18 23:57:22 +00002122 Stream.EnterSubblock(pch::AST_BLOCK_ID, 5);
Sebastian Redl30c514c2010-07-14 23:45:08 +00002123 WriteMetadata(Context, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002124 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregore650c8c2009-07-07 00:12:59 +00002125 if (StatCalls && !isysroot)
Douglas Gregordd41ed52010-07-12 23:48:14 +00002126 WriteStatCache(*StatCalls);
Douglas Gregore650c8c2009-07-07 00:12:59 +00002127 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002128 // Write the record of special types.
2129 Record.clear();
Mike Stump1eb44332009-09-09 15:08:12 +00002130
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002131 AddTypeRef(Context.getBuiltinVaListType(), Record);
2132 AddTypeRef(Context.getObjCIdType(), Record);
2133 AddTypeRef(Context.getObjCSelType(), Record);
2134 AddTypeRef(Context.getObjCProtoType(), Record);
2135 AddTypeRef(Context.getObjCClassType(), Record);
2136 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2137 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
2138 AddTypeRef(Context.getFILEType(), Record);
Mike Stump782fa302009-07-28 02:25:19 +00002139 AddTypeRef(Context.getjmp_bufType(), Record);
2140 AddTypeRef(Context.getsigjmp_bufType(), Record);
Douglas Gregord1571ac2009-08-21 00:27:50 +00002141 AddTypeRef(Context.ObjCIdRedefinitionType, Record);
2142 AddTypeRef(Context.ObjCClassRedefinitionType, Record);
Mike Stumpadaaad32009-10-20 02:12:22 +00002143 AddTypeRef(Context.getRawBlockdescriptorType(), Record);
Mike Stump083c25e2009-10-22 00:49:09 +00002144 AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
Fariborz Jahanian2bb5dda2010-04-23 17:41:07 +00002145 AddTypeRef(Context.ObjCSelRedefinitionType, Record);
2146 AddTypeRef(Context.getRawNSConstantStringType(), Record);
Argyrios Kyrtzidis00611382010-07-04 21:44:19 +00002147 Record.push_back(Context.isInt128Installed());
Steve Naroffc15cb2a2009-07-18 15:33:26 +00002148 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00002149
Douglas Gregor366809a2009-04-26 03:49:13 +00002150 // Keep writing types and declarations until all types and
2151 // declarations have been written.
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002152 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2153 WriteDeclsBlockAbbrevs();
2154 while (!DeclTypesToEmit.empty()) {
2155 DeclOrType DOT = DeclTypesToEmit.front();
2156 DeclTypesToEmit.pop();
2157 if (DOT.isType())
2158 WriteType(DOT.getType());
2159 else
2160 WriteDecl(Context, DOT.getDecl());
2161 }
2162 Stream.ExitBlock();
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002163
Douglas Gregor813a97b2009-10-17 17:25:45 +00002164 WritePreprocessor(PP);
Sebastian Redl059612d2010-08-03 21:58:15 +00002165 WriteSelectors(SemaRef);
Fariborz Jahanian32019832010-07-23 19:11:11 +00002166 WriteReferencedSelectorsPool(SemaRef);
Douglas Gregor37e26842009-04-21 23:56:24 +00002167 WriteIdentifierTable(PP);
Douglas Gregor8f5dc7f2009-04-25 18:35:21 +00002168
Sebastian Redl1476ed42010-07-16 16:36:56 +00002169 WriteTypeDeclOffsets();
Douglas Gregorad1de002009-04-18 05:55:16 +00002170
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002171 // Write the record containing external, unnamed definitions.
Douglas Gregorfdd01722009-04-14 00:24:19 +00002172 if (!ExternalDefinitions.empty())
Douglas Gregorc9490c02009-04-16 22:23:12 +00002173 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor4c0e86b2009-04-22 22:02:47 +00002174
2175 // Write the record containing tentative definitions.
2176 if (!TentativeDefinitions.empty())
2177 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor14c22f22009-04-22 22:18:58 +00002178
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002179 // Write the record containing unused file scoped decls.
2180 if (!UnusedFileScopedDecls.empty())
2181 Stream.EmitRecord(pch::UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002182
Argyrios Kyrtzidis72b90572010-08-05 09:48:08 +00002183 // Write the record containing weak undeclared identifiers.
2184 if (!WeakUndeclaredIdentifiers.empty())
2185 Stream.EmitRecord(pch::WEAK_UNDECLARED_IDENTIFIERS,
2186 WeakUndeclaredIdentifiers);
2187
Douglas Gregor14c22f22009-04-22 22:18:58 +00002188 // Write the record containing locally-scoped external definitions.
2189 if (!LocallyScopedExternalDecls.empty())
Mike Stump1eb44332009-09-09 15:08:12 +00002190 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
Douglas Gregor14c22f22009-04-22 22:18:58 +00002191 LocallyScopedExternalDecls);
Douglas Gregorb81c1702009-04-27 20:06:05 +00002192
2193 // Write the record containing ext_vector type names.
2194 if (!ExtVectorDecls.empty())
2195 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
Mike Stump1eb44332009-09-09 15:08:12 +00002196
Argyrios Kyrtzidisd455add2010-07-06 15:37:04 +00002197 // Write the record containing VTable uses information.
2198 if (!VTableUses.empty())
2199 Stream.EmitRecord(pch::VTABLE_USES, VTableUses);
2200
2201 // Write the record containing dynamic classes declarations.
2202 if (!DynamicClasses.empty())
2203 Stream.EmitRecord(pch::DYNAMIC_CLASSES, DynamicClasses);
2204
Argyrios Kyrtzidis0e036382010-08-05 09:48:16 +00002205 // Write the record containing pending implicit instantiations.
2206 if (!PendingImplicitInstantiations.empty())
2207 Stream.EmitRecord(pch::PENDING_IMPLICIT_INSTANTIATIONS,
2208 PendingImplicitInstantiations);
2209
Argyrios Kyrtzidis76c38d32010-08-02 07:14:54 +00002210 // Write the record containing declaration references of Sema.
2211 if (!SemaDeclRefs.empty())
2212 Stream.EmitRecord(pch::SEMA_DECL_REFS, SemaDeclRefs);
2213
Douglas Gregor3e1af842009-04-17 22:13:46 +00002214 // Some simple statistics
Douglas Gregorad1de002009-04-18 05:55:16 +00002215 Record.clear();
Douglas Gregor3e1af842009-04-17 22:13:46 +00002216 Record.push_back(NumStatements);
Douglas Gregor37e26842009-04-21 23:56:24 +00002217 Record.push_back(NumMacros);
Douglas Gregor25123082009-04-22 22:34:57 +00002218 Record.push_back(NumLexicalDeclContexts);
2219 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor3e1af842009-04-17 22:13:46 +00002220 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc9490c02009-04-16 22:23:12 +00002221 Stream.ExitBlock();
Douglas Gregor2cf26342009-04-09 22:27:44 +00002222}
2223
Sebastian Redla4232eb2010-08-18 23:56:21 +00002224void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
Sebastian Redl30c514c2010-07-14 23:45:08 +00002225 const char *isysroot) {
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002226 using namespace llvm;
2227
Sebastian Redlffaab3e2010-07-30 00:29:29 +00002228 FirstDeclID += Chain->getTotalNumDecls();
2229 FirstTypeID += Chain->getTotalNumTypes();
2230 FirstIdentID += Chain->getTotalNumIdentifiers();
Sebastian Redle58aa892010-08-04 18:21:41 +00002231 FirstSelectorID += Chain->getTotalNumSelectors();
Sebastian Redlffaab3e2010-07-30 00:29:29 +00002232 NextDeclID = FirstDeclID;
2233 NextTypeID = FirstTypeID;
2234 NextIdentID = FirstIdentID;
Sebastian Redle58aa892010-08-04 18:21:41 +00002235 NextSelectorID = FirstSelectorID;
Sebastian Redlffaab3e2010-07-30 00:29:29 +00002236
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002237 ASTContext &Context = SemaRef.Context;
2238 Preprocessor &PP = SemaRef.PP;
Sebastian Redl1476ed42010-07-16 16:36:56 +00002239
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002240 RecordData Record;
Sebastian Redlf29f0a22010-08-18 23:57:22 +00002241 Stream.EnterSubblock(pch::AST_BLOCK_ID, 5);
Sebastian Redl30c514c2010-07-14 23:45:08 +00002242 WriteMetadata(Context, isysroot);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002243 if (StatCalls && !isysroot)
2244 WriteStatCache(*StatCalls);
2245 // FIXME: Source manager block should only write new stuff, which could be
2246 // done by tracking the largest ID in the chain
2247 WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002248
2249 // The special types are in the chained PCH.
2250
2251 // We don't start with the translation unit, but with its decls that
Sebastian Redl3c7f4132010-08-18 23:57:06 +00002252 // don't come from the chained PCH.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002253 const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
Sebastian Redld692af72010-07-27 18:24:41 +00002254 llvm::SmallVector<pch::DeclID, 64> NewGlobalDecls;
Sebastian Redl681d7232010-07-27 00:17:23 +00002255 for (DeclContext::decl_iterator I = TU->noload_decls_begin(),
2256 E = TU->noload_decls_end();
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002257 I != E; ++I) {
Sebastian Redld692af72010-07-27 18:24:41 +00002258 if ((*I)->getPCHLevel() == 0)
2259 NewGlobalDecls.push_back(GetDeclRef(*I));
Sebastian Redl0b17c612010-08-13 00:28:03 +00002260 else if ((*I)->isChangedSinceDeserialization())
2261 (void)GetDeclRef(*I); // Make sure it's written, but don't record it.
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002262 }
Sebastian Redl681d7232010-07-27 00:17:23 +00002263 // We also need to write a lexical updates block for the TU.
Sebastian Redld692af72010-07-27 18:24:41 +00002264 llvm::BitCodeAbbrev *Abv = new llvm::BitCodeAbbrev();
2265 Abv->Add(llvm::BitCodeAbbrevOp(pch::TU_UPDATE_LEXICAL));
2266 Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
2267 unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
2268 Record.clear();
2269 Record.push_back(pch::TU_UPDATE_LEXICAL);
2270 Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
2271 reinterpret_cast<const char*>(NewGlobalDecls.data()),
2272 NewGlobalDecls.size() * sizeof(pch::DeclID));
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002273
Sebastian Redl083abdf2010-07-27 23:01:28 +00002274 // Build a record containing all of the new tentative definitions in this
2275 // file, in TentativeDefinitions order.
2276 RecordData TentativeDefinitions;
2277 for (unsigned i = 0, e = SemaRef.TentativeDefinitions.size(); i != e; ++i) {
2278 if (SemaRef.TentativeDefinitions[i]->getPCHLevel() == 0)
2279 AddDeclRef(SemaRef.TentativeDefinitions[i], TentativeDefinitions);
2280 }
2281
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002282 // Build a record containing all of the file scoped decls in this file.
2283 RecordData UnusedFileScopedDecls;
2284 for (unsigned i=0, e = SemaRef.UnusedFileScopedDecls.size(); i !=e; ++i) {
2285 if (SemaRef.UnusedFileScopedDecls[i]->getPCHLevel() == 0)
2286 AddDeclRef(SemaRef.UnusedFileScopedDecls[i], UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002287 }
2288
Sebastian Redl40566802010-08-05 18:21:25 +00002289 // We write the entire table, overwriting the tables from the chain.
2290 RecordData WeakUndeclaredIdentifiers;
2291 if (!SemaRef.WeakUndeclaredIdentifiers.empty()) {
2292 WeakUndeclaredIdentifiers.push_back(
2293 SemaRef.WeakUndeclaredIdentifiers.size());
2294 for (llvm::DenseMap<IdentifierInfo*,Sema::WeakInfo>::iterator
2295 I = SemaRef.WeakUndeclaredIdentifiers.begin(),
2296 E = SemaRef.WeakUndeclaredIdentifiers.end(); I != E; ++I) {
2297 AddIdentifierRef(I->first, WeakUndeclaredIdentifiers);
2298 AddIdentifierRef(I->second.getAlias(), WeakUndeclaredIdentifiers);
2299 AddSourceLocation(I->second.getLocation(), WeakUndeclaredIdentifiers);
2300 WeakUndeclaredIdentifiers.push_back(I->second.getUsed());
2301 }
2302 }
2303
Sebastian Redl083abdf2010-07-27 23:01:28 +00002304 // Build a record containing all of the locally-scoped external
2305 // declarations in this header file. Generally, this record will be
2306 // empty.
2307 RecordData LocallyScopedExternalDecls;
Sebastian Redl3397c552010-08-18 23:56:27 +00002308 // FIXME: This is filling in the AST file in densemap order which is
Sebastian Redl083abdf2010-07-27 23:01:28 +00002309 // nondeterminstic!
2310 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2311 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2312 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2313 TD != TDEnd; ++TD) {
2314 if (TD->second->getPCHLevel() == 0)
2315 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2316 }
2317
2318 // Build a record containing all of the ext_vector declarations.
2319 RecordData ExtVectorDecls;
2320 for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) {
2321 if (SemaRef.ExtVectorDecls[I]->getPCHLevel() == 0)
2322 AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls);
2323 }
2324
Sebastian Redl40566802010-08-05 18:21:25 +00002325 // Build a record containing all of the VTable uses information.
2326 // We write everything here, because it's too hard to determine whether
2327 // a use is new to this part.
2328 RecordData VTableUses;
2329 if (!SemaRef.VTableUses.empty()) {
2330 VTableUses.push_back(SemaRef.VTableUses.size());
2331 for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
2332 AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
2333 AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
2334 VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
2335 }
2336 }
2337
2338 // Build a record containing all of dynamic classes declarations.
2339 RecordData DynamicClasses;
2340 for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I)
2341 if (SemaRef.DynamicClasses[I]->getPCHLevel() == 0)
2342 AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses);
2343
2344 // Build a record containing all of pending implicit instantiations.
2345 RecordData PendingImplicitInstantiations;
2346 for (std::deque<Sema::PendingImplicitInstantiation>::iterator
2347 I = SemaRef.PendingImplicitInstantiations.begin(),
2348 N = SemaRef.PendingImplicitInstantiations.end(); I != N; ++I) {
2349 if (I->first->getPCHLevel() == 0) {
2350 AddDeclRef(I->first, PendingImplicitInstantiations);
2351 AddSourceLocation(I->second, PendingImplicitInstantiations);
2352 }
2353 }
2354 assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
2355 "There are local ones at end of translation unit!");
2356
2357 // Build a record containing some declaration references.
2358 // It's not worth the effort to avoid duplication here.
2359 RecordData SemaDeclRefs;
2360 if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
2361 AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
2362 AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
2363 }
2364
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002365 Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3);
2366 WriteDeclsBlockAbbrevs();
2367 while (!DeclTypesToEmit.empty()) {
2368 DeclOrType DOT = DeclTypesToEmit.front();
2369 DeclTypesToEmit.pop();
2370 if (DOT.isType())
2371 WriteType(DOT.getType());
2372 else
2373 WriteDecl(Context, DOT.getDecl());
2374 }
2375 Stream.ExitBlock();
2376
Sebastian Redl083abdf2010-07-27 23:01:28 +00002377 WritePreprocessor(PP);
Sebastian Redla68340f2010-08-04 22:21:29 +00002378 WriteSelectors(SemaRef);
2379 WriteReferencedSelectorsPool(SemaRef);
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002380 WriteIdentifierTable(PP);
Sebastian Redl1476ed42010-07-16 16:36:56 +00002381 WriteTypeDeclOffsets();
Sebastian Redl083abdf2010-07-27 23:01:28 +00002382
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002383 /// Build a record containing first declarations from a chained PCH and the
Sebastian Redl3397c552010-08-18 23:56:27 +00002384 /// most recent declarations in this AST that they point to.
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00002385 RecordData FirstLatestDeclIDs;
2386 for (FirstLatestDeclMap::iterator
2387 I = FirstLatestDecls.begin(), E = FirstLatestDecls.end(); I != E; ++I) {
2388 assert(I->first->getPCHLevel() > I->second->getPCHLevel() &&
2389 "Expected first & second to be in different PCHs");
2390 AddDeclRef(I->first, FirstLatestDeclIDs);
2391 AddDeclRef(I->second, FirstLatestDeclIDs);
2392 }
2393 if (!FirstLatestDeclIDs.empty())
2394 Stream.EmitRecord(pch::REDECLS_UPDATE_LATEST, FirstLatestDeclIDs);
2395
Sebastian Redl083abdf2010-07-27 23:01:28 +00002396 // Write the record containing external, unnamed definitions.
2397 if (!ExternalDefinitions.empty())
2398 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
2399
2400 // Write the record containing tentative definitions.
2401 if (!TentativeDefinitions.empty())
2402 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
2403
Argyrios Kyrtzidis49b96d12010-08-13 18:42:17 +00002404 // Write the record containing unused file scoped decls.
2405 if (!UnusedFileScopedDecls.empty())
2406 Stream.EmitRecord(pch::UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002407
Sebastian Redl40566802010-08-05 18:21:25 +00002408 // Write the record containing weak undeclared identifiers.
2409 if (!WeakUndeclaredIdentifiers.empty())
2410 Stream.EmitRecord(pch::WEAK_UNDECLARED_IDENTIFIERS,
2411 WeakUndeclaredIdentifiers);
2412
Sebastian Redl083abdf2010-07-27 23:01:28 +00002413 // Write the record containing locally-scoped external definitions.
2414 if (!LocallyScopedExternalDecls.empty())
2415 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
2416 LocallyScopedExternalDecls);
2417
2418 // Write the record containing ext_vector type names.
2419 if (!ExtVectorDecls.empty())
2420 Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls);
2421
Sebastian Redl40566802010-08-05 18:21:25 +00002422 // Write the record containing VTable uses information.
2423 if (!VTableUses.empty())
2424 Stream.EmitRecord(pch::VTABLE_USES, VTableUses);
2425
2426 // Write the record containing dynamic classes declarations.
2427 if (!DynamicClasses.empty())
2428 Stream.EmitRecord(pch::DYNAMIC_CLASSES, DynamicClasses);
2429
2430 // Write the record containing pending implicit instantiations.
2431 if (!PendingImplicitInstantiations.empty())
2432 Stream.EmitRecord(pch::PENDING_IMPLICIT_INSTANTIATIONS,
2433 PendingImplicitInstantiations);
2434
2435 // Write the record containing declaration references of Sema.
2436 if (!SemaDeclRefs.empty())
2437 Stream.EmitRecord(pch::SEMA_DECL_REFS, SemaDeclRefs);
Sebastian Redl083abdf2010-07-27 23:01:28 +00002438
2439 Record.clear();
2440 Record.push_back(NumStatements);
2441 Record.push_back(NumMacros);
2442 Record.push_back(NumLexicalDeclContexts);
2443 Record.push_back(NumVisibleDeclContexts);
Sebastian Redl0b17c612010-08-13 00:28:03 +00002444 WriteDeclUpdateBlock();
Sebastian Redl083abdf2010-07-27 23:01:28 +00002445 Stream.EmitRecord(pch::STATISTICS, Record);
Sebastian Redl1dc13a12010-07-12 22:02:52 +00002446 Stream.ExitBlock();
2447}
2448
Sebastian Redla4232eb2010-08-18 23:56:21 +00002449void ASTWriter::WriteDeclUpdateBlock() {
Sebastian Redl0b17c612010-08-13 00:28:03 +00002450 if (ReplacedDecls.empty())
2451 return;
2452
2453 RecordData Record;
2454 for (llvm::SmallVector<std::pair<pch::DeclID, uint64_t>, 16>::iterator
2455 I = ReplacedDecls.begin(), E = ReplacedDecls.end(); I != E; ++I) {
2456 Record.push_back(I->first);
2457 Record.push_back(I->second);
2458 }
2459 Stream.EmitRecord(pch::DECL_REPLACEMENTS, Record);
2460}
2461
Sebastian Redla4232eb2010-08-18 23:56:21 +00002462void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002463 Record.push_back(Loc.getRawEncoding());
2464}
2465
Sebastian Redla4232eb2010-08-18 23:56:21 +00002466void ASTWriter::AddSourceRange(SourceRange Range, RecordData &Record) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002467 AddSourceLocation(Range.getBegin(), Record);
2468 AddSourceLocation(Range.getEnd(), Record);
2469}
2470
Sebastian Redla4232eb2010-08-18 23:56:21 +00002471void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002472 Record.push_back(Value.getBitWidth());
2473 unsigned N = Value.getNumWords();
2474 const uint64_t* Words = Value.getRawData();
2475 for (unsigned I = 0; I != N; ++I)
2476 Record.push_back(Words[I]);
2477}
2478
Sebastian Redla4232eb2010-08-18 23:56:21 +00002479void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
Douglas Gregor0a2b45e2009-04-13 18:14:40 +00002480 Record.push_back(Value.isUnsigned());
2481 AddAPInt(Value, Record);
2482}
2483
Sebastian Redla4232eb2010-08-18 23:56:21 +00002484void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
Douglas Gregor17fc2232009-04-14 21:55:33 +00002485 AddAPInt(Value.bitcastToAPInt(), Record);
2486}
2487
Sebastian Redla4232eb2010-08-18 23:56:21 +00002488void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002489 Record.push_back(getIdentifierRef(II));
2490}
2491
Sebastian Redla4232eb2010-08-18 23:56:21 +00002492pch::IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
Douglas Gregor2deaea32009-04-22 18:49:13 +00002493 if (II == 0)
2494 return 0;
Douglas Gregorafaf3082009-04-11 00:14:32 +00002495
2496 pch::IdentID &ID = IdentifierIDs[II];
2497 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002498 ID = NextIdentID++;
Douglas Gregor2deaea32009-04-22 18:49:13 +00002499 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002500}
2501
Sebastian Redla4232eb2010-08-18 23:56:21 +00002502pch::IdentID ASTWriter::getMacroDefinitionID(MacroDefinition *MD) {
Douglas Gregor6a5a23f2010-03-19 21:51:54 +00002503 if (MD == 0)
2504 return 0;
2505
2506 pch::IdentID &ID = MacroDefinitions[MD];
2507 if (ID == 0)
2508 ID = MacroDefinitions.size();
2509 return ID;
2510}
2511
Sebastian Redla4232eb2010-08-18 23:56:21 +00002512void ASTWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
Sebastian Redl5d050072010-08-04 17:20:04 +00002513 Record.push_back(getSelectorRef(SelRef));
2514}
2515
Sebastian Redla4232eb2010-08-18 23:56:21 +00002516pch::SelectorID ASTWriter::getSelectorRef(Selector Sel) {
Sebastian Redl5d050072010-08-04 17:20:04 +00002517 if (Sel.getAsOpaquePtr() == 0) {
2518 return 0;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002519 }
2520
Sebastian Redl5d050072010-08-04 17:20:04 +00002521 pch::SelectorID &SID = SelectorIDs[Sel];
Sebastian Redle58aa892010-08-04 18:21:41 +00002522 if (SID == 0 && Chain) {
2523 // This might trigger a ReadSelector callback, which will set the ID for
2524 // this selector.
2525 Chain->LoadSelector(Sel);
2526 }
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002527 if (SID == 0) {
Sebastian Redle58aa892010-08-04 18:21:41 +00002528 SID = NextSelectorID++;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002529 }
Sebastian Redl5d050072010-08-04 17:20:04 +00002530 return SID;
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002531}
2532
Sebastian Redla4232eb2010-08-18 23:56:21 +00002533void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordData &Record) {
Chris Lattnerd2598362010-05-10 00:25:06 +00002534 AddDeclRef(Temp->getDestructor(), Record);
2535}
2536
Sebastian Redla4232eb2010-08-18 23:56:21 +00002537void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002538 const TemplateArgumentLocInfo &Arg,
2539 RecordData &Record) {
2540 switch (Kind) {
John McCall833ca992009-10-29 08:12:44 +00002541 case TemplateArgument::Expression:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002542 AddStmt(Arg.getAsExpr());
John McCall833ca992009-10-29 08:12:44 +00002543 break;
2544 case TemplateArgument::Type:
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002545 AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
John McCall833ca992009-10-29 08:12:44 +00002546 break;
Douglas Gregor788cd062009-11-11 01:00:40 +00002547 case TemplateArgument::Template:
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002548 AddSourceRange(Arg.getTemplateQualifierRange(), Record);
2549 AddSourceLocation(Arg.getTemplateNameLoc(), Record);
Douglas Gregor788cd062009-11-11 01:00:40 +00002550 break;
John McCall833ca992009-10-29 08:12:44 +00002551 case TemplateArgument::Null:
2552 case TemplateArgument::Integral:
2553 case TemplateArgument::Declaration:
2554 case TemplateArgument::Pack:
2555 break;
2556 }
2557}
2558
Sebastian Redla4232eb2010-08-18 23:56:21 +00002559void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002560 RecordData &Record) {
2561 AddTemplateArgument(Arg.getArgument(), Record);
Argyrios Kyrtzidis17cfded2010-06-28 09:31:42 +00002562
2563 if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
2564 bool InfoHasSameExpr
2565 = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
2566 Record.push_back(InfoHasSameExpr);
2567 if (InfoHasSameExpr)
2568 return; // Avoid storing the same expr twice.
2569 }
Argyrios Kyrtzidis44f8c372010-06-22 09:54:59 +00002570 AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
2571 Record);
2572}
2573
Sebastian Redla4232eb2010-08-18 23:56:21 +00002574void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) {
John McCalla93c9342009-12-07 02:54:59 +00002575 if (TInfo == 0) {
John McCalla1ee0c52009-10-16 21:56:05 +00002576 AddTypeRef(QualType(), Record);
2577 return;
2578 }
2579
John McCalla93c9342009-12-07 02:54:59 +00002580 AddTypeRef(TInfo->getType(), Record);
John McCalla1ee0c52009-10-16 21:56:05 +00002581 TypeLocWriter TLW(*this, Record);
John McCalla93c9342009-12-07 02:54:59 +00002582 for (TypeLoc TL = TInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002583 TLW.Visit(TL);
John McCalla1ee0c52009-10-16 21:56:05 +00002584}
2585
Sebastian Redla4232eb2010-08-18 23:56:21 +00002586void ASTWriter::AddTypeRef(QualType T, RecordData &Record) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002587 if (T.isNull()) {
2588 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2589 return;
2590 }
2591
Douglas Gregora4923eb2009-11-16 21:35:15 +00002592 unsigned FastQuals = T.getLocalFastQualifiers();
John McCall0953e762009-09-24 19:53:00 +00002593 T.removeFastQualifiers();
2594
Douglas Gregora4923eb2009-11-16 21:35:15 +00002595 if (T.hasLocalNonFastQualifiers()) {
John McCall0953e762009-09-24 19:53:00 +00002596 pch::TypeID &ID = TypeIDs[T];
2597 if (ID == 0) {
2598 // We haven't seen these qualifiers applied to this type before.
2599 // Assign it a new ID. This is the only time we enqueue a
2600 // qualified type, and it has no CV qualifiers.
2601 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002602 DeclTypesToEmit.push(T);
John McCall0953e762009-09-24 19:53:00 +00002603 }
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002604
John McCall0953e762009-09-24 19:53:00 +00002605 // Encode the type qualifiers in the type reference.
2606 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
2607 return;
2608 }
2609
Douglas Gregora4923eb2009-11-16 21:35:15 +00002610 assert(!T.hasLocalQualifiers());
Kovarththanan Rajaratnam11a18f12010-03-14 07:06:50 +00002611
Douglas Gregor2cf26342009-04-09 22:27:44 +00002612 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregore1d918e2009-04-10 23:10:45 +00002613 pch::TypeID ID = 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002614 switch (BT->getKind()) {
2615 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2616 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2617 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2618 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2619 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2620 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2621 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2622 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002623 case BuiltinType::UInt128: ID = pch::PREDEF_TYPE_UINT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002624 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2625 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2626 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2627 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2628 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2629 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2630 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002631 case BuiltinType::Int128: ID = pch::PREDEF_TYPE_INT128_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002632 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2633 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2634 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002635 case BuiltinType::NullPtr: ID = pch::PREDEF_TYPE_NULLPTR_ID; break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002636 case BuiltinType::Char16: ID = pch::PREDEF_TYPE_CHAR16_ID; break;
2637 case BuiltinType::Char32: ID = pch::PREDEF_TYPE_CHAR32_ID; break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002638 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2639 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
Steve Naroffde2e22d2009-07-15 18:40:39 +00002640 case BuiltinType::ObjCId: ID = pch::PREDEF_TYPE_OBJC_ID; break;
2641 case BuiltinType::ObjCClass: ID = pch::PREDEF_TYPE_OBJC_CLASS; break;
Fariborz Jahanian13dcd002009-11-21 19:53:08 +00002642 case BuiltinType::ObjCSel: ID = pch::PREDEF_TYPE_OBJC_SEL; break;
Anders Carlssone89d1592009-06-26 18:41:36 +00002643 case BuiltinType::UndeducedAuto:
2644 assert(0 && "Should not see undeduced auto here");
2645 break;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002646 }
2647
John McCall0953e762009-09-24 19:53:00 +00002648 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002649 return;
2650 }
2651
John McCall0953e762009-09-24 19:53:00 +00002652 pch::TypeID &ID = TypeIDs[T];
Douglas Gregor366809a2009-04-26 03:49:13 +00002653 if (ID == 0) {
2654 // We haven't seen this type before. Assign it a new ID and put it
John McCall0953e762009-09-24 19:53:00 +00002655 // into the queue of types to emit.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002656 ID = NextTypeID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002657 DeclTypesToEmit.push(T);
Douglas Gregor366809a2009-04-26 03:49:13 +00002658 }
Douglas Gregor2cf26342009-04-09 22:27:44 +00002659
2660 // Encode the type qualifiers in the type reference.
John McCall0953e762009-09-24 19:53:00 +00002661 Record.push_back((ID << Qualifiers::FastWidth) | FastQuals);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002662}
2663
Sebastian Redla4232eb2010-08-18 23:56:21 +00002664void ASTWriter::AddDeclRef(const Decl *D, RecordData &Record) {
Sebastian Redl681d7232010-07-27 00:17:23 +00002665 Record.push_back(GetDeclRef(D));
2666}
2667
Sebastian Redla4232eb2010-08-18 23:56:21 +00002668pch::DeclID ASTWriter::GetDeclRef(const Decl *D) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002669 if (D == 0) {
Sebastian Redl681d7232010-07-27 00:17:23 +00002670 return 0;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002671 }
2672
Douglas Gregor8038d512009-04-10 17:25:41 +00002673 pch::DeclID &ID = DeclIDs[D];
Mike Stump1eb44332009-09-09 15:08:12 +00002674 if (ID == 0) {
Douglas Gregor2cf26342009-04-09 22:27:44 +00002675 // We haven't seen this declaration before. Give it a new ID and
2676 // enqueue it in the list of declarations to emit.
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002677 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00002678 DeclTypesToEmit.push(const_cast<Decl *>(D));
Sebastian Redl0b17c612010-08-13 00:28:03 +00002679 } else if (ID < FirstDeclID && D->isChangedSinceDeserialization()) {
2680 // We don't add it to the replacement collection here, because we don't
2681 // have the offset yet.
2682 DeclTypesToEmit.push(const_cast<Decl *>(D));
2683 // Reset the flag, so that we don't add this decl multiple times.
2684 const_cast<Decl *>(D)->setChangedSinceDeserialization(false);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002685 }
2686
Sebastian Redl681d7232010-07-27 00:17:23 +00002687 return ID;
Douglas Gregor2cf26342009-04-09 22:27:44 +00002688}
2689
Sebastian Redla4232eb2010-08-18 23:56:21 +00002690pch::DeclID ASTWriter::getDeclID(const Decl *D) {
Douglas Gregor3251ceb2009-04-20 20:36:09 +00002691 if (D == 0)
2692 return 0;
2693
2694 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2695 return DeclIDs[D];
2696}
2697
Sebastian Redla4232eb2010-08-18 23:56:21 +00002698void ASTWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
Chris Lattnerea5ce472009-04-27 07:35:58 +00002699 // FIXME: Emit a stable enum for NameKind. 0 = Identifier etc.
Douglas Gregor2cf26342009-04-09 22:27:44 +00002700 Record.push_back(Name.getNameKind());
2701 switch (Name.getNameKind()) {
2702 case DeclarationName::Identifier:
2703 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2704 break;
2705
2706 case DeclarationName::ObjCZeroArgSelector:
2707 case DeclarationName::ObjCOneArgSelector:
2708 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff90cd1bb2009-04-23 10:39:46 +00002709 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregor2cf26342009-04-09 22:27:44 +00002710 break;
2711
2712 case DeclarationName::CXXConstructorName:
2713 case DeclarationName::CXXDestructorName:
2714 case DeclarationName::CXXConversionFunctionName:
2715 AddTypeRef(Name.getCXXNameType(), Record);
2716 break;
2717
2718 case DeclarationName::CXXOperatorName:
2719 Record.push_back(Name.getCXXOverloadedOperator());
2720 break;
2721
Sean Hunt3e518bd2009-11-29 07:34:05 +00002722 case DeclarationName::CXXLiteralOperatorName:
2723 AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
2724 break;
2725
Douglas Gregor2cf26342009-04-09 22:27:44 +00002726 case DeclarationName::CXXUsingDirective:
2727 // No extra data to emit
2728 break;
2729 }
2730}
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002731
Sebastian Redla4232eb2010-08-18 23:56:21 +00002732void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
Chris Lattner6ad9ac02010-05-07 21:43:38 +00002733 RecordData &Record) {
2734 // Nested name specifiers usually aren't too long. I think that 8 would
2735 // typically accomodate the vast majority.
2736 llvm::SmallVector<NestedNameSpecifier *, 8> NestedNames;
2737
2738 // Push each of the NNS's onto a stack for serialization in reverse order.
2739 while (NNS) {
2740 NestedNames.push_back(NNS);
2741 NNS = NNS->getPrefix();
2742 }
2743
2744 Record.push_back(NestedNames.size());
2745 while(!NestedNames.empty()) {
2746 NNS = NestedNames.pop_back_val();
2747 NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
2748 Record.push_back(Kind);
2749 switch (Kind) {
2750 case NestedNameSpecifier::Identifier:
2751 AddIdentifierRef(NNS->getAsIdentifier(), Record);
2752 break;
2753
2754 case NestedNameSpecifier::Namespace:
2755 AddDeclRef(NNS->getAsNamespace(), Record);
2756 break;
2757
2758 case NestedNameSpecifier::TypeSpec:
2759 case NestedNameSpecifier::TypeSpecWithTemplate:
2760 AddTypeRef(QualType(NNS->getAsType(), 0), Record);
2761 Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
2762 break;
2763
2764 case NestedNameSpecifier::Global:
2765 // Don't need to write an associated value.
2766 break;
2767 }
2768 }
2769}
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00002770
Sebastian Redla4232eb2010-08-18 23:56:21 +00002771void ASTWriter::AddTemplateName(TemplateName Name, RecordData &Record) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00002772 TemplateName::NameKind Kind = Name.getKind();
2773 Record.push_back(Kind);
2774 switch (Kind) {
2775 case TemplateName::Template:
2776 AddDeclRef(Name.getAsTemplateDecl(), Record);
2777 break;
2778
2779 case TemplateName::OverloadedTemplate: {
2780 OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
2781 Record.push_back(OvT->size());
2782 for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end();
2783 I != E; ++I)
2784 AddDeclRef(*I, Record);
2785 break;
2786 }
2787
2788 case TemplateName::QualifiedTemplate: {
2789 QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
2790 AddNestedNameSpecifier(QualT->getQualifier(), Record);
2791 Record.push_back(QualT->hasTemplateKeyword());
2792 AddDeclRef(QualT->getTemplateDecl(), Record);
2793 break;
2794 }
2795
2796 case TemplateName::DependentTemplate: {
2797 DependentTemplateName *DepT = Name.getAsDependentTemplateName();
2798 AddNestedNameSpecifier(DepT->getQualifier(), Record);
2799 Record.push_back(DepT->isIdentifier());
2800 if (DepT->isIdentifier())
2801 AddIdentifierRef(DepT->getIdentifier(), Record);
2802 else
2803 Record.push_back(DepT->getOperator());
2804 break;
2805 }
2806 }
2807}
2808
Sebastian Redla4232eb2010-08-18 23:56:21 +00002809void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +00002810 RecordData &Record) {
2811 Record.push_back(Arg.getKind());
2812 switch (Arg.getKind()) {
2813 case TemplateArgument::Null:
2814 break;
2815 case TemplateArgument::Type:
2816 AddTypeRef(Arg.getAsType(), Record);
2817 break;
2818 case TemplateArgument::Declaration:
2819 AddDeclRef(Arg.getAsDecl(), Record);
2820 break;
2821 case TemplateArgument::Integral:
2822 AddAPSInt(*Arg.getAsIntegral(), Record);
2823 AddTypeRef(Arg.getIntegralType(), Record);
2824 break;
2825 case TemplateArgument::Template:
2826 AddTemplateName(Arg.getAsTemplate(), Record);
2827 break;
2828 case TemplateArgument::Expression:
2829 AddStmt(Arg.getAsExpr());
2830 break;
2831 case TemplateArgument::Pack:
2832 Record.push_back(Arg.pack_size());
2833 for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end();
2834 I != E; ++I)
2835 AddTemplateArgument(*I, Record);
2836 break;
2837 }
2838}
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00002839
2840void
Sebastian Redla4232eb2010-08-18 23:56:21 +00002841ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00002842 RecordData &Record) {
2843 assert(TemplateParams && "No TemplateParams!");
2844 AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
2845 AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
2846 AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
2847 Record.push_back(TemplateParams->size());
2848 for (TemplateParameterList::const_iterator
2849 P = TemplateParams->begin(), PEnd = TemplateParams->end();
2850 P != PEnd; ++P)
2851 AddDeclRef(*P, Record);
2852}
2853
2854/// \brief Emit a template argument list.
2855void
Sebastian Redla4232eb2010-08-18 23:56:21 +00002856ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00002857 RecordData &Record) {
2858 assert(TemplateArgs && "No TemplateArgs!");
2859 Record.push_back(TemplateArgs->flat_size());
2860 for (int i=0, e = TemplateArgs->flat_size(); i != e; ++i)
2861 AddTemplateArgument(TemplateArgs->get(i), Record);
2862}
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00002863
2864
2865void
Sebastian Redla4232eb2010-08-18 23:56:21 +00002866ASTWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordData &Record) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +00002867 Record.push_back(Set.size());
2868 for (UnresolvedSetImpl::const_iterator
2869 I = Set.begin(), E = Set.end(); I != E; ++I) {
2870 AddDeclRef(I.getDecl(), Record);
2871 Record.push_back(I.getAccess());
2872 }
2873}
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00002874
Sebastian Redla4232eb2010-08-18 23:56:21 +00002875void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00002876 RecordData &Record) {
2877 Record.push_back(Base.isVirtual());
2878 Record.push_back(Base.isBaseOfClass());
2879 Record.push_back(Base.getAccessSpecifierAsWritten());
Nick Lewycky56062202010-07-26 16:56:01 +00002880 AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
Argyrios Kyrtzidis0745d0a2010-07-02 23:30:27 +00002881 AddSourceRange(Base.getSourceRange(), Record);
2882}
Sebastian Redl30c514c2010-07-14 23:45:08 +00002883
Sebastian Redla4232eb2010-08-18 23:56:21 +00002884void ASTWriter::AddCXXBaseOrMemberInitializers(
Argyrios Kyrtzidis8e706f42010-08-09 10:54:12 +00002885 const CXXBaseOrMemberInitializer * const *BaseOrMembers,
2886 unsigned NumBaseOrMembers, RecordData &Record) {
2887 Record.push_back(NumBaseOrMembers);
2888 for (unsigned i=0; i != NumBaseOrMembers; ++i) {
2889 const CXXBaseOrMemberInitializer *Init = BaseOrMembers[i];
2890
2891 Record.push_back(Init->isBaseInitializer());
2892 if (Init->isBaseInitializer()) {
2893 AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
2894 Record.push_back(Init->isBaseVirtual());
2895 } else {
2896 AddDeclRef(Init->getMember(), Record);
2897 }
2898 AddSourceLocation(Init->getMemberLocation(), Record);
2899 AddStmt(Init->getInit());
2900 AddDeclRef(Init->getAnonUnionMember(), Record);
2901 AddSourceLocation(Init->getLParenLoc(), Record);
2902 AddSourceLocation(Init->getRParenLoc(), Record);
2903 Record.push_back(Init->isWritten());
2904 if (Init->isWritten()) {
2905 Record.push_back(Init->getSourceOrder());
2906 } else {
2907 Record.push_back(Init->getNumArrayIndices());
2908 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
2909 AddDeclRef(Init->getArrayIndex(i), Record);
2910 }
2911 }
2912}
2913
Sebastian Redlc43b54c2010-08-18 23:56:43 +00002914void ASTWriter::SetReader(ASTReader *Reader) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +00002915 assert(Reader && "Cannot remove chain");
2916 assert(FirstDeclID == NextDeclID &&
2917 FirstTypeID == NextTypeID &&
2918 FirstIdentID == NextIdentID &&
Sebastian Redle58aa892010-08-04 18:21:41 +00002919 FirstSelectorID == NextSelectorID &&
Sebastian Redlffaab3e2010-07-30 00:29:29 +00002920 "Setting chain after writing has started.");
2921 Chain = Reader;
2922}
2923
Sebastian Redla4232eb2010-08-18 23:56:21 +00002924void ASTWriter::IdentifierRead(pch::IdentID ID, IdentifierInfo *II) {
Sebastian Redlf2f0f032010-07-23 23:49:55 +00002925 IdentifierIDs[II] = ID;
2926}
2927
Sebastian Redla4232eb2010-08-18 23:56:21 +00002928void ASTWriter::TypeRead(pch::TypeID ID, QualType T) {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002929 TypeIDs[T] = ID;
Sebastian Redl30c514c2010-07-14 23:45:08 +00002930}
2931
Sebastian Redla4232eb2010-08-18 23:56:21 +00002932void ASTWriter::DeclRead(pch::DeclID ID, const Decl *D) {
Sebastian Redl1476ed42010-07-16 16:36:56 +00002933 DeclIDs[D] = ID;
Sebastian Redl30c514c2010-07-14 23:45:08 +00002934}
Sebastian Redl5d050072010-08-04 17:20:04 +00002935
Sebastian Redla4232eb2010-08-18 23:56:21 +00002936void ASTWriter::SelectorRead(pch::SelectorID ID, Selector S) {
Sebastian Redl5d050072010-08-04 17:20:04 +00002937 SelectorIDs[S] = ID;
2938}