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