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