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