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