blob: d233cfb44117951d58493c76e0dcf6b1adfd4a06 [file] [log] [blame]
Douglas Gregorc34897d2009-04-09 22:27:44 +00001//===--- PCHWriter.h - Precompiled Headers Writer ---------------*- C++ -*-===//
2//
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//
10// This file defines the PCHWriter class, which writes a precompiled header.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Frontend/PCHWriter.h"
Douglas Gregor87887da2009-04-20 15:53:59 +000015#include "../Sema/Sema.h" // FIXME: move header into include/clang/Sema
Douglas Gregorff9a6092009-04-20 20:36:09 +000016#include "../Sema/IdentifierResolver.h" // FIXME: move header
Douglas Gregorc34897d2009-04-09 22:27:44 +000017#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclContextInternals.h"
Douglas Gregorc10f86f2009-04-14 21:18:50 +000020#include "clang/AST/Expr.h"
21#include "clang/AST/StmtVisitor.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000022#include "clang/AST/Type.h"
Chris Lattner1b094952009-04-10 18:00:12 +000023#include "clang/Lex/MacroInfo.h"
24#include "clang/Lex/Preprocessor.h"
Steve Naroffcda68f22009-04-24 20:03:17 +000025#include "clang/Lex/HeaderSearch.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000026#include "clang/Basic/FileManager.h"
Douglas Gregorff9a6092009-04-20 20:36:09 +000027#include "clang/Basic/OnDiskHashTable.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000028#include "clang/Basic/SourceManager.h"
Douglas Gregor635f97f2009-04-13 16:31:14 +000029#include "clang/Basic/SourceManagerInternals.h"
Douglas Gregorb5887f32009-04-10 21:16:55 +000030#include "clang/Basic/TargetInfo.h"
Douglas Gregore2f37202009-04-14 21:55:33 +000031#include "llvm/ADT/APFloat.h"
32#include "llvm/ADT/APInt.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000033#include "llvm/Bitcode/BitstreamWriter.h"
34#include "llvm/Support/Compiler.h"
Douglas Gregorab1cef72009-04-10 03:52:48 +000035#include "llvm/Support/MemoryBuffer.h"
Chris Lattner64b65f82009-04-11 18:40:46 +000036#include <cstdio>
Douglas Gregorc34897d2009-04-09 22:27:44 +000037using namespace clang;
38
39//===----------------------------------------------------------------------===//
40// Type serialization
41//===----------------------------------------------------------------------===//
Chris Lattnerd83ede52009-04-27 06:16:06 +000042
Douglas Gregorc34897d2009-04-09 22:27:44 +000043namespace {
44 class VISIBILITY_HIDDEN PCHTypeWriter {
45 PCHWriter &Writer;
46 PCHWriter::RecordData &Record;
47
48 public:
49 /// \brief Type code that corresponds to the record generated.
50 pch::TypeCode Code;
51
52 PCHTypeWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
53 : Writer(Writer), Record(Record) { }
54
55 void VisitArrayType(const ArrayType *T);
56 void VisitFunctionType(const FunctionType *T);
57 void VisitTagType(const TagType *T);
58
59#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
60#define ABSTRACT_TYPE(Class, Base)
61#define DEPENDENT_TYPE(Class, Base)
62#include "clang/AST/TypeNodes.def"
63 };
64}
65
66void PCHTypeWriter::VisitExtQualType(const ExtQualType *T) {
67 Writer.AddTypeRef(QualType(T->getBaseType(), 0), Record);
68 Record.push_back(T->getObjCGCAttr()); // FIXME: use stable values
69 Record.push_back(T->getAddressSpace());
70 Code = pch::TYPE_EXT_QUAL;
71}
72
73void PCHTypeWriter::VisitBuiltinType(const BuiltinType *T) {
74 assert(false && "Built-in types are never serialized");
75}
76
77void PCHTypeWriter::VisitFixedWidthIntType(const FixedWidthIntType *T) {
78 Record.push_back(T->getWidth());
79 Record.push_back(T->isSigned());
80 Code = pch::TYPE_FIXED_WIDTH_INT;
81}
82
83void PCHTypeWriter::VisitComplexType(const ComplexType *T) {
84 Writer.AddTypeRef(T->getElementType(), Record);
85 Code = pch::TYPE_COMPLEX;
86}
87
88void PCHTypeWriter::VisitPointerType(const PointerType *T) {
89 Writer.AddTypeRef(T->getPointeeType(), Record);
90 Code = pch::TYPE_POINTER;
91}
92
93void PCHTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
94 Writer.AddTypeRef(T->getPointeeType(), Record);
95 Code = pch::TYPE_BLOCK_POINTER;
96}
97
98void PCHTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
99 Writer.AddTypeRef(T->getPointeeType(), Record);
100 Code = pch::TYPE_LVALUE_REFERENCE;
101}
102
103void PCHTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
104 Writer.AddTypeRef(T->getPointeeType(), Record);
105 Code = pch::TYPE_RVALUE_REFERENCE;
106}
107
108void PCHTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
109 Writer.AddTypeRef(T->getPointeeType(), Record);
110 Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
111 Code = pch::TYPE_MEMBER_POINTER;
112}
113
114void PCHTypeWriter::VisitArrayType(const ArrayType *T) {
115 Writer.AddTypeRef(T->getElementType(), Record);
116 Record.push_back(T->getSizeModifier()); // FIXME: stable values
117 Record.push_back(T->getIndexTypeQualifier()); // FIXME: stable values
118}
119
120void PCHTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
121 VisitArrayType(T);
122 Writer.AddAPInt(T->getSize(), Record);
123 Code = pch::TYPE_CONSTANT_ARRAY;
124}
125
126void PCHTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
127 VisitArrayType(T);
128 Code = pch::TYPE_INCOMPLETE_ARRAY;
129}
130
131void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
132 VisitArrayType(T);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000133 Writer.AddStmt(T->getSizeExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000134 Code = pch::TYPE_VARIABLE_ARRAY;
135}
136
137void PCHTypeWriter::VisitVectorType(const VectorType *T) {
138 Writer.AddTypeRef(T->getElementType(), Record);
139 Record.push_back(T->getNumElements());
140 Code = pch::TYPE_VECTOR;
141}
142
143void PCHTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
144 VisitVectorType(T);
145 Code = pch::TYPE_EXT_VECTOR;
146}
147
148void PCHTypeWriter::VisitFunctionType(const FunctionType *T) {
149 Writer.AddTypeRef(T->getResultType(), Record);
150}
151
152void PCHTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
153 VisitFunctionType(T);
154 Code = pch::TYPE_FUNCTION_NO_PROTO;
155}
156
157void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
158 VisitFunctionType(T);
159 Record.push_back(T->getNumArgs());
160 for (unsigned I = 0, N = T->getNumArgs(); I != N; ++I)
161 Writer.AddTypeRef(T->getArgType(I), Record);
162 Record.push_back(T->isVariadic());
163 Record.push_back(T->getTypeQuals());
164 Code = pch::TYPE_FUNCTION_PROTO;
165}
166
167void PCHTypeWriter::VisitTypedefType(const TypedefType *T) {
168 Writer.AddDeclRef(T->getDecl(), Record);
169 Code = pch::TYPE_TYPEDEF;
170}
171
172void PCHTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000173 Writer.AddStmt(T->getUnderlyingExpr());
Douglas Gregorc34897d2009-04-09 22:27:44 +0000174 Code = pch::TYPE_TYPEOF_EXPR;
175}
176
177void PCHTypeWriter::VisitTypeOfType(const TypeOfType *T) {
178 Writer.AddTypeRef(T->getUnderlyingType(), Record);
179 Code = pch::TYPE_TYPEOF;
180}
181
182void PCHTypeWriter::VisitTagType(const TagType *T) {
183 Writer.AddDeclRef(T->getDecl(), Record);
184 assert(!T->isBeingDefined() &&
185 "Cannot serialize in the middle of a type definition");
186}
187
188void PCHTypeWriter::VisitRecordType(const RecordType *T) {
189 VisitTagType(T);
190 Code = pch::TYPE_RECORD;
191}
192
193void PCHTypeWriter::VisitEnumType(const EnumType *T) {
194 VisitTagType(T);
195 Code = pch::TYPE_ENUM;
196}
197
198void
199PCHTypeWriter::VisitTemplateSpecializationType(
200 const TemplateSpecializationType *T) {
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000201 // FIXME: Serialize this type (C++ only)
Douglas Gregorc34897d2009-04-09 22:27:44 +0000202 assert(false && "Cannot serialize template specialization types");
203}
204
205void PCHTypeWriter::VisitQualifiedNameType(const QualifiedNameType *T) {
Douglas Gregor3c8ff3e2009-04-15 18:43:11 +0000206 // FIXME: Serialize this type (C++ only)
Douglas Gregorc34897d2009-04-09 22:27:44 +0000207 assert(false && "Cannot serialize qualified name types");
208}
209
210void PCHTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
211 Writer.AddDeclRef(T->getDecl(), Record);
212 Code = pch::TYPE_OBJC_INTERFACE;
213}
214
215void
216PCHTypeWriter::VisitObjCQualifiedInterfaceType(
217 const ObjCQualifiedInterfaceType *T) {
218 VisitObjCInterfaceType(T);
219 Record.push_back(T->getNumProtocols());
220 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
221 Writer.AddDeclRef(T->getProtocol(I), Record);
222 Code = pch::TYPE_OBJC_QUALIFIED_INTERFACE;
223}
224
225void PCHTypeWriter::VisitObjCQualifiedIdType(const ObjCQualifiedIdType *T) {
226 Record.push_back(T->getNumProtocols());
227 for (unsigned I = 0, N = T->getNumProtocols(); I != N; ++I)
228 Writer.AddDeclRef(T->getProtocols(I), Record);
229 Code = pch::TYPE_OBJC_QUALIFIED_ID;
230}
231
Douglas Gregorc34897d2009-04-09 22:27:44 +0000232
233//===----------------------------------------------------------------------===//
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000234// Statement/expression serialization
235//===----------------------------------------------------------------------===//
236namespace {
237 class VISIBILITY_HIDDEN PCHStmtWriter
238 : public StmtVisitor<PCHStmtWriter, void> {
239
240 PCHWriter &Writer;
241 PCHWriter::RecordData &Record;
242
243 public:
244 pch::StmtCode Code;
245
246 PCHStmtWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
247 : Writer(Writer), Record(Record) { }
248
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000249 void VisitStmt(Stmt *S);
250 void VisitNullStmt(NullStmt *S);
251 void VisitCompoundStmt(CompoundStmt *S);
252 void VisitSwitchCase(SwitchCase *S);
253 void VisitCaseStmt(CaseStmt *S);
254 void VisitDefaultStmt(DefaultStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000255 void VisitLabelStmt(LabelStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000256 void VisitIfStmt(IfStmt *S);
257 void VisitSwitchStmt(SwitchStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000258 void VisitWhileStmt(WhileStmt *S);
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000259 void VisitDoStmt(DoStmt *S);
260 void VisitForStmt(ForStmt *S);
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000261 void VisitGotoStmt(GotoStmt *S);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000262 void VisitIndirectGotoStmt(IndirectGotoStmt *S);
Douglas Gregora6b503f2009-04-17 00:16:09 +0000263 void VisitContinueStmt(ContinueStmt *S);
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000264 void VisitBreakStmt(BreakStmt *S);
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000265 void VisitReturnStmt(ReturnStmt *S);
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000266 void VisitDeclStmt(DeclStmt *S);
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000267 void VisitAsmStmt(AsmStmt *S);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000268 void VisitExpr(Expr *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000269 void VisitPredefinedExpr(PredefinedExpr *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000270 void VisitDeclRefExpr(DeclRefExpr *E);
271 void VisitIntegerLiteral(IntegerLiteral *E);
Douglas Gregore2f37202009-04-14 21:55:33 +0000272 void VisitFloatingLiteral(FloatingLiteral *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000273 void VisitImaginaryLiteral(ImaginaryLiteral *E);
Douglas Gregor596e0932009-04-15 16:35:07 +0000274 void VisitStringLiteral(StringLiteral *E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000275 void VisitCharacterLiteral(CharacterLiteral *E);
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000276 void VisitParenExpr(ParenExpr *E);
Douglas Gregor12d74052009-04-15 15:58:59 +0000277 void VisitUnaryOperator(UnaryOperator *E);
278 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000279 void VisitArraySubscriptExpr(ArraySubscriptExpr *E);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000280 void VisitCallExpr(CallExpr *E);
281 void VisitMemberExpr(MemberExpr *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000282 void VisitCastExpr(CastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000283 void VisitBinaryOperator(BinaryOperator *E);
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000284 void VisitCompoundAssignOperator(CompoundAssignOperator *E);
285 void VisitConditionalOperator(ConditionalOperator *E);
Douglas Gregora151ba42009-04-14 23:32:43 +0000286 void VisitImplicitCastExpr(ImplicitCastExpr *E);
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000287 void VisitExplicitCastExpr(ExplicitCastExpr *E);
288 void VisitCStyleCastExpr(CStyleCastExpr *E);
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000289 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000290 void VisitExtVectorElementExpr(ExtVectorElementExpr *E);
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000291 void VisitInitListExpr(InitListExpr *E);
292 void VisitDesignatedInitExpr(DesignatedInitExpr *E);
293 void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
Douglas Gregorec0b8292009-04-15 23:02:49 +0000294 void VisitVAArgExpr(VAArgExpr *E);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000295 void VisitAddrLabelExpr(AddrLabelExpr *E);
Douglas Gregoreca12f62009-04-17 19:05:30 +0000296 void VisitStmtExpr(StmtExpr *E);
Douglas Gregor209d4622009-04-15 23:33:31 +0000297 void VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
298 void VisitChooseExpr(ChooseExpr *E);
299 void VisitGNUNullExpr(GNUNullExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000300 void VisitShuffleVectorExpr(ShuffleVectorExpr *E);
Douglas Gregore246b742009-04-17 19:21:43 +0000301 void VisitBlockExpr(BlockExpr *E);
Douglas Gregor725e94b2009-04-16 00:01:45 +0000302 void VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
Chris Lattner80f83c62009-04-22 05:57:30 +0000303
Steve Naroff79762bd2009-04-26 18:52:16 +0000304 // Objective-C Expressions
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000305 void VisitObjCStringLiteral(ObjCStringLiteral *E);
Chris Lattner80f83c62009-04-22 05:57:30 +0000306 void VisitObjCEncodeExpr(ObjCEncodeExpr *E);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000307 void VisitObjCSelectorExpr(ObjCSelectorExpr *E);
308 void VisitObjCProtocolExpr(ObjCProtocolExpr *E);
Chris Lattnerc0478bf2009-04-26 00:44:05 +0000309 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E);
310 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
311 void VisitObjCKVCRefExpr(ObjCKVCRefExpr *E);
Steve Narofffb3e4022009-04-25 14:04:28 +0000312 void VisitObjCMessageExpr(ObjCMessageExpr *E);
Chris Lattnerc0478bf2009-04-26 00:44:05 +0000313 void VisitObjCSuperExpr(ObjCSuperExpr *E);
Steve Naroff79762bd2009-04-26 18:52:16 +0000314
315 // Objective-C Statements
316 void VisitObjCForCollectionStmt(ObjCForCollectionStmt *);
Douglas Gregorce066712009-04-26 22:20:50 +0000317 void VisitObjCAtCatchStmt(ObjCAtCatchStmt *);
318 void VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *);
Steve Naroff79762bd2009-04-26 18:52:16 +0000319 void VisitObjCAtTryStmt(ObjCAtTryStmt *);
320 void VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *);
321 void VisitObjCAtThrowStmt(ObjCAtThrowStmt *);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000322 };
323}
324
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000325void PCHStmtWriter::VisitStmt(Stmt *S) {
326}
327
328void PCHStmtWriter::VisitNullStmt(NullStmt *S) {
329 VisitStmt(S);
330 Writer.AddSourceLocation(S->getSemiLoc(), Record);
331 Code = pch::STMT_NULL;
332}
333
334void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
335 VisitStmt(S);
336 Record.push_back(S->size());
337 for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
338 CS != CSEnd; ++CS)
339 Writer.WriteSubStmt(*CS);
340 Writer.AddSourceLocation(S->getLBracLoc(), Record);
341 Writer.AddSourceLocation(S->getRBracLoc(), Record);
342 Code = pch::STMT_COMPOUND;
343}
344
345void PCHStmtWriter::VisitSwitchCase(SwitchCase *S) {
346 VisitStmt(S);
347 Record.push_back(Writer.RecordSwitchCaseID(S));
348}
349
350void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) {
351 VisitSwitchCase(S);
352 Writer.WriteSubStmt(S->getLHS());
353 Writer.WriteSubStmt(S->getRHS());
354 Writer.WriteSubStmt(S->getSubStmt());
355 Writer.AddSourceLocation(S->getCaseLoc(), Record);
356 Code = pch::STMT_CASE;
357}
358
359void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
360 VisitSwitchCase(S);
361 Writer.WriteSubStmt(S->getSubStmt());
362 Writer.AddSourceLocation(S->getDefaultLoc(), Record);
363 Code = pch::STMT_DEFAULT;
364}
365
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000366void PCHStmtWriter::VisitLabelStmt(LabelStmt *S) {
367 VisitStmt(S);
368 Writer.AddIdentifierRef(S->getID(), Record);
369 Writer.WriteSubStmt(S->getSubStmt());
370 Writer.AddSourceLocation(S->getIdentLoc(), Record);
371 Record.push_back(Writer.GetLabelID(S));
372 Code = pch::STMT_LABEL;
373}
374
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000375void PCHStmtWriter::VisitIfStmt(IfStmt *S) {
376 VisitStmt(S);
377 Writer.WriteSubStmt(S->getCond());
378 Writer.WriteSubStmt(S->getThen());
379 Writer.WriteSubStmt(S->getElse());
380 Writer.AddSourceLocation(S->getIfLoc(), Record);
381 Code = pch::STMT_IF;
382}
383
384void PCHStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
385 VisitStmt(S);
386 Writer.WriteSubStmt(S->getCond());
387 Writer.WriteSubStmt(S->getBody());
388 Writer.AddSourceLocation(S->getSwitchLoc(), Record);
389 for (SwitchCase *SC = S->getSwitchCaseList(); SC;
390 SC = SC->getNextSwitchCase())
391 Record.push_back(Writer.getSwitchCaseID(SC));
392 Code = pch::STMT_SWITCH;
393}
394
Douglas Gregora6b503f2009-04-17 00:16:09 +0000395void PCHStmtWriter::VisitWhileStmt(WhileStmt *S) {
396 VisitStmt(S);
397 Writer.WriteSubStmt(S->getCond());
398 Writer.WriteSubStmt(S->getBody());
399 Writer.AddSourceLocation(S->getWhileLoc(), Record);
400 Code = pch::STMT_WHILE;
401}
402
Douglas Gregorfb5f25b2009-04-17 00:29:51 +0000403void PCHStmtWriter::VisitDoStmt(DoStmt *S) {
404 VisitStmt(S);
405 Writer.WriteSubStmt(S->getCond());
406 Writer.WriteSubStmt(S->getBody());
407 Writer.AddSourceLocation(S->getDoLoc(), Record);
408 Code = pch::STMT_DO;
409}
410
411void PCHStmtWriter::VisitForStmt(ForStmt *S) {
412 VisitStmt(S);
413 Writer.WriteSubStmt(S->getInit());
414 Writer.WriteSubStmt(S->getCond());
415 Writer.WriteSubStmt(S->getInc());
416 Writer.WriteSubStmt(S->getBody());
417 Writer.AddSourceLocation(S->getForLoc(), Record);
418 Code = pch::STMT_FOR;
419}
420
Douglas Gregor6e411bf2009-04-17 18:18:49 +0000421void PCHStmtWriter::VisitGotoStmt(GotoStmt *S) {
422 VisitStmt(S);
423 Record.push_back(Writer.GetLabelID(S->getLabel()));
424 Writer.AddSourceLocation(S->getGotoLoc(), Record);
425 Writer.AddSourceLocation(S->getLabelLoc(), Record);
426 Code = pch::STMT_GOTO;
427}
428
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000429void PCHStmtWriter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
430 VisitStmt(S);
Chris Lattner9ef9c282009-04-19 01:04:21 +0000431 Writer.AddSourceLocation(S->getGotoLoc(), Record);
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000432 Writer.WriteSubStmt(S->getTarget());
433 Code = pch::STMT_INDIRECT_GOTO;
434}
435
Douglas Gregora6b503f2009-04-17 00:16:09 +0000436void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) {
437 VisitStmt(S);
438 Writer.AddSourceLocation(S->getContinueLoc(), Record);
439 Code = pch::STMT_CONTINUE;
440}
441
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000442void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) {
443 VisitStmt(S);
444 Writer.AddSourceLocation(S->getBreakLoc(), Record);
445 Code = pch::STMT_BREAK;
446}
447
Douglas Gregor22d2dcd2009-04-17 16:34:57 +0000448void PCHStmtWriter::VisitReturnStmt(ReturnStmt *S) {
449 VisitStmt(S);
450 Writer.WriteSubStmt(S->getRetValue());
451 Writer.AddSourceLocation(S->getReturnLoc(), Record);
452 Code = pch::STMT_RETURN;
453}
454
Douglas Gregor78ff29f2009-04-17 16:55:36 +0000455void PCHStmtWriter::VisitDeclStmt(DeclStmt *S) {
456 VisitStmt(S);
457 Writer.AddSourceLocation(S->getStartLoc(), Record);
458 Writer.AddSourceLocation(S->getEndLoc(), Record);
459 DeclGroupRef DG = S->getDeclGroup();
460 for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D)
461 Writer.AddDeclRef(*D, Record);
462 Code = pch::STMT_DECL;
463}
464
Douglas Gregor3e1f9fb2009-04-17 20:57:14 +0000465void PCHStmtWriter::VisitAsmStmt(AsmStmt *S) {
466 VisitStmt(S);
467 Record.push_back(S->getNumOutputs());
468 Record.push_back(S->getNumInputs());
469 Record.push_back(S->getNumClobbers());
470 Writer.AddSourceLocation(S->getAsmLoc(), Record);
471 Writer.AddSourceLocation(S->getRParenLoc(), Record);
472 Record.push_back(S->isVolatile());
473 Record.push_back(S->isSimple());
474 Writer.WriteSubStmt(S->getAsmString());
475
476 // Outputs
477 for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
478 Writer.AddString(S->getOutputName(I), Record);
479 Writer.WriteSubStmt(S->getOutputConstraintLiteral(I));
480 Writer.WriteSubStmt(S->getOutputExpr(I));
481 }
482
483 // Inputs
484 for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
485 Writer.AddString(S->getInputName(I), Record);
486 Writer.WriteSubStmt(S->getInputConstraintLiteral(I));
487 Writer.WriteSubStmt(S->getInputExpr(I));
488 }
489
490 // Clobbers
491 for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
492 Writer.WriteSubStmt(S->getClobber(I));
493
494 Code = pch::STMT_ASM;
495}
496
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000497void PCHStmtWriter::VisitExpr(Expr *E) {
Douglas Gregor9c4782a2009-04-17 00:04:06 +0000498 VisitStmt(E);
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000499 Writer.AddTypeRef(E->getType(), Record);
500 Record.push_back(E->isTypeDependent());
501 Record.push_back(E->isValueDependent());
502}
503
Douglas Gregore2f37202009-04-14 21:55:33 +0000504void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
505 VisitExpr(E);
506 Writer.AddSourceLocation(E->getLocation(), Record);
507 Record.push_back(E->getIdentType()); // FIXME: stable encoding
508 Code = pch::EXPR_PREDEFINED;
509}
510
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000511void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
512 VisitExpr(E);
513 Writer.AddDeclRef(E->getDecl(), Record);
514 Writer.AddSourceLocation(E->getLocation(), Record);
515 Code = pch::EXPR_DECL_REF;
516}
517
518void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
519 VisitExpr(E);
520 Writer.AddSourceLocation(E->getLocation(), Record);
521 Writer.AddAPInt(E->getValue(), Record);
522 Code = pch::EXPR_INTEGER_LITERAL;
523}
524
Douglas Gregore2f37202009-04-14 21:55:33 +0000525void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
526 VisitExpr(E);
527 Writer.AddAPFloat(E->getValue(), Record);
528 Record.push_back(E->isExact());
529 Writer.AddSourceLocation(E->getLocation(), Record);
530 Code = pch::EXPR_FLOATING_LITERAL;
531}
532
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000533void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
534 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000535 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000536 Code = pch::EXPR_IMAGINARY_LITERAL;
537}
538
Douglas Gregor596e0932009-04-15 16:35:07 +0000539void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
540 VisitExpr(E);
541 Record.push_back(E->getByteLength());
542 Record.push_back(E->getNumConcatenated());
543 Record.push_back(E->isWide());
544 // FIXME: String data should be stored as a blob at the end of the
545 // StringLiteral. However, we can't do so now because we have no
546 // provision for coping with abbreviations when we're jumping around
547 // the PCH file during deserialization.
548 Record.insert(Record.end(),
549 E->getStrData(), E->getStrData() + E->getByteLength());
550 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
551 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
552 Code = pch::EXPR_STRING_LITERAL;
553}
554
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000555void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
556 VisitExpr(E);
557 Record.push_back(E->getValue());
558 Writer.AddSourceLocation(E->getLoc(), Record);
559 Record.push_back(E->isWide());
560 Code = pch::EXPR_CHARACTER_LITERAL;
561}
562
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000563void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
564 VisitExpr(E);
565 Writer.AddSourceLocation(E->getLParen(), Record);
566 Writer.AddSourceLocation(E->getRParen(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000567 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor4ea0b1f2009-04-14 23:59:37 +0000568 Code = pch::EXPR_PAREN;
569}
570
Douglas Gregor12d74052009-04-15 15:58:59 +0000571void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
572 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000573 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000574 Record.push_back(E->getOpcode()); // FIXME: stable encoding
575 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
576 Code = pch::EXPR_UNARY_OPERATOR;
577}
578
579void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
580 VisitExpr(E);
581 Record.push_back(E->isSizeOf());
582 if (E->isArgumentType())
583 Writer.AddTypeRef(E->getArgumentType(), Record);
584 else {
585 Record.push_back(0);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000586 Writer.WriteSubStmt(E->getArgumentExpr());
Douglas Gregor12d74052009-04-15 15:58:59 +0000587 }
588 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
589 Writer.AddSourceLocation(E->getRParenLoc(), Record);
590 Code = pch::EXPR_SIZEOF_ALIGN_OF;
591}
592
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000593void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
594 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000595 Writer.WriteSubStmt(E->getLHS());
596 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor21ddd8c2009-04-15 22:19:53 +0000597 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
598 Code = pch::EXPR_ARRAY_SUBSCRIPT;
599}
600
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000601void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
602 VisitExpr(E);
603 Record.push_back(E->getNumArgs());
604 Writer.AddSourceLocation(E->getRParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000605 Writer.WriteSubStmt(E->getCallee());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000606 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
607 Arg != ArgEnd; ++Arg)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000608 Writer.WriteSubStmt(*Arg);
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000609 Code = pch::EXPR_CALL;
610}
611
612void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
613 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000614 Writer.WriteSubStmt(E->getBase());
Douglas Gregor7e2b1cd2009-04-15 17:43:59 +0000615 Writer.AddDeclRef(E->getMemberDecl(), Record);
616 Writer.AddSourceLocation(E->getMemberLoc(), Record);
617 Record.push_back(E->isArrow());
618 Code = pch::EXPR_MEMBER;
619}
620
Douglas Gregora151ba42009-04-14 23:32:43 +0000621void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
622 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000623 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregora151ba42009-04-14 23:32:43 +0000624}
625
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000626void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
627 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000628 Writer.WriteSubStmt(E->getLHS());
629 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000630 Record.push_back(E->getOpcode()); // FIXME: stable encoding
631 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
632 Code = pch::EXPR_BINARY_OPERATOR;
633}
634
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000635void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
636 VisitBinaryOperator(E);
637 Writer.AddTypeRef(E->getComputationLHSType(), Record);
638 Writer.AddTypeRef(E->getComputationResultType(), Record);
639 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
640}
641
642void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
643 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000644 Writer.WriteSubStmt(E->getCond());
645 Writer.WriteSubStmt(E->getLHS());
646 Writer.WriteSubStmt(E->getRHS());
Douglas Gregorc599bbf2009-04-15 22:40:36 +0000647 Code = pch::EXPR_CONDITIONAL_OPERATOR;
648}
649
Douglas Gregora151ba42009-04-14 23:32:43 +0000650void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
651 VisitCastExpr(E);
652 Record.push_back(E->isLvalueCast());
653 Code = pch::EXPR_IMPLICIT_CAST;
654}
655
Douglas Gregorc75d0cb2009-04-15 00:25:59 +0000656void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
657 VisitCastExpr(E);
658 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
659}
660
661void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
662 VisitExplicitCastExpr(E);
663 Writer.AddSourceLocation(E->getLParenLoc(), Record);
664 Writer.AddSourceLocation(E->getRParenLoc(), Record);
665 Code = pch::EXPR_CSTYLE_CAST;
666}
667
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000668void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
669 VisitExpr(E);
670 Writer.AddSourceLocation(E->getLParenLoc(), Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000671 Writer.WriteSubStmt(E->getInitializer());
Douglas Gregorb70b48f2009-04-16 02:33:48 +0000672 Record.push_back(E->isFileScope());
673 Code = pch::EXPR_COMPOUND_LITERAL;
674}
675
Douglas Gregorec0b8292009-04-15 23:02:49 +0000676void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
677 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000678 Writer.WriteSubStmt(E->getBase());
Douglas Gregorec0b8292009-04-15 23:02:49 +0000679 Writer.AddIdentifierRef(&E->getAccessor(), Record);
680 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
681 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
682}
683
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000684void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) {
685 VisitExpr(E);
686 Record.push_back(E->getNumInits());
687 for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000688 Writer.WriteSubStmt(E->getInit(I));
689 Writer.WriteSubStmt(E->getSyntacticForm());
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000690 Writer.AddSourceLocation(E->getLBraceLoc(), Record);
691 Writer.AddSourceLocation(E->getRBraceLoc(), Record);
692 Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
693 Record.push_back(E->hadArrayRangeDesignator());
694 Code = pch::EXPR_INIT_LIST;
695}
696
697void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
698 VisitExpr(E);
699 Record.push_back(E->getNumSubExprs());
700 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000701 Writer.WriteSubStmt(E->getSubExpr(I));
Douglas Gregor6710a3c2009-04-16 00:55:48 +0000702 Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
703 Record.push_back(E->usesGNUSyntax());
704 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
705 DEnd = E->designators_end();
706 D != DEnd; ++D) {
707 if (D->isFieldDesignator()) {
708 if (FieldDecl *Field = D->getField()) {
709 Record.push_back(pch::DESIG_FIELD_DECL);
710 Writer.AddDeclRef(Field, Record);
711 } else {
712 Record.push_back(pch::DESIG_FIELD_NAME);
713 Writer.AddIdentifierRef(D->getFieldName(), Record);
714 }
715 Writer.AddSourceLocation(D->getDotLoc(), Record);
716 Writer.AddSourceLocation(D->getFieldLoc(), Record);
717 } else if (D->isArrayDesignator()) {
718 Record.push_back(pch::DESIG_ARRAY);
719 Record.push_back(D->getFirstExprIndex());
720 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
721 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
722 } else {
723 assert(D->isArrayRangeDesignator() && "Unknown designator");
724 Record.push_back(pch::DESIG_ARRAY_RANGE);
725 Record.push_back(D->getFirstExprIndex());
726 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
727 Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
728 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
729 }
730 }
731 Code = pch::EXPR_DESIGNATED_INIT;
732}
733
734void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
735 VisitExpr(E);
736 Code = pch::EXPR_IMPLICIT_VALUE_INIT;
737}
738
Douglas Gregorec0b8292009-04-15 23:02:49 +0000739void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
740 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000741 Writer.WriteSubStmt(E->getSubExpr());
Douglas Gregorec0b8292009-04-15 23:02:49 +0000742 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
743 Writer.AddSourceLocation(E->getRParenLoc(), Record);
744 Code = pch::EXPR_VA_ARG;
745}
746
Douglas Gregor95a8fe32009-04-17 18:58:21 +0000747void PCHStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) {
748 VisitExpr(E);
749 Writer.AddSourceLocation(E->getAmpAmpLoc(), Record);
750 Writer.AddSourceLocation(E->getLabelLoc(), Record);
751 Record.push_back(Writer.GetLabelID(E->getLabel()));
752 Code = pch::EXPR_ADDR_LABEL;
753}
754
Douglas Gregoreca12f62009-04-17 19:05:30 +0000755void PCHStmtWriter::VisitStmtExpr(StmtExpr *E) {
756 VisitExpr(E);
757 Writer.WriteSubStmt(E->getSubStmt());
758 Writer.AddSourceLocation(E->getLParenLoc(), Record);
759 Writer.AddSourceLocation(E->getRParenLoc(), Record);
760 Code = pch::EXPR_STMT;
761}
762
Douglas Gregor209d4622009-04-15 23:33:31 +0000763void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
764 VisitExpr(E);
765 Writer.AddTypeRef(E->getArgType1(), Record);
766 Writer.AddTypeRef(E->getArgType2(), Record);
767 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
768 Writer.AddSourceLocation(E->getRParenLoc(), Record);
769 Code = pch::EXPR_TYPES_COMPATIBLE;
770}
771
772void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
773 VisitExpr(E);
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000774 Writer.WriteSubStmt(E->getCond());
775 Writer.WriteSubStmt(E->getLHS());
776 Writer.WriteSubStmt(E->getRHS());
Douglas Gregor209d4622009-04-15 23:33:31 +0000777 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
778 Writer.AddSourceLocation(E->getRParenLoc(), Record);
779 Code = pch::EXPR_CHOOSE;
780}
781
782void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
783 VisitExpr(E);
784 Writer.AddSourceLocation(E->getTokenLocation(), Record);
785 Code = pch::EXPR_GNU_NULL;
786}
787
Douglas Gregor725e94b2009-04-16 00:01:45 +0000788void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
789 VisitExpr(E);
790 Record.push_back(E->getNumSubExprs());
791 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
Douglas Gregorc72f6c82009-04-16 22:23:12 +0000792 Writer.WriteSubStmt(E->getExpr(I));
Douglas Gregor725e94b2009-04-16 00:01:45 +0000793 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
794 Writer.AddSourceLocation(E->getRParenLoc(), Record);
795 Code = pch::EXPR_SHUFFLE_VECTOR;
796}
797
Douglas Gregore246b742009-04-17 19:21:43 +0000798void PCHStmtWriter::VisitBlockExpr(BlockExpr *E) {
799 VisitExpr(E);
800 Writer.AddDeclRef(E->getBlockDecl(), Record);
801 Record.push_back(E->hasBlockDeclRefExprs());
802 Code = pch::EXPR_BLOCK;
803}
804
Douglas Gregor725e94b2009-04-16 00:01:45 +0000805void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
806 VisitExpr(E);
807 Writer.AddDeclRef(E->getDecl(), Record);
808 Writer.AddSourceLocation(E->getLocation(), Record);
809 Record.push_back(E->isByRef());
810 Code = pch::EXPR_BLOCK_DECL_REF;
811}
812
Douglas Gregorc10f86f2009-04-14 21:18:50 +0000813//===----------------------------------------------------------------------===//
Chris Lattner80f83c62009-04-22 05:57:30 +0000814// Objective-C Expressions and Statements.
815//===----------------------------------------------------------------------===//
816
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000817void PCHStmtWriter::VisitObjCStringLiteral(ObjCStringLiteral *E) {
818 VisitExpr(E);
819 Writer.WriteSubStmt(E->getString());
820 Writer.AddSourceLocation(E->getAtLoc(), Record);
821 Code = pch::EXPR_OBJC_STRING_LITERAL;
822}
823
Chris Lattner80f83c62009-04-22 05:57:30 +0000824void PCHStmtWriter::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
825 VisitExpr(E);
826 Writer.AddTypeRef(E->getEncodedType(), Record);
827 Writer.AddSourceLocation(E->getAtLoc(), Record);
828 Writer.AddSourceLocation(E->getRParenLoc(), Record);
829 Code = pch::EXPR_OBJC_ENCODE;
830}
831
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000832void PCHStmtWriter::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
833 VisitExpr(E);
Steve Naroff9e84d782009-04-23 10:39:46 +0000834 Writer.AddSelectorRef(E->getSelector(), Record);
Chris Lattnerc49bbe72009-04-22 06:29:42 +0000835 Writer.AddSourceLocation(E->getAtLoc(), Record);
836 Writer.AddSourceLocation(E->getRParenLoc(), Record);
837 Code = pch::EXPR_OBJC_SELECTOR_EXPR;
838}
839
840void PCHStmtWriter::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
841 VisitExpr(E);
842 Writer.AddDeclRef(E->getProtocol(), Record);
843 Writer.AddSourceLocation(E->getAtLoc(), Record);
844 Writer.AddSourceLocation(E->getRParenLoc(), Record);
845 Code = pch::EXPR_OBJC_PROTOCOL_EXPR;
846}
847
Chris Lattnerc0478bf2009-04-26 00:44:05 +0000848void PCHStmtWriter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
849 VisitExpr(E);
850 Writer.AddDeclRef(E->getDecl(), Record);
851 Writer.AddSourceLocation(E->getLocation(), Record);
852 Writer.WriteSubStmt(E->getBase());
853 Record.push_back(E->isArrow());
854 Record.push_back(E->isFreeIvar());
Steve Naroffa323e972009-04-26 14:11:39 +0000855 Code = pch::EXPR_OBJC_IVAR_REF_EXPR;
Chris Lattnerc0478bf2009-04-26 00:44:05 +0000856}
857
858void PCHStmtWriter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
859 VisitExpr(E);
860 Writer.AddDeclRef(E->getProperty(), Record);
861 Writer.AddSourceLocation(E->getLocation(), Record);
862 Writer.WriteSubStmt(E->getBase());
Steve Naroffa323e972009-04-26 14:11:39 +0000863 Code = pch::EXPR_OBJC_PROPERTY_REF_EXPR;
Chris Lattnerc0478bf2009-04-26 00:44:05 +0000864}
865
866void PCHStmtWriter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *E) {
867 VisitExpr(E);
868 Writer.AddDeclRef(E->getGetterMethod(), Record);
869 Writer.AddDeclRef(E->getSetterMethod(), Record);
870
871 // NOTE: ClassProp and Base are mutually exclusive.
872 Writer.AddDeclRef(E->getClassProp(), Record);
873 Writer.WriteSubStmt(E->getBase());
874 Writer.AddSourceLocation(E->getLocation(), Record);
875 Writer.AddSourceLocation(E->getClassLoc(), Record);
Steve Naroffa323e972009-04-26 14:11:39 +0000876 Code = pch::EXPR_OBJC_KVC_REF_EXPR;
Chris Lattnerc0478bf2009-04-26 00:44:05 +0000877}
878
Steve Narofffb3e4022009-04-25 14:04:28 +0000879void PCHStmtWriter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
880 VisitExpr(E);
881 Record.push_back(E->getNumArgs());
Chris Lattnerc0478bf2009-04-26 00:44:05 +0000882 Writer.AddSourceLocation(E->getLeftLoc(), Record);
883 Writer.AddSourceLocation(E->getRightLoc(), Record);
Steve Narofffb3e4022009-04-25 14:04:28 +0000884 Writer.AddSelectorRef(E->getSelector(), Record);
885 Writer.AddDeclRef(E->getMethodDecl(), Record); // optional
Steve Narofffb3e4022009-04-25 14:04:28 +0000886 Writer.WriteSubStmt(E->getReceiver());
Douglas Gregorce066712009-04-26 22:20:50 +0000887
888 if (!E->getReceiver()) {
889 ObjCMessageExpr::ClassInfo CI = E->getClassInfo();
890 Writer.AddDeclRef(CI.first, Record);
891 Writer.AddIdentifierRef(CI.second, Record);
892 }
893
Steve Narofffb3e4022009-04-25 14:04:28 +0000894 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
895 Arg != ArgEnd; ++Arg)
896 Writer.WriteSubStmt(*Arg);
897 Code = pch::EXPR_OBJC_MESSAGE_EXPR;
898}
899
Chris Lattnerc0478bf2009-04-26 00:44:05 +0000900void PCHStmtWriter::VisitObjCSuperExpr(ObjCSuperExpr *E) {
901 VisitExpr(E);
902 Writer.AddSourceLocation(E->getLoc(), Record);
Steve Naroffa323e972009-04-26 14:11:39 +0000903 Code = pch::EXPR_OBJC_SUPER_EXPR;
Chris Lattnerc0478bf2009-04-26 00:44:05 +0000904}
905
Steve Naroff79762bd2009-04-26 18:52:16 +0000906void PCHStmtWriter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
907 VisitStmt(S);
908 Writer.WriteSubStmt(S->getElement());
909 Writer.WriteSubStmt(S->getCollection());
910 Writer.WriteSubStmt(S->getBody());
911 Writer.AddSourceLocation(S->getForLoc(), Record);
912 Writer.AddSourceLocation(S->getRParenLoc(), Record);
913 Code = pch::STMT_OBJC_FOR_COLLECTION;
914}
915
Douglas Gregorce066712009-04-26 22:20:50 +0000916void PCHStmtWriter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
Steve Naroff79762bd2009-04-26 18:52:16 +0000917 Writer.WriteSubStmt(S->getCatchBody());
918 Writer.WriteSubStmt(S->getNextCatchStmt());
919 Writer.AddDeclRef(S->getCatchParamDecl(), Record);
920 Writer.AddSourceLocation(S->getAtCatchLoc(), Record);
921 Writer.AddSourceLocation(S->getRParenLoc(), Record);
922 Code = pch::STMT_OBJC_CATCH;
923}
924
Douglas Gregorce066712009-04-26 22:20:50 +0000925void PCHStmtWriter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
Steve Naroff79762bd2009-04-26 18:52:16 +0000926 Writer.WriteSubStmt(S->getFinallyBody());
927 Writer.AddSourceLocation(S->getAtFinallyLoc(), Record);
928 Code = pch::STMT_OBJC_FINALLY;
929}
930
931void PCHStmtWriter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
932 Writer.WriteSubStmt(S->getTryBody());
933 Writer.WriteSubStmt(S->getCatchStmts());
934 Writer.WriteSubStmt(S->getFinallyStmt());
935 Writer.AddSourceLocation(S->getAtTryLoc(), Record);
936 Code = pch::STMT_OBJC_AT_TRY;
937}
938
939void PCHStmtWriter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S) {
940 Writer.WriteSubStmt(S->getSynchExpr());
941 Writer.WriteSubStmt(S->getSynchBody());
942 Writer.AddSourceLocation(S->getAtSynchronizedLoc(), Record);
943 Code = pch::STMT_OBJC_AT_SYNCHRONIZED;
944}
945
946void PCHStmtWriter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
947 Writer.WriteSubStmt(S->getThrowExpr());
948 Writer.AddSourceLocation(S->getThrowLoc(), Record);
949 Code = pch::STMT_OBJC_AT_THROW;
950}
Chris Lattner80f83c62009-04-22 05:57:30 +0000951
952//===----------------------------------------------------------------------===//
Douglas Gregorc34897d2009-04-09 22:27:44 +0000953// PCHWriter Implementation
954//===----------------------------------------------------------------------===//
955
Chris Lattner920673a2009-04-26 22:26:21 +0000956static void EmitBlockID(unsigned ID, const char *Name,
957 llvm::BitstreamWriter &Stream,
958 PCHWriter::RecordData &Record) {
959 Record.clear();
960 Record.push_back(ID);
961 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
962
963 // Emit the block name if present.
964 if (Name == 0 || Name[0] == 0) return;
965 Record.clear();
966 while (*Name)
967 Record.push_back(*Name++);
968 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
969}
970
971static void EmitRecordID(unsigned ID, const char *Name,
972 llvm::BitstreamWriter &Stream,
973 PCHWriter::RecordData &Record) {
974 Record.clear();
975 Record.push_back(ID);
976 while (*Name)
977 Record.push_back(*Name++);
978 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
Chris Lattnerd16afaa2009-04-27 00:49:53 +0000979}
980
981static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
982 PCHWriter::RecordData &Record) {
983#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
984 RECORD(STMT_STOP);
985 RECORD(STMT_NULL_PTR);
986 RECORD(STMT_NULL);
987 RECORD(STMT_COMPOUND);
988 RECORD(STMT_CASE);
989 RECORD(STMT_DEFAULT);
990 RECORD(STMT_LABEL);
991 RECORD(STMT_IF);
992 RECORD(STMT_SWITCH);
993 RECORD(STMT_WHILE);
994 RECORD(STMT_DO);
995 RECORD(STMT_FOR);
996 RECORD(STMT_GOTO);
997 RECORD(STMT_INDIRECT_GOTO);
998 RECORD(STMT_CONTINUE);
999 RECORD(STMT_BREAK);
1000 RECORD(STMT_RETURN);
1001 RECORD(STMT_DECL);
1002 RECORD(STMT_ASM);
1003 RECORD(EXPR_PREDEFINED);
1004 RECORD(EXPR_DECL_REF);
1005 RECORD(EXPR_INTEGER_LITERAL);
1006 RECORD(EXPR_FLOATING_LITERAL);
1007 RECORD(EXPR_IMAGINARY_LITERAL);
1008 RECORD(EXPR_STRING_LITERAL);
1009 RECORD(EXPR_CHARACTER_LITERAL);
1010 RECORD(EXPR_PAREN);
1011 RECORD(EXPR_UNARY_OPERATOR);
1012 RECORD(EXPR_SIZEOF_ALIGN_OF);
1013 RECORD(EXPR_ARRAY_SUBSCRIPT);
1014 RECORD(EXPR_CALL);
1015 RECORD(EXPR_MEMBER);
1016 RECORD(EXPR_BINARY_OPERATOR);
1017 RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
1018 RECORD(EXPR_CONDITIONAL_OPERATOR);
1019 RECORD(EXPR_IMPLICIT_CAST);
1020 RECORD(EXPR_CSTYLE_CAST);
1021 RECORD(EXPR_COMPOUND_LITERAL);
1022 RECORD(EXPR_EXT_VECTOR_ELEMENT);
1023 RECORD(EXPR_INIT_LIST);
1024 RECORD(EXPR_DESIGNATED_INIT);
1025 RECORD(EXPR_IMPLICIT_VALUE_INIT);
1026 RECORD(EXPR_VA_ARG);
1027 RECORD(EXPR_ADDR_LABEL);
1028 RECORD(EXPR_STMT);
1029 RECORD(EXPR_TYPES_COMPATIBLE);
1030 RECORD(EXPR_CHOOSE);
1031 RECORD(EXPR_GNU_NULL);
1032 RECORD(EXPR_SHUFFLE_VECTOR);
1033 RECORD(EXPR_BLOCK);
1034 RECORD(EXPR_BLOCK_DECL_REF);
1035 RECORD(EXPR_OBJC_STRING_LITERAL);
1036 RECORD(EXPR_OBJC_ENCODE);
1037 RECORD(EXPR_OBJC_SELECTOR_EXPR);
1038 RECORD(EXPR_OBJC_PROTOCOL_EXPR);
1039 RECORD(EXPR_OBJC_IVAR_REF_EXPR);
1040 RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
1041 RECORD(EXPR_OBJC_KVC_REF_EXPR);
1042 RECORD(EXPR_OBJC_MESSAGE_EXPR);
1043 RECORD(EXPR_OBJC_SUPER_EXPR);
1044 RECORD(STMT_OBJC_FOR_COLLECTION);
1045 RECORD(STMT_OBJC_CATCH);
1046 RECORD(STMT_OBJC_FINALLY);
1047 RECORD(STMT_OBJC_AT_TRY);
1048 RECORD(STMT_OBJC_AT_SYNCHRONIZED);
1049 RECORD(STMT_OBJC_AT_THROW);
1050#undef RECORD
Chris Lattner920673a2009-04-26 22:26:21 +00001051}
1052
1053void PCHWriter::WriteBlockInfoBlock() {
1054 RecordData Record;
1055 Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
1056
Chris Lattner880f3f72009-04-27 00:40:25 +00001057#define BLOCK(X) EmitBlockID(pch::X ## _ID, #X, Stream, Record)
Chris Lattner920673a2009-04-26 22:26:21 +00001058#define RECORD(X) EmitRecordID(pch::X, #X, Stream, Record)
1059
1060 // PCH Top-Level Block.
Chris Lattner880f3f72009-04-27 00:40:25 +00001061 BLOCK(PCH_BLOCK);
Chris Lattner920673a2009-04-26 22:26:21 +00001062 RECORD(TYPE_OFFSET);
1063 RECORD(DECL_OFFSET);
1064 RECORD(LANGUAGE_OPTIONS);
1065 RECORD(TARGET_TRIPLE);
1066 RECORD(IDENTIFIER_OFFSET);
1067 RECORD(IDENTIFIER_TABLE);
1068 RECORD(EXTERNAL_DEFINITIONS);
1069 RECORD(SPECIAL_TYPES);
1070 RECORD(STATISTICS);
1071 RECORD(TENTATIVE_DEFINITIONS);
1072 RECORD(LOCALLY_SCOPED_EXTERNAL_DECLS);
1073 RECORD(SELECTOR_OFFSETS);
1074 RECORD(METHOD_POOL);
1075 RECORD(PP_COUNTER_VALUE);
1076
1077 // SourceManager Block.
Chris Lattner880f3f72009-04-27 00:40:25 +00001078 BLOCK(SOURCE_MANAGER_BLOCK);
Chris Lattner920673a2009-04-26 22:26:21 +00001079 RECORD(SM_SLOC_FILE_ENTRY);
1080 RECORD(SM_SLOC_BUFFER_ENTRY);
1081 RECORD(SM_SLOC_BUFFER_BLOB);
1082 RECORD(SM_SLOC_INSTANTIATION_ENTRY);
1083 RECORD(SM_LINE_TABLE);
1084 RECORD(SM_HEADER_FILE_INFO);
1085
1086 // Preprocessor Block.
Chris Lattner880f3f72009-04-27 00:40:25 +00001087 BLOCK(PREPROCESSOR_BLOCK);
Chris Lattner920673a2009-04-26 22:26:21 +00001088 RECORD(PP_MACRO_OBJECT_LIKE);
1089 RECORD(PP_MACRO_FUNCTION_LIKE);
1090 RECORD(PP_TOKEN);
1091
1092 // Types block.
Chris Lattner880f3f72009-04-27 00:40:25 +00001093 BLOCK(TYPES_BLOCK);
Chris Lattner920673a2009-04-26 22:26:21 +00001094 RECORD(TYPE_EXT_QUAL);
1095 RECORD(TYPE_FIXED_WIDTH_INT);
1096 RECORD(TYPE_COMPLEX);
1097 RECORD(TYPE_POINTER);
1098 RECORD(TYPE_BLOCK_POINTER);
1099 RECORD(TYPE_LVALUE_REFERENCE);
1100 RECORD(TYPE_RVALUE_REFERENCE);
1101 RECORD(TYPE_MEMBER_POINTER);
1102 RECORD(TYPE_CONSTANT_ARRAY);
1103 RECORD(TYPE_INCOMPLETE_ARRAY);
1104 RECORD(TYPE_VARIABLE_ARRAY);
1105 RECORD(TYPE_VECTOR);
1106 RECORD(TYPE_EXT_VECTOR);
1107 RECORD(TYPE_FUNCTION_PROTO);
1108 RECORD(TYPE_FUNCTION_NO_PROTO);
1109 RECORD(TYPE_TYPEDEF);
1110 RECORD(TYPE_TYPEOF_EXPR);
1111 RECORD(TYPE_TYPEOF);
1112 RECORD(TYPE_RECORD);
1113 RECORD(TYPE_ENUM);
1114 RECORD(TYPE_OBJC_INTERFACE);
1115 RECORD(TYPE_OBJC_QUALIFIED_INTERFACE);
1116 RECORD(TYPE_OBJC_QUALIFIED_ID);
Chris Lattnerd16afaa2009-04-27 00:49:53 +00001117 // Statements and Exprs can occur in the Types block.
1118 AddStmtsExprs(Stream, Record);
1119
Chris Lattner920673a2009-04-26 22:26:21 +00001120 // Decls block.
Chris Lattner880f3f72009-04-27 00:40:25 +00001121 BLOCK(DECLS_BLOCK);
Chris Lattner8a0e3162009-04-26 22:32:16 +00001122 RECORD(DECL_ATTR);
1123 RECORD(DECL_TRANSLATION_UNIT);
1124 RECORD(DECL_TYPEDEF);
1125 RECORD(DECL_ENUM);
1126 RECORD(DECL_RECORD);
1127 RECORD(DECL_ENUM_CONSTANT);
1128 RECORD(DECL_FUNCTION);
1129 RECORD(DECL_OBJC_METHOD);
1130 RECORD(DECL_OBJC_INTERFACE);
1131 RECORD(DECL_OBJC_PROTOCOL);
1132 RECORD(DECL_OBJC_IVAR);
1133 RECORD(DECL_OBJC_AT_DEFS_FIELD);
1134 RECORD(DECL_OBJC_CLASS);
1135 RECORD(DECL_OBJC_FORWARD_PROTOCOL);
1136 RECORD(DECL_OBJC_CATEGORY);
1137 RECORD(DECL_OBJC_CATEGORY_IMPL);
1138 RECORD(DECL_OBJC_IMPLEMENTATION);
1139 RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1140 RECORD(DECL_OBJC_PROPERTY);
1141 RECORD(DECL_OBJC_PROPERTY_IMPL);
Chris Lattner920673a2009-04-26 22:26:21 +00001142 RECORD(DECL_FIELD);
1143 RECORD(DECL_VAR);
Chris Lattner8a0e3162009-04-26 22:32:16 +00001144 RECORD(DECL_IMPLICIT_PARAM);
Chris Lattner920673a2009-04-26 22:26:21 +00001145 RECORD(DECL_PARM_VAR);
Chris Lattner8a0e3162009-04-26 22:32:16 +00001146 RECORD(DECL_ORIGINAL_PARM_VAR);
1147 RECORD(DECL_FILE_SCOPE_ASM);
1148 RECORD(DECL_BLOCK);
1149 RECORD(DECL_CONTEXT_LEXICAL);
1150 RECORD(DECL_CONTEXT_VISIBLE);
Chris Lattnerd16afaa2009-04-27 00:49:53 +00001151 // Statements and Exprs can occur in the Decls block.
1152 AddStmtsExprs(Stream, Record);
Chris Lattner920673a2009-04-26 22:26:21 +00001153#undef RECORD
1154#undef BLOCK
1155 Stream.ExitBlock();
1156}
1157
1158
Douglas Gregorb5887f32009-04-10 21:16:55 +00001159/// \brief Write the target triple (e.g., i686-apple-darwin9).
1160void PCHWriter::WriteTargetTriple(const TargetInfo &Target) {
1161 using namespace llvm;
1162 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1163 Abbrev->Add(BitCodeAbbrevOp(pch::TARGET_TRIPLE));
1164 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Triple name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001165 unsigned TripleAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregorb5887f32009-04-10 21:16:55 +00001166
1167 RecordData Record;
1168 Record.push_back(pch::TARGET_TRIPLE);
1169 const char *Triple = Target.getTargetTriple();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001170 Stream.EmitRecordWithBlob(TripleAbbrev, Record, Triple, strlen(Triple));
Douglas Gregorb5887f32009-04-10 21:16:55 +00001171}
1172
1173/// \brief Write the LangOptions structure.
Douglas Gregor179cfb12009-04-10 20:39:37 +00001174void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) {
1175 RecordData Record;
1176 Record.push_back(LangOpts.Trigraphs);
1177 Record.push_back(LangOpts.BCPLComment); // BCPL-style '//' comments.
1178 Record.push_back(LangOpts.DollarIdents); // '$' allowed in identifiers.
1179 Record.push_back(LangOpts.AsmPreprocessor); // Preprocessor in asm mode.
1180 Record.push_back(LangOpts.GNUMode); // True in gnu99 mode false in c99 mode (etc)
1181 Record.push_back(LangOpts.ImplicitInt); // C89 implicit 'int'.
1182 Record.push_back(LangOpts.Digraphs); // C94, C99 and C++
1183 Record.push_back(LangOpts.HexFloats); // C99 Hexadecimal float constants.
1184 Record.push_back(LangOpts.C99); // C99 Support
1185 Record.push_back(LangOpts.Microsoft); // Microsoft extensions.
1186 Record.push_back(LangOpts.CPlusPlus); // C++ Support
1187 Record.push_back(LangOpts.CPlusPlus0x); // C++0x Support
1188 Record.push_back(LangOpts.NoExtensions); // All extensions are disabled, strict mode.
1189 Record.push_back(LangOpts.CXXOperatorNames); // Treat C++ operator names as keywords.
1190
1191 Record.push_back(LangOpts.ObjC1); // Objective-C 1 support enabled.
1192 Record.push_back(LangOpts.ObjC2); // Objective-C 2 support enabled.
1193 Record.push_back(LangOpts.ObjCNonFragileABI); // Objective-C modern abi enabled
1194
1195 Record.push_back(LangOpts.PascalStrings); // Allow Pascal strings
1196 Record.push_back(LangOpts.Boolean); // Allow bool/true/false
1197 Record.push_back(LangOpts.WritableStrings); // Allow writable strings
1198 Record.push_back(LangOpts.LaxVectorConversions);
1199 Record.push_back(LangOpts.Exceptions); // Support exception handling.
1200
1201 Record.push_back(LangOpts.NeXTRuntime); // Use NeXT runtime.
1202 Record.push_back(LangOpts.Freestanding); // Freestanding implementation
1203 Record.push_back(LangOpts.NoBuiltin); // Do not use builtin functions (-fno-builtin)
1204
1205 Record.push_back(LangOpts.ThreadsafeStatics); // Whether static initializers are protected
1206 // by locks.
1207 Record.push_back(LangOpts.Blocks); // block extension to C
1208 Record.push_back(LangOpts.EmitAllDecls); // Emit all declarations, even if
1209 // they are unused.
1210 Record.push_back(LangOpts.MathErrno); // Math functions must respect errno
1211 // (modulo the platform support).
1212
1213 Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when
1214 // signed integer arithmetic overflows.
1215
1216 Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and
1217 // may be ripped out at any time.
1218
1219 Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined.
1220 Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be
1221 // defined.
1222 Record.push_back(LangOpts.Static); // Should __STATIC__ be defined (as
1223 // opposed to __DYNAMIC__).
1224 Record.push_back(LangOpts.PICLevel); // The value for __PIC__, if non-zero.
1225
1226 Record.push_back(LangOpts.GNUInline); // Should GNU inline semantics be
1227 // used (instead of C99 semantics).
1228 Record.push_back(LangOpts.NoInline); // Should __NO_INLINE__ be defined.
1229 Record.push_back(LangOpts.getGCMode());
1230 Record.push_back(LangOpts.getVisibilityMode());
1231 Record.push_back(LangOpts.InstantiationDepth);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001232 Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record);
Douglas Gregor179cfb12009-04-10 20:39:37 +00001233}
1234
Douglas Gregorab1cef72009-04-10 03:52:48 +00001235//===----------------------------------------------------------------------===//
1236// Source Manager Serialization
1237//===----------------------------------------------------------------------===//
1238
1239/// \brief Create an abbreviation for the SLocEntry that refers to a
1240/// file.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001241static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001242 using namespace llvm;
1243 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1244 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_FILE_ENTRY));
1245 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1246 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1247 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1248 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
Douglas Gregorab1cef72009-04-10 03:52:48 +00001249 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001250 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001251}
1252
1253/// \brief Create an abbreviation for the SLocEntry that refers to a
1254/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001255static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001256 using namespace llvm;
1257 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1258 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_ENTRY));
1259 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1260 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1261 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1262 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1263 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001264 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001265}
1266
1267/// \brief Create an abbreviation for the SLocEntry that refers to a
1268/// buffer's blob.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001269static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001270 using namespace llvm;
1271 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1272 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_BUFFER_BLOB));
1273 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001274 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001275}
1276
1277/// \brief Create an abbreviation for the SLocEntry that refers to an
1278/// buffer.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001279static unsigned CreateSLocInstantiationAbbrev(llvm::BitstreamWriter &Stream) {
Douglas Gregorab1cef72009-04-10 03:52:48 +00001280 using namespace llvm;
1281 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1282 Abbrev->Add(BitCodeAbbrevOp(pch::SM_SLOC_INSTANTIATION_ENTRY));
1283 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1284 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1285 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1286 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
Douglas Gregor364e5802009-04-15 18:05:10 +00001287 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001288 return Stream.EmitAbbrev(Abbrev);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001289}
1290
1291/// \brief Writes the block containing the serialized form of the
1292/// source manager.
1293///
1294/// TODO: We should probably use an on-disk hash table (stored in a
1295/// blob), indexed based on the file name, so that we only create
1296/// entries for files that we actually need. In the common case (no
1297/// errors), we probably won't have to create file entries for any of
1298/// the files in the AST.
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001299void PCHWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
1300 const Preprocessor &PP) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001301 // Enter the source manager block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001302 Stream.EnterSubblock(pch::SOURCE_MANAGER_BLOCK_ID, 3);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001303
1304 // Abbreviations for the various kinds of source-location entries.
1305 int SLocFileAbbrv = -1;
1306 int SLocBufferAbbrv = -1;
1307 int SLocBufferBlobAbbrv = -1;
1308 int SLocInstantiationAbbrv = -1;
1309
1310 // Write out the source location entry table. We skip the first
1311 // entry, which is always the same dummy entry.
1312 RecordData Record;
1313 for (SourceManager::sloc_entry_iterator
1314 SLoc = SourceMgr.sloc_entry_begin() + 1,
1315 SLocEnd = SourceMgr.sloc_entry_end();
1316 SLoc != SLocEnd; ++SLoc) {
1317 // Figure out which record code to use.
1318 unsigned Code;
1319 if (SLoc->isFile()) {
1320 if (SLoc->getFile().getContentCache()->Entry)
1321 Code = pch::SM_SLOC_FILE_ENTRY;
1322 else
1323 Code = pch::SM_SLOC_BUFFER_ENTRY;
1324 } else
1325 Code = pch::SM_SLOC_INSTANTIATION_ENTRY;
1326 Record.push_back(Code);
1327
1328 Record.push_back(SLoc->getOffset());
1329 if (SLoc->isFile()) {
1330 const SrcMgr::FileInfo &File = SLoc->getFile();
1331 Record.push_back(File.getIncludeLoc().getRawEncoding());
1332 Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
Douglas Gregor635f97f2009-04-13 16:31:14 +00001333 Record.push_back(File.hasLineDirectives());
Douglas Gregorab1cef72009-04-10 03:52:48 +00001334
1335 const SrcMgr::ContentCache *Content = File.getContentCache();
1336 if (Content->Entry) {
1337 // The source location entry is a file. The blob associated
1338 // with this entry is the file name.
1339 if (SLocFileAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001340 SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1341 Stream.EmitRecordWithBlob(SLocFileAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001342 Content->Entry->getName(),
1343 strlen(Content->Entry->getName()));
1344 } else {
1345 // The source location entry is a buffer. The blob associated
1346 // with this entry contains the contents of the buffer.
1347 if (SLocBufferAbbrv == -1) {
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001348 SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1349 SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001350 }
1351
1352 // We add one to the size so that we capture the trailing NULL
1353 // that is required by llvm::MemoryBuffer::getMemBuffer (on
1354 // the reader side).
1355 const llvm::MemoryBuffer *Buffer = Content->getBuffer();
1356 const char *Name = Buffer->getBufferIdentifier();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001357 Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record, Name, strlen(Name) + 1);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001358 Record.clear();
1359 Record.push_back(pch::SM_SLOC_BUFFER_BLOB);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001360 Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
Douglas Gregorab1cef72009-04-10 03:52:48 +00001361 Buffer->getBufferStart(),
1362 Buffer->getBufferSize() + 1);
1363 }
1364 } else {
1365 // The source location entry is an instantiation.
1366 const SrcMgr::InstantiationInfo &Inst = SLoc->getInstantiation();
1367 Record.push_back(Inst.getSpellingLoc().getRawEncoding());
1368 Record.push_back(Inst.getInstantiationLocStart().getRawEncoding());
1369 Record.push_back(Inst.getInstantiationLocEnd().getRawEncoding());
1370
Douglas Gregor364e5802009-04-15 18:05:10 +00001371 // Compute the token length for this macro expansion.
1372 unsigned NextOffset = SourceMgr.getNextOffset();
1373 SourceManager::sloc_entry_iterator NextSLoc = SLoc;
1374 if (++NextSLoc != SLocEnd)
1375 NextOffset = NextSLoc->getOffset();
1376 Record.push_back(NextOffset - SLoc->getOffset() - 1);
1377
Douglas Gregorab1cef72009-04-10 03:52:48 +00001378 if (SLocInstantiationAbbrv == -1)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001379 SLocInstantiationAbbrv = CreateSLocInstantiationAbbrev(Stream);
1380 Stream.EmitRecordWithAbbrev(SLocInstantiationAbbrv, Record);
Douglas Gregorab1cef72009-04-10 03:52:48 +00001381 }
1382
1383 Record.clear();
1384 }
1385
Douglas Gregor635f97f2009-04-13 16:31:14 +00001386 // Write the line table.
1387 if (SourceMgr.hasLineTable()) {
1388 LineTableInfo &LineTable = SourceMgr.getLineTable();
1389
1390 // Emit the file names
1391 Record.push_back(LineTable.getNumFilenames());
1392 for (unsigned I = 0, N = LineTable.getNumFilenames(); I != N; ++I) {
1393 // Emit the file name
1394 const char *Filename = LineTable.getFilename(I);
1395 unsigned FilenameLen = Filename? strlen(Filename) : 0;
1396 Record.push_back(FilenameLen);
1397 if (FilenameLen)
1398 Record.insert(Record.end(), Filename, Filename + FilenameLen);
1399 }
1400
1401 // Emit the line entries
1402 for (LineTableInfo::iterator L = LineTable.begin(), LEnd = LineTable.end();
1403 L != LEnd; ++L) {
1404 // Emit the file ID
1405 Record.push_back(L->first);
1406
1407 // Emit the line entries
1408 Record.push_back(L->second.size());
1409 for (std::vector<LineEntry>::iterator LE = L->second.begin(),
1410 LEEnd = L->second.end();
1411 LE != LEEnd; ++LE) {
1412 Record.push_back(LE->FileOffset);
1413 Record.push_back(LE->LineNo);
1414 Record.push_back(LE->FilenameID);
1415 Record.push_back((unsigned)LE->FileKind);
1416 Record.push_back(LE->IncludeOffset);
1417 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001418 Stream.EmitRecord(pch::SM_LINE_TABLE, Record);
Douglas Gregor635f97f2009-04-13 16:31:14 +00001419 }
1420 }
1421
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001422 // Loop over all the header files.
1423 HeaderSearch &HS = PP.getHeaderSearchInfo();
1424 for (HeaderSearch::header_file_iterator I = HS.header_file_begin(),
1425 E = HS.header_file_end();
1426 I != E; ++I) {
1427 Record.push_back(I->isImport);
1428 Record.push_back(I->DirInfo);
1429 Record.push_back(I->NumIncludes);
1430 if (I->ControllingMacro)
1431 AddIdentifierRef(I->ControllingMacro, Record);
1432 else
1433 Record.push_back(0);
1434 Stream.EmitRecord(pch::SM_HEADER_FILE_INFO, Record);
1435 Record.clear();
1436 }
1437
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001438 Stream.ExitBlock();
Douglas Gregorab1cef72009-04-10 03:52:48 +00001439}
1440
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001441/// \brief Writes the block containing the serialized form of the
1442/// preprocessor.
1443///
Chris Lattner850eabd2009-04-10 18:08:30 +00001444void PCHWriter::WritePreprocessor(const Preprocessor &PP) {
Chris Lattner1b094952009-04-10 18:00:12 +00001445 RecordData Record;
Chris Lattner84b04f12009-04-10 17:16:57 +00001446
Chris Lattner4b21c202009-04-13 01:29:17 +00001447 // If the preprocessor __COUNTER__ value has been bumped, remember it.
1448 if (PP.getCounterValue() != 0) {
1449 Record.push_back(PP.getCounterValue());
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001450 Stream.EmitRecord(pch::PP_COUNTER_VALUE, Record);
Chris Lattner4b21c202009-04-13 01:29:17 +00001451 Record.clear();
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001452 }
1453
1454 // Enter the preprocessor block.
1455 Stream.EnterSubblock(pch::PREPROCESSOR_BLOCK_ID, 2);
Chris Lattner4b21c202009-04-13 01:29:17 +00001456
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00001457 // If the PCH file contains __DATE__ or __TIME__ emit a warning about this.
1458 // FIXME: use diagnostics subsystem for localization etc.
1459 if (PP.SawDateOrTime())
1460 fprintf(stderr, "warning: precompiled header used __DATE__ or __TIME__.\n");
1461
Chris Lattner1b094952009-04-10 18:00:12 +00001462 // Loop over all the macro definitions that are live at the end of the file,
1463 // emitting each to the PP section.
Chris Lattner1b094952009-04-10 18:00:12 +00001464 for (Preprocessor::macro_iterator I = PP.macro_begin(), E = PP.macro_end();
1465 I != E; ++I) {
Chris Lattnerdb1c81b2009-04-10 21:41:48 +00001466 // FIXME: This emits macros in hash table order, we should do it in a stable
1467 // order so that output is reproducible.
Chris Lattner1b094952009-04-10 18:00:12 +00001468 MacroInfo *MI = I->second;
1469
1470 // Don't emit builtin macros like __LINE__ to the PCH file unless they have
1471 // been redefined by the header (in which case they are not isBuiltinMacro).
1472 if (MI->isBuiltinMacro())
1473 continue;
1474
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001475 // FIXME: Remove this identifier reference?
Chris Lattner29241862009-04-11 21:15:38 +00001476 AddIdentifierRef(I->first, Record);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001477 MacroOffsets[I->first] = Stream.GetCurrentBitNo();
Chris Lattner1b094952009-04-10 18:00:12 +00001478 Record.push_back(MI->getDefinitionLoc().getRawEncoding());
1479 Record.push_back(MI->isUsed());
1480
1481 unsigned Code;
1482 if (MI->isObjectLike()) {
1483 Code = pch::PP_MACRO_OBJECT_LIKE;
1484 } else {
1485 Code = pch::PP_MACRO_FUNCTION_LIKE;
1486
1487 Record.push_back(MI->isC99Varargs());
1488 Record.push_back(MI->isGNUVarargs());
1489 Record.push_back(MI->getNumArgs());
1490 for (MacroInfo::arg_iterator I = MI->arg_begin(), E = MI->arg_end();
1491 I != E; ++I)
Chris Lattner29241862009-04-11 21:15:38 +00001492 AddIdentifierRef(*I, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001493 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001494 Stream.EmitRecord(Code, Record);
Chris Lattner1b094952009-04-10 18:00:12 +00001495 Record.clear();
1496
Chris Lattner850eabd2009-04-10 18:08:30 +00001497 // Emit the tokens array.
1498 for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
1499 // Note that we know that the preprocessor does not have any annotation
1500 // tokens in it because they are created by the parser, and thus can't be
1501 // in a macro definition.
1502 const Token &Tok = MI->getReplacementToken(TokNo);
1503
1504 Record.push_back(Tok.getLocation().getRawEncoding());
1505 Record.push_back(Tok.getLength());
1506
Chris Lattner850eabd2009-04-10 18:08:30 +00001507 // FIXME: When reading literal tokens, reconstruct the literal pointer if
1508 // it is needed.
Chris Lattner29241862009-04-11 21:15:38 +00001509 AddIdentifierRef(Tok.getIdentifierInfo(), Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001510
1511 // FIXME: Should translate token kind to a stable encoding.
1512 Record.push_back(Tok.getKind());
1513 // FIXME: Should translate token flags to a stable encoding.
1514 Record.push_back(Tok.getFlags());
1515
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001516 Stream.EmitRecord(pch::PP_TOKEN, Record);
Chris Lattner850eabd2009-04-10 18:08:30 +00001517 Record.clear();
1518 }
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001519 ++NumMacros;
Chris Lattner1b094952009-04-10 18:00:12 +00001520 }
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001521 Stream.ExitBlock();
Chris Lattnerffc05ed2009-04-10 17:15:23 +00001522}
1523
1524
Douglas Gregorc34897d2009-04-09 22:27:44 +00001525/// \brief Write the representation of a type to the PCH stream.
1526void PCHWriter::WriteType(const Type *T) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001527 pch::TypeID &ID = TypeIDs[T];
Chris Lattner84b04f12009-04-10 17:16:57 +00001528 if (ID == 0) // we haven't seen this type before.
Douglas Gregorc34897d2009-04-09 22:27:44 +00001529 ID = NextTypeID++;
1530
1531 // Record the offset for this type.
1532 if (TypeOffsets.size() == ID - pch::NUM_PREDEF_TYPE_IDS)
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001533 TypeOffsets.push_back(Stream.GetCurrentBitNo());
Douglas Gregorc34897d2009-04-09 22:27:44 +00001534 else if (TypeOffsets.size() < ID - pch::NUM_PREDEF_TYPE_IDS) {
1535 TypeOffsets.resize(ID + 1 - pch::NUM_PREDEF_TYPE_IDS);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001536 TypeOffsets[ID - pch::NUM_PREDEF_TYPE_IDS] = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001537 }
1538
1539 RecordData Record;
1540
1541 // Emit the type's representation.
1542 PCHTypeWriter W(*this, Record);
1543 switch (T->getTypeClass()) {
1544 // For all of the concrete, non-dependent types, call the
1545 // appropriate visitor function.
1546#define TYPE(Class, Base) \
1547 case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
1548#define ABSTRACT_TYPE(Class, Base)
1549#define DEPENDENT_TYPE(Class, Base)
1550#include "clang/AST/TypeNodes.def"
1551
1552 // For all of the dependent type nodes (which only occur in C++
1553 // templates), produce an error.
1554#define TYPE(Class, Base)
1555#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1556#include "clang/AST/TypeNodes.def"
1557 assert(false && "Cannot serialize dependent type nodes");
1558 break;
1559 }
1560
1561 // Emit the serialized record.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001562 Stream.EmitRecord(W.Code, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00001563
1564 // Flush any expressions that were written as part of this type.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001565 FlushStmts();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001566}
1567
1568/// \brief Write a block containing all of the types.
1569void PCHWriter::WriteTypesBlock(ASTContext &Context) {
Chris Lattner84b04f12009-04-10 17:16:57 +00001570 // Enter the types block.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001571 Stream.EnterSubblock(pch::TYPES_BLOCK_ID, 2);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001572
Douglas Gregore43f0972009-04-26 03:49:13 +00001573 // Emit all of the types that need to be emitted (so far).
1574 while (!TypesToEmit.empty()) {
1575 const Type *T = TypesToEmit.front();
1576 TypesToEmit.pop();
1577 assert(!isa<BuiltinType>(T) && "Built-in types are not serialized");
1578 WriteType(T);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001579 }
1580
1581 // Exit the types block
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001582 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001583}
1584
1585/// \brief Write the block containing all of the declaration IDs
1586/// lexically declared within the given DeclContext.
1587///
1588/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
1589/// bistream, or 0 if no block was written.
1590uint64_t PCHWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
1591 DeclContext *DC) {
Douglas Gregorac8f2802009-04-10 17:25:41 +00001592 if (DC->decls_empty(Context))
Douglas Gregorc34897d2009-04-09 22:27:44 +00001593 return 0;
1594
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001595 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001596 RecordData Record;
1597 for (DeclContext::decl_iterator D = DC->decls_begin(Context),
1598 DEnd = DC->decls_end(Context);
1599 D != DEnd; ++D)
1600 AddDeclRef(*D, Record);
1601
Douglas Gregoraf136d92009-04-22 22:34:57 +00001602 ++NumLexicalDeclContexts;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001603 Stream.EmitRecord(pch::DECL_CONTEXT_LEXICAL, Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00001604 return Offset;
1605}
1606
1607/// \brief Write the block containing all of the declaration IDs
1608/// visible from the given DeclContext.
1609///
1610/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
1611/// bistream, or 0 if no block was written.
1612uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
1613 DeclContext *DC) {
1614 if (DC->getPrimaryContext() != DC)
1615 return 0;
1616
Douglas Gregor35ca85e2009-04-21 22:32:33 +00001617 // Since there is no name lookup into functions or methods, and we
1618 // perform name lookup for the translation unit via the
1619 // IdentifierInfo chains, don't bother to build a
1620 // visible-declarations table for these entities.
1621 if (DC->isFunctionOrMethod() || DC->isTranslationUnit())
Douglas Gregor5afd9802009-04-18 15:49:20 +00001622 return 0;
1623
Douglas Gregorc34897d2009-04-09 22:27:44 +00001624 // Force the DeclContext to build a its name-lookup table.
1625 DC->lookup(Context, DeclarationName());
1626
1627 // Serialize the contents of the mapping used for lookup. Note that,
1628 // although we have two very different code paths, the serialized
1629 // representation is the same for both cases: a declaration name,
1630 // followed by a size, followed by references to the visible
1631 // declarations that have that name.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001632 uint64_t Offset = Stream.GetCurrentBitNo();
Douglas Gregorc34897d2009-04-09 22:27:44 +00001633 RecordData Record;
1634 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(DC->getLookupPtr());
Douglas Gregor982365e2009-04-13 21:20:57 +00001635 if (!Map)
1636 return 0;
1637
Douglas Gregorc34897d2009-04-09 22:27:44 +00001638 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
1639 D != DEnd; ++D) {
1640 AddDeclarationName(D->first, Record);
1641 DeclContext::lookup_result Result = D->second.getLookupResult(Context);
1642 Record.push_back(Result.second - Result.first);
1643 for(; Result.first != Result.second; ++Result.first)
1644 AddDeclRef(*Result.first, Record);
1645 }
1646
1647 if (Record.size() == 0)
1648 return 0;
1649
Douglas Gregorc72f6c82009-04-16 22:23:12 +00001650 Stream.EmitRecord(pch::DECL_CONTEXT_VISIBLE, Record);
Douglas Gregoraf136d92009-04-22 22:34:57 +00001651 ++NumVisibleDeclContexts;
Douglas Gregorc34897d2009-04-09 22:27:44 +00001652 return Offset;
1653}
1654
Douglas Gregorff9a6092009-04-20 20:36:09 +00001655namespace {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001656// Trait used for the on-disk hash table used in the method pool.
1657class VISIBILITY_HIDDEN PCHMethodPoolTrait {
1658 PCHWriter &Writer;
1659
1660public:
1661 typedef Selector key_type;
1662 typedef key_type key_type_ref;
1663
1664 typedef std::pair<ObjCMethodList, ObjCMethodList> data_type;
1665 typedef const data_type& data_type_ref;
1666
1667 explicit PCHMethodPoolTrait(PCHWriter &Writer) : Writer(Writer) { }
1668
1669 static unsigned ComputeHash(Selector Sel) {
1670 unsigned N = Sel.getNumArgs();
1671 if (N == 0)
1672 ++N;
1673 unsigned R = 5381;
1674 for (unsigned I = 0; I != N; ++I)
1675 if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
1676 R = clang::BernsteinHashPartial(II->getName(), II->getLength(), R);
1677 return R;
1678 }
1679
1680 std::pair<unsigned,unsigned>
1681 EmitKeyDataLength(llvm::raw_ostream& Out, Selector Sel,
1682 data_type_ref Methods) {
1683 unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
1684 clang::io::Emit16(Out, KeyLen);
1685 unsigned DataLen = 2 + 2; // 2 bytes for each of the method counts
1686 for (const ObjCMethodList *Method = &Methods.first; Method;
1687 Method = Method->Next)
1688 if (Method->Method)
1689 DataLen += 4;
1690 for (const ObjCMethodList *Method = &Methods.second; Method;
1691 Method = Method->Next)
1692 if (Method->Method)
1693 DataLen += 4;
1694 clang::io::Emit16(Out, DataLen);
1695 return std::make_pair(KeyLen, DataLen);
1696 }
1697
Douglas Gregor2d711832009-04-25 17:48:32 +00001698 void EmitKey(llvm::raw_ostream& Out, Selector Sel, unsigned) {
1699 uint64_t Start = Out.tell();
1700 assert((Start >> 32) == 0 && "Selector key offset too large");
1701 Writer.SetSelectorOffset(Sel, Start);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001702 unsigned N = Sel.getNumArgs();
1703 clang::io::Emit16(Out, N);
1704 if (N == 0)
1705 N = 1;
1706 for (unsigned I = 0; I != N; ++I)
1707 clang::io::Emit32(Out,
1708 Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
1709 }
1710
1711 void EmitData(llvm::raw_ostream& Out, key_type_ref,
Douglas Gregor9c266982009-04-24 21:49:02 +00001712 data_type_ref Methods, unsigned DataLen) {
1713 uint64_t Start = Out.tell(); (void)Start;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001714 unsigned NumInstanceMethods = 0;
1715 for (const ObjCMethodList *Method = &Methods.first; Method;
1716 Method = Method->Next)
1717 if (Method->Method)
1718 ++NumInstanceMethods;
1719
1720 unsigned NumFactoryMethods = 0;
1721 for (const ObjCMethodList *Method = &Methods.second; Method;
1722 Method = Method->Next)
1723 if (Method->Method)
1724 ++NumFactoryMethods;
1725
1726 clang::io::Emit16(Out, NumInstanceMethods);
1727 clang::io::Emit16(Out, NumFactoryMethods);
1728 for (const ObjCMethodList *Method = &Methods.first; Method;
1729 Method = Method->Next)
1730 if (Method->Method)
1731 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001732 for (const ObjCMethodList *Method = &Methods.second; Method;
1733 Method = Method->Next)
1734 if (Method->Method)
1735 clang::io::Emit32(Out, Writer.getDeclID(Method->Method));
Douglas Gregor9c266982009-04-24 21:49:02 +00001736
1737 assert(Out.tell() - Start == DataLen && "Data length is wrong");
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001738 }
1739};
1740} // end anonymous namespace
1741
1742/// \brief Write the method pool into the PCH file.
1743///
1744/// The method pool contains both instance and factory methods, stored
1745/// in an on-disk hash table indexed by the selector.
1746void PCHWriter::WriteMethodPool(Sema &SemaRef) {
1747 using namespace llvm;
1748
1749 // Create and write out the blob that contains the instance and
1750 // factor method pools.
1751 bool Empty = true;
1752 {
1753 OnDiskChainedHashTableGenerator<PCHMethodPoolTrait> Generator;
1754
1755 // Create the on-disk hash table representation. Start by
1756 // iterating through the instance method pool.
1757 PCHMethodPoolTrait::key_type Key;
Douglas Gregor2d711832009-04-25 17:48:32 +00001758 unsigned NumSelectorsInMethodPool = 0;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001759 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
1760 Instance = SemaRef.InstanceMethodPool.begin(),
1761 InstanceEnd = SemaRef.InstanceMethodPool.end();
1762 Instance != InstanceEnd; ++Instance) {
1763 // Check whether there is a factory method with the same
1764 // selector.
1765 llvm::DenseMap<Selector, ObjCMethodList>::iterator Factory
1766 = SemaRef.FactoryMethodPool.find(Instance->first);
1767
1768 if (Factory == SemaRef.FactoryMethodPool.end())
1769 Generator.insert(Instance->first,
1770 std::make_pair(Instance->second,
1771 ObjCMethodList()));
1772 else
1773 Generator.insert(Instance->first,
1774 std::make_pair(Instance->second, Factory->second));
1775
Douglas Gregor2d711832009-04-25 17:48:32 +00001776 ++NumSelectorsInMethodPool;
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001777 Empty = false;
1778 }
1779
1780 // Now iterate through the factory method pool, to pick up any
1781 // selectors that weren't already in the instance method pool.
1782 for (llvm::DenseMap<Selector, ObjCMethodList>::iterator
1783 Factory = SemaRef.FactoryMethodPool.begin(),
1784 FactoryEnd = SemaRef.FactoryMethodPool.end();
1785 Factory != FactoryEnd; ++Factory) {
1786 // Check whether there is an instance method with the same
1787 // selector. If so, there is no work to do here.
1788 llvm::DenseMap<Selector, ObjCMethodList>::iterator Instance
1789 = SemaRef.InstanceMethodPool.find(Factory->first);
1790
Douglas Gregor2d711832009-04-25 17:48:32 +00001791 if (Instance == SemaRef.InstanceMethodPool.end()) {
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001792 Generator.insert(Factory->first,
1793 std::make_pair(ObjCMethodList(), Factory->second));
Douglas Gregor2d711832009-04-25 17:48:32 +00001794 ++NumSelectorsInMethodPool;
1795 }
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001796
1797 Empty = false;
1798 }
1799
Douglas Gregor2d711832009-04-25 17:48:32 +00001800 if (Empty && SelectorOffsets.empty())
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001801 return;
1802
1803 // Create the on-disk hash table in a buffer.
1804 llvm::SmallVector<char, 4096> MethodPool;
1805 uint32_t BucketOffset;
Douglas Gregor2d711832009-04-25 17:48:32 +00001806 SelectorOffsets.resize(SelVector.size());
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001807 {
1808 PCHMethodPoolTrait Trait(*this);
1809 llvm::raw_svector_ostream Out(MethodPool);
1810 // Make sure that no bucket is at offset 0
Douglas Gregor9c266982009-04-24 21:49:02 +00001811 clang::io::Emit32(Out, 0);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001812 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor2d711832009-04-25 17:48:32 +00001813
1814 // For every selector that we have seen but which was not
1815 // written into the hash table, write the selector itself and
1816 // record it's offset.
1817 for (unsigned I = 0, N = SelVector.size(); I != N; ++I)
1818 if (SelectorOffsets[I] == 0)
1819 Trait.EmitKey(Out, SelVector[I], 0);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001820 }
1821
1822 // Create a blob abbreviation
1823 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
1824 Abbrev->Add(BitCodeAbbrevOp(pch::METHOD_POOL));
1825 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregor2d711832009-04-25 17:48:32 +00001826 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001827 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1828 unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
1829
Douglas Gregor2d711832009-04-25 17:48:32 +00001830 // Write the method pool
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001831 RecordData Record;
1832 Record.push_back(pch::METHOD_POOL);
1833 Record.push_back(BucketOffset);
Douglas Gregor2d711832009-04-25 17:48:32 +00001834 Record.push_back(NumSelectorsInMethodPool);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001835 Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record,
1836 &MethodPool.front(),
1837 MethodPool.size());
Douglas Gregor2d711832009-04-25 17:48:32 +00001838
1839 // Create a blob abbreviation for the selector table offsets.
1840 Abbrev = new BitCodeAbbrev();
1841 Abbrev->Add(BitCodeAbbrevOp(pch::SELECTOR_OFFSETS));
1842 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // index
1843 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1844 unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
1845
1846 // Write the selector offsets table.
1847 Record.clear();
1848 Record.push_back(pch::SELECTOR_OFFSETS);
1849 Record.push_back(SelectorOffsets.size());
1850 Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
1851 (const char *)&SelectorOffsets.front(),
1852 SelectorOffsets.size() * 4);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001853 }
1854}
1855
1856namespace {
Douglas Gregorff9a6092009-04-20 20:36:09 +00001857class VISIBILITY_HIDDEN PCHIdentifierTableTrait {
1858 PCHWriter &Writer;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001859 Preprocessor &PP;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001860
1861public:
1862 typedef const IdentifierInfo* key_type;
1863 typedef key_type key_type_ref;
1864
1865 typedef pch::IdentID data_type;
1866 typedef data_type data_type_ref;
1867
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001868 PCHIdentifierTableTrait(PCHWriter &Writer, Preprocessor &PP)
1869 : Writer(Writer), PP(PP) { }
Douglas Gregorff9a6092009-04-20 20:36:09 +00001870
1871 static unsigned ComputeHash(const IdentifierInfo* II) {
1872 return clang::BernsteinHash(II->getName());
1873 }
1874
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001875 std::pair<unsigned,unsigned>
Douglas Gregorff9a6092009-04-20 20:36:09 +00001876 EmitKeyDataLength(llvm::raw_ostream& Out, const IdentifierInfo* II,
1877 pch::IdentID ID) {
1878 unsigned KeyLen = strlen(II->getName()) + 1;
Douglas Gregorc713da92009-04-21 22:25:48 +00001879 unsigned DataLen = 4 + 4; // 4 bytes for token ID, builtin, flags
1880 // 4 bytes for the persistent ID
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001881 if (II->hasMacroDefinition() &&
1882 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro())
1883 DataLen += 8;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001884 for (IdentifierResolver::iterator D = IdentifierResolver::begin(II),
1885 DEnd = IdentifierResolver::end();
1886 D != DEnd; ++D)
1887 DataLen += sizeof(pch::DeclID);
Douglas Gregor85c4a872009-04-25 21:04:17 +00001888 // We emit the key length after the data length so that the
1889 // "uninteresting" identifiers following the identifier hash table
1890 // structure will have the same (key length, key characters)
1891 // layout as the keys in the hash table. This also matches the
1892 // format for identifiers in pretokenized headers.
Douglas Gregorc713da92009-04-21 22:25:48 +00001893 clang::io::Emit16(Out, DataLen);
Douglas Gregor85c4a872009-04-25 21:04:17 +00001894 clang::io::Emit16(Out, KeyLen);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001895 return std::make_pair(KeyLen, DataLen);
1896 }
1897
1898 void EmitKey(llvm::raw_ostream& Out, const IdentifierInfo* II,
1899 unsigned KeyLen) {
1900 // Record the location of the key data. This is used when generating
1901 // the mapping from persistent IDs to strings.
1902 Writer.SetIdentifierOffset(II, Out.tell());
1903 Out.write(II->getName(), KeyLen);
1904 }
1905
1906 void EmitData(llvm::raw_ostream& Out, const IdentifierInfo* II,
1907 pch::IdentID ID, unsigned) {
1908 uint32_t Bits = 0;
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001909 bool hasMacroDefinition =
1910 II->hasMacroDefinition() &&
1911 !PP.getMacroInfo(const_cast<IdentifierInfo *>(II))->isBuiltinMacro();
Douglas Gregorff9a6092009-04-20 20:36:09 +00001912 Bits = Bits | (uint32_t)II->getTokenID();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00001913 Bits = (Bits << 10) | (uint32_t)II->getObjCOrBuiltinID();
1914 Bits = (Bits << 1) | hasMacroDefinition;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001915 Bits = (Bits << 1) | II->isExtensionToken();
1916 Bits = (Bits << 1) | II->isPoisoned();
1917 Bits = (Bits << 1) | II->isCPlusPlusOperatorKeyword();
1918 clang::io::Emit32(Out, Bits);
1919 clang::io::Emit32(Out, ID);
1920
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001921 if (hasMacroDefinition)
1922 clang::io::Emit64(Out, Writer.getMacroOffset(II));
1923
Douglas Gregorc713da92009-04-21 22:25:48 +00001924 // Emit the declaration IDs in reverse order, because the
1925 // IdentifierResolver provides the declarations as they would be
1926 // visible (e.g., the function "stat" would come before the struct
1927 // "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
1928 // adds declarations to the end of the list (so we need to see the
1929 // struct "status" before the function "status").
1930 llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
1931 IdentifierResolver::end());
1932 for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
1933 DEnd = Decls.rend();
Douglas Gregorff9a6092009-04-20 20:36:09 +00001934 D != DEnd; ++D)
Douglas Gregorc713da92009-04-21 22:25:48 +00001935 clang::io::Emit32(Out, Writer.getDeclID(*D));
Douglas Gregorff9a6092009-04-20 20:36:09 +00001936 }
1937};
1938} // end anonymous namespace
1939
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001940/// \brief Write the identifier table into the PCH file.
1941///
1942/// The identifier table consists of a blob containing string data
1943/// (the actual identifiers themselves) and a separate "offsets" index
1944/// that maps identifier IDs to locations within the blob.
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001945void PCHWriter::WriteIdentifierTable(Preprocessor &PP) {
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001946 using namespace llvm;
1947
1948 // Create and write out the blob that contains the identifier
1949 // strings.
Douglas Gregorff9a6092009-04-20 20:36:09 +00001950 IdentifierOffsets.resize(IdentifierIDs.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001951 {
Douglas Gregorff9a6092009-04-20 20:36:09 +00001952 OnDiskChainedHashTableGenerator<PCHIdentifierTableTrait> Generator;
1953
Douglas Gregor85c4a872009-04-25 21:04:17 +00001954 llvm::SmallVector<const IdentifierInfo *, 32> UninterestingIdentifiers;
1955
Douglas Gregorff9a6092009-04-20 20:36:09 +00001956 // Create the on-disk hash table representation.
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001957 for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
1958 ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
1959 ID != IDEnd; ++ID) {
1960 assert(ID->first && "NULL identifier in identifier table");
Douglas Gregor85c4a872009-04-25 21:04:17 +00001961
1962 // Classify each identifier as either "interesting" or "not
1963 // interesting". Interesting identifiers are those that have
1964 // additional information that needs to be read from the PCH
1965 // file, e.g., a built-in ID, declaration chain, or macro
1966 // definition. These identifiers are placed into the hash table
1967 // so that they can be found when looked up in the user program.
1968 // All other identifiers are "uninteresting", which means that
1969 // the IdentifierInfo built by default has all of the
1970 // information we care about. Such identifiers are placed after
1971 // the hash table.
1972 const IdentifierInfo *II = ID->first;
1973 if (II->isPoisoned() ||
1974 II->isExtensionToken() ||
1975 II->hasMacroDefinition() ||
1976 II->getObjCOrBuiltinID() ||
1977 II->getFETokenInfo<void>())
1978 Generator.insert(ID->first, ID->second);
1979 else
1980 UninterestingIdentifiers.push_back(II);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001981 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00001982
Douglas Gregorff9a6092009-04-20 20:36:09 +00001983 // Create the on-disk hash table in a buffer.
1984 llvm::SmallVector<char, 4096> IdentifierTable;
Douglas Gregorc713da92009-04-21 22:25:48 +00001985 uint32_t BucketOffset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00001986 {
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00001987 PCHIdentifierTableTrait Trait(*this, PP);
Douglas Gregorff9a6092009-04-20 20:36:09 +00001988 llvm::raw_svector_ostream Out(IdentifierTable);
Douglas Gregorc3221aa2009-04-24 21:10:55 +00001989 // Make sure that no bucket is at offset 0
Douglas Gregor9c266982009-04-24 21:49:02 +00001990 clang::io::Emit32(Out, 0);
Douglas Gregorc713da92009-04-21 22:25:48 +00001991 BucketOffset = Generator.Emit(Out, Trait);
Douglas Gregor85c4a872009-04-25 21:04:17 +00001992
1993 for (unsigned I = 0, N = UninterestingIdentifiers.size(); I != N; ++I) {
1994 const IdentifierInfo *II = UninterestingIdentifiers[I];
1995 unsigned N = II->getLength() + 1;
1996 clang::io::Emit16(Out, N);
1997 SetIdentifierOffset(II, Out.tell());
1998 Out.write(II->getName(), N);
1999 }
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002000 }
2001
2002 // Create a blob abbreviation
2003 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2004 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_TABLE));
Douglas Gregorc713da92009-04-21 22:25:48 +00002005 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
Douglas Gregorff9a6092009-04-20 20:36:09 +00002006 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002007 unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002008
2009 // Write the identifier table
2010 RecordData Record;
2011 Record.push_back(pch::IDENTIFIER_TABLE);
Douglas Gregorc713da92009-04-21 22:25:48 +00002012 Record.push_back(BucketOffset);
Douglas Gregorff9a6092009-04-20 20:36:09 +00002013 Stream.EmitRecordWithBlob(IDTableAbbrev, Record,
2014 &IdentifierTable.front(),
2015 IdentifierTable.size());
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002016 }
2017
2018 // Write the offsets table for identifier IDs.
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002019 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2020 Abbrev->Add(BitCodeAbbrevOp(pch::IDENTIFIER_OFFSET));
2021 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
2022 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2023 unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2024
2025 RecordData Record;
2026 Record.push_back(pch::IDENTIFIER_OFFSET);
2027 Record.push_back(IdentifierOffsets.size());
2028 Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
2029 (const char *)&IdentifierOffsets.front(),
2030 IdentifierOffsets.size() * sizeof(uint32_t));
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002031}
2032
Douglas Gregor1c507882009-04-15 21:30:51 +00002033/// \brief Write a record containing the given attributes.
2034void PCHWriter::WriteAttributeRecord(const Attr *Attr) {
2035 RecordData Record;
2036 for (; Attr; Attr = Attr->getNext()) {
2037 Record.push_back(Attr->getKind()); // FIXME: stable encoding
2038 Record.push_back(Attr->isInherited());
2039 switch (Attr->getKind()) {
2040 case Attr::Alias:
2041 AddString(cast<AliasAttr>(Attr)->getAliasee(), Record);
2042 break;
2043
2044 case Attr::Aligned:
2045 Record.push_back(cast<AlignedAttr>(Attr)->getAlignment());
2046 break;
2047
2048 case Attr::AlwaysInline:
2049 break;
2050
2051 case Attr::AnalyzerNoReturn:
2052 break;
2053
2054 case Attr::Annotate:
2055 AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record);
2056 break;
2057
2058 case Attr::AsmLabel:
2059 AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record);
2060 break;
2061
2062 case Attr::Blocks:
2063 Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable
2064 break;
2065
2066 case Attr::Cleanup:
2067 AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record);
2068 break;
2069
2070 case Attr::Const:
2071 break;
2072
2073 case Attr::Constructor:
2074 Record.push_back(cast<ConstructorAttr>(Attr)->getPriority());
2075 break;
2076
2077 case Attr::DLLExport:
2078 case Attr::DLLImport:
2079 case Attr::Deprecated:
2080 break;
2081
2082 case Attr::Destructor:
2083 Record.push_back(cast<DestructorAttr>(Attr)->getPriority());
2084 break;
2085
2086 case Attr::FastCall:
2087 break;
2088
2089 case Attr::Format: {
2090 const FormatAttr *Format = cast<FormatAttr>(Attr);
2091 AddString(Format->getType(), Record);
2092 Record.push_back(Format->getFormatIdx());
2093 Record.push_back(Format->getFirstArg());
2094 break;
2095 }
2096
Chris Lattner15ce6cc2009-04-20 19:12:28 +00002097 case Attr::GNUInline:
Douglas Gregor1c507882009-04-15 21:30:51 +00002098 case Attr::IBOutletKind:
2099 case Attr::NoReturn:
2100 case Attr::NoThrow:
2101 case Attr::Nodebug:
2102 case Attr::Noinline:
2103 break;
2104
2105 case Attr::NonNull: {
2106 const NonNullAttr *NonNull = cast<NonNullAttr>(Attr);
2107 Record.push_back(NonNull->size());
2108 Record.insert(Record.end(), NonNull->begin(), NonNull->end());
2109 break;
2110 }
2111
2112 case Attr::ObjCException:
2113 case Attr::ObjCNSObject:
Ted Kremenekb98860c2009-04-25 00:17:17 +00002114 case Attr::ObjCOwnershipRetain:
Ted Kremenekaa6e3182009-04-24 23:09:54 +00002115 case Attr::ObjCOwnershipReturns:
Douglas Gregor1c507882009-04-15 21:30:51 +00002116 case Attr::Overloadable:
2117 break;
2118
2119 case Attr::Packed:
2120 Record.push_back(cast<PackedAttr>(Attr)->getAlignment());
2121 break;
2122
2123 case Attr::Pure:
2124 break;
2125
2126 case Attr::Regparm:
2127 Record.push_back(cast<RegparmAttr>(Attr)->getNumParams());
2128 break;
2129
2130 case Attr::Section:
2131 AddString(cast<SectionAttr>(Attr)->getName(), Record);
2132 break;
2133
2134 case Attr::StdCall:
2135 case Attr::TransparentUnion:
2136 case Attr::Unavailable:
2137 case Attr::Unused:
2138 case Attr::Used:
2139 break;
2140
2141 case Attr::Visibility:
2142 // FIXME: stable encoding
2143 Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility());
2144 break;
2145
2146 case Attr::WarnUnusedResult:
2147 case Attr::Weak:
2148 case Attr::WeakImport:
2149 break;
2150 }
2151 }
2152
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002153 Stream.EmitRecord(pch::DECL_ATTR, Record);
Douglas Gregor1c507882009-04-15 21:30:51 +00002154}
2155
2156void PCHWriter::AddString(const std::string &Str, RecordData &Record) {
2157 Record.push_back(Str.size());
2158 Record.insert(Record.end(), Str.begin(), Str.end());
2159}
2160
Douglas Gregorff9a6092009-04-20 20:36:09 +00002161/// \brief Note that the identifier II occurs at the given offset
2162/// within the identifier table.
2163void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
Douglas Gregorde44c9f2009-04-25 19:10:14 +00002164 IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
Douglas Gregorff9a6092009-04-20 20:36:09 +00002165}
2166
Douglas Gregor2d711832009-04-25 17:48:32 +00002167/// \brief Note that the selector Sel occurs at the given offset
2168/// within the method pool/selector table.
2169void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
2170 unsigned ID = SelectorIDs[Sel];
2171 assert(ID && "Unknown selector");
2172 SelectorOffsets[ID - 1] = Offset;
2173}
2174
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002175PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream)
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002176 : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS),
Douglas Gregoraf136d92009-04-22 22:34:57 +00002177 NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0),
2178 NumVisibleDeclContexts(0) { }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002179
Douglas Gregor87887da2009-04-20 15:53:59 +00002180void PCHWriter::WritePCH(Sema &SemaRef) {
Douglas Gregor24a224c2009-04-25 18:35:21 +00002181 using namespace llvm;
2182
Douglas Gregor87887da2009-04-20 15:53:59 +00002183 ASTContext &Context = SemaRef.Context;
2184 Preprocessor &PP = SemaRef.PP;
2185
Douglas Gregorc34897d2009-04-09 22:27:44 +00002186 // Emit the file header.
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002187 Stream.Emit((unsigned)'C', 8);
2188 Stream.Emit((unsigned)'P', 8);
2189 Stream.Emit((unsigned)'C', 8);
2190 Stream.Emit((unsigned)'H', 8);
Chris Lattner920673a2009-04-26 22:26:21 +00002191
2192 WriteBlockInfoBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002193
2194 // The translation unit is the first declaration we'll emit.
2195 DeclIDs[Context.getTranslationUnitDecl()] = 1;
2196 DeclsToEmit.push(Context.getTranslationUnitDecl());
2197
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002198 // Make sure that we emit IdentifierInfos (and any attached
2199 // declarations) for builtins.
2200 {
2201 IdentifierTable &Table = PP.getIdentifierTable();
2202 llvm::SmallVector<const char *, 32> BuiltinNames;
2203 Context.BuiltinInfo.GetBuiltinNames(BuiltinNames,
2204 Context.getLangOptions().NoBuiltin);
2205 for (unsigned I = 0, N = BuiltinNames.size(); I != N; ++I)
2206 getIdentifierRef(&Table.get(BuiltinNames[I]));
2207 }
2208
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002209 // Build a record containing all of the tentative definitions in
2210 // this header file. Generally, this record will be empty.
2211 RecordData TentativeDefinitions;
2212 for (llvm::DenseMap<DeclarationName, VarDecl *>::iterator
2213 TD = SemaRef.TentativeDefinitions.begin(),
2214 TDEnd = SemaRef.TentativeDefinitions.end();
2215 TD != TDEnd; ++TD)
2216 AddDeclRef(TD->second, TentativeDefinitions);
2217
Douglas Gregor062d9482009-04-22 22:18:58 +00002218 // Build a record containing all of the locally-scoped external
2219 // declarations in this header file. Generally, this record will be
2220 // empty.
2221 RecordData LocallyScopedExternalDecls;
2222 for (llvm::DenseMap<DeclarationName, NamedDecl *>::iterator
2223 TD = SemaRef.LocallyScopedExternalDecls.begin(),
2224 TDEnd = SemaRef.LocallyScopedExternalDecls.end();
2225 TD != TDEnd; ++TD)
2226 AddDeclRef(TD->second, LocallyScopedExternalDecls);
2227
Douglas Gregorc34897d2009-04-09 22:27:44 +00002228 // Write the remaining PCH contents.
Douglas Gregore01ad442009-04-18 05:55:16 +00002229 RecordData Record;
Douglas Gregor24a224c2009-04-25 18:35:21 +00002230 Stream.EnterSubblock(pch::PCH_BLOCK_ID, 4);
Douglas Gregorb5887f32009-04-10 21:16:55 +00002231 WriteTargetTriple(Context.Target);
Douglas Gregor179cfb12009-04-10 20:39:37 +00002232 WriteLanguageOptions(Context.getLangOptions());
Douglas Gregorf6e1fb22009-04-26 00:07:37 +00002233 WriteSourceManagerBlock(Context.getSourceManager(), PP);
Chris Lattnerffc05ed2009-04-10 17:15:23 +00002234 WritePreprocessor(PP);
Douglas Gregore43f0972009-04-26 03:49:13 +00002235
2236 // Keep writing types and declarations until all types and
2237 // declarations have been written.
2238 do {
2239 if (!DeclsToEmit.empty())
2240 WriteDeclsBlock(Context);
2241 if (!TypesToEmit.empty())
2242 WriteTypesBlock(Context);
2243 } while (!(DeclsToEmit.empty() && TypesToEmit.empty()));
2244
Douglas Gregorc3221aa2009-04-24 21:10:55 +00002245 WriteMethodPool(SemaRef);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002246 WriteIdentifierTable(PP);
Douglas Gregor24a224c2009-04-25 18:35:21 +00002247
2248 // Write the type offsets array
2249 BitCodeAbbrev *Abbrev = new BitCodeAbbrev();
2250 Abbrev->Add(BitCodeAbbrevOp(pch::TYPE_OFFSET));
2251 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2252 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2253 unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2254 Record.clear();
2255 Record.push_back(pch::TYPE_OFFSET);
2256 Record.push_back(TypeOffsets.size());
2257 Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record,
2258 (const char *)&TypeOffsets.front(),
2259 TypeOffsets.size() * sizeof(uint64_t));
2260
2261 // Write the declaration offsets array
2262 Abbrev = new BitCodeAbbrev();
2263 Abbrev->Add(BitCodeAbbrevOp(pch::DECL_OFFSET));
2264 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2265 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2266 unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2267 Record.clear();
2268 Record.push_back(pch::DECL_OFFSET);
2269 Record.push_back(DeclOffsets.size());
2270 Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record,
2271 (const char *)&DeclOffsets.front(),
2272 DeclOffsets.size() * sizeof(uint64_t));
Douglas Gregore01ad442009-04-18 05:55:16 +00002273
2274 // Write the record of special types.
2275 Record.clear();
2276 AddTypeRef(Context.getBuiltinVaListType(), Record);
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002277 AddTypeRef(Context.getObjCIdType(), Record);
2278 AddTypeRef(Context.getObjCSelType(), Record);
2279 AddTypeRef(Context.getObjCProtoType(), Record);
2280 AddTypeRef(Context.getObjCClassType(), Record);
2281 AddTypeRef(Context.getRawCFConstantStringType(), Record);
2282 AddTypeRef(Context.getRawObjCFastEnumerationStateType(), Record);
Douglas Gregore01ad442009-04-18 05:55:16 +00002283 Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
2284
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002285 // Write the record containing external, unnamed definitions.
Douglas Gregor631f6c62009-04-14 00:24:19 +00002286 if (!ExternalDefinitions.empty())
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002287 Stream.EmitRecord(pch::EXTERNAL_DEFINITIONS, ExternalDefinitions);
Douglas Gregor77b2cd52009-04-22 22:02:47 +00002288
2289 // Write the record containing tentative definitions.
2290 if (!TentativeDefinitions.empty())
2291 Stream.EmitRecord(pch::TENTATIVE_DEFINITIONS, TentativeDefinitions);
Douglas Gregor062d9482009-04-22 22:18:58 +00002292
2293 // Write the record containing locally-scoped external definitions.
2294 if (!LocallyScopedExternalDecls.empty())
2295 Stream.EmitRecord(pch::LOCALLY_SCOPED_EXTERNAL_DECLS,
2296 LocallyScopedExternalDecls);
Douglas Gregor456e0952009-04-17 22:13:46 +00002297
2298 // Some simple statistics
Douglas Gregore01ad442009-04-18 05:55:16 +00002299 Record.clear();
Douglas Gregor456e0952009-04-17 22:13:46 +00002300 Record.push_back(NumStatements);
Douglas Gregore0ad2dd2009-04-21 23:56:24 +00002301 Record.push_back(NumMacros);
Douglas Gregoraf136d92009-04-22 22:34:57 +00002302 Record.push_back(NumLexicalDeclContexts);
2303 Record.push_back(NumVisibleDeclContexts);
Douglas Gregor456e0952009-04-17 22:13:46 +00002304 Stream.EmitRecord(pch::STATISTICS, Record);
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002305 Stream.ExitBlock();
Douglas Gregorc34897d2009-04-09 22:27:44 +00002306}
2307
2308void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) {
2309 Record.push_back(Loc.getRawEncoding());
2310}
2311
2312void PCHWriter::AddAPInt(const llvm::APInt &Value, RecordData &Record) {
2313 Record.push_back(Value.getBitWidth());
2314 unsigned N = Value.getNumWords();
2315 const uint64_t* Words = Value.getRawData();
2316 for (unsigned I = 0; I != N; ++I)
2317 Record.push_back(Words[I]);
2318}
2319
Douglas Gregor47f1b2c2009-04-13 18:14:40 +00002320void PCHWriter::AddAPSInt(const llvm::APSInt &Value, RecordData &Record) {
2321 Record.push_back(Value.isUnsigned());
2322 AddAPInt(Value, Record);
2323}
2324
Douglas Gregore2f37202009-04-14 21:55:33 +00002325void PCHWriter::AddAPFloat(const llvm::APFloat &Value, RecordData &Record) {
2326 AddAPInt(Value.bitcastToAPInt(), Record);
2327}
2328
Douglas Gregorc34897d2009-04-09 22:27:44 +00002329void PCHWriter::AddIdentifierRef(const IdentifierInfo *II, RecordData &Record) {
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002330 Record.push_back(getIdentifierRef(II));
2331}
2332
2333pch::IdentID PCHWriter::getIdentifierRef(const IdentifierInfo *II) {
2334 if (II == 0)
2335 return 0;
Douglas Gregor7a224cf2009-04-11 00:14:32 +00002336
2337 pch::IdentID &ID = IdentifierIDs[II];
2338 if (ID == 0)
2339 ID = IdentifierIDs.size();
Douglas Gregorda38c6c2009-04-22 18:49:13 +00002340 return ID;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002341}
2342
Steve Naroff9e84d782009-04-23 10:39:46 +00002343void PCHWriter::AddSelectorRef(const Selector SelRef, RecordData &Record) {
2344 if (SelRef.getAsOpaquePtr() == 0) {
2345 Record.push_back(0);
2346 return;
2347 }
2348
2349 pch::SelectorID &SID = SelectorIDs[SelRef];
2350 if (SID == 0) {
2351 SID = SelectorIDs.size();
2352 SelVector.push_back(SelRef);
2353 }
2354 Record.push_back(SID);
2355}
2356
Douglas Gregorc34897d2009-04-09 22:27:44 +00002357void PCHWriter::AddTypeRef(QualType T, RecordData &Record) {
2358 if (T.isNull()) {
2359 Record.push_back(pch::PREDEF_TYPE_NULL_ID);
2360 return;
2361 }
2362
2363 if (const BuiltinType *BT = dyn_cast<BuiltinType>(T.getTypePtr())) {
Douglas Gregorb3a04c82009-04-10 23:10:45 +00002364 pch::TypeID ID = 0;
Douglas Gregorc34897d2009-04-09 22:27:44 +00002365 switch (BT->getKind()) {
2366 case BuiltinType::Void: ID = pch::PREDEF_TYPE_VOID_ID; break;
2367 case BuiltinType::Bool: ID = pch::PREDEF_TYPE_BOOL_ID; break;
2368 case BuiltinType::Char_U: ID = pch::PREDEF_TYPE_CHAR_U_ID; break;
2369 case BuiltinType::UChar: ID = pch::PREDEF_TYPE_UCHAR_ID; break;
2370 case BuiltinType::UShort: ID = pch::PREDEF_TYPE_USHORT_ID; break;
2371 case BuiltinType::UInt: ID = pch::PREDEF_TYPE_UINT_ID; break;
2372 case BuiltinType::ULong: ID = pch::PREDEF_TYPE_ULONG_ID; break;
2373 case BuiltinType::ULongLong: ID = pch::PREDEF_TYPE_ULONGLONG_ID; break;
2374 case BuiltinType::Char_S: ID = pch::PREDEF_TYPE_CHAR_S_ID; break;
2375 case BuiltinType::SChar: ID = pch::PREDEF_TYPE_SCHAR_ID; break;
2376 case BuiltinType::WChar: ID = pch::PREDEF_TYPE_WCHAR_ID; break;
2377 case BuiltinType::Short: ID = pch::PREDEF_TYPE_SHORT_ID; break;
2378 case BuiltinType::Int: ID = pch::PREDEF_TYPE_INT_ID; break;
2379 case BuiltinType::Long: ID = pch::PREDEF_TYPE_LONG_ID; break;
2380 case BuiltinType::LongLong: ID = pch::PREDEF_TYPE_LONGLONG_ID; break;
2381 case BuiltinType::Float: ID = pch::PREDEF_TYPE_FLOAT_ID; break;
2382 case BuiltinType::Double: ID = pch::PREDEF_TYPE_DOUBLE_ID; break;
2383 case BuiltinType::LongDouble: ID = pch::PREDEF_TYPE_LONGDOUBLE_ID; break;
2384 case BuiltinType::Overload: ID = pch::PREDEF_TYPE_OVERLOAD_ID; break;
2385 case BuiltinType::Dependent: ID = pch::PREDEF_TYPE_DEPENDENT_ID; break;
2386 }
2387
2388 Record.push_back((ID << 3) | T.getCVRQualifiers());
2389 return;
2390 }
2391
Douglas Gregorac8f2802009-04-10 17:25:41 +00002392 pch::TypeID &ID = TypeIDs[T.getTypePtr()];
Douglas Gregore43f0972009-04-26 03:49:13 +00002393 if (ID == 0) {
2394 // We haven't seen this type before. Assign it a new ID and put it
2395 // into the queu of types to emit.
Douglas Gregorc34897d2009-04-09 22:27:44 +00002396 ID = NextTypeID++;
Douglas Gregore43f0972009-04-26 03:49:13 +00002397 TypesToEmit.push(T.getTypePtr());
2398 }
Douglas Gregorc34897d2009-04-09 22:27:44 +00002399
2400 // Encode the type qualifiers in the type reference.
2401 Record.push_back((ID << 3) | T.getCVRQualifiers());
2402}
2403
2404void PCHWriter::AddDeclRef(const Decl *D, RecordData &Record) {
2405 if (D == 0) {
2406 Record.push_back(0);
2407 return;
2408 }
2409
Douglas Gregorac8f2802009-04-10 17:25:41 +00002410 pch::DeclID &ID = DeclIDs[D];
Douglas Gregorc34897d2009-04-09 22:27:44 +00002411 if (ID == 0) {
2412 // We haven't seen this declaration before. Give it a new ID and
2413 // enqueue it in the list of declarations to emit.
2414 ID = DeclIDs.size();
2415 DeclsToEmit.push(const_cast<Decl *>(D));
2416 }
2417
2418 Record.push_back(ID);
2419}
2420
Douglas Gregorff9a6092009-04-20 20:36:09 +00002421pch::DeclID PCHWriter::getDeclID(const Decl *D) {
2422 if (D == 0)
2423 return 0;
2424
2425 assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
2426 return DeclIDs[D];
2427}
2428
Douglas Gregorc34897d2009-04-09 22:27:44 +00002429void PCHWriter::AddDeclarationName(DeclarationName Name, RecordData &Record) {
2430 Record.push_back(Name.getNameKind());
2431 switch (Name.getNameKind()) {
2432 case DeclarationName::Identifier:
2433 AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
2434 break;
2435
2436 case DeclarationName::ObjCZeroArgSelector:
2437 case DeclarationName::ObjCOneArgSelector:
2438 case DeclarationName::ObjCMultiArgSelector:
Steve Naroff9e84d782009-04-23 10:39:46 +00002439 AddSelectorRef(Name.getObjCSelector(), Record);
Douglas Gregorc34897d2009-04-09 22:27:44 +00002440 break;
2441
2442 case DeclarationName::CXXConstructorName:
2443 case DeclarationName::CXXDestructorName:
2444 case DeclarationName::CXXConversionFunctionName:
2445 AddTypeRef(Name.getCXXNameType(), Record);
2446 break;
2447
2448 case DeclarationName::CXXOperatorName:
2449 Record.push_back(Name.getCXXOverloadedOperator());
2450 break;
2451
2452 case DeclarationName::CXXUsingDirective:
2453 // No extra data to emit
2454 break;
2455 }
2456}
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002457
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002458/// \brief Write the given substatement or subexpression to the
2459/// bitstream.
2460void PCHWriter::WriteSubStmt(Stmt *S) {
Douglas Gregora151ba42009-04-14 23:32:43 +00002461 RecordData Record;
2462 PCHStmtWriter Writer(*this, Record);
Douglas Gregor456e0952009-04-17 22:13:46 +00002463 ++NumStatements;
Douglas Gregora151ba42009-04-14 23:32:43 +00002464
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002465 if (!S) {
2466 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002467 return;
2468 }
2469
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002470 Writer.Code = pch::STMT_NULL_PTR;
2471 Writer.Visit(S);
2472 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregora151ba42009-04-14 23:32:43 +00002473 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002474 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002475}
2476
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002477/// \brief Flush all of the statements that have been added to the
2478/// queue via AddStmt().
2479void PCHWriter::FlushStmts() {
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002480 RecordData Record;
2481 PCHStmtWriter Writer(*this, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002482
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002483 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
Douglas Gregor456e0952009-04-17 22:13:46 +00002484 ++NumStatements;
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002485 Stmt *S = StmtsToEmit[I];
Douglas Gregora151ba42009-04-14 23:32:43 +00002486
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002487 if (!S) {
2488 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002489 continue;
2490 }
2491
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002492 Writer.Code = pch::STMT_NULL_PTR;
2493 Writer.Visit(S);
2494 assert(Writer.Code != pch::STMT_NULL_PTR &&
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002495 "Unhandled expression writing PCH file");
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002496 Stream.EmitRecord(Writer.Code, Record);
Douglas Gregora151ba42009-04-14 23:32:43 +00002497
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002498 assert(N == StmtsToEmit.size() &&
2499 "Substatement writen via AddStmt rather than WriteSubStmt!");
Douglas Gregora151ba42009-04-14 23:32:43 +00002500
2501 // Note that we are at the end of a full expression. Any
2502 // expression records that follow this one are part of a different
2503 // expression.
2504 Record.clear();
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002505 Stream.EmitRecord(pch::STMT_STOP, Record);
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002506 }
Douglas Gregora151ba42009-04-14 23:32:43 +00002507
Douglas Gregorc72f6c82009-04-16 22:23:12 +00002508 StmtsToEmit.clear();
Douglas Gregor22d2dcd2009-04-17 16:34:57 +00002509 SwitchCaseIDs.clear();
Douglas Gregorc10f86f2009-04-14 21:18:50 +00002510}
Douglas Gregor9c4782a2009-04-17 00:04:06 +00002511
2512unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
2513 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
2514 "SwitchCase recorded twice");
2515 unsigned NextID = SwitchCaseIDs.size();
2516 SwitchCaseIDs[S] = NextID;
2517 return NextID;
2518}
2519
2520unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
2521 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
2522 "SwitchCase hasn't been seen yet");
2523 return SwitchCaseIDs[S];
2524}
Douglas Gregor6e411bf2009-04-17 18:18:49 +00002525
2526/// \brief Retrieve the ID for the given label statement, which may
2527/// or may not have been emitted yet.
2528unsigned PCHWriter::GetLabelID(LabelStmt *S) {
2529 std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S);
2530 if (Pos != LabelIDs.end())
2531 return Pos->second;
2532
2533 unsigned NextID = LabelIDs.size();
2534 LabelIDs[S] = NextID;
2535 return NextID;
2536}