ASTWriterStmt.cpp revision 9fcce65e7e1307b5b8da9be13e4092d6bb94dc1d
1//===--- ASTWriterStmt.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/Serialization/ASTWriter.h"
15#include "clang/AST/DeclCXX.h"
16#include "clang/AST/DeclObjC.h"
17#include "clang/AST/DeclTemplate.h"
18#include "clang/AST/StmtVisitor.h"
19#include "llvm/Bitcode/BitstreamWriter.h"
20using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// Statement/expression serialization
24//===----------------------------------------------------------------------===//
25
26namespace clang {
27  class ASTStmtWriter : public StmtVisitor<ASTStmtWriter, void> {
28    ASTWriter &Writer;
29    ASTWriter::RecordData &Record;
30
31  public:
32    serialization::StmtCode Code;
33    unsigned AbbrevToUse;
34
35    ASTStmtWriter(ASTWriter &Writer, ASTWriter::RecordData &Record)
36      : Writer(Writer), Record(Record) { }
37
38    void AddTemplateKWAndArgsInfo(const ASTTemplateKWAndArgsInfo &Args);
39
40    void VisitStmt(Stmt *S);
41#define STMT(Type, Base) \
42    void Visit##Type(Type *);
43#include "clang/AST/StmtNodes.inc"
44  };
45}
46
47void ASTStmtWriter::
48AddTemplateKWAndArgsInfo(const ASTTemplateKWAndArgsInfo &Args) {
49  Writer.AddSourceLocation(Args.getTemplateKeywordLoc(), Record);
50  Writer.AddSourceLocation(Args.LAngleLoc, Record);
51  Writer.AddSourceLocation(Args.RAngleLoc, Record);
52  for (unsigned i=0; i != Args.NumTemplateArgs; ++i)
53    Writer.AddTemplateArgumentLoc(Args.getTemplateArgs()[i], Record);
54}
55
56void ASTStmtWriter::VisitStmt(Stmt *S) {
57}
58
59void ASTStmtWriter::VisitNullStmt(NullStmt *S) {
60  VisitStmt(S);
61  Writer.AddSourceLocation(S->getSemiLoc(), Record);
62  Record.push_back(S->HasLeadingEmptyMacro);
63  Code = serialization::STMT_NULL;
64}
65
66void ASTStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
67  VisitStmt(S);
68  Record.push_back(S->size());
69  for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
70       CS != CSEnd; ++CS)
71    Writer.AddStmt(*CS);
72  Writer.AddSourceLocation(S->getLBracLoc(), Record);
73  Writer.AddSourceLocation(S->getRBracLoc(), Record);
74  Code = serialization::STMT_COMPOUND;
75}
76
77void ASTStmtWriter::VisitSwitchCase(SwitchCase *S) {
78  VisitStmt(S);
79  Record.push_back(Writer.getSwitchCaseID(S));
80}
81
82void ASTStmtWriter::VisitCaseStmt(CaseStmt *S) {
83  VisitSwitchCase(S);
84  Writer.AddStmt(S->getLHS());
85  Writer.AddStmt(S->getRHS());
86  Writer.AddStmt(S->getSubStmt());
87  Writer.AddSourceLocation(S->getCaseLoc(), Record);
88  Writer.AddSourceLocation(S->getEllipsisLoc(), Record);
89  Writer.AddSourceLocation(S->getColonLoc(), Record);
90  Code = serialization::STMT_CASE;
91}
92
93void ASTStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
94  VisitSwitchCase(S);
95  Writer.AddStmt(S->getSubStmt());
96  Writer.AddSourceLocation(S->getDefaultLoc(), Record);
97  Writer.AddSourceLocation(S->getColonLoc(), Record);
98  Code = serialization::STMT_DEFAULT;
99}
100
101void ASTStmtWriter::VisitLabelStmt(LabelStmt *S) {
102  VisitStmt(S);
103  Writer.AddDeclRef(S->getDecl(), Record);
104  Writer.AddStmt(S->getSubStmt());
105  Writer.AddSourceLocation(S->getIdentLoc(), Record);
106  Code = serialization::STMT_LABEL;
107}
108
109void ASTStmtWriter::VisitIfStmt(IfStmt *S) {
110  VisitStmt(S);
111  Writer.AddDeclRef(S->getConditionVariable(), Record);
112  Writer.AddStmt(S->getCond());
113  Writer.AddStmt(S->getThen());
114  Writer.AddStmt(S->getElse());
115  Writer.AddSourceLocation(S->getIfLoc(), Record);
116  Writer.AddSourceLocation(S->getElseLoc(), Record);
117  Code = serialization::STMT_IF;
118}
119
120void ASTStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
121  VisitStmt(S);
122  Writer.AddDeclRef(S->getConditionVariable(), Record);
123  Writer.AddStmt(S->getCond());
124  Writer.AddStmt(S->getBody());
125  Writer.AddSourceLocation(S->getSwitchLoc(), Record);
126  Record.push_back(S->isAllEnumCasesCovered());
127  for (SwitchCase *SC = S->getSwitchCaseList(); SC;
128       SC = SC->getNextSwitchCase())
129    Record.push_back(Writer.RecordSwitchCaseID(SC));
130  Code = serialization::STMT_SWITCH;
131}
132
133void ASTStmtWriter::VisitWhileStmt(WhileStmt *S) {
134  VisitStmt(S);
135  Writer.AddDeclRef(S->getConditionVariable(), Record);
136  Writer.AddStmt(S->getCond());
137  Writer.AddStmt(S->getBody());
138  Writer.AddSourceLocation(S->getWhileLoc(), Record);
139  Code = serialization::STMT_WHILE;
140}
141
142void ASTStmtWriter::VisitDoStmt(DoStmt *S) {
143  VisitStmt(S);
144  Writer.AddStmt(S->getCond());
145  Writer.AddStmt(S->getBody());
146  Writer.AddSourceLocation(S->getDoLoc(), Record);
147  Writer.AddSourceLocation(S->getWhileLoc(), Record);
148  Writer.AddSourceLocation(S->getRParenLoc(), Record);
149  Code = serialization::STMT_DO;
150}
151
152void ASTStmtWriter::VisitForStmt(ForStmt *S) {
153  VisitStmt(S);
154  Writer.AddStmt(S->getInit());
155  Writer.AddStmt(S->getCond());
156  Writer.AddDeclRef(S->getConditionVariable(), Record);
157  Writer.AddStmt(S->getInc());
158  Writer.AddStmt(S->getBody());
159  Writer.AddSourceLocation(S->getForLoc(), Record);
160  Writer.AddSourceLocation(S->getLParenLoc(), Record);
161  Writer.AddSourceLocation(S->getRParenLoc(), Record);
162  Code = serialization::STMT_FOR;
163}
164
165void ASTStmtWriter::VisitGotoStmt(GotoStmt *S) {
166  VisitStmt(S);
167  Writer.AddDeclRef(S->getLabel(), Record);
168  Writer.AddSourceLocation(S->getGotoLoc(), Record);
169  Writer.AddSourceLocation(S->getLabelLoc(), Record);
170  Code = serialization::STMT_GOTO;
171}
172
173void ASTStmtWriter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
174  VisitStmt(S);
175  Writer.AddSourceLocation(S->getGotoLoc(), Record);
176  Writer.AddSourceLocation(S->getStarLoc(), Record);
177  Writer.AddStmt(S->getTarget());
178  Code = serialization::STMT_INDIRECT_GOTO;
179}
180
181void ASTStmtWriter::VisitContinueStmt(ContinueStmt *S) {
182  VisitStmt(S);
183  Writer.AddSourceLocation(S->getContinueLoc(), Record);
184  Code = serialization::STMT_CONTINUE;
185}
186
187void ASTStmtWriter::VisitBreakStmt(BreakStmt *S) {
188  VisitStmt(S);
189  Writer.AddSourceLocation(S->getBreakLoc(), Record);
190  Code = serialization::STMT_BREAK;
191}
192
193void ASTStmtWriter::VisitReturnStmt(ReturnStmt *S) {
194  VisitStmt(S);
195  Writer.AddStmt(S->getRetValue());
196  Writer.AddSourceLocation(S->getReturnLoc(), Record);
197  Writer.AddDeclRef(S->getNRVOCandidate(), Record);
198  Code = serialization::STMT_RETURN;
199}
200
201void ASTStmtWriter::VisitDeclStmt(DeclStmt *S) {
202  VisitStmt(S);
203  Writer.AddSourceLocation(S->getStartLoc(), Record);
204  Writer.AddSourceLocation(S->getEndLoc(), Record);
205  DeclGroupRef DG = S->getDeclGroup();
206  for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D)
207    Writer.AddDeclRef(*D, Record);
208  Code = serialization::STMT_DECL;
209}
210
211void ASTStmtWriter::VisitAsmStmt(AsmStmt *S) {
212  VisitStmt(S);
213  Record.push_back(S->getNumOutputs());
214  Record.push_back(S->getNumInputs());
215  Record.push_back(S->getNumClobbers());
216  Writer.AddSourceLocation(S->getAsmLoc(), Record);
217  Writer.AddSourceLocation(S->getRParenLoc(), Record);
218  Record.push_back(S->isVolatile());
219  Record.push_back(S->isSimple());
220  Record.push_back(S->isMSAsm());
221  Writer.AddStmt(S->getAsmString());
222
223  // Outputs
224  for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
225    Writer.AddIdentifierRef(S->getOutputIdentifier(I), Record);
226    Writer.AddStmt(S->getOutputConstraintLiteral(I));
227    Writer.AddStmt(S->getOutputExpr(I));
228  }
229
230  // Inputs
231  for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
232    Writer.AddIdentifierRef(S->getInputIdentifier(I), Record);
233    Writer.AddStmt(S->getInputConstraintLiteral(I));
234    Writer.AddStmt(S->getInputExpr(I));
235  }
236
237  // Clobbers
238  for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
239    Writer.AddStmt(S->getClobber(I));
240
241  Code = serialization::STMT_ASM;
242}
243
244void ASTStmtWriter::VisitExpr(Expr *E) {
245  VisitStmt(E);
246  Writer.AddTypeRef(E->getType(), Record);
247  Record.push_back(E->isTypeDependent());
248  Record.push_back(E->isValueDependent());
249  Record.push_back(E->isInstantiationDependent());
250  Record.push_back(E->containsUnexpandedParameterPack());
251  Record.push_back(E->getValueKind());
252  Record.push_back(E->getObjectKind());
253}
254
255void ASTStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
256  VisitExpr(E);
257  Writer.AddSourceLocation(E->getLocation(), Record);
258  Record.push_back(E->getIdentType()); // FIXME: stable encoding
259  Code = serialization::EXPR_PREDEFINED;
260}
261
262void ASTStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
263  VisitExpr(E);
264
265  Record.push_back(E->hasQualifier());
266  Record.push_back(E->getDecl() != E->getFoundDecl());
267  Record.push_back(E->hasTemplateKWAndArgsInfo());
268  Record.push_back(E->hadMultipleCandidates());
269
270  if (E->hasTemplateKWAndArgsInfo()) {
271    unsigned NumTemplateArgs = E->getNumTemplateArgs();
272    Record.push_back(NumTemplateArgs);
273  }
274
275  DeclarationName::NameKind nk = (E->getDecl()->getDeclName().getNameKind());
276
277  if ((!E->hasTemplateKWAndArgsInfo()) && (!E->hasQualifier()) &&
278      (E->getDecl() == E->getFoundDecl()) &&
279      nk == DeclarationName::Identifier) {
280    AbbrevToUse = Writer.getDeclRefExprAbbrev();
281  }
282
283  if (E->hasQualifier())
284    Writer.AddNestedNameSpecifierLoc(E->getQualifierLoc(), Record);
285
286  if (E->getDecl() != E->getFoundDecl())
287    Writer.AddDeclRef(E->getFoundDecl(), Record);
288
289  if (E->hasTemplateKWAndArgsInfo())
290    AddTemplateKWAndArgsInfo(*E->getTemplateKWAndArgsInfo());
291
292  Writer.AddDeclRef(E->getDecl(), Record);
293  Writer.AddSourceLocation(E->getLocation(), Record);
294  Writer.AddDeclarationNameLoc(E->DNLoc, E->getDecl()->getDeclName(), Record);
295  Code = serialization::EXPR_DECL_REF;
296}
297
298void ASTStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
299  VisitExpr(E);
300  Writer.AddSourceLocation(E->getLocation(), Record);
301  Writer.AddAPInt(E->getValue(), Record);
302
303  if (E->getValue().getBitWidth() == 32) {
304    AbbrevToUse = Writer.getIntegerLiteralAbbrev();
305  }
306
307  Code = serialization::EXPR_INTEGER_LITERAL;
308}
309
310void ASTStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
311  VisitExpr(E);
312  Writer.AddAPFloat(E->getValue(), Record);
313  Record.push_back(E->isExact());
314  Writer.AddSourceLocation(E->getLocation(), Record);
315  Code = serialization::EXPR_FLOATING_LITERAL;
316}
317
318void ASTStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
319  VisitExpr(E);
320  Writer.AddStmt(E->getSubExpr());
321  Code = serialization::EXPR_IMAGINARY_LITERAL;
322}
323
324void ASTStmtWriter::VisitStringLiteral(StringLiteral *E) {
325  VisitExpr(E);
326  Record.push_back(E->getByteLength());
327  Record.push_back(E->getNumConcatenated());
328  Record.push_back(E->getKind());
329  Record.push_back(E->isPascal());
330  // FIXME: String data should be stored as a blob at the end of the
331  // StringLiteral. However, we can't do so now because we have no
332  // provision for coping with abbreviations when we're jumping around
333  // the AST file during deserialization.
334  Record.append(E->getBytes().begin(), E->getBytes().end());
335  for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
336    Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
337  Code = serialization::EXPR_STRING_LITERAL;
338}
339
340void ASTStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
341  VisitExpr(E);
342  Record.push_back(E->getValue());
343  Writer.AddSourceLocation(E->getLocation(), Record);
344  Record.push_back(E->getKind());
345
346  AbbrevToUse = Writer.getCharacterLiteralAbbrev();
347
348  Code = serialization::EXPR_CHARACTER_LITERAL;
349}
350
351void ASTStmtWriter::VisitParenExpr(ParenExpr *E) {
352  VisitExpr(E);
353  Writer.AddSourceLocation(E->getLParen(), Record);
354  Writer.AddSourceLocation(E->getRParen(), Record);
355  Writer.AddStmt(E->getSubExpr());
356  Code = serialization::EXPR_PAREN;
357}
358
359void ASTStmtWriter::VisitParenListExpr(ParenListExpr *E) {
360  VisitExpr(E);
361  Record.push_back(E->NumExprs);
362  for (unsigned i=0; i != E->NumExprs; ++i)
363    Writer.AddStmt(E->Exprs[i]);
364  Writer.AddSourceLocation(E->LParenLoc, Record);
365  Writer.AddSourceLocation(E->RParenLoc, Record);
366  Code = serialization::EXPR_PAREN_LIST;
367}
368
369void ASTStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
370  VisitExpr(E);
371  Writer.AddStmt(E->getSubExpr());
372  Record.push_back(E->getOpcode()); // FIXME: stable encoding
373  Writer.AddSourceLocation(E->getOperatorLoc(), Record);
374  Code = serialization::EXPR_UNARY_OPERATOR;
375}
376
377void ASTStmtWriter::VisitOffsetOfExpr(OffsetOfExpr *E) {
378  VisitExpr(E);
379  Record.push_back(E->getNumComponents());
380  Record.push_back(E->getNumExpressions());
381  Writer.AddSourceLocation(E->getOperatorLoc(), Record);
382  Writer.AddSourceLocation(E->getRParenLoc(), Record);
383  Writer.AddTypeSourceInfo(E->getTypeSourceInfo(), Record);
384  for (unsigned I = 0, N = E->getNumComponents(); I != N; ++I) {
385    const OffsetOfExpr::OffsetOfNode &ON = E->getComponent(I);
386    Record.push_back(ON.getKind()); // FIXME: Stable encoding
387    Writer.AddSourceLocation(ON.getSourceRange().getBegin(), Record);
388    Writer.AddSourceLocation(ON.getSourceRange().getEnd(), Record);
389    switch (ON.getKind()) {
390    case OffsetOfExpr::OffsetOfNode::Array:
391      Record.push_back(ON.getArrayExprIndex());
392      break;
393
394    case OffsetOfExpr::OffsetOfNode::Field:
395      Writer.AddDeclRef(ON.getField(), Record);
396      break;
397
398    case OffsetOfExpr::OffsetOfNode::Identifier:
399      Writer.AddIdentifierRef(ON.getFieldName(), Record);
400      break;
401
402    case OffsetOfExpr::OffsetOfNode::Base:
403      Writer.AddCXXBaseSpecifier(*ON.getBase(), Record);
404      break;
405    }
406  }
407  for (unsigned I = 0, N = E->getNumExpressions(); I != N; ++I)
408    Writer.AddStmt(E->getIndexExpr(I));
409  Code = serialization::EXPR_OFFSETOF;
410}
411
412void ASTStmtWriter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
413  VisitExpr(E);
414  Record.push_back(E->getKind());
415  if (E->isArgumentType())
416    Writer.AddTypeSourceInfo(E->getArgumentTypeInfo(), Record);
417  else {
418    Record.push_back(0);
419    Writer.AddStmt(E->getArgumentExpr());
420  }
421  Writer.AddSourceLocation(E->getOperatorLoc(), Record);
422  Writer.AddSourceLocation(E->getRParenLoc(), Record);
423  Code = serialization::EXPR_SIZEOF_ALIGN_OF;
424}
425
426void ASTStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
427  VisitExpr(E);
428  Writer.AddStmt(E->getLHS());
429  Writer.AddStmt(E->getRHS());
430  Writer.AddSourceLocation(E->getRBracketLoc(), Record);
431  Code = serialization::EXPR_ARRAY_SUBSCRIPT;
432}
433
434void ASTStmtWriter::VisitCallExpr(CallExpr *E) {
435  VisitExpr(E);
436  Record.push_back(E->getNumArgs());
437  Writer.AddSourceLocation(E->getRParenLoc(), Record);
438  Writer.AddStmt(E->getCallee());
439  for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
440       Arg != ArgEnd; ++Arg)
441    Writer.AddStmt(*Arg);
442  Code = serialization::EXPR_CALL;
443}
444
445void ASTStmtWriter::VisitMemberExpr(MemberExpr *E) {
446  // Don't call VisitExpr, we'll write everything here.
447
448  Record.push_back(E->hasQualifier());
449  if (E->hasQualifier())
450    Writer.AddNestedNameSpecifierLoc(E->getQualifierLoc(), Record);
451
452  Record.push_back(E->HasTemplateKWAndArgsInfo);
453  if (E->HasTemplateKWAndArgsInfo) {
454    Writer.AddSourceLocation(E->getTemplateKeywordLoc(), Record);
455    unsigned NumTemplateArgs = E->getNumTemplateArgs();
456    Record.push_back(NumTemplateArgs);
457    Writer.AddSourceLocation(E->getLAngleLoc(), Record);
458    Writer.AddSourceLocation(E->getRAngleLoc(), Record);
459    for (unsigned i=0; i != NumTemplateArgs; ++i)
460      Writer.AddTemplateArgumentLoc(E->getTemplateArgs()[i], Record);
461  }
462
463  Record.push_back(E->hadMultipleCandidates());
464
465  DeclAccessPair FoundDecl = E->getFoundDecl();
466  Writer.AddDeclRef(FoundDecl.getDecl(), Record);
467  Record.push_back(FoundDecl.getAccess());
468
469  Writer.AddTypeRef(E->getType(), Record);
470  Record.push_back(E->getValueKind());
471  Record.push_back(E->getObjectKind());
472  Writer.AddStmt(E->getBase());
473  Writer.AddDeclRef(E->getMemberDecl(), Record);
474  Writer.AddSourceLocation(E->getMemberLoc(), Record);
475  Record.push_back(E->isArrow());
476  Writer.AddDeclarationNameLoc(E->MemberDNLoc,
477                               E->getMemberDecl()->getDeclName(), Record);
478  Code = serialization::EXPR_MEMBER;
479}
480
481void ASTStmtWriter::VisitObjCIsaExpr(ObjCIsaExpr *E) {
482  VisitExpr(E);
483  Writer.AddStmt(E->getBase());
484  Writer.AddSourceLocation(E->getIsaMemberLoc(), Record);
485  Record.push_back(E->isArrow());
486  Code = serialization::EXPR_OBJC_ISA;
487}
488
489void ASTStmtWriter::
490VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
491  VisitExpr(E);
492  Writer.AddStmt(E->getSubExpr());
493  Record.push_back(E->shouldCopy());
494  Code = serialization::EXPR_OBJC_INDIRECT_COPY_RESTORE;
495}
496
497void ASTStmtWriter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
498  VisitExplicitCastExpr(E);
499  Writer.AddSourceLocation(E->getLParenLoc(), Record);
500  Writer.AddSourceLocation(E->getBridgeKeywordLoc(), Record);
501  Record.push_back(E->getBridgeKind()); // FIXME: Stable encoding
502  Code = serialization::EXPR_OBJC_BRIDGED_CAST;
503}
504
505void ASTStmtWriter::VisitCastExpr(CastExpr *E) {
506  VisitExpr(E);
507  Record.push_back(E->path_size());
508  Writer.AddStmt(E->getSubExpr());
509  Record.push_back(E->getCastKind()); // FIXME: stable encoding
510
511  for (CastExpr::path_iterator
512         PI = E->path_begin(), PE = E->path_end(); PI != PE; ++PI)
513    Writer.AddCXXBaseSpecifier(**PI, Record);
514}
515
516void ASTStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
517  VisitExpr(E);
518  Writer.AddStmt(E->getLHS());
519  Writer.AddStmt(E->getRHS());
520  Record.push_back(E->getOpcode()); // FIXME: stable encoding
521  Writer.AddSourceLocation(E->getOperatorLoc(), Record);
522  Code = serialization::EXPR_BINARY_OPERATOR;
523}
524
525void ASTStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
526  VisitBinaryOperator(E);
527  Writer.AddTypeRef(E->getComputationLHSType(), Record);
528  Writer.AddTypeRef(E->getComputationResultType(), Record);
529  Code = serialization::EXPR_COMPOUND_ASSIGN_OPERATOR;
530}
531
532void ASTStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
533  VisitExpr(E);
534  Writer.AddStmt(E->getCond());
535  Writer.AddStmt(E->getLHS());
536  Writer.AddStmt(E->getRHS());
537  Writer.AddSourceLocation(E->getQuestionLoc(), Record);
538  Writer.AddSourceLocation(E->getColonLoc(), Record);
539  Code = serialization::EXPR_CONDITIONAL_OPERATOR;
540}
541
542void
543ASTStmtWriter::VisitBinaryConditionalOperator(BinaryConditionalOperator *E) {
544  VisitExpr(E);
545  Writer.AddStmt(E->getOpaqueValue());
546  Writer.AddStmt(E->getCommon());
547  Writer.AddStmt(E->getCond());
548  Writer.AddStmt(E->getTrueExpr());
549  Writer.AddStmt(E->getFalseExpr());
550  Writer.AddSourceLocation(E->getQuestionLoc(), Record);
551  Writer.AddSourceLocation(E->getColonLoc(), Record);
552  Code = serialization::EXPR_BINARY_CONDITIONAL_OPERATOR;
553}
554
555void ASTStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
556  VisitCastExpr(E);
557  Code = serialization::EXPR_IMPLICIT_CAST;
558}
559
560void ASTStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
561  VisitCastExpr(E);
562  Writer.AddTypeSourceInfo(E->getTypeInfoAsWritten(), Record);
563}
564
565void ASTStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
566  VisitExplicitCastExpr(E);
567  Writer.AddSourceLocation(E->getLParenLoc(), Record);
568  Writer.AddSourceLocation(E->getRParenLoc(), Record);
569  Code = serialization::EXPR_CSTYLE_CAST;
570}
571
572void ASTStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
573  VisitExpr(E);
574  Writer.AddSourceLocation(E->getLParenLoc(), Record);
575  Writer.AddTypeSourceInfo(E->getTypeSourceInfo(), Record);
576  Writer.AddStmt(E->getInitializer());
577  Record.push_back(E->isFileScope());
578  Code = serialization::EXPR_COMPOUND_LITERAL;
579}
580
581void ASTStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
582  VisitExpr(E);
583  Writer.AddStmt(E->getBase());
584  Writer.AddIdentifierRef(&E->getAccessor(), Record);
585  Writer.AddSourceLocation(E->getAccessorLoc(), Record);
586  Code = serialization::EXPR_EXT_VECTOR_ELEMENT;
587}
588
589void ASTStmtWriter::VisitInitListExpr(InitListExpr *E) {
590  VisitExpr(E);
591  Writer.AddStmt(E->getSyntacticForm());
592  Writer.AddSourceLocation(E->getLBraceLoc(), Record);
593  Writer.AddSourceLocation(E->getRBraceLoc(), Record);
594  bool isArrayFiller = E->ArrayFillerOrUnionFieldInit.is<Expr*>();
595  Record.push_back(isArrayFiller);
596  if (isArrayFiller)
597    Writer.AddStmt(E->getArrayFiller());
598  else
599    Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
600  Record.push_back(E->hadArrayRangeDesignator());
601  Record.push_back(E->initializesStdInitializerList());
602  Record.push_back(E->getNumInits());
603  if (isArrayFiller) {
604    // ArrayFiller may have filled "holes" due to designated initializer.
605    // Replace them by 0 to indicate that the filler goes in that place.
606    Expr *filler = E->getArrayFiller();
607    for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
608      Writer.AddStmt(E->getInit(I) != filler ? E->getInit(I) : 0);
609  } else {
610    for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
611      Writer.AddStmt(E->getInit(I));
612  }
613  Code = serialization::EXPR_INIT_LIST;
614}
615
616void ASTStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
617  VisitExpr(E);
618  Record.push_back(E->getNumSubExprs());
619  for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
620    Writer.AddStmt(E->getSubExpr(I));
621  Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
622  Record.push_back(E->usesGNUSyntax());
623  for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
624                                             DEnd = E->designators_end();
625       D != DEnd; ++D) {
626    if (D->isFieldDesignator()) {
627      if (FieldDecl *Field = D->getField()) {
628        Record.push_back(serialization::DESIG_FIELD_DECL);
629        Writer.AddDeclRef(Field, Record);
630      } else {
631        Record.push_back(serialization::DESIG_FIELD_NAME);
632        Writer.AddIdentifierRef(D->getFieldName(), Record);
633      }
634      Writer.AddSourceLocation(D->getDotLoc(), Record);
635      Writer.AddSourceLocation(D->getFieldLoc(), Record);
636    } else if (D->isArrayDesignator()) {
637      Record.push_back(serialization::DESIG_ARRAY);
638      Record.push_back(D->getFirstExprIndex());
639      Writer.AddSourceLocation(D->getLBracketLoc(), Record);
640      Writer.AddSourceLocation(D->getRBracketLoc(), Record);
641    } else {
642      assert(D->isArrayRangeDesignator() && "Unknown designator");
643      Record.push_back(serialization::DESIG_ARRAY_RANGE);
644      Record.push_back(D->getFirstExprIndex());
645      Writer.AddSourceLocation(D->getLBracketLoc(), Record);
646      Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
647      Writer.AddSourceLocation(D->getRBracketLoc(), Record);
648    }
649  }
650  Code = serialization::EXPR_DESIGNATED_INIT;
651}
652
653void ASTStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
654  VisitExpr(E);
655  Code = serialization::EXPR_IMPLICIT_VALUE_INIT;
656}
657
658void ASTStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
659  VisitExpr(E);
660  Writer.AddStmt(E->getSubExpr());
661  Writer.AddTypeSourceInfo(E->getWrittenTypeInfo(), Record);
662  Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
663  Writer.AddSourceLocation(E->getRParenLoc(), Record);
664  Code = serialization::EXPR_VA_ARG;
665}
666
667void ASTStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) {
668  VisitExpr(E);
669  Writer.AddSourceLocation(E->getAmpAmpLoc(), Record);
670  Writer.AddSourceLocation(E->getLabelLoc(), Record);
671  Writer.AddDeclRef(E->getLabel(), Record);
672  Code = serialization::EXPR_ADDR_LABEL;
673}
674
675void ASTStmtWriter::VisitStmtExpr(StmtExpr *E) {
676  VisitExpr(E);
677  Writer.AddStmt(E->getSubStmt());
678  Writer.AddSourceLocation(E->getLParenLoc(), Record);
679  Writer.AddSourceLocation(E->getRParenLoc(), Record);
680  Code = serialization::EXPR_STMT;
681}
682
683void ASTStmtWriter::VisitChooseExpr(ChooseExpr *E) {
684  VisitExpr(E);
685  Writer.AddStmt(E->getCond());
686  Writer.AddStmt(E->getLHS());
687  Writer.AddStmt(E->getRHS());
688  Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
689  Writer.AddSourceLocation(E->getRParenLoc(), Record);
690  Code = serialization::EXPR_CHOOSE;
691}
692
693void ASTStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
694  VisitExpr(E);
695  Writer.AddSourceLocation(E->getTokenLocation(), Record);
696  Code = serialization::EXPR_GNU_NULL;
697}
698
699void ASTStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
700  VisitExpr(E);
701  Record.push_back(E->getNumSubExprs());
702  for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
703    Writer.AddStmt(E->getExpr(I));
704  Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
705  Writer.AddSourceLocation(E->getRParenLoc(), Record);
706  Code = serialization::EXPR_SHUFFLE_VECTOR;
707}
708
709void ASTStmtWriter::VisitBlockExpr(BlockExpr *E) {
710  VisitExpr(E);
711  Writer.AddDeclRef(E->getBlockDecl(), Record);
712  Code = serialization::EXPR_BLOCK;
713}
714
715void ASTStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
716  VisitExpr(E);
717  Writer.AddDeclRef(E->getDecl(), Record);
718  Writer.AddSourceLocation(E->getLocation(), Record);
719  Record.push_back(E->isByRef());
720  Record.push_back(E->isConstQualAdded());
721  Code = serialization::EXPR_BLOCK_DECL_REF;
722}
723
724void ASTStmtWriter::VisitGenericSelectionExpr(GenericSelectionExpr *E) {
725  VisitExpr(E);
726  Record.push_back(E->getNumAssocs());
727
728  Writer.AddStmt(E->getControllingExpr());
729  for (unsigned I = 0, N = E->getNumAssocs(); I != N; ++I) {
730    Writer.AddTypeSourceInfo(E->getAssocTypeSourceInfo(I), Record);
731    Writer.AddStmt(E->getAssocExpr(I));
732  }
733  Record.push_back(E->isResultDependent() ? -1U : E->getResultIndex());
734
735  Writer.AddSourceLocation(E->getGenericLoc(), Record);
736  Writer.AddSourceLocation(E->getDefaultLoc(), Record);
737  Writer.AddSourceLocation(E->getRParenLoc(), Record);
738  Code = serialization::EXPR_GENERIC_SELECTION;
739}
740
741void ASTStmtWriter::VisitPseudoObjectExpr(PseudoObjectExpr *E) {
742  VisitExpr(E);
743  Record.push_back(E->getNumSemanticExprs());
744
745  // Push the result index.  Currently, this needs to exactly match
746  // the encoding used internally for ResultIndex.
747  unsigned result = E->getResultExprIndex();
748  result = (result == PseudoObjectExpr::NoResult ? 0 : result + 1);
749  Record.push_back(result);
750
751  Writer.AddStmt(E->getSyntacticForm());
752  for (PseudoObjectExpr::semantics_iterator
753         i = E->semantics_begin(), e = E->semantics_end(); i != e; ++i) {
754    Writer.AddStmt(*i);
755  }
756  Code = serialization::EXPR_PSEUDO_OBJECT;
757}
758
759void ASTStmtWriter::VisitAtomicExpr(AtomicExpr *E) {
760  VisitExpr(E);
761  Record.push_back(E->getOp());
762  Writer.AddStmt(E->getPtr());
763  Writer.AddStmt(E->getOrder());
764  if (E->getOp() != AtomicExpr::Load)
765    Writer.AddStmt(E->getVal1());
766  if (E->isCmpXChg()) {
767    Writer.AddStmt(E->getOrderFail());
768    Writer.AddStmt(E->getVal2());
769  }
770  Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
771  Writer.AddSourceLocation(E->getRParenLoc(), Record);
772  Code = serialization::EXPR_ATOMIC;
773}
774
775//===----------------------------------------------------------------------===//
776// Objective-C Expressions and Statements.
777//===----------------------------------------------------------------------===//
778
779void ASTStmtWriter::VisitObjCStringLiteral(ObjCStringLiteral *E) {
780  VisitExpr(E);
781  Writer.AddStmt(E->getString());
782  Writer.AddSourceLocation(E->getAtLoc(), Record);
783  Code = serialization::EXPR_OBJC_STRING_LITERAL;
784}
785
786void ASTStmtWriter::VisitObjCNumericLiteral(ObjCNumericLiteral *E) {
787  VisitExpr(E);
788  Writer.AddStmt(E->getNumber());
789  Writer.AddDeclRef(E->getObjCNumericLiteralMethod(), Record);
790  Writer.AddSourceLocation(E->getAtLoc(), Record);
791  Code = serialization::EXPR_OBJC_NUMERIC_LITERAL;
792}
793
794void ASTStmtWriter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
795  VisitExpr(E);
796  Record.push_back(E->getNumElements());
797  for (unsigned i = 0; i < E->getNumElements(); i++)
798    Writer.AddStmt(E->getElement(i));
799  Writer.AddDeclRef(E->getArrayWithObjectsMethod(), Record);
800  Writer.AddSourceRange(E->getSourceRange(), Record);
801  Code = serialization::EXPR_OBJC_ARRAY_LITERAL;
802}
803
804void ASTStmtWriter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
805  VisitExpr(E);
806  Record.push_back(E->getNumElements());
807  Record.push_back(E->HasPackExpansions);
808  for (unsigned i = 0; i < E->getNumElements(); i++) {
809    ObjCDictionaryElement Element = E->getKeyValueElement(i);
810    Writer.AddStmt(Element.Key);
811    Writer.AddStmt(Element.Value);
812    if (E->HasPackExpansions) {
813      Writer.AddSourceLocation(Element.EllipsisLoc, Record);
814      unsigned NumExpansions = 0;
815      if (Element.NumExpansions)
816        NumExpansions = *Element.NumExpansions + 1;
817      Record.push_back(NumExpansions);
818    }
819  }
820
821  Writer.AddDeclRef(E->getDictWithObjectsMethod(), Record);
822  Writer.AddSourceRange(E->getSourceRange(), Record);
823  Code = serialization::EXPR_OBJC_DICTIONARY_LITERAL;
824}
825
826void ASTStmtWriter::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
827  VisitExpr(E);
828  Writer.AddTypeSourceInfo(E->getEncodedTypeSourceInfo(), Record);
829  Writer.AddSourceLocation(E->getAtLoc(), Record);
830  Writer.AddSourceLocation(E->getRParenLoc(), Record);
831  Code = serialization::EXPR_OBJC_ENCODE;
832}
833
834void ASTStmtWriter::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
835  VisitExpr(E);
836  Writer.AddSelectorRef(E->getSelector(), Record);
837  Writer.AddSourceLocation(E->getAtLoc(), Record);
838  Writer.AddSourceLocation(E->getRParenLoc(), Record);
839  Code = serialization::EXPR_OBJC_SELECTOR_EXPR;
840}
841
842void ASTStmtWriter::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
843  VisitExpr(E);
844  Writer.AddDeclRef(E->getProtocol(), Record);
845  Writer.AddSourceLocation(E->getAtLoc(), Record);
846  Writer.AddSourceLocation(E->getRParenLoc(), Record);
847  Code = serialization::EXPR_OBJC_PROTOCOL_EXPR;
848}
849
850void ASTStmtWriter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
851  VisitExpr(E);
852  Writer.AddDeclRef(E->getDecl(), Record);
853  Writer.AddSourceLocation(E->getLocation(), Record);
854  Writer.AddStmt(E->getBase());
855  Record.push_back(E->isArrow());
856  Record.push_back(E->isFreeIvar());
857  Code = serialization::EXPR_OBJC_IVAR_REF_EXPR;
858}
859
860void ASTStmtWriter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
861  VisitExpr(E);
862  Record.push_back(E->isImplicitProperty());
863  if (E->isImplicitProperty()) {
864    Writer.AddDeclRef(E->getImplicitPropertyGetter(), Record);
865    Writer.AddDeclRef(E->getImplicitPropertySetter(), Record);
866  } else {
867    Writer.AddDeclRef(E->getExplicitProperty(), Record);
868  }
869  Writer.AddSourceLocation(E->getLocation(), Record);
870  Writer.AddSourceLocation(E->getReceiverLocation(), Record);
871  if (E->isObjectReceiver()) {
872    Record.push_back(0);
873    Writer.AddStmt(E->getBase());
874  } else if (E->isSuperReceiver()) {
875    Record.push_back(1);
876    Writer.AddTypeRef(E->getSuperReceiverType(), Record);
877  } else {
878    Record.push_back(2);
879    Writer.AddDeclRef(E->getClassReceiver(), Record);
880  }
881
882  Code = serialization::EXPR_OBJC_PROPERTY_REF_EXPR;
883}
884
885void ASTStmtWriter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *E) {
886  VisitExpr(E);
887  Writer.AddSourceLocation(E->getRBracket(), Record);
888  Writer.AddStmt(E->getBaseExpr());
889  Writer.AddStmt(E->getKeyExpr());
890  Writer.AddDeclRef(E->getAtIndexMethodDecl(), Record);
891  Writer.AddDeclRef(E->setAtIndexMethodDecl(), Record);
892
893  Code = serialization::EXPR_OBJC_SUBSCRIPT_REF_EXPR;
894}
895
896void ASTStmtWriter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
897  VisitExpr(E);
898  Record.push_back(E->getNumArgs());
899  Record.push_back(E->getNumStoredSelLocs());
900  Record.push_back(E->SelLocsKind);
901  Record.push_back(E->isDelegateInitCall());
902  Record.push_back(E->IsImplicit);
903  Record.push_back((unsigned)E->getReceiverKind()); // FIXME: stable encoding
904  switch (E->getReceiverKind()) {
905  case ObjCMessageExpr::Instance:
906    Writer.AddStmt(E->getInstanceReceiver());
907    break;
908
909  case ObjCMessageExpr::Class:
910    Writer.AddTypeSourceInfo(E->getClassReceiverTypeInfo(), Record);
911    break;
912
913  case ObjCMessageExpr::SuperClass:
914  case ObjCMessageExpr::SuperInstance:
915    Writer.AddTypeRef(E->getSuperType(), Record);
916    Writer.AddSourceLocation(E->getSuperLoc(), Record);
917    break;
918  }
919
920  if (E->getMethodDecl()) {
921    Record.push_back(1);
922    Writer.AddDeclRef(E->getMethodDecl(), Record);
923  } else {
924    Record.push_back(0);
925    Writer.AddSelectorRef(E->getSelector(), Record);
926  }
927
928  Writer.AddSourceLocation(E->getLeftLoc(), Record);
929  Writer.AddSourceLocation(E->getRightLoc(), Record);
930
931  for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
932       Arg != ArgEnd; ++Arg)
933    Writer.AddStmt(*Arg);
934
935  SourceLocation *Locs = E->getStoredSelLocs();
936  for (unsigned i = 0, e = E->getNumStoredSelLocs(); i != e; ++i)
937    Writer.AddSourceLocation(Locs[i], Record);
938
939  Code = serialization::EXPR_OBJC_MESSAGE_EXPR;
940}
941
942void ASTStmtWriter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
943  VisitStmt(S);
944  Writer.AddStmt(S->getElement());
945  Writer.AddStmt(S->getCollection());
946  Writer.AddStmt(S->getBody());
947  Writer.AddSourceLocation(S->getForLoc(), Record);
948  Writer.AddSourceLocation(S->getRParenLoc(), Record);
949  Code = serialization::STMT_OBJC_FOR_COLLECTION;
950}
951
952void ASTStmtWriter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
953  Writer.AddStmt(S->getCatchBody());
954  Writer.AddDeclRef(S->getCatchParamDecl(), Record);
955  Writer.AddSourceLocation(S->getAtCatchLoc(), Record);
956  Writer.AddSourceLocation(S->getRParenLoc(), Record);
957  Code = serialization::STMT_OBJC_CATCH;
958}
959
960void ASTStmtWriter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
961  Writer.AddStmt(S->getFinallyBody());
962  Writer.AddSourceLocation(S->getAtFinallyLoc(), Record);
963  Code = serialization::STMT_OBJC_FINALLY;
964}
965
966void ASTStmtWriter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S) {
967  Writer.AddStmt(S->getSubStmt());
968  Writer.AddSourceLocation(S->getAtLoc(), Record);
969  Code = serialization::STMT_OBJC_AUTORELEASE_POOL;
970}
971
972void ASTStmtWriter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
973  Record.push_back(S->getNumCatchStmts());
974  Record.push_back(S->getFinallyStmt() != 0);
975  Writer.AddStmt(S->getTryBody());
976  for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I)
977    Writer.AddStmt(S->getCatchStmt(I));
978  if (S->getFinallyStmt())
979    Writer.AddStmt(S->getFinallyStmt());
980  Writer.AddSourceLocation(S->getAtTryLoc(), Record);
981  Code = serialization::STMT_OBJC_AT_TRY;
982}
983
984void ASTStmtWriter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S) {
985  Writer.AddStmt(S->getSynchExpr());
986  Writer.AddStmt(S->getSynchBody());
987  Writer.AddSourceLocation(S->getAtSynchronizedLoc(), Record);
988  Code = serialization::STMT_OBJC_AT_SYNCHRONIZED;
989}
990
991void ASTStmtWriter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
992  Writer.AddStmt(S->getThrowExpr());
993  Writer.AddSourceLocation(S->getThrowLoc(), Record);
994  Code = serialization::STMT_OBJC_AT_THROW;
995}
996
997void ASTStmtWriter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *E) {
998  VisitExpr(E);
999  Record.push_back(E->getValue());
1000  Writer.AddSourceLocation(E->getLocation(), Record);
1001  Code = serialization::EXPR_OBJC_BOOL_LITERAL;
1002}
1003
1004//===----------------------------------------------------------------------===//
1005// C++ Expressions and Statements.
1006//===----------------------------------------------------------------------===//
1007
1008void ASTStmtWriter::VisitCXXCatchStmt(CXXCatchStmt *S) {
1009  VisitStmt(S);
1010  Writer.AddSourceLocation(S->getCatchLoc(), Record);
1011  Writer.AddDeclRef(S->getExceptionDecl(), Record);
1012  Writer.AddStmt(S->getHandlerBlock());
1013  Code = serialization::STMT_CXX_CATCH;
1014}
1015
1016void ASTStmtWriter::VisitCXXTryStmt(CXXTryStmt *S) {
1017  VisitStmt(S);
1018  Record.push_back(S->getNumHandlers());
1019  Writer.AddSourceLocation(S->getTryLoc(), Record);
1020  Writer.AddStmt(S->getTryBlock());
1021  for (unsigned i = 0, e = S->getNumHandlers(); i != e; ++i)
1022    Writer.AddStmt(S->getHandler(i));
1023  Code = serialization::STMT_CXX_TRY;
1024}
1025
1026void ASTStmtWriter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
1027  VisitStmt(S);
1028  Writer.AddSourceLocation(S->getForLoc(), Record);
1029  Writer.AddSourceLocation(S->getColonLoc(), Record);
1030  Writer.AddSourceLocation(S->getRParenLoc(), Record);
1031  Writer.AddStmt(S->getRangeStmt());
1032  Writer.AddStmt(S->getBeginEndStmt());
1033  Writer.AddStmt(S->getCond());
1034  Writer.AddStmt(S->getInc());
1035  Writer.AddStmt(S->getLoopVarStmt());
1036  Writer.AddStmt(S->getBody());
1037  Code = serialization::STMT_CXX_FOR_RANGE;
1038}
1039
1040void ASTStmtWriter::VisitMSDependentExistsStmt(MSDependentExistsStmt *S) {
1041  VisitStmt(S);
1042  Writer.AddSourceLocation(S->getKeywordLoc(), Record);
1043  Record.push_back(S->isIfExists());
1044  Writer.AddNestedNameSpecifierLoc(S->getQualifierLoc(), Record);
1045  Writer.AddDeclarationNameInfo(S->getNameInfo(), Record);
1046  Writer.AddStmt(S->getSubStmt());
1047  Code = serialization::STMT_MS_DEPENDENT_EXISTS;
1048}
1049
1050void ASTStmtWriter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) {
1051  VisitCallExpr(E);
1052  Record.push_back(E->getOperator());
1053  Code = serialization::EXPR_CXX_OPERATOR_CALL;
1054}
1055
1056void ASTStmtWriter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
1057  VisitCallExpr(E);
1058  Code = serialization::EXPR_CXX_MEMBER_CALL;
1059}
1060
1061void ASTStmtWriter::VisitCXXConstructExpr(CXXConstructExpr *E) {
1062  VisitExpr(E);
1063  Record.push_back(E->getNumArgs());
1064  for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
1065    Writer.AddStmt(E->getArg(I));
1066  Writer.AddDeclRef(E->getConstructor(), Record);
1067  Writer.AddSourceLocation(E->getLocation(), Record);
1068  Record.push_back(E->isElidable());
1069  Record.push_back(E->hadMultipleCandidates());
1070  Record.push_back(E->requiresZeroInitialization());
1071  Record.push_back(E->getConstructionKind()); // FIXME: stable encoding
1072  Writer.AddSourceRange(E->getParenRange(), Record);
1073  Code = serialization::EXPR_CXX_CONSTRUCT;
1074}
1075
1076void ASTStmtWriter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) {
1077  VisitCXXConstructExpr(E);
1078  Writer.AddTypeSourceInfo(E->getTypeSourceInfo(), Record);
1079  Code = serialization::EXPR_CXX_TEMPORARY_OBJECT;
1080}
1081
1082void ASTStmtWriter::VisitLambdaExpr(LambdaExpr *E) {
1083  VisitExpr(E);
1084  Record.push_back(E->NumCaptures);
1085  unsigned NumArrayIndexVars = 0;
1086  if (E->HasArrayIndexVars)
1087    NumArrayIndexVars = E->getArrayIndexStarts()[E->NumCaptures];
1088  Record.push_back(NumArrayIndexVars);
1089  Writer.AddSourceRange(E->IntroducerRange, Record);
1090  Record.push_back(E->CaptureDefault); // FIXME: stable encoding
1091  Record.push_back(E->ExplicitParams);
1092  Record.push_back(E->ExplicitResultType);
1093  Writer.AddSourceLocation(E->ClosingBrace, Record);
1094
1095  // Add capture initializers.
1096  for (LambdaExpr::capture_init_iterator C = E->capture_init_begin(),
1097                                      CEnd = E->capture_init_end();
1098       C != CEnd; ++C) {
1099    Writer.AddStmt(*C);
1100  }
1101
1102  // Add array index variables, if any.
1103  if (NumArrayIndexVars) {
1104    Record.append(E->getArrayIndexStarts(),
1105                  E->getArrayIndexStarts() + E->NumCaptures + 1);
1106    VarDecl **ArrayIndexVars = E->getArrayIndexVars();
1107    for (unsigned I = 0; I != NumArrayIndexVars; ++I)
1108      Writer.AddDeclRef(ArrayIndexVars[I], Record);
1109  }
1110
1111  Code = serialization::EXPR_LAMBDA;
1112}
1113
1114void ASTStmtWriter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
1115  VisitExplicitCastExpr(E);
1116  Writer.AddSourceRange(SourceRange(E->getOperatorLoc(), E->getRParenLoc()),
1117                        Record);
1118}
1119
1120void ASTStmtWriter::VisitCXXStaticCastExpr(CXXStaticCastExpr *E) {
1121  VisitCXXNamedCastExpr(E);
1122  Code = serialization::EXPR_CXX_STATIC_CAST;
1123}
1124
1125void ASTStmtWriter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *E) {
1126  VisitCXXNamedCastExpr(E);
1127  Code = serialization::EXPR_CXX_DYNAMIC_CAST;
1128}
1129
1130void ASTStmtWriter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *E) {
1131  VisitCXXNamedCastExpr(E);
1132  Code = serialization::EXPR_CXX_REINTERPRET_CAST;
1133}
1134
1135void ASTStmtWriter::VisitCXXConstCastExpr(CXXConstCastExpr *E) {
1136  VisitCXXNamedCastExpr(E);
1137  Code = serialization::EXPR_CXX_CONST_CAST;
1138}
1139
1140void ASTStmtWriter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E) {
1141  VisitExplicitCastExpr(E);
1142  Writer.AddSourceLocation(E->getTypeBeginLoc(), Record);
1143  Writer.AddSourceLocation(E->getRParenLoc(), Record);
1144  Code = serialization::EXPR_CXX_FUNCTIONAL_CAST;
1145}
1146
1147void ASTStmtWriter::VisitUserDefinedLiteral(UserDefinedLiteral *E) {
1148  VisitCallExpr(E);
1149  Writer.AddSourceLocation(E->UDSuffixLoc, Record);
1150  Code = serialization::EXPR_USER_DEFINED_LITERAL;
1151}
1152
1153void ASTStmtWriter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
1154  VisitExpr(E);
1155  Record.push_back(E->getValue());
1156  Writer.AddSourceLocation(E->getLocation(), Record);
1157  Code = serialization::EXPR_CXX_BOOL_LITERAL;
1158}
1159
1160void ASTStmtWriter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
1161  VisitExpr(E);
1162  Writer.AddSourceLocation(E->getLocation(), Record);
1163  Code = serialization::EXPR_CXX_NULL_PTR_LITERAL;
1164}
1165
1166void ASTStmtWriter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
1167  VisitExpr(E);
1168  Writer.AddSourceRange(E->getSourceRange(), Record);
1169  if (E->isTypeOperand()) {
1170    Writer.AddTypeSourceInfo(E->getTypeOperandSourceInfo(), Record);
1171    Code = serialization::EXPR_CXX_TYPEID_TYPE;
1172  } else {
1173    Writer.AddStmt(E->getExprOperand());
1174    Code = serialization::EXPR_CXX_TYPEID_EXPR;
1175  }
1176}
1177
1178void ASTStmtWriter::VisitCXXThisExpr(CXXThisExpr *E) {
1179  VisitExpr(E);
1180  Writer.AddSourceLocation(E->getLocation(), Record);
1181  Record.push_back(E->isImplicit());
1182  Code = serialization::EXPR_CXX_THIS;
1183}
1184
1185void ASTStmtWriter::VisitCXXThrowExpr(CXXThrowExpr *E) {
1186  VisitExpr(E);
1187  Writer.AddSourceLocation(E->getThrowLoc(), Record);
1188  Writer.AddStmt(E->getSubExpr());
1189  Record.push_back(E->isThrownVariableInScope());
1190  Code = serialization::EXPR_CXX_THROW;
1191}
1192
1193void ASTStmtWriter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
1194  VisitExpr(E);
1195
1196  bool HasOtherExprStored = E->Param.getInt();
1197  // Store these first, the reader reads them before creation.
1198  Record.push_back(HasOtherExprStored);
1199  if (HasOtherExprStored)
1200    Writer.AddStmt(E->getExpr());
1201  Writer.AddDeclRef(E->getParam(), Record);
1202  Writer.AddSourceLocation(E->getUsedLocation(), Record);
1203
1204  Code = serialization::EXPR_CXX_DEFAULT_ARG;
1205}
1206
1207void ASTStmtWriter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
1208  VisitExpr(E);
1209  Writer.AddCXXTemporary(E->getTemporary(), Record);
1210  Writer.AddStmt(E->getSubExpr());
1211  Code = serialization::EXPR_CXX_BIND_TEMPORARY;
1212}
1213
1214void ASTStmtWriter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
1215  VisitExpr(E);
1216  Writer.AddTypeSourceInfo(E->getTypeSourceInfo(), Record);
1217  Writer.AddSourceLocation(E->getRParenLoc(), Record);
1218  Code = serialization::EXPR_CXX_SCALAR_VALUE_INIT;
1219}
1220
1221void ASTStmtWriter::VisitCXXNewExpr(CXXNewExpr *E) {
1222  VisitExpr(E);
1223  Record.push_back(E->isGlobalNew());
1224  Record.push_back(E->isArray());
1225  Record.push_back(E->doesUsualArrayDeleteWantSize());
1226  Record.push_back(E->getNumPlacementArgs());
1227  Record.push_back(E->StoredInitializationStyle);
1228  Writer.AddDeclRef(E->getOperatorNew(), Record);
1229  Writer.AddDeclRef(E->getOperatorDelete(), Record);
1230  Writer.AddTypeSourceInfo(E->getAllocatedTypeSourceInfo(), Record);
1231  Writer.AddSourceRange(E->getTypeIdParens(), Record);
1232  Writer.AddSourceLocation(E->getStartLoc(), Record);
1233  Writer.AddSourceRange(E->getDirectInitRange(), Record);
1234  for (CXXNewExpr::arg_iterator I = E->raw_arg_begin(), e = E->raw_arg_end();
1235       I != e; ++I)
1236    Writer.AddStmt(*I);
1237
1238  Code = serialization::EXPR_CXX_NEW;
1239}
1240
1241void ASTStmtWriter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1242  VisitExpr(E);
1243  Record.push_back(E->isGlobalDelete());
1244  Record.push_back(E->isArrayForm());
1245  Record.push_back(E->isArrayFormAsWritten());
1246  Record.push_back(E->doesUsualArrayDeleteWantSize());
1247  Writer.AddDeclRef(E->getOperatorDelete(), Record);
1248  Writer.AddStmt(E->getArgument());
1249  Writer.AddSourceLocation(E->getSourceRange().getBegin(), Record);
1250
1251  Code = serialization::EXPR_CXX_DELETE;
1252}
1253
1254void ASTStmtWriter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1255  VisitExpr(E);
1256
1257  Writer.AddStmt(E->getBase());
1258  Record.push_back(E->isArrow());
1259  Writer.AddSourceLocation(E->getOperatorLoc(), Record);
1260  Writer.AddNestedNameSpecifierLoc(E->getQualifierLoc(), Record);
1261  Writer.AddTypeSourceInfo(E->getScopeTypeInfo(), Record);
1262  Writer.AddSourceLocation(E->getColonColonLoc(), Record);
1263  Writer.AddSourceLocation(E->getTildeLoc(), Record);
1264
1265  // PseudoDestructorTypeStorage.
1266  Writer.AddIdentifierRef(E->getDestroyedTypeIdentifier(), Record);
1267  if (E->getDestroyedTypeIdentifier())
1268    Writer.AddSourceLocation(E->getDestroyedTypeLoc(), Record);
1269  else
1270    Writer.AddTypeSourceInfo(E->getDestroyedTypeInfo(), Record);
1271
1272  Code = serialization::EXPR_CXX_PSEUDO_DESTRUCTOR;
1273}
1274
1275void ASTStmtWriter::VisitExprWithCleanups(ExprWithCleanups *E) {
1276  VisitExpr(E);
1277  Record.push_back(E->getNumObjects());
1278  for (unsigned i = 0, e = E->getNumObjects(); i != e; ++i)
1279    Writer.AddDeclRef(E->getObject(i), Record);
1280
1281  Writer.AddStmt(E->getSubExpr());
1282  Code = serialization::EXPR_EXPR_WITH_CLEANUPS;
1283}
1284
1285void
1286ASTStmtWriter::VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E){
1287  VisitExpr(E);
1288
1289  // Don't emit anything here, HasTemplateKWAndArgsInfo must be
1290  // emitted first.
1291
1292  Record.push_back(E->HasTemplateKWAndArgsInfo);
1293  if (E->HasTemplateKWAndArgsInfo) {
1294    const ASTTemplateKWAndArgsInfo &Args = *E->getTemplateKWAndArgsInfo();
1295    Record.push_back(Args.NumTemplateArgs);
1296    AddTemplateKWAndArgsInfo(Args);
1297  }
1298
1299  if (!E->isImplicitAccess())
1300    Writer.AddStmt(E->getBase());
1301  else
1302    Writer.AddStmt(0);
1303  Writer.AddTypeRef(E->getBaseType(), Record);
1304  Record.push_back(E->isArrow());
1305  Writer.AddSourceLocation(E->getOperatorLoc(), Record);
1306  Writer.AddNestedNameSpecifierLoc(E->getQualifierLoc(), Record);
1307  Writer.AddDeclRef(E->getFirstQualifierFoundInScope(), Record);
1308  Writer.AddDeclarationNameInfo(E->MemberNameInfo, Record);
1309  Code = serialization::EXPR_CXX_DEPENDENT_SCOPE_MEMBER;
1310}
1311
1312void
1313ASTStmtWriter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
1314  VisitExpr(E);
1315
1316  // Don't emit anything here, HasTemplateKWAndArgsInfo must be
1317  // emitted first.
1318
1319  Record.push_back(E->HasTemplateKWAndArgsInfo);
1320  if (E->HasTemplateKWAndArgsInfo) {
1321    const ASTTemplateKWAndArgsInfo &Args = *E->getTemplateKWAndArgsInfo();
1322    Record.push_back(Args.NumTemplateArgs);
1323    AddTemplateKWAndArgsInfo(Args);
1324  }
1325
1326  Writer.AddNestedNameSpecifierLoc(E->getQualifierLoc(), Record);
1327  Writer.AddDeclarationNameInfo(E->NameInfo, Record);
1328  Code = serialization::EXPR_CXX_DEPENDENT_SCOPE_DECL_REF;
1329}
1330
1331void
1332ASTStmtWriter::VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E) {
1333  VisitExpr(E);
1334  Record.push_back(E->arg_size());
1335  for (CXXUnresolvedConstructExpr::arg_iterator
1336         ArgI = E->arg_begin(), ArgE = E->arg_end(); ArgI != ArgE; ++ArgI)
1337    Writer.AddStmt(*ArgI);
1338  Writer.AddTypeSourceInfo(E->getTypeSourceInfo(), Record);
1339  Writer.AddSourceLocation(E->getLParenLoc(), Record);
1340  Writer.AddSourceLocation(E->getRParenLoc(), Record);
1341  Code = serialization::EXPR_CXX_UNRESOLVED_CONSTRUCT;
1342}
1343
1344void ASTStmtWriter::VisitOverloadExpr(OverloadExpr *E) {
1345  VisitExpr(E);
1346
1347  // Don't emit anything here, HasTemplateKWAndArgsInfo must be
1348  // emitted first.
1349
1350  Record.push_back(E->HasTemplateKWAndArgsInfo);
1351  if (E->HasTemplateKWAndArgsInfo) {
1352    const ASTTemplateKWAndArgsInfo &Args = *E->getTemplateKWAndArgsInfo();
1353    Record.push_back(Args.NumTemplateArgs);
1354    AddTemplateKWAndArgsInfo(Args);
1355  }
1356
1357  Record.push_back(E->getNumDecls());
1358  for (OverloadExpr::decls_iterator
1359         OvI = E->decls_begin(), OvE = E->decls_end(); OvI != OvE; ++OvI) {
1360    Writer.AddDeclRef(OvI.getDecl(), Record);
1361    Record.push_back(OvI.getAccess());
1362  }
1363
1364  Writer.AddDeclarationNameInfo(E->NameInfo, Record);
1365  Writer.AddNestedNameSpecifierLoc(E->getQualifierLoc(), Record);
1366}
1367
1368void ASTStmtWriter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
1369  VisitOverloadExpr(E);
1370  Record.push_back(E->isArrow());
1371  Record.push_back(E->hasUnresolvedUsing());
1372  Writer.AddStmt(!E->isImplicitAccess() ? E->getBase() : 0);
1373  Writer.AddTypeRef(E->getBaseType(), Record);
1374  Writer.AddSourceLocation(E->getOperatorLoc(), Record);
1375  Code = serialization::EXPR_CXX_UNRESOLVED_MEMBER;
1376}
1377
1378void ASTStmtWriter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
1379  VisitOverloadExpr(E);
1380  Record.push_back(E->requiresADL());
1381  if (E->requiresADL())
1382    Record.push_back(E->isStdAssociatedNamespace());
1383  Record.push_back(E->isOverloaded());
1384  Writer.AddDeclRef(E->getNamingClass(), Record);
1385  Code = serialization::EXPR_CXX_UNRESOLVED_LOOKUP;
1386}
1387
1388void ASTStmtWriter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1389  VisitExpr(E);
1390  Record.push_back(E->getTrait());
1391  Record.push_back(E->getValue());
1392  Writer.AddSourceRange(E->getSourceRange(), Record);
1393  Writer.AddTypeSourceInfo(E->getQueriedTypeSourceInfo(), Record);
1394  Code = serialization::EXPR_CXX_UNARY_TYPE_TRAIT;
1395}
1396
1397void ASTStmtWriter::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) {
1398  VisitExpr(E);
1399  Record.push_back(E->getTrait());
1400  Record.push_back(E->getValue());
1401  Writer.AddSourceRange(E->getSourceRange(), Record);
1402  Writer.AddTypeSourceInfo(E->getLhsTypeSourceInfo(), Record);
1403  Writer.AddTypeSourceInfo(E->getRhsTypeSourceInfo(), Record);
1404  Code = serialization::EXPR_BINARY_TYPE_TRAIT;
1405}
1406
1407void ASTStmtWriter::VisitTypeTraitExpr(TypeTraitExpr *E) {
1408  VisitExpr(E);
1409  Record.push_back(E->TypeTraitExprBits.NumArgs);
1410  Record.push_back(E->TypeTraitExprBits.Kind); // FIXME: Stable encoding
1411  Record.push_back(E->TypeTraitExprBits.Value);
1412  for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
1413    Writer.AddTypeSourceInfo(E->getArg(I), Record);
1414  Code = serialization::EXPR_TYPE_TRAIT;
1415}
1416
1417void ASTStmtWriter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
1418  VisitExpr(E);
1419  Record.push_back(E->getTrait());
1420  Record.push_back(E->getValue());
1421  Writer.AddSourceRange(E->getSourceRange(), Record);
1422  Writer.AddTypeSourceInfo(E->getQueriedTypeSourceInfo(), Record);
1423  Code = serialization::EXPR_ARRAY_TYPE_TRAIT;
1424}
1425
1426void ASTStmtWriter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
1427  VisitExpr(E);
1428  Record.push_back(E->getTrait());
1429  Record.push_back(E->getValue());
1430  Writer.AddSourceRange(E->getSourceRange(), Record);
1431  Writer.AddStmt(E->getQueriedExpression());
1432  Code = serialization::EXPR_CXX_EXPRESSION_TRAIT;
1433}
1434
1435void ASTStmtWriter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1436  VisitExpr(E);
1437  Record.push_back(E->getValue());
1438  Writer.AddSourceRange(E->getSourceRange(), Record);
1439  Writer.AddStmt(E->getOperand());
1440  Code = serialization::EXPR_CXX_NOEXCEPT;
1441}
1442
1443void ASTStmtWriter::VisitPackExpansionExpr(PackExpansionExpr *E) {
1444  VisitExpr(E);
1445  Writer.AddSourceLocation(E->getEllipsisLoc(), Record);
1446  Record.push_back(E->NumExpansions);
1447  Writer.AddStmt(E->getPattern());
1448  Code = serialization::EXPR_PACK_EXPANSION;
1449}
1450
1451void ASTStmtWriter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
1452  VisitExpr(E);
1453  Writer.AddSourceLocation(E->OperatorLoc, Record);
1454  Writer.AddSourceLocation(E->PackLoc, Record);
1455  Writer.AddSourceLocation(E->RParenLoc, Record);
1456  Record.push_back(E->Length);
1457  Writer.AddDeclRef(E->Pack, Record);
1458  Code = serialization::EXPR_SIZEOF_PACK;
1459}
1460
1461void ASTStmtWriter::VisitSubstNonTypeTemplateParmExpr(
1462                                              SubstNonTypeTemplateParmExpr *E) {
1463  VisitExpr(E);
1464  Writer.AddDeclRef(E->getParameter(), Record);
1465  Writer.AddSourceLocation(E->getNameLoc(), Record);
1466  Writer.AddStmt(E->getReplacement());
1467  Code = serialization::EXPR_SUBST_NON_TYPE_TEMPLATE_PARM;
1468}
1469
1470void ASTStmtWriter::VisitSubstNonTypeTemplateParmPackExpr(
1471                                          SubstNonTypeTemplateParmPackExpr *E) {
1472  VisitExpr(E);
1473  Writer.AddDeclRef(E->getParameterPack(), Record);
1474  Writer.AddTemplateArgument(E->getArgumentPack(), Record);
1475  Writer.AddSourceLocation(E->getParameterPackLocation(), Record);
1476  Code = serialization::EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK;
1477}
1478
1479void ASTStmtWriter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
1480  VisitExpr(E);
1481  Writer.AddStmt(E->Temporary);
1482  Code = serialization::EXPR_MATERIALIZE_TEMPORARY;
1483}
1484
1485void ASTStmtWriter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
1486  VisitExpr(E);
1487  Writer.AddStmt(E->getSourceExpr());
1488  Writer.AddSourceLocation(E->getLocation(), Record);
1489  Code = serialization::EXPR_OPAQUE_VALUE;
1490}
1491
1492//===----------------------------------------------------------------------===//
1493// CUDA Expressions and Statements.
1494//===----------------------------------------------------------------------===//
1495
1496void ASTStmtWriter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *E) {
1497  VisitCallExpr(E);
1498  Writer.AddStmt(E->getConfig());
1499  Code = serialization::EXPR_CUDA_KERNEL_CALL;
1500}
1501
1502//===----------------------------------------------------------------------===//
1503// OpenCL Expressions and Statements.
1504//===----------------------------------------------------------------------===//
1505void ASTStmtWriter::VisitAsTypeExpr(AsTypeExpr *E) {
1506  VisitExpr(E);
1507  Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1508  Writer.AddSourceLocation(E->getRParenLoc(), Record);
1509  Writer.AddStmt(E->getSrcExpr());
1510  Code = serialization::EXPR_ASTYPE;
1511}
1512
1513//===----------------------------------------------------------------------===//
1514// Microsoft Expressions and Statements.
1515//===----------------------------------------------------------------------===//
1516void ASTStmtWriter::VisitCXXUuidofExpr(CXXUuidofExpr *E) {
1517  VisitExpr(E);
1518  Writer.AddSourceRange(E->getSourceRange(), Record);
1519  if (E->isTypeOperand()) {
1520    Writer.AddTypeSourceInfo(E->getTypeOperandSourceInfo(), Record);
1521    Code = serialization::EXPR_CXX_UUIDOF_TYPE;
1522  } else {
1523    Writer.AddStmt(E->getExprOperand());
1524    Code = serialization::EXPR_CXX_UUIDOF_EXPR;
1525  }
1526}
1527
1528void ASTStmtWriter::VisitSEHExceptStmt(SEHExceptStmt *S) {
1529  VisitStmt(S);
1530  Writer.AddSourceLocation(S->getExceptLoc(), Record);
1531  Writer.AddStmt(S->getFilterExpr());
1532  Writer.AddStmt(S->getBlock());
1533  Code = serialization::STMT_SEH_EXCEPT;
1534}
1535
1536void ASTStmtWriter::VisitSEHFinallyStmt(SEHFinallyStmt *S) {
1537  VisitStmt(S);
1538  Writer.AddSourceLocation(S->getFinallyLoc(), Record);
1539  Writer.AddStmt(S->getBlock());
1540  Code = serialization::STMT_SEH_FINALLY;
1541}
1542
1543void ASTStmtWriter::VisitSEHTryStmt(SEHTryStmt *S) {
1544  VisitStmt(S);
1545  Record.push_back(S->getIsCXXTry());
1546  Writer.AddSourceLocation(S->getTryLoc(), Record);
1547  Writer.AddStmt(S->getTryBlock());
1548  Writer.AddStmt(S->getHandler());
1549  Code = serialization::STMT_SEH_TRY;
1550}
1551
1552//===----------------------------------------------------------------------===//
1553// ASTWriter Implementation
1554//===----------------------------------------------------------------------===//
1555
1556unsigned ASTWriter::RecordSwitchCaseID(SwitchCase *S) {
1557  assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
1558         "SwitchCase recorded twice");
1559  unsigned NextID = SwitchCaseIDs.size();
1560  SwitchCaseIDs[S] = NextID;
1561  return NextID;
1562}
1563
1564unsigned ASTWriter::getSwitchCaseID(SwitchCase *S) {
1565  assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
1566         "SwitchCase hasn't been seen yet");
1567  return SwitchCaseIDs[S];
1568}
1569
1570void ASTWriter::ClearSwitchCaseIDs() {
1571  SwitchCaseIDs.clear();
1572}
1573
1574/// \brief Write the given substatement or subexpression to the
1575/// bitstream.
1576void ASTWriter::WriteSubStmt(Stmt *S,
1577                             llvm::DenseMap<Stmt *, uint64_t> &SubStmtEntries,
1578                             llvm::DenseSet<Stmt *> &ParentStmts) {
1579  RecordData Record;
1580  ASTStmtWriter Writer(*this, Record);
1581  ++NumStatements;
1582
1583  if (!S) {
1584    Stream.EmitRecord(serialization::STMT_NULL_PTR, Record);
1585    return;
1586  }
1587
1588  llvm::DenseMap<Stmt *, uint64_t>::iterator I = SubStmtEntries.find(S);
1589  if (I != SubStmtEntries.end()) {
1590    Record.push_back(I->second);
1591    Stream.EmitRecord(serialization::STMT_REF_PTR, Record);
1592    return;
1593  }
1594
1595#ifndef NDEBUG
1596  assert(!ParentStmts.count(S) && "There is a Stmt cycle!");
1597
1598  struct ParentStmtInserterRAII {
1599    Stmt *S;
1600    llvm::DenseSet<Stmt *> &ParentStmts;
1601
1602    ParentStmtInserterRAII(Stmt *S, llvm::DenseSet<Stmt *> &ParentStmts)
1603      : S(S), ParentStmts(ParentStmts) {
1604      ParentStmts.insert(S);
1605    }
1606    ~ParentStmtInserterRAII() {
1607      ParentStmts.erase(S);
1608    }
1609  };
1610
1611  ParentStmtInserterRAII ParentStmtInserter(S, ParentStmts);
1612#endif
1613
1614  // Redirect ASTWriter::AddStmt to collect sub stmts.
1615  SmallVector<Stmt *, 16> SubStmts;
1616  CollectedStmts = &SubStmts;
1617
1618  Writer.Code = serialization::STMT_NULL_PTR;
1619  Writer.AbbrevToUse = 0;
1620  Writer.Visit(S);
1621
1622#ifndef NDEBUG
1623  if (Writer.Code == serialization::STMT_NULL_PTR) {
1624    SourceManager &SrcMgr
1625      = DeclIDs.begin()->first->getASTContext().getSourceManager();
1626    S->dump(SrcMgr);
1627    llvm_unreachable("Unhandled sub statement writing AST file");
1628  }
1629#endif
1630
1631  // Revert ASTWriter::AddStmt.
1632  CollectedStmts = &StmtsToEmit;
1633
1634  // Write the sub stmts in reverse order, last to first. When reading them back
1635  // we will read them in correct order by "pop"ing them from the Stmts stack.
1636  // This simplifies reading and allows to store a variable number of sub stmts
1637  // without knowing it in advance.
1638  while (!SubStmts.empty())
1639    WriteSubStmt(SubStmts.pop_back_val(), SubStmtEntries, ParentStmts);
1640
1641  Stream.EmitRecord(Writer.Code, Record, Writer.AbbrevToUse);
1642
1643  SubStmtEntries[S] = Stream.GetCurrentBitNo();
1644}
1645
1646/// \brief Flush all of the statements that have been added to the
1647/// queue via AddStmt().
1648void ASTWriter::FlushStmts() {
1649  RecordData Record;
1650
1651  // We expect to be the only consumer of the two temporary statement maps,
1652  // assert that they are empty.
1653  assert(SubStmtEntries.empty() && "unexpected entries in sub stmt map");
1654  assert(ParentStmts.empty() && "unexpected entries in parent stmt map");
1655
1656  for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
1657    WriteSubStmt(StmtsToEmit[I], SubStmtEntries, ParentStmts);
1658
1659    assert(N == StmtsToEmit.size() &&
1660           "Substatement written via AddStmt rather than WriteSubStmt!");
1661
1662    // Note that we are at the end of a full expression. Any
1663    // expression records that follow this one are part of a different
1664    // expression.
1665    Stream.EmitRecord(serialization::STMT_STOP, Record);
1666
1667    SubStmtEntries.clear();
1668    ParentStmts.clear();
1669  }
1670
1671  StmtsToEmit.clear();
1672}
1673