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