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