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