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