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