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