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