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