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