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