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 /// \file
11 /// \brief Implements serialization for Statements and Expressions.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #include "clang/Serialization/ASTWriter.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/AST/DeclObjC.h"
19 #include "clang/AST/DeclTemplate.h"
20 #include "clang/AST/StmtVisitor.h"
21 #include "clang/Lex/Token.h"
22 #include "llvm/Bitcode/BitstreamWriter.h"
23 using namespace clang;
24 
25 //===----------------------------------------------------------------------===//
26 // Statement/expression serialization
27 //===----------------------------------------------------------------------===//
28 
29 namespace clang {
30 
31   class ASTStmtWriter : public StmtVisitor<ASTStmtWriter, void> {
32     ASTWriter &Writer;
33     ASTRecordWriter Record;
34 
35     serialization::StmtCode Code;
36     unsigned AbbrevToUse;
37 
38   public:
39     ASTStmtWriter(ASTWriter &Writer, ASTWriter::RecordData &Record)
40         : Writer(Writer), Record(Writer, Record),
41           Code(serialization::STMT_NULL_PTR), AbbrevToUse(0) {}
42 
43     ASTStmtWriter(const ASTStmtWriter&) = delete;
44 
45     uint64_t Emit() {
46       assert(Code != serialization::STMT_NULL_PTR &&
47              "unhandled sub-statement writing AST file");
48       return Record.EmitStmt(Code, AbbrevToUse);
49     }
50 
51     void AddTemplateKWAndArgsInfo(const ASTTemplateKWAndArgsInfo &ArgInfo,
52                                   const TemplateArgumentLoc *Args);
53 
54     void VisitStmt(Stmt *S);
55 #define STMT(Type, Base) \
56     void Visit##Type(Type *);
57 #include "clang/AST/StmtNodes.inc"
58   };
59 }
60 
61 void ASTStmtWriter::AddTemplateKWAndArgsInfo(
62     const ASTTemplateKWAndArgsInfo &ArgInfo, const TemplateArgumentLoc *Args) {
63   Record.AddSourceLocation(ArgInfo.TemplateKWLoc);
64   Record.AddSourceLocation(ArgInfo.LAngleLoc);
65   Record.AddSourceLocation(ArgInfo.RAngleLoc);
66   for (unsigned i = 0; i != ArgInfo.NumTemplateArgs; ++i)
67     Record.AddTemplateArgumentLoc(Args[i]);
68 }
69 
70 void ASTStmtWriter::VisitStmt(Stmt *S) {
71 }
72 
73 void ASTStmtWriter::VisitNullStmt(NullStmt *S) {
74   VisitStmt(S);
75   Record.AddSourceLocation(S->getSemiLoc());
76   Record.push_back(S->HasLeadingEmptyMacro);
77   Code = serialization::STMT_NULL;
78 }
79 
80 void ASTStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
81   VisitStmt(S);
82   Record.push_back(S->size());
83   for (auto *CS : S->body())
84     Record.AddStmt(CS);
85   Record.AddSourceLocation(S->getLBracLoc());
86   Record.AddSourceLocation(S->getRBracLoc());
87   Code = serialization::STMT_COMPOUND;
88 }
89 
90 void ASTStmtWriter::VisitSwitchCase(SwitchCase *S) {
91   VisitStmt(S);
92   Record.push_back(Writer.getSwitchCaseID(S));
93   Record.AddSourceLocation(S->getKeywordLoc());
94   Record.AddSourceLocation(S->getColonLoc());
95 }
96 
97 void ASTStmtWriter::VisitCaseStmt(CaseStmt *S) {
98   VisitSwitchCase(S);
99   Record.AddStmt(S->getLHS());
100   Record.AddStmt(S->getRHS());
101   Record.AddStmt(S->getSubStmt());
102   Record.AddSourceLocation(S->getEllipsisLoc());
103   Code = serialization::STMT_CASE;
104 }
105 
106 void ASTStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
107   VisitSwitchCase(S);
108   Record.AddStmt(S->getSubStmt());
109   Code = serialization::STMT_DEFAULT;
110 }
111 
112 void ASTStmtWriter::VisitLabelStmt(LabelStmt *S) {
113   VisitStmt(S);
114   Record.AddDeclRef(S->getDecl());
115   Record.AddStmt(S->getSubStmt());
116   Record.AddSourceLocation(S->getIdentLoc());
117   Code = serialization::STMT_LABEL;
118 }
119 
120 void ASTStmtWriter::VisitAttributedStmt(AttributedStmt *S) {
121   VisitStmt(S);
122   Record.push_back(S->getAttrs().size());
123   Record.AddAttributes(S->getAttrs());
124   Record.AddStmt(S->getSubStmt());
125   Record.AddSourceLocation(S->getAttrLoc());
126   Code = serialization::STMT_ATTRIBUTED;
127 }
128 
129 void ASTStmtWriter::VisitIfStmt(IfStmt *S) {
130   VisitStmt(S);
131   Record.push_back(S->isConstexpr());
132   Record.AddStmt(S->getInit());
133   Record.AddDeclRef(S->getConditionVariable());
134   Record.AddStmt(S->getCond());
135   Record.AddStmt(S->getThen());
136   Record.AddStmt(S->getElse());
137   Record.AddSourceLocation(S->getIfLoc());
138   Record.AddSourceLocation(S->getElseLoc());
139   Code = serialization::STMT_IF;
140 }
141 
142 void ASTStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
143   VisitStmt(S);
144   Record.AddStmt(S->getInit());
145   Record.AddDeclRef(S->getConditionVariable());
146   Record.AddStmt(S->getCond());
147   Record.AddStmt(S->getBody());
148   Record.AddSourceLocation(S->getSwitchLoc());
149   Record.push_back(S->isAllEnumCasesCovered());
150   for (SwitchCase *SC = S->getSwitchCaseList(); SC;
151        SC = SC->getNextSwitchCase())
152     Record.push_back(Writer.RecordSwitchCaseID(SC));
153   Code = serialization::STMT_SWITCH;
154 }
155 
156 void ASTStmtWriter::VisitWhileStmt(WhileStmt *S) {
157   VisitStmt(S);
158   Record.AddDeclRef(S->getConditionVariable());
159   Record.AddStmt(S->getCond());
160   Record.AddStmt(S->getBody());
161   Record.AddSourceLocation(S->getWhileLoc());
162   Code = serialization::STMT_WHILE;
163 }
164 
165 void ASTStmtWriter::VisitDoStmt(DoStmt *S) {
166   VisitStmt(S);
167   Record.AddStmt(S->getCond());
168   Record.AddStmt(S->getBody());
169   Record.AddSourceLocation(S->getDoLoc());
170   Record.AddSourceLocation(S->getWhileLoc());
171   Record.AddSourceLocation(S->getRParenLoc());
172   Code = serialization::STMT_DO;
173 }
174 
175 void ASTStmtWriter::VisitForStmt(ForStmt *S) {
176   VisitStmt(S);
177   Record.AddStmt(S->getInit());
178   Record.AddStmt(S->getCond());
179   Record.AddDeclRef(S->getConditionVariable());
180   Record.AddStmt(S->getInc());
181   Record.AddStmt(S->getBody());
182   Record.AddSourceLocation(S->getForLoc());
183   Record.AddSourceLocation(S->getLParenLoc());
184   Record.AddSourceLocation(S->getRParenLoc());
185   Code = serialization::STMT_FOR;
186 }
187 
188 void ASTStmtWriter::VisitGotoStmt(GotoStmt *S) {
189   VisitStmt(S);
190   Record.AddDeclRef(S->getLabel());
191   Record.AddSourceLocation(S->getGotoLoc());
192   Record.AddSourceLocation(S->getLabelLoc());
193   Code = serialization::STMT_GOTO;
194 }
195 
196 void ASTStmtWriter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
197   VisitStmt(S);
198   Record.AddSourceLocation(S->getGotoLoc());
199   Record.AddSourceLocation(S->getStarLoc());
200   Record.AddStmt(S->getTarget());
201   Code = serialization::STMT_INDIRECT_GOTO;
202 }
203 
204 void ASTStmtWriter::VisitContinueStmt(ContinueStmt *S) {
205   VisitStmt(S);
206   Record.AddSourceLocation(S->getContinueLoc());
207   Code = serialization::STMT_CONTINUE;
208 }
209 
210 void ASTStmtWriter::VisitBreakStmt(BreakStmt *S) {
211   VisitStmt(S);
212   Record.AddSourceLocation(S->getBreakLoc());
213   Code = serialization::STMT_BREAK;
214 }
215 
216 void ASTStmtWriter::VisitReturnStmt(ReturnStmt *S) {
217   VisitStmt(S);
218   Record.AddStmt(S->getRetValue());
219   Record.AddSourceLocation(S->getReturnLoc());
220   Record.AddDeclRef(S->getNRVOCandidate());
221   Code = serialization::STMT_RETURN;
222 }
223 
224 void ASTStmtWriter::VisitDeclStmt(DeclStmt *S) {
225   VisitStmt(S);
226   Record.AddSourceLocation(S->getStartLoc());
227   Record.AddSourceLocation(S->getEndLoc());
228   DeclGroupRef DG = S->getDeclGroup();
229   for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D)
230     Record.AddDeclRef(*D);
231   Code = serialization::STMT_DECL;
232 }
233 
234 void ASTStmtWriter::VisitAsmStmt(AsmStmt *S) {
235   VisitStmt(S);
236   Record.push_back(S->getNumOutputs());
237   Record.push_back(S->getNumInputs());
238   Record.push_back(S->getNumClobbers());
239   Record.AddSourceLocation(S->getAsmLoc());
240   Record.push_back(S->isVolatile());
241   Record.push_back(S->isSimple());
242 }
243 
244 void ASTStmtWriter::VisitGCCAsmStmt(GCCAsmStmt *S) {
245   VisitAsmStmt(S);
246   Record.AddSourceLocation(S->getRParenLoc());
247   Record.AddStmt(S->getAsmString());
248 
249   // Outputs
250   for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
251     Record.AddIdentifierRef(S->getOutputIdentifier(I));
252     Record.AddStmt(S->getOutputConstraintLiteral(I));
253     Record.AddStmt(S->getOutputExpr(I));
254   }
255 
256   // Inputs
257   for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
258     Record.AddIdentifierRef(S->getInputIdentifier(I));
259     Record.AddStmt(S->getInputConstraintLiteral(I));
260     Record.AddStmt(S->getInputExpr(I));
261   }
262 
263   // Clobbers
264   for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
265     Record.AddStmt(S->getClobberStringLiteral(I));
266 
267   Code = serialization::STMT_GCCASM;
268 }
269 
270 void ASTStmtWriter::VisitMSAsmStmt(MSAsmStmt *S) {
271   VisitAsmStmt(S);
272   Record.AddSourceLocation(S->getLBraceLoc());
273   Record.AddSourceLocation(S->getEndLoc());
274   Record.push_back(S->getNumAsmToks());
275   Record.AddString(S->getAsmString());
276 
277   // Tokens
278   for (unsigned I = 0, N = S->getNumAsmToks(); I != N; ++I) {
279     // FIXME: Move this to ASTRecordWriter?
280     Writer.AddToken(S->getAsmToks()[I], Record.getRecordData());
281   }
282 
283   // Clobbers
284   for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I) {
285     Record.AddString(S->getClobber(I));
286   }
287 
288   // Outputs
289   for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
290     Record.AddStmt(S->getOutputExpr(I));
291     Record.AddString(S->getOutputConstraint(I));
292   }
293 
294   // Inputs
295   for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
296     Record.AddStmt(S->getInputExpr(I));
297     Record.AddString(S->getInputConstraint(I));
298   }
299 
300   Code = serialization::STMT_MSASM;
301 }
302 
303 void ASTStmtWriter::VisitCoroutineBodyStmt(CoroutineBodyStmt *S) {
304   // FIXME: Implement coroutine serialization.
305   llvm_unreachable("unimplemented");
306 }
307 
308 void ASTStmtWriter::VisitCoreturnStmt(CoreturnStmt *S) {
309   // FIXME: Implement coroutine serialization.
310   llvm_unreachable("unimplemented");
311 }
312 
313 void ASTStmtWriter::VisitCoawaitExpr(CoawaitExpr *S) {
314   // FIXME: Implement coroutine serialization.
315   llvm_unreachable("unimplemented");
316 }
317 
318 void ASTStmtWriter::VisitCoyieldExpr(CoyieldExpr *S) {
319   // FIXME: Implement coroutine serialization.
320   llvm_unreachable("unimplemented");
321 }
322 
323 void ASTStmtWriter::VisitCapturedStmt(CapturedStmt *S) {
324   VisitStmt(S);
325   // NumCaptures
326   Record.push_back(std::distance(S->capture_begin(), S->capture_end()));
327 
328   // CapturedDecl and captured region kind
329   Record.AddDeclRef(S->getCapturedDecl());
330   Record.push_back(S->getCapturedRegionKind());
331 
332   Record.AddDeclRef(S->getCapturedRecordDecl());
333 
334   // Capture inits
335   for (auto *I : S->capture_inits())
336     Record.AddStmt(I);
337 
338   // Body
339   Record.AddStmt(S->getCapturedStmt());
340 
341   // Captures
342   for (const auto &I : S->captures()) {
343     if (I.capturesThis() || I.capturesVariableArrayType())
344       Record.AddDeclRef(nullptr);
345     else
346       Record.AddDeclRef(I.getCapturedVar());
347     Record.push_back(I.getCaptureKind());
348     Record.AddSourceLocation(I.getLocation());
349   }
350 
351   Code = serialization::STMT_CAPTURED;
352 }
353 
354 void ASTStmtWriter::VisitExpr(Expr *E) {
355   VisitStmt(E);
356   Record.AddTypeRef(E->getType());
357   Record.push_back(E->isTypeDependent());
358   Record.push_back(E->isValueDependent());
359   Record.push_back(E->isInstantiationDependent());
360   Record.push_back(E->containsUnexpandedParameterPack());
361   Record.push_back(E->getValueKind());
362   Record.push_back(E->getObjectKind());
363 }
364 
365 void ASTStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
366   VisitExpr(E);
367   Record.AddSourceLocation(E->getLocation());
368   Record.push_back(E->getIdentType()); // FIXME: stable encoding
369   Record.AddStmt(E->getFunctionName());
370   Code = serialization::EXPR_PREDEFINED;
371 }
372 
373 void ASTStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
374   VisitExpr(E);
375 
376   Record.push_back(E->hasQualifier());
377   Record.push_back(E->getDecl() != E->getFoundDecl());
378   Record.push_back(E->hasTemplateKWAndArgsInfo());
379   Record.push_back(E->hadMultipleCandidates());
380   Record.push_back(E->refersToEnclosingVariableOrCapture());
381 
382   if (E->hasTemplateKWAndArgsInfo()) {
383     unsigned NumTemplateArgs = E->getNumTemplateArgs();
384     Record.push_back(NumTemplateArgs);
385   }
386 
387   DeclarationName::NameKind nk = (E->getDecl()->getDeclName().getNameKind());
388 
389   if ((!E->hasTemplateKWAndArgsInfo()) && (!E->hasQualifier()) &&
390       (E->getDecl() == E->getFoundDecl()) &&
391       nk == DeclarationName::Identifier) {
392     AbbrevToUse = Writer.getDeclRefExprAbbrev();
393   }
394 
395   if (E->hasQualifier())
396     Record.AddNestedNameSpecifierLoc(E->getQualifierLoc());
397 
398   if (E->getDecl() != E->getFoundDecl())
399     Record.AddDeclRef(E->getFoundDecl());
400 
401   if (E->hasTemplateKWAndArgsInfo())
402     AddTemplateKWAndArgsInfo(*E->getTrailingObjects<ASTTemplateKWAndArgsInfo>(),
403                              E->getTrailingObjects<TemplateArgumentLoc>());
404 
405   Record.AddDeclRef(E->getDecl());
406   Record.AddSourceLocation(E->getLocation());
407   Record.AddDeclarationNameLoc(E->DNLoc, E->getDecl()->getDeclName());
408   Code = serialization::EXPR_DECL_REF;
409 }
410 
411 void ASTStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
412   VisitExpr(E);
413   Record.AddSourceLocation(E->getLocation());
414   Record.AddAPInt(E->getValue());
415 
416   if (E->getValue().getBitWidth() == 32) {
417     AbbrevToUse = Writer.getIntegerLiteralAbbrev();
418   }
419 
420   Code = serialization::EXPR_INTEGER_LITERAL;
421 }
422 
423 void ASTStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
424   VisitExpr(E);
425   Record.push_back(E->getRawSemantics());
426   Record.push_back(E->isExact());
427   Record.AddAPFloat(E->getValue());
428   Record.AddSourceLocation(E->getLocation());
429   Code = serialization::EXPR_FLOATING_LITERAL;
430 }
431 
432 void ASTStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
433   VisitExpr(E);
434   Record.AddStmt(E->getSubExpr());
435   Code = serialization::EXPR_IMAGINARY_LITERAL;
436 }
437 
438 void ASTStmtWriter::VisitStringLiteral(StringLiteral *E) {
439   VisitExpr(E);
440   Record.push_back(E->getByteLength());
441   Record.push_back(E->getNumConcatenated());
442   Record.push_back(E->getKind());
443   Record.push_back(E->isPascal());
444   // FIXME: String data should be stored as a blob at the end of the
445   // StringLiteral. However, we can't do so now because we have no
446   // provision for coping with abbreviations when we're jumping around
447   // the AST file during deserialization.
448   Record.append(E->getBytes().begin(), E->getBytes().end());
449   for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
450     Record.AddSourceLocation(E->getStrTokenLoc(I));
451   Code = serialization::EXPR_STRING_LITERAL;
452 }
453 
454 void ASTStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
455   VisitExpr(E);
456   Record.push_back(E->getValue());
457   Record.AddSourceLocation(E->getLocation());
458   Record.push_back(E->getKind());
459 
460   AbbrevToUse = Writer.getCharacterLiteralAbbrev();
461 
462   Code = serialization::EXPR_CHARACTER_LITERAL;
463 }
464 
465 void ASTStmtWriter::VisitParenExpr(ParenExpr *E) {
466   VisitExpr(E);
467   Record.AddSourceLocation(E->getLParen());
468   Record.AddSourceLocation(E->getRParen());
469   Record.AddStmt(E->getSubExpr());
470   Code = serialization::EXPR_PAREN;
471 }
472 
473 void ASTStmtWriter::VisitParenListExpr(ParenListExpr *E) {
474   VisitExpr(E);
475   Record.push_back(E->NumExprs);
476   for (unsigned i=0; i != E->NumExprs; ++i)
477     Record.AddStmt(E->Exprs[i]);
478   Record.AddSourceLocation(E->LParenLoc);
479   Record.AddSourceLocation(E->RParenLoc);
480   Code = serialization::EXPR_PAREN_LIST;
481 }
482 
483 void ASTStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
484   VisitExpr(E);
485   Record.AddStmt(E->getSubExpr());
486   Record.push_back(E->getOpcode()); // FIXME: stable encoding
487   Record.AddSourceLocation(E->getOperatorLoc());
488   Code = serialization::EXPR_UNARY_OPERATOR;
489 }
490 
491 void ASTStmtWriter::VisitOffsetOfExpr(OffsetOfExpr *E) {
492   VisitExpr(E);
493   Record.push_back(E->getNumComponents());
494   Record.push_back(E->getNumExpressions());
495   Record.AddSourceLocation(E->getOperatorLoc());
496   Record.AddSourceLocation(E->getRParenLoc());
497   Record.AddTypeSourceInfo(E->getTypeSourceInfo());
498   for (unsigned I = 0, N = E->getNumComponents(); I != N; ++I) {
499     const OffsetOfNode &ON = E->getComponent(I);
500     Record.push_back(ON.getKind()); // FIXME: Stable encoding
501     Record.AddSourceLocation(ON.getSourceRange().getBegin());
502     Record.AddSourceLocation(ON.getSourceRange().getEnd());
503     switch (ON.getKind()) {
504     case OffsetOfNode::Array:
505       Record.push_back(ON.getArrayExprIndex());
506       break;
507 
508     case OffsetOfNode::Field:
509       Record.AddDeclRef(ON.getField());
510       break;
511 
512     case OffsetOfNode::Identifier:
513       Record.AddIdentifierRef(ON.getFieldName());
514       break;
515 
516     case OffsetOfNode::Base:
517       Record.AddCXXBaseSpecifier(*ON.getBase());
518       break;
519     }
520   }
521   for (unsigned I = 0, N = E->getNumExpressions(); I != N; ++I)
522     Record.AddStmt(E->getIndexExpr(I));
523   Code = serialization::EXPR_OFFSETOF;
524 }
525 
526 void ASTStmtWriter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
527   VisitExpr(E);
528   Record.push_back(E->getKind());
529   if (E->isArgumentType())
530     Record.AddTypeSourceInfo(E->getArgumentTypeInfo());
531   else {
532     Record.push_back(0);
533     Record.AddStmt(E->getArgumentExpr());
534   }
535   Record.AddSourceLocation(E->getOperatorLoc());
536   Record.AddSourceLocation(E->getRParenLoc());
537   Code = serialization::EXPR_SIZEOF_ALIGN_OF;
538 }
539 
540 void ASTStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
541   VisitExpr(E);
542   Record.AddStmt(E->getLHS());
543   Record.AddStmt(E->getRHS());
544   Record.AddSourceLocation(E->getRBracketLoc());
545   Code = serialization::EXPR_ARRAY_SUBSCRIPT;
546 }
547 
548 void ASTStmtWriter::VisitOMPArraySectionExpr(OMPArraySectionExpr *E) {
549   VisitExpr(E);
550   Record.AddStmt(E->getBase());
551   Record.AddStmt(E->getLowerBound());
552   Record.AddStmt(E->getLength());
553   Record.AddSourceLocation(E->getColonLoc());
554   Record.AddSourceLocation(E->getRBracketLoc());
555   Code = serialization::EXPR_OMP_ARRAY_SECTION;
556 }
557 
558 void ASTStmtWriter::VisitCallExpr(CallExpr *E) {
559   VisitExpr(E);
560   Record.push_back(E->getNumArgs());
561   Record.AddSourceLocation(E->getRParenLoc());
562   Record.AddStmt(E->getCallee());
563   for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
564        Arg != ArgEnd; ++Arg)
565     Record.AddStmt(*Arg);
566   Code = serialization::EXPR_CALL;
567 }
568 
569 void ASTStmtWriter::VisitMemberExpr(MemberExpr *E) {
570   // Don't call VisitExpr, we'll write everything here.
571 
572   Record.push_back(E->hasQualifier());
573   if (E->hasQualifier())
574     Record.AddNestedNameSpecifierLoc(E->getQualifierLoc());
575 
576   Record.push_back(E->HasTemplateKWAndArgsInfo);
577   if (E->HasTemplateKWAndArgsInfo) {
578     Record.AddSourceLocation(E->getTemplateKeywordLoc());
579     unsigned NumTemplateArgs = E->getNumTemplateArgs();
580     Record.push_back(NumTemplateArgs);
581     Record.AddSourceLocation(E->getLAngleLoc());
582     Record.AddSourceLocation(E->getRAngleLoc());
583     for (unsigned i=0; i != NumTemplateArgs; ++i)
584       Record.AddTemplateArgumentLoc(E->getTemplateArgs()[i]);
585   }
586 
587   Record.push_back(E->hadMultipleCandidates());
588 
589   DeclAccessPair FoundDecl = E->getFoundDecl();
590   Record.AddDeclRef(FoundDecl.getDecl());
591   Record.push_back(FoundDecl.getAccess());
592 
593   Record.AddTypeRef(E->getType());
594   Record.push_back(E->getValueKind());
595   Record.push_back(E->getObjectKind());
596   Record.AddStmt(E->getBase());
597   Record.AddDeclRef(E->getMemberDecl());
598   Record.AddSourceLocation(E->getMemberLoc());
599   Record.push_back(E->isArrow());
600   Record.AddSourceLocation(E->getOperatorLoc());
601   Record.AddDeclarationNameLoc(E->MemberDNLoc,
602                                E->getMemberDecl()->getDeclName());
603   Code = serialization::EXPR_MEMBER;
604 }
605 
606 void ASTStmtWriter::VisitObjCIsaExpr(ObjCIsaExpr *E) {
607   VisitExpr(E);
608   Record.AddStmt(E->getBase());
609   Record.AddSourceLocation(E->getIsaMemberLoc());
610   Record.AddSourceLocation(E->getOpLoc());
611   Record.push_back(E->isArrow());
612   Code = serialization::EXPR_OBJC_ISA;
613 }
614 
615 void ASTStmtWriter::
616 VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
617   VisitExpr(E);
618   Record.AddStmt(E->getSubExpr());
619   Record.push_back(E->shouldCopy());
620   Code = serialization::EXPR_OBJC_INDIRECT_COPY_RESTORE;
621 }
622 
623 void ASTStmtWriter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
624   VisitExplicitCastExpr(E);
625   Record.AddSourceLocation(E->getLParenLoc());
626   Record.AddSourceLocation(E->getBridgeKeywordLoc());
627   Record.push_back(E->getBridgeKind()); // FIXME: Stable encoding
628   Code = serialization::EXPR_OBJC_BRIDGED_CAST;
629 }
630 
631 void ASTStmtWriter::VisitCastExpr(CastExpr *E) {
632   VisitExpr(E);
633   Record.push_back(E->path_size());
634   Record.AddStmt(E->getSubExpr());
635   Record.push_back(E->getCastKind()); // FIXME: stable encoding
636 
637   for (CastExpr::path_iterator
638          PI = E->path_begin(), PE = E->path_end(); PI != PE; ++PI)
639     Record.AddCXXBaseSpecifier(**PI);
640 }
641 
642 void ASTStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
643   VisitExpr(E);
644   Record.AddStmt(E->getLHS());
645   Record.AddStmt(E->getRHS());
646   Record.push_back(E->getOpcode()); // FIXME: stable encoding
647   Record.AddSourceLocation(E->getOperatorLoc());
648   Record.push_back(E->isFPContractable());
649   Code = serialization::EXPR_BINARY_OPERATOR;
650 }
651 
652 void ASTStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
653   VisitBinaryOperator(E);
654   Record.AddTypeRef(E->getComputationLHSType());
655   Record.AddTypeRef(E->getComputationResultType());
656   Code = serialization::EXPR_COMPOUND_ASSIGN_OPERATOR;
657 }
658 
659 void ASTStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
660   VisitExpr(E);
661   Record.AddStmt(E->getCond());
662   Record.AddStmt(E->getLHS());
663   Record.AddStmt(E->getRHS());
664   Record.AddSourceLocation(E->getQuestionLoc());
665   Record.AddSourceLocation(E->getColonLoc());
666   Code = serialization::EXPR_CONDITIONAL_OPERATOR;
667 }
668 
669 void
670 ASTStmtWriter::VisitBinaryConditionalOperator(BinaryConditionalOperator *E) {
671   VisitExpr(E);
672   Record.AddStmt(E->getOpaqueValue());
673   Record.AddStmt(E->getCommon());
674   Record.AddStmt(E->getCond());
675   Record.AddStmt(E->getTrueExpr());
676   Record.AddStmt(E->getFalseExpr());
677   Record.AddSourceLocation(E->getQuestionLoc());
678   Record.AddSourceLocation(E->getColonLoc());
679   Code = serialization::EXPR_BINARY_CONDITIONAL_OPERATOR;
680 }
681 
682 void ASTStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
683   VisitCastExpr(E);
684 
685   if (E->path_size() == 0)
686     AbbrevToUse = Writer.getExprImplicitCastAbbrev();
687 
688   Code = serialization::EXPR_IMPLICIT_CAST;
689 }
690 
691 void ASTStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
692   VisitCastExpr(E);
693   Record.AddTypeSourceInfo(E->getTypeInfoAsWritten());
694 }
695 
696 void ASTStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
697   VisitExplicitCastExpr(E);
698   Record.AddSourceLocation(E->getLParenLoc());
699   Record.AddSourceLocation(E->getRParenLoc());
700   Code = serialization::EXPR_CSTYLE_CAST;
701 }
702 
703 void ASTStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
704   VisitExpr(E);
705   Record.AddSourceLocation(E->getLParenLoc());
706   Record.AddTypeSourceInfo(E->getTypeSourceInfo());
707   Record.AddStmt(E->getInitializer());
708   Record.push_back(E->isFileScope());
709   Code = serialization::EXPR_COMPOUND_LITERAL;
710 }
711 
712 void ASTStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
713   VisitExpr(E);
714   Record.AddStmt(E->getBase());
715   Record.AddIdentifierRef(&E->getAccessor());
716   Record.AddSourceLocation(E->getAccessorLoc());
717   Code = serialization::EXPR_EXT_VECTOR_ELEMENT;
718 }
719 
720 void ASTStmtWriter::VisitInitListExpr(InitListExpr *E) {
721   VisitExpr(E);
722   // NOTE: only add the (possibly null) syntactic form.
723   // No need to serialize the isSemanticForm flag and the semantic form.
724   Record.AddStmt(E->getSyntacticForm());
725   Record.AddSourceLocation(E->getLBraceLoc());
726   Record.AddSourceLocation(E->getRBraceLoc());
727   bool isArrayFiller = E->ArrayFillerOrUnionFieldInit.is<Expr*>();
728   Record.push_back(isArrayFiller);
729   if (isArrayFiller)
730     Record.AddStmt(E->getArrayFiller());
731   else
732     Record.AddDeclRef(E->getInitializedFieldInUnion());
733   Record.push_back(E->hadArrayRangeDesignator());
734   Record.push_back(E->getNumInits());
735   if (isArrayFiller) {
736     // ArrayFiller may have filled "holes" due to designated initializer.
737     // Replace them by 0 to indicate that the filler goes in that place.
738     Expr *filler = E->getArrayFiller();
739     for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
740       Record.AddStmt(E->getInit(I) != filler ? E->getInit(I) : nullptr);
741   } else {
742     for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
743       Record.AddStmt(E->getInit(I));
744   }
745   Code = serialization::EXPR_INIT_LIST;
746 }
747 
748 void ASTStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
749   VisitExpr(E);
750   Record.push_back(E->getNumSubExprs());
751   for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
752     Record.AddStmt(E->getSubExpr(I));
753   Record.AddSourceLocation(E->getEqualOrColonLoc());
754   Record.push_back(E->usesGNUSyntax());
755   for (const DesignatedInitExpr::Designator &D : E->designators()) {
756     if (D.isFieldDesignator()) {
757       if (FieldDecl *Field = D.getField()) {
758         Record.push_back(serialization::DESIG_FIELD_DECL);
759         Record.AddDeclRef(Field);
760       } else {
761         Record.push_back(serialization::DESIG_FIELD_NAME);
762         Record.AddIdentifierRef(D.getFieldName());
763       }
764       Record.AddSourceLocation(D.getDotLoc());
765       Record.AddSourceLocation(D.getFieldLoc());
766     } else if (D.isArrayDesignator()) {
767       Record.push_back(serialization::DESIG_ARRAY);
768       Record.push_back(D.getFirstExprIndex());
769       Record.AddSourceLocation(D.getLBracketLoc());
770       Record.AddSourceLocation(D.getRBracketLoc());
771     } else {
772       assert(D.isArrayRangeDesignator() && "Unknown designator");
773       Record.push_back(serialization::DESIG_ARRAY_RANGE);
774       Record.push_back(D.getFirstExprIndex());
775       Record.AddSourceLocation(D.getLBracketLoc());
776       Record.AddSourceLocation(D.getEllipsisLoc());
777       Record.AddSourceLocation(D.getRBracketLoc());
778     }
779   }
780   Code = serialization::EXPR_DESIGNATED_INIT;
781 }
782 
783 void ASTStmtWriter::VisitDesignatedInitUpdateExpr(DesignatedInitUpdateExpr *E) {
784   VisitExpr(E);
785   Record.AddStmt(E->getBase());
786   Record.AddStmt(E->getUpdater());
787   Code = serialization::EXPR_DESIGNATED_INIT_UPDATE;
788 }
789 
790 void ASTStmtWriter::VisitNoInitExpr(NoInitExpr *E) {
791   VisitExpr(E);
792   Code = serialization::EXPR_NO_INIT;
793 }
794 
795 void ASTStmtWriter::VisitArrayInitLoopExpr(ArrayInitLoopExpr *E) {
796   VisitExpr(E);
797   Record.AddStmt(E->SubExprs[0]);
798   Record.AddStmt(E->SubExprs[1]);
799   Code = serialization::EXPR_ARRAY_INIT_LOOP;
800 }
801 
802 void ASTStmtWriter::VisitArrayInitIndexExpr(ArrayInitIndexExpr *E) {
803   VisitExpr(E);
804   Code = serialization::EXPR_ARRAY_INIT_INDEX;
805 }
806 
807 void ASTStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
808   VisitExpr(E);
809   Code = serialization::EXPR_IMPLICIT_VALUE_INIT;
810 }
811 
812 void ASTStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
813   VisitExpr(E);
814   Record.AddStmt(E->getSubExpr());
815   Record.AddTypeSourceInfo(E->getWrittenTypeInfo());
816   Record.AddSourceLocation(E->getBuiltinLoc());
817   Record.AddSourceLocation(E->getRParenLoc());
818   Record.push_back(E->isMicrosoftABI());
819   Code = serialization::EXPR_VA_ARG;
820 }
821 
822 void ASTStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) {
823   VisitExpr(E);
824   Record.AddSourceLocation(E->getAmpAmpLoc());
825   Record.AddSourceLocation(E->getLabelLoc());
826   Record.AddDeclRef(E->getLabel());
827   Code = serialization::EXPR_ADDR_LABEL;
828 }
829 
830 void ASTStmtWriter::VisitStmtExpr(StmtExpr *E) {
831   VisitExpr(E);
832   Record.AddStmt(E->getSubStmt());
833   Record.AddSourceLocation(E->getLParenLoc());
834   Record.AddSourceLocation(E->getRParenLoc());
835   Code = serialization::EXPR_STMT;
836 }
837 
838 void ASTStmtWriter::VisitChooseExpr(ChooseExpr *E) {
839   VisitExpr(E);
840   Record.AddStmt(E->getCond());
841   Record.AddStmt(E->getLHS());
842   Record.AddStmt(E->getRHS());
843   Record.AddSourceLocation(E->getBuiltinLoc());
844   Record.AddSourceLocation(E->getRParenLoc());
845   Record.push_back(E->isConditionDependent() ? false : E->isConditionTrue());
846   Code = serialization::EXPR_CHOOSE;
847 }
848 
849 void ASTStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
850   VisitExpr(E);
851   Record.AddSourceLocation(E->getTokenLocation());
852   Code = serialization::EXPR_GNU_NULL;
853 }
854 
855 void ASTStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
856   VisitExpr(E);
857   Record.push_back(E->getNumSubExprs());
858   for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
859     Record.AddStmt(E->getExpr(I));
860   Record.AddSourceLocation(E->getBuiltinLoc());
861   Record.AddSourceLocation(E->getRParenLoc());
862   Code = serialization::EXPR_SHUFFLE_VECTOR;
863 }
864 
865 void ASTStmtWriter::VisitConvertVectorExpr(ConvertVectorExpr *E) {
866   VisitExpr(E);
867   Record.AddSourceLocation(E->getBuiltinLoc());
868   Record.AddSourceLocation(E->getRParenLoc());
869   Record.AddTypeSourceInfo(E->getTypeSourceInfo());
870   Record.AddStmt(E->getSrcExpr());
871   Code = serialization::EXPR_CONVERT_VECTOR;
872 }
873 
874 void ASTStmtWriter::VisitBlockExpr(BlockExpr *E) {
875   VisitExpr(E);
876   Record.AddDeclRef(E->getBlockDecl());
877   Code = serialization::EXPR_BLOCK;
878 }
879 
880 void ASTStmtWriter::VisitGenericSelectionExpr(GenericSelectionExpr *E) {
881   VisitExpr(E);
882   Record.push_back(E->getNumAssocs());
883 
884   Record.AddStmt(E->getControllingExpr());
885   for (unsigned I = 0, N = E->getNumAssocs(); I != N; ++I) {
886     Record.AddTypeSourceInfo(E->getAssocTypeSourceInfo(I));
887     Record.AddStmt(E->getAssocExpr(I));
888   }
889   Record.push_back(E->isResultDependent() ? -1U : E->getResultIndex());
890 
891   Record.AddSourceLocation(E->getGenericLoc());
892   Record.AddSourceLocation(E->getDefaultLoc());
893   Record.AddSourceLocation(E->getRParenLoc());
894   Code = serialization::EXPR_GENERIC_SELECTION;
895 }
896 
897 void ASTStmtWriter::VisitPseudoObjectExpr(PseudoObjectExpr *E) {
898   VisitExpr(E);
899   Record.push_back(E->getNumSemanticExprs());
900 
901   // Push the result index.  Currently, this needs to exactly match
902   // the encoding used internally for ResultIndex.
903   unsigned result = E->getResultExprIndex();
904   result = (result == PseudoObjectExpr::NoResult ? 0 : result + 1);
905   Record.push_back(result);
906 
907   Record.AddStmt(E->getSyntacticForm());
908   for (PseudoObjectExpr::semantics_iterator
909          i = E->semantics_begin(), e = E->semantics_end(); i != e; ++i) {
910     Record.AddStmt(*i);
911   }
912   Code = serialization::EXPR_PSEUDO_OBJECT;
913 }
914 
915 void ASTStmtWriter::VisitAtomicExpr(AtomicExpr *E) {
916   VisitExpr(E);
917   Record.push_back(E->getOp());
918   for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
919     Record.AddStmt(E->getSubExprs()[I]);
920   Record.AddSourceLocation(E->getBuiltinLoc());
921   Record.AddSourceLocation(E->getRParenLoc());
922   Code = serialization::EXPR_ATOMIC;
923 }
924 
925 //===----------------------------------------------------------------------===//
926 // Objective-C Expressions and Statements.
927 //===----------------------------------------------------------------------===//
928 
929 void ASTStmtWriter::VisitObjCStringLiteral(ObjCStringLiteral *E) {
930   VisitExpr(E);
931   Record.AddStmt(E->getString());
932   Record.AddSourceLocation(E->getAtLoc());
933   Code = serialization::EXPR_OBJC_STRING_LITERAL;
934 }
935 
936 void ASTStmtWriter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
937   VisitExpr(E);
938   Record.AddStmt(E->getSubExpr());
939   Record.AddDeclRef(E->getBoxingMethod());
940   Record.AddSourceRange(E->getSourceRange());
941   Code = serialization::EXPR_OBJC_BOXED_EXPRESSION;
942 }
943 
944 void ASTStmtWriter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
945   VisitExpr(E);
946   Record.push_back(E->getNumElements());
947   for (unsigned i = 0; i < E->getNumElements(); i++)
948     Record.AddStmt(E->getElement(i));
949   Record.AddDeclRef(E->getArrayWithObjectsMethod());
950   Record.AddSourceRange(E->getSourceRange());
951   Code = serialization::EXPR_OBJC_ARRAY_LITERAL;
952 }
953 
954 void ASTStmtWriter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
955   VisitExpr(E);
956   Record.push_back(E->getNumElements());
957   Record.push_back(E->HasPackExpansions);
958   for (unsigned i = 0; i < E->getNumElements(); i++) {
959     ObjCDictionaryElement Element = E->getKeyValueElement(i);
960     Record.AddStmt(Element.Key);
961     Record.AddStmt(Element.Value);
962     if (E->HasPackExpansions) {
963       Record.AddSourceLocation(Element.EllipsisLoc);
964       unsigned NumExpansions = 0;
965       if (Element.NumExpansions)
966         NumExpansions = *Element.NumExpansions + 1;
967       Record.push_back(NumExpansions);
968     }
969   }
970 
971   Record.AddDeclRef(E->getDictWithObjectsMethod());
972   Record.AddSourceRange(E->getSourceRange());
973   Code = serialization::EXPR_OBJC_DICTIONARY_LITERAL;
974 }
975 
976 void ASTStmtWriter::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
977   VisitExpr(E);
978   Record.AddTypeSourceInfo(E->getEncodedTypeSourceInfo());
979   Record.AddSourceLocation(E->getAtLoc());
980   Record.AddSourceLocation(E->getRParenLoc());
981   Code = serialization::EXPR_OBJC_ENCODE;
982 }
983 
984 void ASTStmtWriter::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
985   VisitExpr(E);
986   Record.AddSelectorRef(E->getSelector());
987   Record.AddSourceLocation(E->getAtLoc());
988   Record.AddSourceLocation(E->getRParenLoc());
989   Code = serialization::EXPR_OBJC_SELECTOR_EXPR;
990 }
991 
992 void ASTStmtWriter::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
993   VisitExpr(E);
994   Record.AddDeclRef(E->getProtocol());
995   Record.AddSourceLocation(E->getAtLoc());
996   Record.AddSourceLocation(E->ProtoLoc);
997   Record.AddSourceLocation(E->getRParenLoc());
998   Code = serialization::EXPR_OBJC_PROTOCOL_EXPR;
999 }
1000 
1001 void ASTStmtWriter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
1002   VisitExpr(E);
1003   Record.AddDeclRef(E->getDecl());
1004   Record.AddSourceLocation(E->getLocation());
1005   Record.AddSourceLocation(E->getOpLoc());
1006   Record.AddStmt(E->getBase());
1007   Record.push_back(E->isArrow());
1008   Record.push_back(E->isFreeIvar());
1009   Code = serialization::EXPR_OBJC_IVAR_REF_EXPR;
1010 }
1011 
1012 void ASTStmtWriter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
1013   VisitExpr(E);
1014   Record.push_back(E->SetterAndMethodRefFlags.getInt());
1015   Record.push_back(E->isImplicitProperty());
1016   if (E->isImplicitProperty()) {
1017     Record.AddDeclRef(E->getImplicitPropertyGetter());
1018     Record.AddDeclRef(E->getImplicitPropertySetter());
1019   } else {
1020     Record.AddDeclRef(E->getExplicitProperty());
1021   }
1022   Record.AddSourceLocation(E->getLocation());
1023   Record.AddSourceLocation(E->getReceiverLocation());
1024   if (E->isObjectReceiver()) {
1025     Record.push_back(0);
1026     Record.AddStmt(E->getBase());
1027   } else if (E->isSuperReceiver()) {
1028     Record.push_back(1);
1029     Record.AddTypeRef(E->getSuperReceiverType());
1030   } else {
1031     Record.push_back(2);
1032     Record.AddDeclRef(E->getClassReceiver());
1033   }
1034 
1035   Code = serialization::EXPR_OBJC_PROPERTY_REF_EXPR;
1036 }
1037 
1038 void ASTStmtWriter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *E) {
1039   VisitExpr(E);
1040   Record.AddSourceLocation(E->getRBracket());
1041   Record.AddStmt(E->getBaseExpr());
1042   Record.AddStmt(E->getKeyExpr());
1043   Record.AddDeclRef(E->getAtIndexMethodDecl());
1044   Record.AddDeclRef(E->setAtIndexMethodDecl());
1045 
1046   Code = serialization::EXPR_OBJC_SUBSCRIPT_REF_EXPR;
1047 }
1048 
1049 void ASTStmtWriter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
1050   VisitExpr(E);
1051   Record.push_back(E->getNumArgs());
1052   Record.push_back(E->getNumStoredSelLocs());
1053   Record.push_back(E->SelLocsKind);
1054   Record.push_back(E->isDelegateInitCall());
1055   Record.push_back(E->IsImplicit);
1056   Record.push_back((unsigned)E->getReceiverKind()); // FIXME: stable encoding
1057   switch (E->getReceiverKind()) {
1058   case ObjCMessageExpr::Instance:
1059     Record.AddStmt(E->getInstanceReceiver());
1060     break;
1061 
1062   case ObjCMessageExpr::Class:
1063     Record.AddTypeSourceInfo(E->getClassReceiverTypeInfo());
1064     break;
1065 
1066   case ObjCMessageExpr::SuperClass:
1067   case ObjCMessageExpr::SuperInstance:
1068     Record.AddTypeRef(E->getSuperType());
1069     Record.AddSourceLocation(E->getSuperLoc());
1070     break;
1071   }
1072 
1073   if (E->getMethodDecl()) {
1074     Record.push_back(1);
1075     Record.AddDeclRef(E->getMethodDecl());
1076   } else {
1077     Record.push_back(0);
1078     Record.AddSelectorRef(E->getSelector());
1079   }
1080 
1081   Record.AddSourceLocation(E->getLeftLoc());
1082   Record.AddSourceLocation(E->getRightLoc());
1083 
1084   for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
1085        Arg != ArgEnd; ++Arg)
1086     Record.AddStmt(*Arg);
1087 
1088   SourceLocation *Locs = E->getStoredSelLocs();
1089   for (unsigned i = 0, e = E->getNumStoredSelLocs(); i != e; ++i)
1090     Record.AddSourceLocation(Locs[i]);
1091 
1092   Code = serialization::EXPR_OBJC_MESSAGE_EXPR;
1093 }
1094 
1095 void ASTStmtWriter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
1096   VisitStmt(S);
1097   Record.AddStmt(S->getElement());
1098   Record.AddStmt(S->getCollection());
1099   Record.AddStmt(S->getBody());
1100   Record.AddSourceLocation(S->getForLoc());
1101   Record.AddSourceLocation(S->getRParenLoc());
1102   Code = serialization::STMT_OBJC_FOR_COLLECTION;
1103 }
1104 
1105 void ASTStmtWriter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
1106   Record.AddStmt(S->getCatchBody());
1107   Record.AddDeclRef(S->getCatchParamDecl());
1108   Record.AddSourceLocation(S->getAtCatchLoc());
1109   Record.AddSourceLocation(S->getRParenLoc());
1110   Code = serialization::STMT_OBJC_CATCH;
1111 }
1112 
1113 void ASTStmtWriter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
1114   Record.AddStmt(S->getFinallyBody());
1115   Record.AddSourceLocation(S->getAtFinallyLoc());
1116   Code = serialization::STMT_OBJC_FINALLY;
1117 }
1118 
1119 void ASTStmtWriter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S) {
1120   Record.AddStmt(S->getSubStmt());
1121   Record.AddSourceLocation(S->getAtLoc());
1122   Code = serialization::STMT_OBJC_AUTORELEASE_POOL;
1123 }
1124 
1125 void ASTStmtWriter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
1126   Record.push_back(S->getNumCatchStmts());
1127   Record.push_back(S->getFinallyStmt() != nullptr);
1128   Record.AddStmt(S->getTryBody());
1129   for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I)
1130     Record.AddStmt(S->getCatchStmt(I));
1131   if (S->getFinallyStmt())
1132     Record.AddStmt(S->getFinallyStmt());
1133   Record.AddSourceLocation(S->getAtTryLoc());
1134   Code = serialization::STMT_OBJC_AT_TRY;
1135 }
1136 
1137 void ASTStmtWriter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S) {
1138   Record.AddStmt(S->getSynchExpr());
1139   Record.AddStmt(S->getSynchBody());
1140   Record.AddSourceLocation(S->getAtSynchronizedLoc());
1141   Code = serialization::STMT_OBJC_AT_SYNCHRONIZED;
1142 }
1143 
1144 void ASTStmtWriter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
1145   Record.AddStmt(S->getThrowExpr());
1146   Record.AddSourceLocation(S->getThrowLoc());
1147   Code = serialization::STMT_OBJC_AT_THROW;
1148 }
1149 
1150 void ASTStmtWriter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *E) {
1151   VisitExpr(E);
1152   Record.push_back(E->getValue());
1153   Record.AddSourceLocation(E->getLocation());
1154   Code = serialization::EXPR_OBJC_BOOL_LITERAL;
1155 }
1156 
1157 void ASTStmtWriter::VisitObjCAvailabilityCheckExpr(ObjCAvailabilityCheckExpr *E) {
1158   VisitExpr(E);
1159   Record.AddSourceRange(E->getSourceRange());
1160   Record.AddVersionTuple(E->getVersion());
1161   Code = serialization::EXPR_OBJC_AVAILABILITY_CHECK;
1162 }
1163 
1164 //===----------------------------------------------------------------------===//
1165 // C++ Expressions and Statements.
1166 //===----------------------------------------------------------------------===//
1167 
1168 void ASTStmtWriter::VisitCXXCatchStmt(CXXCatchStmt *S) {
1169   VisitStmt(S);
1170   Record.AddSourceLocation(S->getCatchLoc());
1171   Record.AddDeclRef(S->getExceptionDecl());
1172   Record.AddStmt(S->getHandlerBlock());
1173   Code = serialization::STMT_CXX_CATCH;
1174 }
1175 
1176 void ASTStmtWriter::VisitCXXTryStmt(CXXTryStmt *S) {
1177   VisitStmt(S);
1178   Record.push_back(S->getNumHandlers());
1179   Record.AddSourceLocation(S->getTryLoc());
1180   Record.AddStmt(S->getTryBlock());
1181   for (unsigned i = 0, e = S->getNumHandlers(); i != e; ++i)
1182     Record.AddStmt(S->getHandler(i));
1183   Code = serialization::STMT_CXX_TRY;
1184 }
1185 
1186 void ASTStmtWriter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
1187   VisitStmt(S);
1188   Record.AddSourceLocation(S->getForLoc());
1189   Record.AddSourceLocation(S->getCoawaitLoc());
1190   Record.AddSourceLocation(S->getColonLoc());
1191   Record.AddSourceLocation(S->getRParenLoc());
1192   Record.AddStmt(S->getRangeStmt());
1193   Record.AddStmt(S->getBeginStmt());
1194   Record.AddStmt(S->getEndStmt());
1195   Record.AddStmt(S->getCond());
1196   Record.AddStmt(S->getInc());
1197   Record.AddStmt(S->getLoopVarStmt());
1198   Record.AddStmt(S->getBody());
1199   Code = serialization::STMT_CXX_FOR_RANGE;
1200 }
1201 
1202 void ASTStmtWriter::VisitMSDependentExistsStmt(MSDependentExistsStmt *S) {
1203   VisitStmt(S);
1204   Record.AddSourceLocation(S->getKeywordLoc());
1205   Record.push_back(S->isIfExists());
1206   Record.AddNestedNameSpecifierLoc(S->getQualifierLoc());
1207   Record.AddDeclarationNameInfo(S->getNameInfo());
1208   Record.AddStmt(S->getSubStmt());
1209   Code = serialization::STMT_MS_DEPENDENT_EXISTS;
1210 }
1211 
1212 void ASTStmtWriter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) {
1213   VisitCallExpr(E);
1214   Record.push_back(E->getOperator());
1215   Record.AddSourceRange(E->Range);
1216   Record.push_back(E->isFPContractable());
1217   Code = serialization::EXPR_CXX_OPERATOR_CALL;
1218 }
1219 
1220 void ASTStmtWriter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
1221   VisitCallExpr(E);
1222   Code = serialization::EXPR_CXX_MEMBER_CALL;
1223 }
1224 
1225 void ASTStmtWriter::VisitCXXConstructExpr(CXXConstructExpr *E) {
1226   VisitExpr(E);
1227   Record.push_back(E->getNumArgs());
1228   for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
1229     Record.AddStmt(E->getArg(I));
1230   Record.AddDeclRef(E->getConstructor());
1231   Record.AddSourceLocation(E->getLocation());
1232   Record.push_back(E->isElidable());
1233   Record.push_back(E->hadMultipleCandidates());
1234   Record.push_back(E->isListInitialization());
1235   Record.push_back(E->isStdInitListInitialization());
1236   Record.push_back(E->requiresZeroInitialization());
1237   Record.push_back(E->getConstructionKind()); // FIXME: stable encoding
1238   Record.AddSourceRange(E->getParenOrBraceRange());
1239   Code = serialization::EXPR_CXX_CONSTRUCT;
1240 }
1241 
1242 void ASTStmtWriter::VisitCXXInheritedCtorInitExpr(CXXInheritedCtorInitExpr *E) {
1243   VisitExpr(E);
1244   Record.AddDeclRef(E->getConstructor());
1245   Record.AddSourceLocation(E->getLocation());
1246   Record.push_back(E->constructsVBase());
1247   Record.push_back(E->inheritedFromVBase());
1248   Code = serialization::EXPR_CXX_INHERITED_CTOR_INIT;
1249 }
1250 
1251 void ASTStmtWriter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) {
1252   VisitCXXConstructExpr(E);
1253   Record.AddTypeSourceInfo(E->getTypeSourceInfo());
1254   Code = serialization::EXPR_CXX_TEMPORARY_OBJECT;
1255 }
1256 
1257 void ASTStmtWriter::VisitLambdaExpr(LambdaExpr *E) {
1258   VisitExpr(E);
1259   Record.push_back(E->NumCaptures);
1260   unsigned NumArrayIndexVars = 0;
1261   if (E->HasArrayIndexVars)
1262     NumArrayIndexVars = E->getArrayIndexStarts()[E->NumCaptures];
1263   Record.push_back(NumArrayIndexVars);
1264   Record.AddSourceRange(E->IntroducerRange);
1265   Record.push_back(E->CaptureDefault); // FIXME: stable encoding
1266   Record.AddSourceLocation(E->CaptureDefaultLoc);
1267   Record.push_back(E->ExplicitParams);
1268   Record.push_back(E->ExplicitResultType);
1269   Record.AddSourceLocation(E->ClosingBrace);
1270 
1271   // Add capture initializers.
1272   for (LambdaExpr::capture_init_iterator C = E->capture_init_begin(),
1273                                       CEnd = E->capture_init_end();
1274        C != CEnd; ++C) {
1275     Record.AddStmt(*C);
1276   }
1277 
1278   // Add array index variables, if any.
1279   if (NumArrayIndexVars) {
1280     Record.append(E->getArrayIndexStarts(),
1281                   E->getArrayIndexStarts() + E->NumCaptures + 1);
1282     VarDecl **ArrayIndexVars = E->getArrayIndexVars();
1283     for (unsigned I = 0; I != NumArrayIndexVars; ++I)
1284       Record.AddDeclRef(ArrayIndexVars[I]);
1285   }
1286 
1287   Code = serialization::EXPR_LAMBDA;
1288 }
1289 
1290 void ASTStmtWriter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
1291   VisitExpr(E);
1292   Record.AddStmt(E->getSubExpr());
1293   Code = serialization::EXPR_CXX_STD_INITIALIZER_LIST;
1294 }
1295 
1296 void ASTStmtWriter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
1297   VisitExplicitCastExpr(E);
1298   Record.AddSourceRange(SourceRange(E->getOperatorLoc(), E->getRParenLoc()));
1299   Record.AddSourceRange(E->getAngleBrackets());
1300 }
1301 
1302 void ASTStmtWriter::VisitCXXStaticCastExpr(CXXStaticCastExpr *E) {
1303   VisitCXXNamedCastExpr(E);
1304   Code = serialization::EXPR_CXX_STATIC_CAST;
1305 }
1306 
1307 void ASTStmtWriter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *E) {
1308   VisitCXXNamedCastExpr(E);
1309   Code = serialization::EXPR_CXX_DYNAMIC_CAST;
1310 }
1311 
1312 void ASTStmtWriter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *E) {
1313   VisitCXXNamedCastExpr(E);
1314   Code = serialization::EXPR_CXX_REINTERPRET_CAST;
1315 }
1316 
1317 void ASTStmtWriter::VisitCXXConstCastExpr(CXXConstCastExpr *E) {
1318   VisitCXXNamedCastExpr(E);
1319   Code = serialization::EXPR_CXX_CONST_CAST;
1320 }
1321 
1322 void ASTStmtWriter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E) {
1323   VisitExplicitCastExpr(E);
1324   Record.AddSourceLocation(E->getLParenLoc());
1325   Record.AddSourceLocation(E->getRParenLoc());
1326   Code = serialization::EXPR_CXX_FUNCTIONAL_CAST;
1327 }
1328 
1329 void ASTStmtWriter::VisitUserDefinedLiteral(UserDefinedLiteral *E) {
1330   VisitCallExpr(E);
1331   Record.AddSourceLocation(E->UDSuffixLoc);
1332   Code = serialization::EXPR_USER_DEFINED_LITERAL;
1333 }
1334 
1335 void ASTStmtWriter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
1336   VisitExpr(E);
1337   Record.push_back(E->getValue());
1338   Record.AddSourceLocation(E->getLocation());
1339   Code = serialization::EXPR_CXX_BOOL_LITERAL;
1340 }
1341 
1342 void ASTStmtWriter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
1343   VisitExpr(E);
1344   Record.AddSourceLocation(E->getLocation());
1345   Code = serialization::EXPR_CXX_NULL_PTR_LITERAL;
1346 }
1347 
1348 void ASTStmtWriter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
1349   VisitExpr(E);
1350   Record.AddSourceRange(E->getSourceRange());
1351   if (E->isTypeOperand()) {
1352     Record.AddTypeSourceInfo(E->getTypeOperandSourceInfo());
1353     Code = serialization::EXPR_CXX_TYPEID_TYPE;
1354   } else {
1355     Record.AddStmt(E->getExprOperand());
1356     Code = serialization::EXPR_CXX_TYPEID_EXPR;
1357   }
1358 }
1359 
1360 void ASTStmtWriter::VisitCXXThisExpr(CXXThisExpr *E) {
1361   VisitExpr(E);
1362   Record.AddSourceLocation(E->getLocation());
1363   Record.push_back(E->isImplicit());
1364   Code = serialization::EXPR_CXX_THIS;
1365 }
1366 
1367 void ASTStmtWriter::VisitCXXThrowExpr(CXXThrowExpr *E) {
1368   VisitExpr(E);
1369   Record.AddSourceLocation(E->getThrowLoc());
1370   Record.AddStmt(E->getSubExpr());
1371   Record.push_back(E->isThrownVariableInScope());
1372   Code = serialization::EXPR_CXX_THROW;
1373 }
1374 
1375 void ASTStmtWriter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
1376   VisitExpr(E);
1377   Record.AddDeclRef(E->getParam());
1378   Record.AddSourceLocation(E->getUsedLocation());
1379   Code = serialization::EXPR_CXX_DEFAULT_ARG;
1380 }
1381 
1382 void ASTStmtWriter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) {
1383   VisitExpr(E);
1384   Record.AddDeclRef(E->getField());
1385   Record.AddSourceLocation(E->getExprLoc());
1386   Code = serialization::EXPR_CXX_DEFAULT_INIT;
1387 }
1388 
1389 void ASTStmtWriter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
1390   VisitExpr(E);
1391   Record.AddCXXTemporary(E->getTemporary());
1392   Record.AddStmt(E->getSubExpr());
1393   Code = serialization::EXPR_CXX_BIND_TEMPORARY;
1394 }
1395 
1396 void ASTStmtWriter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
1397   VisitExpr(E);
1398   Record.AddTypeSourceInfo(E->getTypeSourceInfo());
1399   Record.AddSourceLocation(E->getRParenLoc());
1400   Code = serialization::EXPR_CXX_SCALAR_VALUE_INIT;
1401 }
1402 
1403 void ASTStmtWriter::VisitCXXNewExpr(CXXNewExpr *E) {
1404   VisitExpr(E);
1405   Record.push_back(E->isGlobalNew());
1406   Record.push_back(E->isArray());
1407   Record.push_back(E->passAlignment());
1408   Record.push_back(E->doesUsualArrayDeleteWantSize());
1409   Record.push_back(E->getNumPlacementArgs());
1410   Record.push_back(E->StoredInitializationStyle);
1411   Record.AddDeclRef(E->getOperatorNew());
1412   Record.AddDeclRef(E->getOperatorDelete());
1413   Record.AddTypeSourceInfo(E->getAllocatedTypeSourceInfo());
1414   Record.AddSourceRange(E->getTypeIdParens());
1415   Record.AddSourceRange(E->getSourceRange());
1416   Record.AddSourceRange(E->getDirectInitRange());
1417   for (CXXNewExpr::arg_iterator I = E->raw_arg_begin(), e = E->raw_arg_end();
1418        I != e; ++I)
1419     Record.AddStmt(*I);
1420 
1421   Code = serialization::EXPR_CXX_NEW;
1422 }
1423 
1424 void ASTStmtWriter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1425   VisitExpr(E);
1426   Record.push_back(E->isGlobalDelete());
1427   Record.push_back(E->isArrayForm());
1428   Record.push_back(E->isArrayFormAsWritten());
1429   Record.push_back(E->doesUsualArrayDeleteWantSize());
1430   Record.AddDeclRef(E->getOperatorDelete());
1431   Record.AddStmt(E->getArgument());
1432   Record.AddSourceLocation(E->getSourceRange().getBegin());
1433 
1434   Code = serialization::EXPR_CXX_DELETE;
1435 }
1436 
1437 void ASTStmtWriter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1438   VisitExpr(E);
1439 
1440   Record.AddStmt(E->getBase());
1441   Record.push_back(E->isArrow());
1442   Record.AddSourceLocation(E->getOperatorLoc());
1443   Record.AddNestedNameSpecifierLoc(E->getQualifierLoc());
1444   Record.AddTypeSourceInfo(E->getScopeTypeInfo());
1445   Record.AddSourceLocation(E->getColonColonLoc());
1446   Record.AddSourceLocation(E->getTildeLoc());
1447 
1448   // PseudoDestructorTypeStorage.
1449   Record.AddIdentifierRef(E->getDestroyedTypeIdentifier());
1450   if (E->getDestroyedTypeIdentifier())
1451     Record.AddSourceLocation(E->getDestroyedTypeLoc());
1452   else
1453     Record.AddTypeSourceInfo(E->getDestroyedTypeInfo());
1454 
1455   Code = serialization::EXPR_CXX_PSEUDO_DESTRUCTOR;
1456 }
1457 
1458 void ASTStmtWriter::VisitExprWithCleanups(ExprWithCleanups *E) {
1459   VisitExpr(E);
1460   Record.push_back(E->getNumObjects());
1461   for (unsigned i = 0, e = E->getNumObjects(); i != e; ++i)
1462     Record.AddDeclRef(E->getObject(i));
1463 
1464   Record.push_back(E->cleanupsHaveSideEffects());
1465   Record.AddStmt(E->getSubExpr());
1466   Code = serialization::EXPR_EXPR_WITH_CLEANUPS;
1467 }
1468 
1469 void
1470 ASTStmtWriter::VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E){
1471   VisitExpr(E);
1472 
1473   // Don't emit anything here, HasTemplateKWAndArgsInfo must be
1474   // emitted first.
1475 
1476   Record.push_back(E->HasTemplateKWAndArgsInfo);
1477   if (E->HasTemplateKWAndArgsInfo) {
1478     const ASTTemplateKWAndArgsInfo &ArgInfo =
1479         *E->getTrailingObjects<ASTTemplateKWAndArgsInfo>();
1480     Record.push_back(ArgInfo.NumTemplateArgs);
1481     AddTemplateKWAndArgsInfo(ArgInfo,
1482                              E->getTrailingObjects<TemplateArgumentLoc>());
1483   }
1484 
1485   if (!E->isImplicitAccess())
1486     Record.AddStmt(E->getBase());
1487   else
1488     Record.AddStmt(nullptr);
1489   Record.AddTypeRef(E->getBaseType());
1490   Record.push_back(E->isArrow());
1491   Record.AddSourceLocation(E->getOperatorLoc());
1492   Record.AddNestedNameSpecifierLoc(E->getQualifierLoc());
1493   Record.AddDeclRef(E->getFirstQualifierFoundInScope());
1494   Record.AddDeclarationNameInfo(E->MemberNameInfo);
1495   Code = serialization::EXPR_CXX_DEPENDENT_SCOPE_MEMBER;
1496 }
1497 
1498 void
1499 ASTStmtWriter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
1500   VisitExpr(E);
1501 
1502   // Don't emit anything here, HasTemplateKWAndArgsInfo must be
1503   // emitted first.
1504 
1505   Record.push_back(E->HasTemplateKWAndArgsInfo);
1506   if (E->HasTemplateKWAndArgsInfo) {
1507     const ASTTemplateKWAndArgsInfo &ArgInfo =
1508         *E->getTrailingObjects<ASTTemplateKWAndArgsInfo>();
1509     Record.push_back(ArgInfo.NumTemplateArgs);
1510     AddTemplateKWAndArgsInfo(ArgInfo,
1511                              E->getTrailingObjects<TemplateArgumentLoc>());
1512   }
1513 
1514   Record.AddNestedNameSpecifierLoc(E->getQualifierLoc());
1515   Record.AddDeclarationNameInfo(E->NameInfo);
1516   Code = serialization::EXPR_CXX_DEPENDENT_SCOPE_DECL_REF;
1517 }
1518 
1519 void
1520 ASTStmtWriter::VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E) {
1521   VisitExpr(E);
1522   Record.push_back(E->arg_size());
1523   for (CXXUnresolvedConstructExpr::arg_iterator
1524          ArgI = E->arg_begin(), ArgE = E->arg_end(); ArgI != ArgE; ++ArgI)
1525     Record.AddStmt(*ArgI);
1526   Record.AddTypeSourceInfo(E->getTypeSourceInfo());
1527   Record.AddSourceLocation(E->getLParenLoc());
1528   Record.AddSourceLocation(E->getRParenLoc());
1529   Code = serialization::EXPR_CXX_UNRESOLVED_CONSTRUCT;
1530 }
1531 
1532 void ASTStmtWriter::VisitOverloadExpr(OverloadExpr *E) {
1533   VisitExpr(E);
1534 
1535   // Don't emit anything here, HasTemplateKWAndArgsInfo must be
1536   // emitted first.
1537 
1538   Record.push_back(E->HasTemplateKWAndArgsInfo);
1539   if (E->HasTemplateKWAndArgsInfo) {
1540     const ASTTemplateKWAndArgsInfo &ArgInfo =
1541         *E->getTrailingASTTemplateKWAndArgsInfo();
1542     Record.push_back(ArgInfo.NumTemplateArgs);
1543     AddTemplateKWAndArgsInfo(ArgInfo, E->getTrailingTemplateArgumentLoc());
1544   }
1545 
1546   Record.push_back(E->getNumDecls());
1547   for (OverloadExpr::decls_iterator
1548          OvI = E->decls_begin(), OvE = E->decls_end(); OvI != OvE; ++OvI) {
1549     Record.AddDeclRef(OvI.getDecl());
1550     Record.push_back(OvI.getAccess());
1551   }
1552 
1553   Record.AddDeclarationNameInfo(E->NameInfo);
1554   Record.AddNestedNameSpecifierLoc(E->getQualifierLoc());
1555 }
1556 
1557 void ASTStmtWriter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
1558   VisitOverloadExpr(E);
1559   Record.push_back(E->isArrow());
1560   Record.push_back(E->hasUnresolvedUsing());
1561   Record.AddStmt(!E->isImplicitAccess() ? E->getBase() : nullptr);
1562   Record.AddTypeRef(E->getBaseType());
1563   Record.AddSourceLocation(E->getOperatorLoc());
1564   Code = serialization::EXPR_CXX_UNRESOLVED_MEMBER;
1565 }
1566 
1567 void ASTStmtWriter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
1568   VisitOverloadExpr(E);
1569   Record.push_back(E->requiresADL());
1570   Record.push_back(E->isOverloaded());
1571   Record.AddDeclRef(E->getNamingClass());
1572   Code = serialization::EXPR_CXX_UNRESOLVED_LOOKUP;
1573 }
1574 
1575 void ASTStmtWriter::VisitTypeTraitExpr(TypeTraitExpr *E) {
1576   VisitExpr(E);
1577   Record.push_back(E->TypeTraitExprBits.NumArgs);
1578   Record.push_back(E->TypeTraitExprBits.Kind); // FIXME: Stable encoding
1579   Record.push_back(E->TypeTraitExprBits.Value);
1580   Record.AddSourceRange(E->getSourceRange());
1581   for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
1582     Record.AddTypeSourceInfo(E->getArg(I));
1583   Code = serialization::EXPR_TYPE_TRAIT;
1584 }
1585 
1586 void ASTStmtWriter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
1587   VisitExpr(E);
1588   Record.push_back(E->getTrait());
1589   Record.push_back(E->getValue());
1590   Record.AddSourceRange(E->getSourceRange());
1591   Record.AddTypeSourceInfo(E->getQueriedTypeSourceInfo());
1592   Record.AddStmt(E->getDimensionExpression());
1593   Code = serialization::EXPR_ARRAY_TYPE_TRAIT;
1594 }
1595 
1596 void ASTStmtWriter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
1597   VisitExpr(E);
1598   Record.push_back(E->getTrait());
1599   Record.push_back(E->getValue());
1600   Record.AddSourceRange(E->getSourceRange());
1601   Record.AddStmt(E->getQueriedExpression());
1602   Code = serialization::EXPR_CXX_EXPRESSION_TRAIT;
1603 }
1604 
1605 void ASTStmtWriter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1606   VisitExpr(E);
1607   Record.push_back(E->getValue());
1608   Record.AddSourceRange(E->getSourceRange());
1609   Record.AddStmt(E->getOperand());
1610   Code = serialization::EXPR_CXX_NOEXCEPT;
1611 }
1612 
1613 void ASTStmtWriter::VisitPackExpansionExpr(PackExpansionExpr *E) {
1614   VisitExpr(E);
1615   Record.AddSourceLocation(E->getEllipsisLoc());
1616   Record.push_back(E->NumExpansions);
1617   Record.AddStmt(E->getPattern());
1618   Code = serialization::EXPR_PACK_EXPANSION;
1619 }
1620 
1621 void ASTStmtWriter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
1622   VisitExpr(E);
1623   Record.push_back(E->isPartiallySubstituted() ? E->getPartialArguments().size()
1624                                                : 0);
1625   Record.AddSourceLocation(E->OperatorLoc);
1626   Record.AddSourceLocation(E->PackLoc);
1627   Record.AddSourceLocation(E->RParenLoc);
1628   Record.AddDeclRef(E->Pack);
1629   if (E->isPartiallySubstituted()) {
1630     for (const auto &TA : E->getPartialArguments())
1631       Record.AddTemplateArgument(TA);
1632   } else if (!E->isValueDependent()) {
1633     Record.push_back(E->getPackLength());
1634   }
1635   Code = serialization::EXPR_SIZEOF_PACK;
1636 }
1637 
1638 void ASTStmtWriter::VisitSubstNonTypeTemplateParmExpr(
1639                                               SubstNonTypeTemplateParmExpr *E) {
1640   VisitExpr(E);
1641   Record.AddDeclRef(E->getParameter());
1642   Record.AddSourceLocation(E->getNameLoc());
1643   Record.AddStmt(E->getReplacement());
1644   Code = serialization::EXPR_SUBST_NON_TYPE_TEMPLATE_PARM;
1645 }
1646 
1647 void ASTStmtWriter::VisitSubstNonTypeTemplateParmPackExpr(
1648                                           SubstNonTypeTemplateParmPackExpr *E) {
1649   VisitExpr(E);
1650   Record.AddDeclRef(E->getParameterPack());
1651   Record.AddTemplateArgument(E->getArgumentPack());
1652   Record.AddSourceLocation(E->getParameterPackLocation());
1653   Code = serialization::EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK;
1654 }
1655 
1656 void ASTStmtWriter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
1657   VisitExpr(E);
1658   Record.push_back(E->getNumExpansions());
1659   Record.AddDeclRef(E->getParameterPack());
1660   Record.AddSourceLocation(E->getParameterPackLocation());
1661   for (FunctionParmPackExpr::iterator I = E->begin(), End = E->end();
1662        I != End; ++I)
1663     Record.AddDeclRef(*I);
1664   Code = serialization::EXPR_FUNCTION_PARM_PACK;
1665 }
1666 
1667 void ASTStmtWriter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
1668   VisitExpr(E);
1669   Record.AddStmt(E->getTemporary());
1670   Record.AddDeclRef(E->getExtendingDecl());
1671   Record.push_back(E->getManglingNumber());
1672   Code = serialization::EXPR_MATERIALIZE_TEMPORARY;
1673 }
1674 
1675 void ASTStmtWriter::VisitCXXFoldExpr(CXXFoldExpr *E) {
1676   VisitExpr(E);
1677   Record.AddSourceLocation(E->LParenLoc);
1678   Record.AddSourceLocation(E->EllipsisLoc);
1679   Record.AddSourceLocation(E->RParenLoc);
1680   Record.AddStmt(E->SubExprs[0]);
1681   Record.AddStmt(E->SubExprs[1]);
1682   Record.push_back(E->Opcode);
1683   Code = serialization::EXPR_CXX_FOLD;
1684 }
1685 
1686 void ASTStmtWriter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
1687   VisitExpr(E);
1688   Record.AddStmt(E->getSourceExpr());
1689   Record.AddSourceLocation(E->getLocation());
1690   Code = serialization::EXPR_OPAQUE_VALUE;
1691 }
1692 
1693 void ASTStmtWriter::VisitTypoExpr(TypoExpr *E) {
1694   VisitExpr(E);
1695   // TODO: Figure out sane writer behavior for a TypoExpr, if necessary
1696   llvm_unreachable("Cannot write TypoExpr nodes");
1697 }
1698 
1699 //===----------------------------------------------------------------------===//
1700 // CUDA Expressions and Statements.
1701 //===----------------------------------------------------------------------===//
1702 
1703 void ASTStmtWriter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *E) {
1704   VisitCallExpr(E);
1705   Record.AddStmt(E->getConfig());
1706   Code = serialization::EXPR_CUDA_KERNEL_CALL;
1707 }
1708 
1709 //===----------------------------------------------------------------------===//
1710 // OpenCL Expressions and Statements.
1711 //===----------------------------------------------------------------------===//
1712 void ASTStmtWriter::VisitAsTypeExpr(AsTypeExpr *E) {
1713   VisitExpr(E);
1714   Record.AddSourceLocation(E->getBuiltinLoc());
1715   Record.AddSourceLocation(E->getRParenLoc());
1716   Record.AddStmt(E->getSrcExpr());
1717   Code = serialization::EXPR_ASTYPE;
1718 }
1719 
1720 //===----------------------------------------------------------------------===//
1721 // Microsoft Expressions and Statements.
1722 //===----------------------------------------------------------------------===//
1723 void ASTStmtWriter::VisitMSPropertyRefExpr(MSPropertyRefExpr *E) {
1724   VisitExpr(E);
1725   Record.push_back(E->isArrow());
1726   Record.AddStmt(E->getBaseExpr());
1727   Record.AddNestedNameSpecifierLoc(E->getQualifierLoc());
1728   Record.AddSourceLocation(E->getMemberLoc());
1729   Record.AddDeclRef(E->getPropertyDecl());
1730   Code = serialization::EXPR_CXX_PROPERTY_REF_EXPR;
1731 }
1732 
1733 void ASTStmtWriter::VisitMSPropertySubscriptExpr(MSPropertySubscriptExpr *E) {
1734   VisitExpr(E);
1735   Record.AddStmt(E->getBase());
1736   Record.AddStmt(E->getIdx());
1737   Record.AddSourceLocation(E->getRBracketLoc());
1738   Code = serialization::EXPR_CXX_PROPERTY_SUBSCRIPT_EXPR;
1739 }
1740 
1741 void ASTStmtWriter::VisitCXXUuidofExpr(CXXUuidofExpr *E) {
1742   VisitExpr(E);
1743   Record.AddSourceRange(E->getSourceRange());
1744   Record.AddString(E->getUuidStr());
1745   if (E->isTypeOperand()) {
1746     Record.AddTypeSourceInfo(E->getTypeOperandSourceInfo());
1747     Code = serialization::EXPR_CXX_UUIDOF_TYPE;
1748   } else {
1749     Record.AddStmt(E->getExprOperand());
1750     Code = serialization::EXPR_CXX_UUIDOF_EXPR;
1751   }
1752 }
1753 
1754 void ASTStmtWriter::VisitSEHExceptStmt(SEHExceptStmt *S) {
1755   VisitStmt(S);
1756   Record.AddSourceLocation(S->getExceptLoc());
1757   Record.AddStmt(S->getFilterExpr());
1758   Record.AddStmt(S->getBlock());
1759   Code = serialization::STMT_SEH_EXCEPT;
1760 }
1761 
1762 void ASTStmtWriter::VisitSEHFinallyStmt(SEHFinallyStmt *S) {
1763   VisitStmt(S);
1764   Record.AddSourceLocation(S->getFinallyLoc());
1765   Record.AddStmt(S->getBlock());
1766   Code = serialization::STMT_SEH_FINALLY;
1767 }
1768 
1769 void ASTStmtWriter::VisitSEHTryStmt(SEHTryStmt *S) {
1770   VisitStmt(S);
1771   Record.push_back(S->getIsCXXTry());
1772   Record.AddSourceLocation(S->getTryLoc());
1773   Record.AddStmt(S->getTryBlock());
1774   Record.AddStmt(S->getHandler());
1775   Code = serialization::STMT_SEH_TRY;
1776 }
1777 
1778 void ASTStmtWriter::VisitSEHLeaveStmt(SEHLeaveStmt *S) {
1779   VisitStmt(S);
1780   Record.AddSourceLocation(S->getLeaveLoc());
1781   Code = serialization::STMT_SEH_LEAVE;
1782 }
1783 
1784 //===----------------------------------------------------------------------===//
1785 // OpenMP Clauses.
1786 //===----------------------------------------------------------------------===//
1787 
1788 namespace clang {
1789 class OMPClauseWriter : public OMPClauseVisitor<OMPClauseWriter> {
1790   ASTRecordWriter &Record;
1791 public:
1792   OMPClauseWriter(ASTRecordWriter &Record) : Record(Record) {}
1793 #define OPENMP_CLAUSE(Name, Class)    \
1794   void Visit##Class(Class *S);
1795 #include "clang/Basic/OpenMPKinds.def"
1796   void writeClause(OMPClause *C);
1797   void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C);
1798   void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C);
1799 };
1800 }
1801 
1802 void OMPClauseWriter::writeClause(OMPClause *C) {
1803   Record.push_back(C->getClauseKind());
1804   Visit(C);
1805   Record.AddSourceLocation(C->getLocStart());
1806   Record.AddSourceLocation(C->getLocEnd());
1807 }
1808 
1809 void OMPClauseWriter::VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C) {
1810   Record.AddStmt(C->getPreInitStmt());
1811 }
1812 
1813 void OMPClauseWriter::VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C) {
1814   VisitOMPClauseWithPreInit(C);
1815   Record.AddStmt(C->getPostUpdateExpr());
1816 }
1817 
1818 void OMPClauseWriter::VisitOMPIfClause(OMPIfClause *C) {
1819   Record.push_back(C->getNameModifier());
1820   Record.AddSourceLocation(C->getNameModifierLoc());
1821   Record.AddSourceLocation(C->getColonLoc());
1822   Record.AddStmt(C->getCondition());
1823   Record.AddSourceLocation(C->getLParenLoc());
1824 }
1825 
1826 void OMPClauseWriter::VisitOMPFinalClause(OMPFinalClause *C) {
1827   Record.AddStmt(C->getCondition());
1828   Record.AddSourceLocation(C->getLParenLoc());
1829 }
1830 
1831 void OMPClauseWriter::VisitOMPNumThreadsClause(OMPNumThreadsClause *C) {
1832   Record.AddStmt(C->getNumThreads());
1833   Record.AddSourceLocation(C->getLParenLoc());
1834 }
1835 
1836 void OMPClauseWriter::VisitOMPSafelenClause(OMPSafelenClause *C) {
1837   Record.AddStmt(C->getSafelen());
1838   Record.AddSourceLocation(C->getLParenLoc());
1839 }
1840 
1841 void OMPClauseWriter::VisitOMPSimdlenClause(OMPSimdlenClause *C) {
1842   Record.AddStmt(C->getSimdlen());
1843   Record.AddSourceLocation(C->getLParenLoc());
1844 }
1845 
1846 void OMPClauseWriter::VisitOMPCollapseClause(OMPCollapseClause *C) {
1847   Record.AddStmt(C->getNumForLoops());
1848   Record.AddSourceLocation(C->getLParenLoc());
1849 }
1850 
1851 void OMPClauseWriter::VisitOMPDefaultClause(OMPDefaultClause *C) {
1852   Record.push_back(C->getDefaultKind());
1853   Record.AddSourceLocation(C->getLParenLoc());
1854   Record.AddSourceLocation(C->getDefaultKindKwLoc());
1855 }
1856 
1857 void OMPClauseWriter::VisitOMPProcBindClause(OMPProcBindClause *C) {
1858   Record.push_back(C->getProcBindKind());
1859   Record.AddSourceLocation(C->getLParenLoc());
1860   Record.AddSourceLocation(C->getProcBindKindKwLoc());
1861 }
1862 
1863 void OMPClauseWriter::VisitOMPScheduleClause(OMPScheduleClause *C) {
1864   VisitOMPClauseWithPreInit(C);
1865   Record.push_back(C->getScheduleKind());
1866   Record.push_back(C->getFirstScheduleModifier());
1867   Record.push_back(C->getSecondScheduleModifier());
1868   Record.AddStmt(C->getChunkSize());
1869   Record.AddSourceLocation(C->getLParenLoc());
1870   Record.AddSourceLocation(C->getFirstScheduleModifierLoc());
1871   Record.AddSourceLocation(C->getSecondScheduleModifierLoc());
1872   Record.AddSourceLocation(C->getScheduleKindLoc());
1873   Record.AddSourceLocation(C->getCommaLoc());
1874 }
1875 
1876 void OMPClauseWriter::VisitOMPOrderedClause(OMPOrderedClause *C) {
1877   Record.AddStmt(C->getNumForLoops());
1878   Record.AddSourceLocation(C->getLParenLoc());
1879 }
1880 
1881 void OMPClauseWriter::VisitOMPNowaitClause(OMPNowaitClause *) {}
1882 
1883 void OMPClauseWriter::VisitOMPUntiedClause(OMPUntiedClause *) {}
1884 
1885 void OMPClauseWriter::VisitOMPMergeableClause(OMPMergeableClause *) {}
1886 
1887 void OMPClauseWriter::VisitOMPReadClause(OMPReadClause *) {}
1888 
1889 void OMPClauseWriter::VisitOMPWriteClause(OMPWriteClause *) {}
1890 
1891 void OMPClauseWriter::VisitOMPUpdateClause(OMPUpdateClause *) {}
1892 
1893 void OMPClauseWriter::VisitOMPCaptureClause(OMPCaptureClause *) {}
1894 
1895 void OMPClauseWriter::VisitOMPSeqCstClause(OMPSeqCstClause *) {}
1896 
1897 void OMPClauseWriter::VisitOMPThreadsClause(OMPThreadsClause *) {}
1898 
1899 void OMPClauseWriter::VisitOMPSIMDClause(OMPSIMDClause *) {}
1900 
1901 void OMPClauseWriter::VisitOMPNogroupClause(OMPNogroupClause *) {}
1902 
1903 void OMPClauseWriter::VisitOMPPrivateClause(OMPPrivateClause *C) {
1904   Record.push_back(C->varlist_size());
1905   Record.AddSourceLocation(C->getLParenLoc());
1906   for (auto *VE : C->varlists()) {
1907     Record.AddStmt(VE);
1908   }
1909   for (auto *VE : C->private_copies()) {
1910     Record.AddStmt(VE);
1911   }
1912 }
1913 
1914 void OMPClauseWriter::VisitOMPFirstprivateClause(OMPFirstprivateClause *C) {
1915   Record.push_back(C->varlist_size());
1916   VisitOMPClauseWithPreInit(C);
1917   Record.AddSourceLocation(C->getLParenLoc());
1918   for (auto *VE : C->varlists()) {
1919     Record.AddStmt(VE);
1920   }
1921   for (auto *VE : C->private_copies()) {
1922     Record.AddStmt(VE);
1923   }
1924   for (auto *VE : C->inits()) {
1925     Record.AddStmt(VE);
1926   }
1927 }
1928 
1929 void OMPClauseWriter::VisitOMPLastprivateClause(OMPLastprivateClause *C) {
1930   Record.push_back(C->varlist_size());
1931   VisitOMPClauseWithPostUpdate(C);
1932   Record.AddSourceLocation(C->getLParenLoc());
1933   for (auto *VE : C->varlists())
1934     Record.AddStmt(VE);
1935   for (auto *E : C->private_copies())
1936     Record.AddStmt(E);
1937   for (auto *E : C->source_exprs())
1938     Record.AddStmt(E);
1939   for (auto *E : C->destination_exprs())
1940     Record.AddStmt(E);
1941   for (auto *E : C->assignment_ops())
1942     Record.AddStmt(E);
1943 }
1944 
1945 void OMPClauseWriter::VisitOMPSharedClause(OMPSharedClause *C) {
1946   Record.push_back(C->varlist_size());
1947   Record.AddSourceLocation(C->getLParenLoc());
1948   for (auto *VE : C->varlists())
1949     Record.AddStmt(VE);
1950 }
1951 
1952 void OMPClauseWriter::VisitOMPReductionClause(OMPReductionClause *C) {
1953   Record.push_back(C->varlist_size());
1954   VisitOMPClauseWithPostUpdate(C);
1955   Record.AddSourceLocation(C->getLParenLoc());
1956   Record.AddSourceLocation(C->getColonLoc());
1957   Record.AddNestedNameSpecifierLoc(C->getQualifierLoc());
1958   Record.AddDeclarationNameInfo(C->getNameInfo());
1959   for (auto *VE : C->varlists())
1960     Record.AddStmt(VE);
1961   for (auto *VE : C->privates())
1962     Record.AddStmt(VE);
1963   for (auto *E : C->lhs_exprs())
1964     Record.AddStmt(E);
1965   for (auto *E : C->rhs_exprs())
1966     Record.AddStmt(E);
1967   for (auto *E : C->reduction_ops())
1968     Record.AddStmt(E);
1969 }
1970 
1971 void OMPClauseWriter::VisitOMPLinearClause(OMPLinearClause *C) {
1972   Record.push_back(C->varlist_size());
1973   VisitOMPClauseWithPostUpdate(C);
1974   Record.AddSourceLocation(C->getLParenLoc());
1975   Record.AddSourceLocation(C->getColonLoc());
1976   Record.push_back(C->getModifier());
1977   Record.AddSourceLocation(C->getModifierLoc());
1978   for (auto *VE : C->varlists()) {
1979     Record.AddStmt(VE);
1980   }
1981   for (auto *VE : C->privates()) {
1982     Record.AddStmt(VE);
1983   }
1984   for (auto *VE : C->inits()) {
1985     Record.AddStmt(VE);
1986   }
1987   for (auto *VE : C->updates()) {
1988     Record.AddStmt(VE);
1989   }
1990   for (auto *VE : C->finals()) {
1991     Record.AddStmt(VE);
1992   }
1993   Record.AddStmt(C->getStep());
1994   Record.AddStmt(C->getCalcStep());
1995 }
1996 
1997 void OMPClauseWriter::VisitOMPAlignedClause(OMPAlignedClause *C) {
1998   Record.push_back(C->varlist_size());
1999   Record.AddSourceLocation(C->getLParenLoc());
2000   Record.AddSourceLocation(C->getColonLoc());
2001   for (auto *VE : C->varlists())
2002     Record.AddStmt(VE);
2003   Record.AddStmt(C->getAlignment());
2004 }
2005 
2006 void OMPClauseWriter::VisitOMPCopyinClause(OMPCopyinClause *C) {
2007   Record.push_back(C->varlist_size());
2008   Record.AddSourceLocation(C->getLParenLoc());
2009   for (auto *VE : C->varlists())
2010     Record.AddStmt(VE);
2011   for (auto *E : C->source_exprs())
2012     Record.AddStmt(E);
2013   for (auto *E : C->destination_exprs())
2014     Record.AddStmt(E);
2015   for (auto *E : C->assignment_ops())
2016     Record.AddStmt(E);
2017 }
2018 
2019 void OMPClauseWriter::VisitOMPCopyprivateClause(OMPCopyprivateClause *C) {
2020   Record.push_back(C->varlist_size());
2021   Record.AddSourceLocation(C->getLParenLoc());
2022   for (auto *VE : C->varlists())
2023     Record.AddStmt(VE);
2024   for (auto *E : C->source_exprs())
2025     Record.AddStmt(E);
2026   for (auto *E : C->destination_exprs())
2027     Record.AddStmt(E);
2028   for (auto *E : C->assignment_ops())
2029     Record.AddStmt(E);
2030 }
2031 
2032 void OMPClauseWriter::VisitOMPFlushClause(OMPFlushClause *C) {
2033   Record.push_back(C->varlist_size());
2034   Record.AddSourceLocation(C->getLParenLoc());
2035   for (auto *VE : C->varlists())
2036     Record.AddStmt(VE);
2037 }
2038 
2039 void OMPClauseWriter::VisitOMPDependClause(OMPDependClause *C) {
2040   Record.push_back(C->varlist_size());
2041   Record.AddSourceLocation(C->getLParenLoc());
2042   Record.push_back(C->getDependencyKind());
2043   Record.AddSourceLocation(C->getDependencyLoc());
2044   Record.AddSourceLocation(C->getColonLoc());
2045   for (auto *VE : C->varlists())
2046     Record.AddStmt(VE);
2047   Record.AddStmt(C->getCounterValue());
2048 }
2049 
2050 void OMPClauseWriter::VisitOMPDeviceClause(OMPDeviceClause *C) {
2051   Record.AddStmt(C->getDevice());
2052   Record.AddSourceLocation(C->getLParenLoc());
2053 }
2054 
2055 void OMPClauseWriter::VisitOMPMapClause(OMPMapClause *C) {
2056   Record.push_back(C->varlist_size());
2057   Record.push_back(C->getUniqueDeclarationsNum());
2058   Record.push_back(C->getTotalComponentListNum());
2059   Record.push_back(C->getTotalComponentsNum());
2060   Record.AddSourceLocation(C->getLParenLoc());
2061   Record.push_back(C->getMapTypeModifier());
2062   Record.push_back(C->getMapType());
2063   Record.AddSourceLocation(C->getMapLoc());
2064   Record.AddSourceLocation(C->getColonLoc());
2065   for (auto *E : C->varlists())
2066     Record.AddStmt(E);
2067   for (auto *D : C->all_decls())
2068     Record.AddDeclRef(D);
2069   for (auto N : C->all_num_lists())
2070     Record.push_back(N);
2071   for (auto N : C->all_lists_sizes())
2072     Record.push_back(N);
2073   for (auto &M : C->all_components()) {
2074     Record.AddStmt(M.getAssociatedExpression());
2075     Record.AddDeclRef(M.getAssociatedDeclaration());
2076   }
2077 }
2078 
2079 void OMPClauseWriter::VisitOMPNumTeamsClause(OMPNumTeamsClause *C) {
2080   Record.AddStmt(C->getNumTeams());
2081   Record.AddSourceLocation(C->getLParenLoc());
2082 }
2083 
2084 void OMPClauseWriter::VisitOMPThreadLimitClause(OMPThreadLimitClause *C) {
2085   Record.AddStmt(C->getThreadLimit());
2086   Record.AddSourceLocation(C->getLParenLoc());
2087 }
2088 
2089 void OMPClauseWriter::VisitOMPPriorityClause(OMPPriorityClause *C) {
2090   Record.AddStmt(C->getPriority());
2091   Record.AddSourceLocation(C->getLParenLoc());
2092 }
2093 
2094 void OMPClauseWriter::VisitOMPGrainsizeClause(OMPGrainsizeClause *C) {
2095   Record.AddStmt(C->getGrainsize());
2096   Record.AddSourceLocation(C->getLParenLoc());
2097 }
2098 
2099 void OMPClauseWriter::VisitOMPNumTasksClause(OMPNumTasksClause *C) {
2100   Record.AddStmt(C->getNumTasks());
2101   Record.AddSourceLocation(C->getLParenLoc());
2102 }
2103 
2104 void OMPClauseWriter::VisitOMPHintClause(OMPHintClause *C) {
2105   Record.AddStmt(C->getHint());
2106   Record.AddSourceLocation(C->getLParenLoc());
2107 }
2108 
2109 void OMPClauseWriter::VisitOMPDistScheduleClause(OMPDistScheduleClause *C) {
2110   VisitOMPClauseWithPreInit(C);
2111   Record.push_back(C->getDistScheduleKind());
2112   Record.AddStmt(C->getChunkSize());
2113   Record.AddSourceLocation(C->getLParenLoc());
2114   Record.AddSourceLocation(C->getDistScheduleKindLoc());
2115   Record.AddSourceLocation(C->getCommaLoc());
2116 }
2117 
2118 void OMPClauseWriter::VisitOMPDefaultmapClause(OMPDefaultmapClause *C) {
2119   Record.push_back(C->getDefaultmapKind());
2120   Record.push_back(C->getDefaultmapModifier());
2121   Record.AddSourceLocation(C->getLParenLoc());
2122   Record.AddSourceLocation(C->getDefaultmapModifierLoc());
2123   Record.AddSourceLocation(C->getDefaultmapKindLoc());
2124 }
2125 
2126 void OMPClauseWriter::VisitOMPToClause(OMPToClause *C) {
2127   Record.push_back(C->varlist_size());
2128   Record.push_back(C->getUniqueDeclarationsNum());
2129   Record.push_back(C->getTotalComponentListNum());
2130   Record.push_back(C->getTotalComponentsNum());
2131   Record.AddSourceLocation(C->getLParenLoc());
2132   for (auto *E : C->varlists())
2133     Record.AddStmt(E);
2134   for (auto *D : C->all_decls())
2135     Record.AddDeclRef(D);
2136   for (auto N : C->all_num_lists())
2137     Record.push_back(N);
2138   for (auto N : C->all_lists_sizes())
2139     Record.push_back(N);
2140   for (auto &M : C->all_components()) {
2141     Record.AddStmt(M.getAssociatedExpression());
2142     Record.AddDeclRef(M.getAssociatedDeclaration());
2143   }
2144 }
2145 
2146 void OMPClauseWriter::VisitOMPFromClause(OMPFromClause *C) {
2147   Record.push_back(C->varlist_size());
2148   Record.push_back(C->getUniqueDeclarationsNum());
2149   Record.push_back(C->getTotalComponentListNum());
2150   Record.push_back(C->getTotalComponentsNum());
2151   Record.AddSourceLocation(C->getLParenLoc());
2152   for (auto *E : C->varlists())
2153     Record.AddStmt(E);
2154   for (auto *D : C->all_decls())
2155     Record.AddDeclRef(D);
2156   for (auto N : C->all_num_lists())
2157     Record.push_back(N);
2158   for (auto N : C->all_lists_sizes())
2159     Record.push_back(N);
2160   for (auto &M : C->all_components()) {
2161     Record.AddStmt(M.getAssociatedExpression());
2162     Record.AddDeclRef(M.getAssociatedDeclaration());
2163   }
2164 }
2165 
2166 void OMPClauseWriter::VisitOMPUseDevicePtrClause(OMPUseDevicePtrClause *C) {
2167   Record.push_back(C->varlist_size());
2168   Record.push_back(C->getUniqueDeclarationsNum());
2169   Record.push_back(C->getTotalComponentListNum());
2170   Record.push_back(C->getTotalComponentsNum());
2171   Record.AddSourceLocation(C->getLParenLoc());
2172   for (auto *E : C->varlists())
2173     Record.AddStmt(E);
2174   for (auto *VE : C->private_copies())
2175     Record.AddStmt(VE);
2176   for (auto *VE : C->inits())
2177     Record.AddStmt(VE);
2178   for (auto *D : C->all_decls())
2179     Record.AddDeclRef(D);
2180   for (auto N : C->all_num_lists())
2181     Record.push_back(N);
2182   for (auto N : C->all_lists_sizes())
2183     Record.push_back(N);
2184   for (auto &M : C->all_components()) {
2185     Record.AddStmt(M.getAssociatedExpression());
2186     Record.AddDeclRef(M.getAssociatedDeclaration());
2187   }
2188 }
2189 
2190 void OMPClauseWriter::VisitOMPIsDevicePtrClause(OMPIsDevicePtrClause *C) {
2191   Record.push_back(C->varlist_size());
2192   Record.push_back(C->getUniqueDeclarationsNum());
2193   Record.push_back(C->getTotalComponentListNum());
2194   Record.push_back(C->getTotalComponentsNum());
2195   Record.AddSourceLocation(C->getLParenLoc());
2196   for (auto *E : C->varlists())
2197     Record.AddStmt(E);
2198   for (auto *D : C->all_decls())
2199     Record.AddDeclRef(D);
2200   for (auto N : C->all_num_lists())
2201     Record.push_back(N);
2202   for (auto N : C->all_lists_sizes())
2203     Record.push_back(N);
2204   for (auto &M : C->all_components()) {
2205     Record.AddStmt(M.getAssociatedExpression());
2206     Record.AddDeclRef(M.getAssociatedDeclaration());
2207   }
2208 }
2209 
2210 //===----------------------------------------------------------------------===//
2211 // OpenMP Directives.
2212 //===----------------------------------------------------------------------===//
2213 void ASTStmtWriter::VisitOMPExecutableDirective(OMPExecutableDirective *E) {
2214   Record.AddSourceLocation(E->getLocStart());
2215   Record.AddSourceLocation(E->getLocEnd());
2216   OMPClauseWriter ClauseWriter(Record);
2217   for (unsigned i = 0; i < E->getNumClauses(); ++i) {
2218     ClauseWriter.writeClause(E->getClause(i));
2219   }
2220   if (E->hasAssociatedStmt())
2221     Record.AddStmt(E->getAssociatedStmt());
2222 }
2223 
2224 void ASTStmtWriter::VisitOMPLoopDirective(OMPLoopDirective *D) {
2225   VisitStmt(D);
2226   Record.push_back(D->getNumClauses());
2227   Record.push_back(D->getCollapsedNumber());
2228   VisitOMPExecutableDirective(D);
2229   Record.AddStmt(D->getIterationVariable());
2230   Record.AddStmt(D->getLastIteration());
2231   Record.AddStmt(D->getCalcLastIteration());
2232   Record.AddStmt(D->getPreCond());
2233   Record.AddStmt(D->getCond());
2234   Record.AddStmt(D->getInit());
2235   Record.AddStmt(D->getInc());
2236   Record.AddStmt(D->getPreInits());
2237   if (isOpenMPWorksharingDirective(D->getDirectiveKind()) ||
2238       isOpenMPTaskLoopDirective(D->getDirectiveKind()) ||
2239       isOpenMPDistributeDirective(D->getDirectiveKind())) {
2240     Record.AddStmt(D->getIsLastIterVariable());
2241     Record.AddStmt(D->getLowerBoundVariable());
2242     Record.AddStmt(D->getUpperBoundVariable());
2243     Record.AddStmt(D->getStrideVariable());
2244     Record.AddStmt(D->getEnsureUpperBound());
2245     Record.AddStmt(D->getNextLowerBound());
2246     Record.AddStmt(D->getNextUpperBound());
2247     Record.AddStmt(D->getNumIterations());
2248   }
2249   if (isOpenMPLoopBoundSharingDirective(D->getDirectiveKind())) {
2250     Record.AddStmt(D->getPrevLowerBoundVariable());
2251     Record.AddStmt(D->getPrevUpperBoundVariable());
2252   }
2253   for (auto I : D->counters()) {
2254     Record.AddStmt(I);
2255   }
2256   for (auto I : D->private_counters()) {
2257     Record.AddStmt(I);
2258   }
2259   for (auto I : D->inits()) {
2260     Record.AddStmt(I);
2261   }
2262   for (auto I : D->updates()) {
2263     Record.AddStmt(I);
2264   }
2265   for (auto I : D->finals()) {
2266     Record.AddStmt(I);
2267   }
2268 }
2269 
2270 void ASTStmtWriter::VisitOMPParallelDirective(OMPParallelDirective *D) {
2271   VisitStmt(D);
2272   Record.push_back(D->getNumClauses());
2273   VisitOMPExecutableDirective(D);
2274   Record.push_back(D->hasCancel() ? 1 : 0);
2275   Code = serialization::STMT_OMP_PARALLEL_DIRECTIVE;
2276 }
2277 
2278 void ASTStmtWriter::VisitOMPSimdDirective(OMPSimdDirective *D) {
2279   VisitOMPLoopDirective(D);
2280   Code = serialization::STMT_OMP_SIMD_DIRECTIVE;
2281 }
2282 
2283 void ASTStmtWriter::VisitOMPForDirective(OMPForDirective *D) {
2284   VisitOMPLoopDirective(D);
2285   Record.push_back(D->hasCancel() ? 1 : 0);
2286   Code = serialization::STMT_OMP_FOR_DIRECTIVE;
2287 }
2288 
2289 void ASTStmtWriter::VisitOMPForSimdDirective(OMPForSimdDirective *D) {
2290   VisitOMPLoopDirective(D);
2291   Code = serialization::STMT_OMP_FOR_SIMD_DIRECTIVE;
2292 }
2293 
2294 void ASTStmtWriter::VisitOMPSectionsDirective(OMPSectionsDirective *D) {
2295   VisitStmt(D);
2296   Record.push_back(D->getNumClauses());
2297   VisitOMPExecutableDirective(D);
2298   Record.push_back(D->hasCancel() ? 1 : 0);
2299   Code = serialization::STMT_OMP_SECTIONS_DIRECTIVE;
2300 }
2301 
2302 void ASTStmtWriter::VisitOMPSectionDirective(OMPSectionDirective *D) {
2303   VisitStmt(D);
2304   VisitOMPExecutableDirective(D);
2305   Record.push_back(D->hasCancel() ? 1 : 0);
2306   Code = serialization::STMT_OMP_SECTION_DIRECTIVE;
2307 }
2308 
2309 void ASTStmtWriter::VisitOMPSingleDirective(OMPSingleDirective *D) {
2310   VisitStmt(D);
2311   Record.push_back(D->getNumClauses());
2312   VisitOMPExecutableDirective(D);
2313   Code = serialization::STMT_OMP_SINGLE_DIRECTIVE;
2314 }
2315 
2316 void ASTStmtWriter::VisitOMPMasterDirective(OMPMasterDirective *D) {
2317   VisitStmt(D);
2318   VisitOMPExecutableDirective(D);
2319   Code = serialization::STMT_OMP_MASTER_DIRECTIVE;
2320 }
2321 
2322 void ASTStmtWriter::VisitOMPCriticalDirective(OMPCriticalDirective *D) {
2323   VisitStmt(D);
2324   Record.push_back(D->getNumClauses());
2325   VisitOMPExecutableDirective(D);
2326   Record.AddDeclarationNameInfo(D->getDirectiveName());
2327   Code = serialization::STMT_OMP_CRITICAL_DIRECTIVE;
2328 }
2329 
2330 void ASTStmtWriter::VisitOMPParallelForDirective(OMPParallelForDirective *D) {
2331   VisitOMPLoopDirective(D);
2332   Record.push_back(D->hasCancel() ? 1 : 0);
2333   Code = serialization::STMT_OMP_PARALLEL_FOR_DIRECTIVE;
2334 }
2335 
2336 void ASTStmtWriter::VisitOMPParallelForSimdDirective(
2337     OMPParallelForSimdDirective *D) {
2338   VisitOMPLoopDirective(D);
2339   Code = serialization::STMT_OMP_PARALLEL_FOR_SIMD_DIRECTIVE;
2340 }
2341 
2342 void ASTStmtWriter::VisitOMPParallelSectionsDirective(
2343     OMPParallelSectionsDirective *D) {
2344   VisitStmt(D);
2345   Record.push_back(D->getNumClauses());
2346   VisitOMPExecutableDirective(D);
2347   Record.push_back(D->hasCancel() ? 1 : 0);
2348   Code = serialization::STMT_OMP_PARALLEL_SECTIONS_DIRECTIVE;
2349 }
2350 
2351 void ASTStmtWriter::VisitOMPTaskDirective(OMPTaskDirective *D) {
2352   VisitStmt(D);
2353   Record.push_back(D->getNumClauses());
2354   VisitOMPExecutableDirective(D);
2355   Record.push_back(D->hasCancel() ? 1 : 0);
2356   Code = serialization::STMT_OMP_TASK_DIRECTIVE;
2357 }
2358 
2359 void ASTStmtWriter::VisitOMPAtomicDirective(OMPAtomicDirective *D) {
2360   VisitStmt(D);
2361   Record.push_back(D->getNumClauses());
2362   VisitOMPExecutableDirective(D);
2363   Record.AddStmt(D->getX());
2364   Record.AddStmt(D->getV());
2365   Record.AddStmt(D->getExpr());
2366   Record.AddStmt(D->getUpdateExpr());
2367   Record.push_back(D->isXLHSInRHSPart() ? 1 : 0);
2368   Record.push_back(D->isPostfixUpdate() ? 1 : 0);
2369   Code = serialization::STMT_OMP_ATOMIC_DIRECTIVE;
2370 }
2371 
2372 void ASTStmtWriter::VisitOMPTargetDirective(OMPTargetDirective *D) {
2373   VisitStmt(D);
2374   Record.push_back(D->getNumClauses());
2375   VisitOMPExecutableDirective(D);
2376   Code = serialization::STMT_OMP_TARGET_DIRECTIVE;
2377 }
2378 
2379 void ASTStmtWriter::VisitOMPTargetDataDirective(OMPTargetDataDirective *D) {
2380   VisitStmt(D);
2381   Record.push_back(D->getNumClauses());
2382   VisitOMPExecutableDirective(D);
2383   Code = serialization::STMT_OMP_TARGET_DATA_DIRECTIVE;
2384 }
2385 
2386 void ASTStmtWriter::VisitOMPTargetEnterDataDirective(
2387     OMPTargetEnterDataDirective *D) {
2388   VisitStmt(D);
2389   Record.push_back(D->getNumClauses());
2390   VisitOMPExecutableDirective(D);
2391   Code = serialization::STMT_OMP_TARGET_ENTER_DATA_DIRECTIVE;
2392 }
2393 
2394 void ASTStmtWriter::VisitOMPTargetExitDataDirective(
2395     OMPTargetExitDataDirective *D) {
2396   VisitStmt(D);
2397   Record.push_back(D->getNumClauses());
2398   VisitOMPExecutableDirective(D);
2399   Code = serialization::STMT_OMP_TARGET_EXIT_DATA_DIRECTIVE;
2400 }
2401 
2402 void ASTStmtWriter::VisitOMPTargetParallelDirective(
2403     OMPTargetParallelDirective *D) {
2404   VisitStmt(D);
2405   Record.push_back(D->getNumClauses());
2406   VisitOMPExecutableDirective(D);
2407   Code = serialization::STMT_OMP_TARGET_PARALLEL_DIRECTIVE;
2408 }
2409 
2410 void ASTStmtWriter::VisitOMPTargetParallelForDirective(
2411     OMPTargetParallelForDirective *D) {
2412   VisitOMPLoopDirective(D);
2413   Record.push_back(D->hasCancel() ? 1 : 0);
2414   Code = serialization::STMT_OMP_TARGET_PARALLEL_FOR_DIRECTIVE;
2415 }
2416 
2417 void ASTStmtWriter::VisitOMPTaskyieldDirective(OMPTaskyieldDirective *D) {
2418   VisitStmt(D);
2419   VisitOMPExecutableDirective(D);
2420   Code = serialization::STMT_OMP_TASKYIELD_DIRECTIVE;
2421 }
2422 
2423 void ASTStmtWriter::VisitOMPBarrierDirective(OMPBarrierDirective *D) {
2424   VisitStmt(D);
2425   VisitOMPExecutableDirective(D);
2426   Code = serialization::STMT_OMP_BARRIER_DIRECTIVE;
2427 }
2428 
2429 void ASTStmtWriter::VisitOMPTaskwaitDirective(OMPTaskwaitDirective *D) {
2430   VisitStmt(D);
2431   VisitOMPExecutableDirective(D);
2432   Code = serialization::STMT_OMP_TASKWAIT_DIRECTIVE;
2433 }
2434 
2435 void ASTStmtWriter::VisitOMPTaskgroupDirective(OMPTaskgroupDirective *D) {
2436   VisitStmt(D);
2437   VisitOMPExecutableDirective(D);
2438   Code = serialization::STMT_OMP_TASKGROUP_DIRECTIVE;
2439 }
2440 
2441 void ASTStmtWriter::VisitOMPFlushDirective(OMPFlushDirective *D) {
2442   VisitStmt(D);
2443   Record.push_back(D->getNumClauses());
2444   VisitOMPExecutableDirective(D);
2445   Code = serialization::STMT_OMP_FLUSH_DIRECTIVE;
2446 }
2447 
2448 void ASTStmtWriter::VisitOMPOrderedDirective(OMPOrderedDirective *D) {
2449   VisitStmt(D);
2450   Record.push_back(D->getNumClauses());
2451   VisitOMPExecutableDirective(D);
2452   Code = serialization::STMT_OMP_ORDERED_DIRECTIVE;
2453 }
2454 
2455 void ASTStmtWriter::VisitOMPTeamsDirective(OMPTeamsDirective *D) {
2456   VisitStmt(D);
2457   Record.push_back(D->getNumClauses());
2458   VisitOMPExecutableDirective(D);
2459   Code = serialization::STMT_OMP_TEAMS_DIRECTIVE;
2460 }
2461 
2462 void ASTStmtWriter::VisitOMPCancellationPointDirective(
2463     OMPCancellationPointDirective *D) {
2464   VisitStmt(D);
2465   VisitOMPExecutableDirective(D);
2466   Record.push_back(D->getCancelRegion());
2467   Code = serialization::STMT_OMP_CANCELLATION_POINT_DIRECTIVE;
2468 }
2469 
2470 void ASTStmtWriter::VisitOMPCancelDirective(OMPCancelDirective *D) {
2471   VisitStmt(D);
2472   Record.push_back(D->getNumClauses());
2473   VisitOMPExecutableDirective(D);
2474   Record.push_back(D->getCancelRegion());
2475   Code = serialization::STMT_OMP_CANCEL_DIRECTIVE;
2476 }
2477 
2478 void ASTStmtWriter::VisitOMPTaskLoopDirective(OMPTaskLoopDirective *D) {
2479   VisitOMPLoopDirective(D);
2480   Code = serialization::STMT_OMP_TASKLOOP_DIRECTIVE;
2481 }
2482 
2483 void ASTStmtWriter::VisitOMPTaskLoopSimdDirective(OMPTaskLoopSimdDirective *D) {
2484   VisitOMPLoopDirective(D);
2485   Code = serialization::STMT_OMP_TASKLOOP_SIMD_DIRECTIVE;
2486 }
2487 
2488 void ASTStmtWriter::VisitOMPDistributeDirective(OMPDistributeDirective *D) {
2489   VisitOMPLoopDirective(D);
2490   Code = serialization::STMT_OMP_DISTRIBUTE_DIRECTIVE;
2491 }
2492 
2493 void ASTStmtWriter::VisitOMPTargetUpdateDirective(OMPTargetUpdateDirective *D) {
2494   VisitStmt(D);
2495   Record.push_back(D->getNumClauses());
2496   VisitOMPExecutableDirective(D);
2497   Code = serialization::STMT_OMP_TARGET_UPDATE_DIRECTIVE;
2498 }
2499 
2500 void ASTStmtWriter::VisitOMPDistributeParallelForDirective(
2501     OMPDistributeParallelForDirective *D) {
2502   VisitOMPLoopDirective(D);
2503   Code = serialization::STMT_OMP_DISTRIBUTE_PARALLEL_FOR_DIRECTIVE;
2504 }
2505 
2506 void ASTStmtWriter::VisitOMPDistributeParallelForSimdDirective(
2507     OMPDistributeParallelForSimdDirective *D) {
2508   VisitOMPLoopDirective(D);
2509   Code = serialization::STMT_OMP_DISTRIBUTE_PARALLEL_FOR_SIMD_DIRECTIVE;
2510 }
2511 
2512 void ASTStmtWriter::VisitOMPDistributeSimdDirective(
2513     OMPDistributeSimdDirective *D) {
2514   VisitOMPLoopDirective(D);
2515   Code = serialization::STMT_OMP_DISTRIBUTE_SIMD_DIRECTIVE;
2516 }
2517 
2518 void ASTStmtWriter::VisitOMPTargetParallelForSimdDirective(
2519     OMPTargetParallelForSimdDirective *D) {
2520   VisitOMPLoopDirective(D);
2521   Code = serialization::STMT_OMP_TARGET_PARALLEL_FOR_SIMD_DIRECTIVE;
2522 }
2523 
2524 void ASTStmtWriter::VisitOMPTargetSimdDirective(OMPTargetSimdDirective *D) {
2525   VisitOMPLoopDirective(D);
2526   Code = serialization::STMT_OMP_TARGET_SIMD_DIRECTIVE;
2527 }
2528 
2529 void ASTStmtWriter::VisitOMPTeamsDistributeDirective(
2530     OMPTeamsDistributeDirective *D) {
2531   VisitOMPLoopDirective(D);
2532   Code = serialization::STMT_OMP_TEAMS_DISTRIBUTE_DIRECTIVE;
2533 }
2534 
2535 void ASTStmtWriter::VisitOMPTeamsDistributeSimdDirective(
2536     OMPTeamsDistributeSimdDirective *D) {
2537   VisitOMPLoopDirective(D);
2538   Code = serialization::STMT_OMP_TEAMS_DISTRIBUTE_SIMD_DIRECTIVE;
2539 }
2540 
2541 void ASTStmtWriter::VisitOMPTeamsDistributeParallelForSimdDirective(
2542     OMPTeamsDistributeParallelForSimdDirective *D) {
2543   VisitOMPLoopDirective(D);
2544   Code = serialization::STMT_OMP_TEAMS_DISTRIBUTE_PARALLEL_FOR_SIMD_DIRECTIVE;
2545 }
2546 
2547 void ASTStmtWriter::VisitOMPTeamsDistributeParallelForDirective(
2548     OMPTeamsDistributeParallelForDirective *D) {
2549   VisitOMPLoopDirective(D);
2550   Code = serialization::STMT_OMP_TEAMS_DISTRIBUTE_PARALLEL_FOR_DIRECTIVE;
2551 }
2552 
2553 //===----------------------------------------------------------------------===//
2554 // ASTWriter Implementation
2555 //===----------------------------------------------------------------------===//
2556 
2557 unsigned ASTWriter::RecordSwitchCaseID(SwitchCase *S) {
2558   assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
2559          "SwitchCase recorded twice");
2560   unsigned NextID = SwitchCaseIDs.size();
2561   SwitchCaseIDs[S] = NextID;
2562   return NextID;
2563 }
2564 
2565 unsigned ASTWriter::getSwitchCaseID(SwitchCase *S) {
2566   assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
2567          "SwitchCase hasn't been seen yet");
2568   return SwitchCaseIDs[S];
2569 }
2570 
2571 void ASTWriter::ClearSwitchCaseIDs() {
2572   SwitchCaseIDs.clear();
2573 }
2574 
2575 /// \brief Write the given substatement or subexpression to the
2576 /// bitstream.
2577 void ASTWriter::WriteSubStmt(Stmt *S) {
2578   RecordData Record;
2579   ASTStmtWriter Writer(*this, Record);
2580   ++NumStatements;
2581 
2582   if (!S) {
2583     Stream.EmitRecord(serialization::STMT_NULL_PTR, Record);
2584     return;
2585   }
2586 
2587   llvm::DenseMap<Stmt *, uint64_t>::iterator I = SubStmtEntries.find(S);
2588   if (I != SubStmtEntries.end()) {
2589     Record.push_back(I->second);
2590     Stream.EmitRecord(serialization::STMT_REF_PTR, Record);
2591     return;
2592   }
2593 
2594 #ifndef NDEBUG
2595   assert(!ParentStmts.count(S) && "There is a Stmt cycle!");
2596 
2597   struct ParentStmtInserterRAII {
2598     Stmt *S;
2599     llvm::DenseSet<Stmt *> &ParentStmts;
2600 
2601     ParentStmtInserterRAII(Stmt *S, llvm::DenseSet<Stmt *> &ParentStmts)
2602       : S(S), ParentStmts(ParentStmts) {
2603       ParentStmts.insert(S);
2604     }
2605     ~ParentStmtInserterRAII() {
2606       ParentStmts.erase(S);
2607     }
2608   };
2609 
2610   ParentStmtInserterRAII ParentStmtInserter(S, ParentStmts);
2611 #endif
2612 
2613   Writer.Visit(S);
2614 
2615   uint64_t Offset = Writer.Emit();
2616   SubStmtEntries[S] = Offset;
2617 }
2618 
2619 /// \brief Flush all of the statements that have been added to the
2620 /// queue via AddStmt().
2621 void ASTRecordWriter::FlushStmts() {
2622   // We expect to be the only consumer of the two temporary statement maps,
2623   // assert that they are empty.
2624   assert(Writer->SubStmtEntries.empty() && "unexpected entries in sub-stmt map");
2625   assert(Writer->ParentStmts.empty() && "unexpected entries in parent stmt map");
2626 
2627   for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
2628     Writer->WriteSubStmt(StmtsToEmit[I]);
2629 
2630     assert(N == StmtsToEmit.size() && "record modified while being written!");
2631 
2632     // Note that we are at the end of a full expression. Any
2633     // expression records that follow this one are part of a different
2634     // expression.
2635     Writer->Stream.EmitRecord(serialization::STMT_STOP, ArrayRef<uint32_t>());
2636 
2637     Writer->SubStmtEntries.clear();
2638     Writer->ParentStmts.clear();
2639   }
2640 
2641   StmtsToEmit.clear();
2642 }
2643 
2644 void ASTRecordWriter::FlushSubStmts() {
2645   // For a nested statement, write out the substatements in reverse order (so
2646   // that a simple stack machine can be used when loading), and don't emit a
2647   // STMT_STOP after each one.
2648   for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
2649     Writer->WriteSubStmt(StmtsToEmit[N - I - 1]);
2650     assert(N == StmtsToEmit.size() && "record modified while being written!");
2651   }
2652 
2653   StmtsToEmit.clear();
2654 }
2655