blob: b7caee5e55109ccc53ae912a8270f34d86e0523f [file] [log] [blame]
Chris Lattner63377d52009-04-27 06:20:01 +00001//===--- PCHWriterStmt.cpp - Statement and Expression Serialization -------===//
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 implements serialization for Statements and Expressions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Frontend/PCHWriter.h"
15#include "clang/AST/DeclObjC.h"
16#include "clang/AST/StmtVisitor.h"
17#include "llvm/Bitcode/BitstreamWriter.h"
18using namespace clang;
19
20//===----------------------------------------------------------------------===//
21// Statement/expression serialization
22//===----------------------------------------------------------------------===//
23
24namespace {
25 class PCHStmtWriter : public StmtVisitor<PCHStmtWriter, void> {
26
27 PCHWriter &Writer;
28 PCHWriter::RecordData &Record;
29
30 public:
31 pch::StmtCode Code;
32
33 PCHStmtWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
34 : Writer(Writer), Record(Record) { }
35
36 void VisitStmt(Stmt *S);
37 void VisitNullStmt(NullStmt *S);
38 void VisitCompoundStmt(CompoundStmt *S);
39 void VisitSwitchCase(SwitchCase *S);
40 void VisitCaseStmt(CaseStmt *S);
41 void VisitDefaultStmt(DefaultStmt *S);
42 void VisitLabelStmt(LabelStmt *S);
43 void VisitIfStmt(IfStmt *S);
44 void VisitSwitchStmt(SwitchStmt *S);
45 void VisitWhileStmt(WhileStmt *S);
46 void VisitDoStmt(DoStmt *S);
47 void VisitForStmt(ForStmt *S);
48 void VisitGotoStmt(GotoStmt *S);
49 void VisitIndirectGotoStmt(IndirectGotoStmt *S);
50 void VisitContinueStmt(ContinueStmt *S);
51 void VisitBreakStmt(BreakStmt *S);
52 void VisitReturnStmt(ReturnStmt *S);
53 void VisitDeclStmt(DeclStmt *S);
54 void VisitAsmStmt(AsmStmt *S);
55 void VisitExpr(Expr *E);
56 void VisitPredefinedExpr(PredefinedExpr *E);
57 void VisitDeclRefExpr(DeclRefExpr *E);
58 void VisitIntegerLiteral(IntegerLiteral *E);
59 void VisitFloatingLiteral(FloatingLiteral *E);
60 void VisitImaginaryLiteral(ImaginaryLiteral *E);
61 void VisitStringLiteral(StringLiteral *E);
62 void VisitCharacterLiteral(CharacterLiteral *E);
63 void VisitParenExpr(ParenExpr *E);
64 void VisitUnaryOperator(UnaryOperator *E);
65 void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
66 void VisitArraySubscriptExpr(ArraySubscriptExpr *E);
67 void VisitCallExpr(CallExpr *E);
68 void VisitMemberExpr(MemberExpr *E);
69 void VisitCastExpr(CastExpr *E);
70 void VisitBinaryOperator(BinaryOperator *E);
71 void VisitCompoundAssignOperator(CompoundAssignOperator *E);
72 void VisitConditionalOperator(ConditionalOperator *E);
73 void VisitImplicitCastExpr(ImplicitCastExpr *E);
74 void VisitExplicitCastExpr(ExplicitCastExpr *E);
75 void VisitCStyleCastExpr(CStyleCastExpr *E);
76 void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
77 void VisitExtVectorElementExpr(ExtVectorElementExpr *E);
78 void VisitInitListExpr(InitListExpr *E);
79 void VisitDesignatedInitExpr(DesignatedInitExpr *E);
80 void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
81 void VisitVAArgExpr(VAArgExpr *E);
82 void VisitAddrLabelExpr(AddrLabelExpr *E);
83 void VisitStmtExpr(StmtExpr *E);
84 void VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
85 void VisitChooseExpr(ChooseExpr *E);
86 void VisitGNUNullExpr(GNUNullExpr *E);
87 void VisitShuffleVectorExpr(ShuffleVectorExpr *E);
88 void VisitBlockExpr(BlockExpr *E);
89 void VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
90
91 // Objective-C Expressions
92 void VisitObjCStringLiteral(ObjCStringLiteral *E);
93 void VisitObjCEncodeExpr(ObjCEncodeExpr *E);
94 void VisitObjCSelectorExpr(ObjCSelectorExpr *E);
95 void VisitObjCProtocolExpr(ObjCProtocolExpr *E);
96 void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E);
97 void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
98 void VisitObjCKVCRefExpr(ObjCKVCRefExpr *E);
99 void VisitObjCMessageExpr(ObjCMessageExpr *E);
100 void VisitObjCSuperExpr(ObjCSuperExpr *E);
101
102 // Objective-C Statements
103 void VisitObjCForCollectionStmt(ObjCForCollectionStmt *);
104 void VisitObjCAtCatchStmt(ObjCAtCatchStmt *);
105 void VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *);
106 void VisitObjCAtTryStmt(ObjCAtTryStmt *);
107 void VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *);
108 void VisitObjCAtThrowStmt(ObjCAtThrowStmt *);
109 };
110}
111
112void PCHStmtWriter::VisitStmt(Stmt *S) {
113}
114
115void PCHStmtWriter::VisitNullStmt(NullStmt *S) {
116 VisitStmt(S);
117 Writer.AddSourceLocation(S->getSemiLoc(), Record);
118 Code = pch::STMT_NULL;
119}
120
121void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
122 VisitStmt(S);
123 Record.push_back(S->size());
124 for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
125 CS != CSEnd; ++CS)
126 Writer.WriteSubStmt(*CS);
127 Writer.AddSourceLocation(S->getLBracLoc(), Record);
128 Writer.AddSourceLocation(S->getRBracLoc(), Record);
129 Code = pch::STMT_COMPOUND;
130}
131
132void PCHStmtWriter::VisitSwitchCase(SwitchCase *S) {
133 VisitStmt(S);
134 Record.push_back(Writer.RecordSwitchCaseID(S));
135}
136
137void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) {
138 VisitSwitchCase(S);
139 Writer.WriteSubStmt(S->getLHS());
140 Writer.WriteSubStmt(S->getRHS());
141 Writer.WriteSubStmt(S->getSubStmt());
142 Writer.AddSourceLocation(S->getCaseLoc(), Record);
Douglas Gregordbb26db2009-05-15 23:57:33 +0000143 Writer.AddSourceLocation(S->getEllipsisLoc(), Record);
144 Writer.AddSourceLocation(S->getColonLoc(), Record);
Chris Lattner63377d52009-04-27 06:20:01 +0000145 Code = pch::STMT_CASE;
146}
147
148void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
149 VisitSwitchCase(S);
150 Writer.WriteSubStmt(S->getSubStmt());
151 Writer.AddSourceLocation(S->getDefaultLoc(), Record);
Douglas Gregordbb26db2009-05-15 23:57:33 +0000152 Writer.AddSourceLocation(S->getColonLoc(), Record);
Chris Lattner63377d52009-04-27 06:20:01 +0000153 Code = pch::STMT_DEFAULT;
154}
155
156void PCHStmtWriter::VisitLabelStmt(LabelStmt *S) {
157 VisitStmt(S);
158 Writer.AddIdentifierRef(S->getID(), Record);
159 Writer.WriteSubStmt(S->getSubStmt());
160 Writer.AddSourceLocation(S->getIdentLoc(), Record);
161 Record.push_back(Writer.GetLabelID(S));
162 Code = pch::STMT_LABEL;
163}
164
165void PCHStmtWriter::VisitIfStmt(IfStmt *S) {
166 VisitStmt(S);
167 Writer.WriteSubStmt(S->getCond());
168 Writer.WriteSubStmt(S->getThen());
169 Writer.WriteSubStmt(S->getElse());
170 Writer.AddSourceLocation(S->getIfLoc(), Record);
Douglas Gregord06f6ca2009-05-15 18:53:42 +0000171 Writer.AddSourceLocation(S->getElseLoc(), Record);
Chris Lattner63377d52009-04-27 06:20:01 +0000172 Code = pch::STMT_IF;
173}
174
175void PCHStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
176 VisitStmt(S);
177 Writer.WriteSubStmt(S->getCond());
178 Writer.WriteSubStmt(S->getBody());
179 Writer.AddSourceLocation(S->getSwitchLoc(), Record);
180 for (SwitchCase *SC = S->getSwitchCaseList(); SC;
181 SC = SC->getNextSwitchCase())
182 Record.push_back(Writer.getSwitchCaseID(SC));
183 Code = pch::STMT_SWITCH;
184}
185
186void PCHStmtWriter::VisitWhileStmt(WhileStmt *S) {
187 VisitStmt(S);
188 Writer.WriteSubStmt(S->getCond());
189 Writer.WriteSubStmt(S->getBody());
190 Writer.AddSourceLocation(S->getWhileLoc(), Record);
191 Code = pch::STMT_WHILE;
192}
193
194void PCHStmtWriter::VisitDoStmt(DoStmt *S) {
195 VisitStmt(S);
196 Writer.WriteSubStmt(S->getCond());
197 Writer.WriteSubStmt(S->getBody());
198 Writer.AddSourceLocation(S->getDoLoc(), Record);
Douglas Gregor9f3ca2a2009-05-15 21:56:04 +0000199 Writer.AddSourceLocation(S->getWhileLoc(), Record);
Chris Lattner63377d52009-04-27 06:20:01 +0000200 Code = pch::STMT_DO;
201}
202
203void PCHStmtWriter::VisitForStmt(ForStmt *S) {
204 VisitStmt(S);
205 Writer.WriteSubStmt(S->getInit());
206 Writer.WriteSubStmt(S->getCond());
207 Writer.WriteSubStmt(S->getInc());
208 Writer.WriteSubStmt(S->getBody());
209 Writer.AddSourceLocation(S->getForLoc(), Record);
Douglas Gregor5831c6a2009-05-15 22:12:32 +0000210 Writer.AddSourceLocation(S->getLParenLoc(), Record);
211 Writer.AddSourceLocation(S->getRParenLoc(), Record);
Chris Lattner63377d52009-04-27 06:20:01 +0000212 Code = pch::STMT_FOR;
213}
214
215void PCHStmtWriter::VisitGotoStmt(GotoStmt *S) {
216 VisitStmt(S);
217 Record.push_back(Writer.GetLabelID(S->getLabel()));
218 Writer.AddSourceLocation(S->getGotoLoc(), Record);
219 Writer.AddSourceLocation(S->getLabelLoc(), Record);
220 Code = pch::STMT_GOTO;
221}
222
223void PCHStmtWriter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
224 VisitStmt(S);
225 Writer.AddSourceLocation(S->getGotoLoc(), Record);
Douglas Gregor5f1b9e62009-05-16 00:20:29 +0000226 Writer.AddSourceLocation(S->getStarLoc(), Record);
Chris Lattner63377d52009-04-27 06:20:01 +0000227 Writer.WriteSubStmt(S->getTarget());
228 Code = pch::STMT_INDIRECT_GOTO;
229}
230
231void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) {
232 VisitStmt(S);
233 Writer.AddSourceLocation(S->getContinueLoc(), Record);
234 Code = pch::STMT_CONTINUE;
235}
236
237void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) {
238 VisitStmt(S);
239 Writer.AddSourceLocation(S->getBreakLoc(), Record);
240 Code = pch::STMT_BREAK;
241}
242
243void PCHStmtWriter::VisitReturnStmt(ReturnStmt *S) {
244 VisitStmt(S);
245 Writer.WriteSubStmt(S->getRetValue());
246 Writer.AddSourceLocation(S->getReturnLoc(), Record);
247 Code = pch::STMT_RETURN;
248}
249
250void PCHStmtWriter::VisitDeclStmt(DeclStmt *S) {
251 VisitStmt(S);
252 Writer.AddSourceLocation(S->getStartLoc(), Record);
253 Writer.AddSourceLocation(S->getEndLoc(), Record);
254 DeclGroupRef DG = S->getDeclGroup();
255 for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D)
256 Writer.AddDeclRef(*D, Record);
257 Code = pch::STMT_DECL;
258}
259
260void PCHStmtWriter::VisitAsmStmt(AsmStmt *S) {
261 VisitStmt(S);
262 Record.push_back(S->getNumOutputs());
263 Record.push_back(S->getNumInputs());
264 Record.push_back(S->getNumClobbers());
265 Writer.AddSourceLocation(S->getAsmLoc(), Record);
266 Writer.AddSourceLocation(S->getRParenLoc(), Record);
267 Record.push_back(S->isVolatile());
268 Record.push_back(S->isSimple());
269 Writer.WriteSubStmt(S->getAsmString());
270
271 // Outputs
272 for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
273 Writer.AddString(S->getOutputName(I), Record);
274 Writer.WriteSubStmt(S->getOutputConstraintLiteral(I));
275 Writer.WriteSubStmt(S->getOutputExpr(I));
276 }
277
278 // Inputs
279 for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
280 Writer.AddString(S->getInputName(I), Record);
281 Writer.WriteSubStmt(S->getInputConstraintLiteral(I));
282 Writer.WriteSubStmt(S->getInputExpr(I));
283 }
284
285 // Clobbers
286 for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
287 Writer.WriteSubStmt(S->getClobber(I));
288
289 Code = pch::STMT_ASM;
290}
291
292void PCHStmtWriter::VisitExpr(Expr *E) {
293 VisitStmt(E);
294 Writer.AddTypeRef(E->getType(), Record);
295 Record.push_back(E->isTypeDependent());
296 Record.push_back(E->isValueDependent());
297}
298
299void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
300 VisitExpr(E);
301 Writer.AddSourceLocation(E->getLocation(), Record);
302 Record.push_back(E->getIdentType()); // FIXME: stable encoding
303 Code = pch::EXPR_PREDEFINED;
304}
305
306void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
307 VisitExpr(E);
308 Writer.AddDeclRef(E->getDecl(), Record);
309 Writer.AddSourceLocation(E->getLocation(), Record);
310 Code = pch::EXPR_DECL_REF;
311}
312
313void PCHStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
314 VisitExpr(E);
315 Writer.AddSourceLocation(E->getLocation(), Record);
316 Writer.AddAPInt(E->getValue(), Record);
317 Code = pch::EXPR_INTEGER_LITERAL;
318}
319
320void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
321 VisitExpr(E);
322 Writer.AddAPFloat(E->getValue(), Record);
323 Record.push_back(E->isExact());
324 Writer.AddSourceLocation(E->getLocation(), Record);
325 Code = pch::EXPR_FLOATING_LITERAL;
326}
327
328void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
329 VisitExpr(E);
330 Writer.WriteSubStmt(E->getSubExpr());
331 Code = pch::EXPR_IMAGINARY_LITERAL;
332}
333
334void PCHStmtWriter::VisitStringLiteral(StringLiteral *E) {
335 VisitExpr(E);
336 Record.push_back(E->getByteLength());
337 Record.push_back(E->getNumConcatenated());
338 Record.push_back(E->isWide());
339 // FIXME: String data should be stored as a blob at the end of the
340 // StringLiteral. However, we can't do so now because we have no
341 // provision for coping with abbreviations when we're jumping around
342 // the PCH file during deserialization.
343 Record.insert(Record.end(),
344 E->getStrData(), E->getStrData() + E->getByteLength());
345 for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
346 Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
347 Code = pch::EXPR_STRING_LITERAL;
348}
349
350void PCHStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
351 VisitExpr(E);
352 Record.push_back(E->getValue());
353 Writer.AddSourceLocation(E->getLoc(), Record);
354 Record.push_back(E->isWide());
355 Code = pch::EXPR_CHARACTER_LITERAL;
356}
357
358void PCHStmtWriter::VisitParenExpr(ParenExpr *E) {
359 VisitExpr(E);
360 Writer.AddSourceLocation(E->getLParen(), Record);
361 Writer.AddSourceLocation(E->getRParen(), Record);
362 Writer.WriteSubStmt(E->getSubExpr());
363 Code = pch::EXPR_PAREN;
364}
365
366void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
367 VisitExpr(E);
368 Writer.WriteSubStmt(E->getSubExpr());
369 Record.push_back(E->getOpcode()); // FIXME: stable encoding
370 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
371 Code = pch::EXPR_UNARY_OPERATOR;
372}
373
374void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
375 VisitExpr(E);
376 Record.push_back(E->isSizeOf());
377 if (E->isArgumentType())
378 Writer.AddTypeRef(E->getArgumentType(), Record);
379 else {
380 Record.push_back(0);
381 Writer.WriteSubStmt(E->getArgumentExpr());
382 }
383 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
384 Writer.AddSourceLocation(E->getRParenLoc(), Record);
385 Code = pch::EXPR_SIZEOF_ALIGN_OF;
386}
387
388void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
389 VisitExpr(E);
390 Writer.WriteSubStmt(E->getLHS());
391 Writer.WriteSubStmt(E->getRHS());
392 Writer.AddSourceLocation(E->getRBracketLoc(), Record);
393 Code = pch::EXPR_ARRAY_SUBSCRIPT;
394}
395
396void PCHStmtWriter::VisitCallExpr(CallExpr *E) {
397 VisitExpr(E);
398 Record.push_back(E->getNumArgs());
399 Writer.AddSourceLocation(E->getRParenLoc(), Record);
400 Writer.WriteSubStmt(E->getCallee());
401 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
402 Arg != ArgEnd; ++Arg)
403 Writer.WriteSubStmt(*Arg);
404 Code = pch::EXPR_CALL;
405}
406
407void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) {
408 VisitExpr(E);
409 Writer.WriteSubStmt(E->getBase());
410 Writer.AddDeclRef(E->getMemberDecl(), Record);
411 Writer.AddSourceLocation(E->getMemberLoc(), Record);
412 Record.push_back(E->isArrow());
413 Code = pch::EXPR_MEMBER;
414}
415
416void PCHStmtWriter::VisitCastExpr(CastExpr *E) {
417 VisitExpr(E);
418 Writer.WriteSubStmt(E->getSubExpr());
419}
420
421void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
422 VisitExpr(E);
423 Writer.WriteSubStmt(E->getLHS());
424 Writer.WriteSubStmt(E->getRHS());
425 Record.push_back(E->getOpcode()); // FIXME: stable encoding
426 Writer.AddSourceLocation(E->getOperatorLoc(), Record);
427 Code = pch::EXPR_BINARY_OPERATOR;
428}
429
430void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
431 VisitBinaryOperator(E);
432 Writer.AddTypeRef(E->getComputationLHSType(), Record);
433 Writer.AddTypeRef(E->getComputationResultType(), Record);
434 Code = pch::EXPR_COMPOUND_ASSIGN_OPERATOR;
435}
436
437void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
438 VisitExpr(E);
439 Writer.WriteSubStmt(E->getCond());
440 Writer.WriteSubStmt(E->getLHS());
441 Writer.WriteSubStmt(E->getRHS());
442 Code = pch::EXPR_CONDITIONAL_OPERATOR;
443}
444
445void PCHStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
446 VisitCastExpr(E);
447 Record.push_back(E->isLvalueCast());
448 Code = pch::EXPR_IMPLICIT_CAST;
449}
450
451void PCHStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
452 VisitCastExpr(E);
453 Writer.AddTypeRef(E->getTypeAsWritten(), Record);
454}
455
456void PCHStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
457 VisitExplicitCastExpr(E);
458 Writer.AddSourceLocation(E->getLParenLoc(), Record);
459 Writer.AddSourceLocation(E->getRParenLoc(), Record);
460 Code = pch::EXPR_CSTYLE_CAST;
461}
462
463void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
464 VisitExpr(E);
465 Writer.AddSourceLocation(E->getLParenLoc(), Record);
466 Writer.WriteSubStmt(E->getInitializer());
467 Record.push_back(E->isFileScope());
468 Code = pch::EXPR_COMPOUND_LITERAL;
469}
470
471void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
472 VisitExpr(E);
473 Writer.WriteSubStmt(E->getBase());
474 Writer.AddIdentifierRef(&E->getAccessor(), Record);
475 Writer.AddSourceLocation(E->getAccessorLoc(), Record);
476 Code = pch::EXPR_EXT_VECTOR_ELEMENT;
477}
478
479void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) {
480 VisitExpr(E);
481 Record.push_back(E->getNumInits());
482 for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
483 Writer.WriteSubStmt(E->getInit(I));
484 Writer.WriteSubStmt(E->getSyntacticForm());
485 Writer.AddSourceLocation(E->getLBraceLoc(), Record);
486 Writer.AddSourceLocation(E->getRBraceLoc(), Record);
487 Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
488 Record.push_back(E->hadArrayRangeDesignator());
489 Code = pch::EXPR_INIT_LIST;
490}
491
492void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
493 VisitExpr(E);
494 Record.push_back(E->getNumSubExprs());
495 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
496 Writer.WriteSubStmt(E->getSubExpr(I));
497 Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
498 Record.push_back(E->usesGNUSyntax());
499 for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
500 DEnd = E->designators_end();
501 D != DEnd; ++D) {
502 if (D->isFieldDesignator()) {
503 if (FieldDecl *Field = D->getField()) {
504 Record.push_back(pch::DESIG_FIELD_DECL);
505 Writer.AddDeclRef(Field, Record);
506 } else {
507 Record.push_back(pch::DESIG_FIELD_NAME);
508 Writer.AddIdentifierRef(D->getFieldName(), Record);
509 }
510 Writer.AddSourceLocation(D->getDotLoc(), Record);
511 Writer.AddSourceLocation(D->getFieldLoc(), Record);
512 } else if (D->isArrayDesignator()) {
513 Record.push_back(pch::DESIG_ARRAY);
514 Record.push_back(D->getFirstExprIndex());
515 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
516 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
517 } else {
518 assert(D->isArrayRangeDesignator() && "Unknown designator");
519 Record.push_back(pch::DESIG_ARRAY_RANGE);
520 Record.push_back(D->getFirstExprIndex());
521 Writer.AddSourceLocation(D->getLBracketLoc(), Record);
522 Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
523 Writer.AddSourceLocation(D->getRBracketLoc(), Record);
524 }
525 }
526 Code = pch::EXPR_DESIGNATED_INIT;
527}
528
529void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
530 VisitExpr(E);
531 Code = pch::EXPR_IMPLICIT_VALUE_INIT;
532}
533
534void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
535 VisitExpr(E);
536 Writer.WriteSubStmt(E->getSubExpr());
537 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
538 Writer.AddSourceLocation(E->getRParenLoc(), Record);
539 Code = pch::EXPR_VA_ARG;
540}
541
542void PCHStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) {
543 VisitExpr(E);
544 Writer.AddSourceLocation(E->getAmpAmpLoc(), Record);
545 Writer.AddSourceLocation(E->getLabelLoc(), Record);
546 Record.push_back(Writer.GetLabelID(E->getLabel()));
547 Code = pch::EXPR_ADDR_LABEL;
548}
549
550void PCHStmtWriter::VisitStmtExpr(StmtExpr *E) {
551 VisitExpr(E);
552 Writer.WriteSubStmt(E->getSubStmt());
553 Writer.AddSourceLocation(E->getLParenLoc(), Record);
554 Writer.AddSourceLocation(E->getRParenLoc(), Record);
555 Code = pch::EXPR_STMT;
556}
557
558void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
559 VisitExpr(E);
560 Writer.AddTypeRef(E->getArgType1(), Record);
561 Writer.AddTypeRef(E->getArgType2(), Record);
562 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
563 Writer.AddSourceLocation(E->getRParenLoc(), Record);
564 Code = pch::EXPR_TYPES_COMPATIBLE;
565}
566
567void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) {
568 VisitExpr(E);
569 Writer.WriteSubStmt(E->getCond());
570 Writer.WriteSubStmt(E->getLHS());
571 Writer.WriteSubStmt(E->getRHS());
572 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
573 Writer.AddSourceLocation(E->getRParenLoc(), Record);
574 Code = pch::EXPR_CHOOSE;
575}
576
577void PCHStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
578 VisitExpr(E);
579 Writer.AddSourceLocation(E->getTokenLocation(), Record);
580 Code = pch::EXPR_GNU_NULL;
581}
582
583void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
584 VisitExpr(E);
585 Record.push_back(E->getNumSubExprs());
586 for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
587 Writer.WriteSubStmt(E->getExpr(I));
588 Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
589 Writer.AddSourceLocation(E->getRParenLoc(), Record);
590 Code = pch::EXPR_SHUFFLE_VECTOR;
591}
592
593void PCHStmtWriter::VisitBlockExpr(BlockExpr *E) {
594 VisitExpr(E);
595 Writer.AddDeclRef(E->getBlockDecl(), Record);
596 Record.push_back(E->hasBlockDeclRefExprs());
597 Code = pch::EXPR_BLOCK;
598}
599
600void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
601 VisitExpr(E);
602 Writer.AddDeclRef(E->getDecl(), Record);
603 Writer.AddSourceLocation(E->getLocation(), Record);
604 Record.push_back(E->isByRef());
605 Code = pch::EXPR_BLOCK_DECL_REF;
606}
607
608//===----------------------------------------------------------------------===//
609// Objective-C Expressions and Statements.
610//===----------------------------------------------------------------------===//
611
612void PCHStmtWriter::VisitObjCStringLiteral(ObjCStringLiteral *E) {
613 VisitExpr(E);
614 Writer.WriteSubStmt(E->getString());
615 Writer.AddSourceLocation(E->getAtLoc(), Record);
616 Code = pch::EXPR_OBJC_STRING_LITERAL;
617}
618
619void PCHStmtWriter::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
620 VisitExpr(E);
621 Writer.AddTypeRef(E->getEncodedType(), Record);
622 Writer.AddSourceLocation(E->getAtLoc(), Record);
623 Writer.AddSourceLocation(E->getRParenLoc(), Record);
624 Code = pch::EXPR_OBJC_ENCODE;
625}
626
627void PCHStmtWriter::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
628 VisitExpr(E);
629 Writer.AddSelectorRef(E->getSelector(), Record);
630 Writer.AddSourceLocation(E->getAtLoc(), Record);
631 Writer.AddSourceLocation(E->getRParenLoc(), Record);
632 Code = pch::EXPR_OBJC_SELECTOR_EXPR;
633}
634
635void PCHStmtWriter::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
636 VisitExpr(E);
637 Writer.AddDeclRef(E->getProtocol(), Record);
638 Writer.AddSourceLocation(E->getAtLoc(), Record);
639 Writer.AddSourceLocation(E->getRParenLoc(), Record);
640 Code = pch::EXPR_OBJC_PROTOCOL_EXPR;
641}
642
643void PCHStmtWriter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
644 VisitExpr(E);
645 Writer.AddDeclRef(E->getDecl(), Record);
646 Writer.AddSourceLocation(E->getLocation(), Record);
647 Writer.WriteSubStmt(E->getBase());
648 Record.push_back(E->isArrow());
649 Record.push_back(E->isFreeIvar());
650 Code = pch::EXPR_OBJC_IVAR_REF_EXPR;
651}
652
653void PCHStmtWriter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
654 VisitExpr(E);
655 Writer.AddDeclRef(E->getProperty(), Record);
656 Writer.AddSourceLocation(E->getLocation(), Record);
657 Writer.WriteSubStmt(E->getBase());
658 Code = pch::EXPR_OBJC_PROPERTY_REF_EXPR;
659}
660
661void PCHStmtWriter::VisitObjCKVCRefExpr(ObjCKVCRefExpr *E) {
662 VisitExpr(E);
663 Writer.AddDeclRef(E->getGetterMethod(), Record);
664 Writer.AddDeclRef(E->getSetterMethod(), Record);
665
666 // NOTE: ClassProp and Base are mutually exclusive.
667 Writer.AddDeclRef(E->getClassProp(), Record);
668 Writer.WriteSubStmt(E->getBase());
669 Writer.AddSourceLocation(E->getLocation(), Record);
670 Writer.AddSourceLocation(E->getClassLoc(), Record);
671 Code = pch::EXPR_OBJC_KVC_REF_EXPR;
672}
673
674void PCHStmtWriter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
675 VisitExpr(E);
676 Record.push_back(E->getNumArgs());
677 Writer.AddSourceLocation(E->getLeftLoc(), Record);
678 Writer.AddSourceLocation(E->getRightLoc(), Record);
679 Writer.AddSelectorRef(E->getSelector(), Record);
680 Writer.AddDeclRef(E->getMethodDecl(), Record); // optional
681 Writer.WriteSubStmt(E->getReceiver());
682
683 if (!E->getReceiver()) {
684 ObjCMessageExpr::ClassInfo CI = E->getClassInfo();
685 Writer.AddDeclRef(CI.first, Record);
686 Writer.AddIdentifierRef(CI.second, Record);
687 }
688
689 for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
690 Arg != ArgEnd; ++Arg)
691 Writer.WriteSubStmt(*Arg);
692 Code = pch::EXPR_OBJC_MESSAGE_EXPR;
693}
694
695void PCHStmtWriter::VisitObjCSuperExpr(ObjCSuperExpr *E) {
696 VisitExpr(E);
697 Writer.AddSourceLocation(E->getLoc(), Record);
698 Code = pch::EXPR_OBJC_SUPER_EXPR;
699}
700
701void PCHStmtWriter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
702 VisitStmt(S);
703 Writer.WriteSubStmt(S->getElement());
704 Writer.WriteSubStmt(S->getCollection());
705 Writer.WriteSubStmt(S->getBody());
706 Writer.AddSourceLocation(S->getForLoc(), Record);
707 Writer.AddSourceLocation(S->getRParenLoc(), Record);
708 Code = pch::STMT_OBJC_FOR_COLLECTION;
709}
710
711void PCHStmtWriter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
712 Writer.WriteSubStmt(S->getCatchBody());
713 Writer.WriteSubStmt(S->getNextCatchStmt());
714 Writer.AddDeclRef(S->getCatchParamDecl(), Record);
715 Writer.AddSourceLocation(S->getAtCatchLoc(), Record);
716 Writer.AddSourceLocation(S->getRParenLoc(), Record);
717 Code = pch::STMT_OBJC_CATCH;
718}
719
720void PCHStmtWriter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
721 Writer.WriteSubStmt(S->getFinallyBody());
722 Writer.AddSourceLocation(S->getAtFinallyLoc(), Record);
723 Code = pch::STMT_OBJC_FINALLY;
724}
725
726void PCHStmtWriter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
727 Writer.WriteSubStmt(S->getTryBody());
728 Writer.WriteSubStmt(S->getCatchStmts());
729 Writer.WriteSubStmt(S->getFinallyStmt());
730 Writer.AddSourceLocation(S->getAtTryLoc(), Record);
731 Code = pch::STMT_OBJC_AT_TRY;
732}
733
734void PCHStmtWriter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S) {
735 Writer.WriteSubStmt(S->getSynchExpr());
736 Writer.WriteSubStmt(S->getSynchBody());
737 Writer.AddSourceLocation(S->getAtSynchronizedLoc(), Record);
738 Code = pch::STMT_OBJC_AT_SYNCHRONIZED;
739}
740
741void PCHStmtWriter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
742 Writer.WriteSubStmt(S->getThrowExpr());
743 Writer.AddSourceLocation(S->getThrowLoc(), Record);
744 Code = pch::STMT_OBJC_AT_THROW;
745}
746
747//===----------------------------------------------------------------------===//
748// PCHWriter Implementation
749//===----------------------------------------------------------------------===//
750
751unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
752 assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
753 "SwitchCase recorded twice");
754 unsigned NextID = SwitchCaseIDs.size();
755 SwitchCaseIDs[S] = NextID;
756 return NextID;
757}
758
759unsigned PCHWriter::getSwitchCaseID(SwitchCase *S) {
760 assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
761 "SwitchCase hasn't been seen yet");
762 return SwitchCaseIDs[S];
763}
764
765/// \brief Retrieve the ID for the given label statement, which may
766/// or may not have been emitted yet.
767unsigned PCHWriter::GetLabelID(LabelStmt *S) {
768 std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S);
769 if (Pos != LabelIDs.end())
770 return Pos->second;
771
772 unsigned NextID = LabelIDs.size();
773 LabelIDs[S] = NextID;
774 return NextID;
775}
776
777/// \brief Write the given substatement or subexpression to the
778/// bitstream.
779void PCHWriter::WriteSubStmt(Stmt *S) {
780 RecordData Record;
781 PCHStmtWriter Writer(*this, Record);
782 ++NumStatements;
783
784 if (!S) {
785 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
786 return;
787 }
788
789 Writer.Code = pch::STMT_NULL_PTR;
790 Writer.Visit(S);
791 assert(Writer.Code != pch::STMT_NULL_PTR &&
792 "Unhandled expression writing PCH file");
793 Stream.EmitRecord(Writer.Code, Record);
794}
795
796/// \brief Flush all of the statements that have been added to the
797/// queue via AddStmt().
798void PCHWriter::FlushStmts() {
799 RecordData Record;
800 PCHStmtWriter Writer(*this, Record);
801
802 for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
803 ++NumStatements;
804 Stmt *S = StmtsToEmit[I];
805
806 if (!S) {
807 Stream.EmitRecord(pch::STMT_NULL_PTR, Record);
808 continue;
809 }
810
811 Writer.Code = pch::STMT_NULL_PTR;
812 Writer.Visit(S);
813 assert(Writer.Code != pch::STMT_NULL_PTR &&
814 "Unhandled expression writing PCH file");
815 Stream.EmitRecord(Writer.Code, Record);
816
817 assert(N == StmtsToEmit.size() &&
818 "Substatement writen via AddStmt rather than WriteSubStmt!");
819
820 // Note that we are at the end of a full expression. Any
821 // expression records that follow this one are part of a different
822 // expression.
823 Record.clear();
824 Stream.EmitRecord(pch::STMT_STOP, Record);
825 }
826
827 StmtsToEmit.clear();
828 SwitchCaseIDs.clear();
829}