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