ASTWriterStmt.cpp revision cdd4b78583120222b82148626119b3e80ae1d291
1//===--- ASTWriterStmt.cpp - Statement and Expression Serialization -------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// \brief Implements serialization for Statements and Expressions.
12///
13//===----------------------------------------------------------------------===//
14
15#include "clang/Serialization/ASTWriter.h"
16#include "clang/AST/ASTContext.h"
17#include "clang/AST/DeclCXX.h"
18#include "clang/AST/DeclObjC.h"
19#include "clang/AST/DeclTemplate.h"
20#include "clang/AST/StmtVisitor.h"
21#include "clang/Lex/Token.h"
22#include "llvm/Bitcode/BitstreamWriter.h"
23using namespace clang;
24
25//===----------------------------------------------------------------------===//
26// Statement/expression serialization
27//===----------------------------------------------------------------------===//
28
29namespace clang {
30
31  class ASTStmtWriter : public StmtVisitor<ASTStmtWriter, void> {
32    friend class OMPClauseWriter;
33    ASTWriter &Writer;
34    ASTWriter::RecordData &Record;
35
36  public:
37    serialization::StmtCode Code;
38    unsigned AbbrevToUse;
39
40    ASTStmtWriter(ASTWriter &Writer, ASTWriter::RecordData &Record)
41      : Writer(Writer), Record(Record) { }
42
43    void AddTemplateKWAndArgsInfo(const ASTTemplateKWAndArgsInfo &Args);
44
45    void VisitStmt(Stmt *S);
46#define STMT(Type, Base) \
47    void Visit##Type(Type *);
48#include "clang/AST/StmtNodes.inc"
49  };
50}
51
52void ASTStmtWriter::
53AddTemplateKWAndArgsInfo(const ASTTemplateKWAndArgsInfo &Args) {
54  Writer.AddSourceLocation(Args.getTemplateKeywordLoc(), Record);
55  Writer.AddSourceLocation(Args.LAngleLoc, Record);
56  Writer.AddSourceLocation(Args.RAngleLoc, Record);
57  for (unsigned i=0; i != Args.NumTemplateArgs; ++i)
58    Writer.AddTemplateArgumentLoc(Args.getTemplateArgs()[i], Record);
59}
60
61void ASTStmtWriter::VisitStmt(Stmt *S) {
62}
63
64void ASTStmtWriter::VisitNullStmt(NullStmt *S) {
65  VisitStmt(S);
66  Writer.AddSourceLocation(S->getSemiLoc(), Record);
67  Record.push_back(S->HasLeadingEmptyMacro);
68  Code = serialization::STMT_NULL;
69}
70
71void ASTStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
72  VisitStmt(S);
73  Record.push_back(S->size());
74  for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
75       CS != CSEnd; ++CS)
76    Writer.AddStmt(*CS);
77  Writer.AddSourceLocation(S->getLBracLoc(), Record);
78  Writer.AddSourceLocation(S->getRBracLoc(), Record);
79  Code = serialization::STMT_COMPOUND;
80}
81
82void ASTStmtWriter::VisitSwitchCase(SwitchCase *S) {
83  VisitStmt(S);
84  Record.push_back(Writer.getSwitchCaseID(S));
85  Writer.AddSourceLocation(S->getKeywordLoc(), Record);
86  Writer.AddSourceLocation(S->getColonLoc(), Record);
87}
88
89void ASTStmtWriter::VisitCaseStmt(CaseStmt *S) {
90  VisitSwitchCase(S);
91  Writer.AddStmt(S->getLHS());
92  Writer.AddStmt(S->getRHS());
93  Writer.AddStmt(S->getSubStmt());
94  Writer.AddSourceLocation(S->getEllipsisLoc(), Record);
95  Code = serialization::STMT_CASE;
96}
97
98void ASTStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
99  VisitSwitchCase(S);
100  Writer.AddStmt(S->getSubStmt());
101  Code = serialization::STMT_DEFAULT;
102}
103
104void ASTStmtWriter::VisitLabelStmt(LabelStmt *S) {
105  VisitStmt(S);
106  Writer.AddDeclRef(S->getDecl(), Record);
107  Writer.AddStmt(S->getSubStmt());
108  Writer.AddSourceLocation(S->getIdentLoc(), Record);
109  Code = serialization::STMT_LABEL;
110}
111
112void ASTStmtWriter::VisitAttributedStmt(AttributedStmt *S) {
113  VisitStmt(S);
114  Record.push_back(S->getAttrs().size());
115  Writer.WriteAttributes(S->getAttrs(), Record);
116  Writer.AddStmt(S->getSubStmt());
117  Writer.AddSourceLocation(S->getAttrLoc(), Record);
118  Code = serialization::STMT_ATTRIBUTED;
119}
120
121void ASTStmtWriter::VisitIfStmt(IfStmt *S) {
122  VisitStmt(S);
123  Writer.AddDeclRef(S->getConditionVariable(), Record);
124  Writer.AddStmt(S->getCond());
125  Writer.AddStmt(S->getThen());
126  Writer.AddStmt(S->getElse());
127  Writer.AddSourceLocation(S->getIfLoc(), Record);
128  Writer.AddSourceLocation(S->getElseLoc(), Record);
129  Code = serialization::STMT_IF;
130}
131
132void ASTStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
133  VisitStmt(S);
134  Writer.AddDeclRef(S->getConditionVariable(), Record);
135  Writer.AddStmt(S->getCond());
136  Writer.AddStmt(S->getBody());
137  Writer.AddSourceLocation(S->getSwitchLoc(), Record);
138  Record.push_back(S->isAllEnumCasesCovered());
139  for (SwitchCase *SC = S->getSwitchCaseList(); SC;
140       SC = SC->getNextSwitchCase())
141    Record.push_back(Writer.RecordSwitchCaseID(SC));
142  Code = serialization::STMT_SWITCH;
143}
144
145void ASTStmtWriter::VisitWhileStmt(WhileStmt *S) {
146  VisitStmt(S);
147  Writer.AddDeclRef(S->getConditionVariable(), Record);
148  Writer.AddStmt(S->getCond());
149  Writer.AddStmt(S->getBody());
150  Writer.AddSourceLocation(S->getWhileLoc(), Record);
151  Code = serialization::STMT_WHILE;
152}
153
154void ASTStmtWriter::VisitDoStmt(DoStmt *S) {
155  VisitStmt(S);
156  Writer.AddStmt(S->getCond());
157  Writer.AddStmt(S->getBody());
158  Writer.AddSourceLocation(S->getDoLoc(), Record);
159  Writer.AddSourceLocation(S->getWhileLoc(), Record);
160  Writer.AddSourceLocation(S->getRParenLoc(), Record);
161  Code = serialization::STMT_DO;
162}
163
164void ASTStmtWriter::VisitForStmt(ForStmt *S) {
165  VisitStmt(S);
166  Writer.AddStmt(S->getInit());
167  Writer.AddStmt(S->getCond());
168  Writer.AddDeclRef(S->getConditionVariable(), Record);
169  Writer.AddStmt(S->getInc());
170  Writer.AddStmt(S->getBody());
171  Writer.AddSourceLocation(S->getForLoc(), Record);
172  Writer.AddSourceLocation(S->getLParenLoc(), Record);
173  Writer.AddSourceLocation(S->getRParenLoc(), Record);
174  Code = serialization::STMT_FOR;
175}
176
177void ASTStmtWriter::VisitGotoStmt(GotoStmt *S) {
178  VisitStmt(S);
179  Writer.AddDeclRef(S->getLabel(), Record);
180  Writer.AddSourceLocation(S->getGotoLoc(), Record);
181  Writer.AddSourceLocation(S->getLabelLoc(), Record);
182  Code = serialization::STMT_GOTO;
183}
184
185void ASTStmtWriter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
186  VisitStmt(S);
187  Writer.AddSourceLocation(S->getGotoLoc(), Record);
188  Writer.AddSourceLocation(S->getStarLoc(), Record);
189  Writer.AddStmt(S->getTarget());
190  Code = serialization::STMT_INDIRECT_GOTO;
191}
192
193void ASTStmtWriter::VisitContinueStmt(ContinueStmt *S) {
194  VisitStmt(S);
195  Writer.AddSourceLocation(S->getContinueLoc(), Record);
196  Code = serialization::STMT_CONTINUE;
197}
198
199void ASTStmtWriter::VisitBreakStmt(BreakStmt *S) {
200  VisitStmt(S);
201  Writer.AddSourceLocation(S->getBreakLoc(), Record);
202  Code = serialization::STMT_BREAK;
203}
204
205void ASTStmtWriter::VisitReturnStmt(ReturnStmt *S) {
206  VisitStmt(S);
207  Writer.AddStmt(S->getRetValue());
208  Writer.AddSourceLocation(S->getReturnLoc(), Record);
209  Writer.AddDeclRef(S->getNRVOCandidate(), Record);
210  Code = serialization::STMT_RETURN;
211}
212
213void ASTStmtWriter::VisitDeclStmt(DeclStmt *S) {
214  VisitStmt(S);
215  Writer.AddSourceLocation(S->getStartLoc(), Record);
216  Writer.AddSourceLocation(S->getEndLoc(), Record);
217  DeclGroupRef DG = S->getDeclGroup();
218  for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D)
219    Writer.AddDeclRef(*D, Record);
220  Code = serialization::STMT_DECL;
221}
222
223void ASTStmtWriter::VisitAsmStmt(AsmStmt *S) {
224  VisitStmt(S);
225  Record.push_back(S->getNumOutputs());
226  Record.push_back(S->getNumInputs());
227  Record.push_back(S->getNumClobbers());
228  Writer.AddSourceLocation(S->getAsmLoc(), Record);
229  Record.push_back(S->isVolatile());
230  Record.push_back(S->isSimple());
231}
232
233void ASTStmtWriter::VisitGCCAsmStmt(GCCAsmStmt *S) {
234  VisitAsmStmt(S);
235  Writer.AddSourceLocation(S->getRParenLoc(), Record);
236  Writer.AddStmt(S->getAsmString());
237
238  // Outputs
239  for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
240    Writer.AddIdentifierRef(S->getOutputIdentifier(I), Record);
241    Writer.AddStmt(S->getOutputConstraintLiteral(I));
242    Writer.AddStmt(S->getOutputExpr(I));
243  }
244
245  // Inputs
246  for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
247    Writer.AddIdentifierRef(S->getInputIdentifier(I), Record);
248    Writer.AddStmt(S->getInputConstraintLiteral(I));
249    Writer.AddStmt(S->getInputExpr(I));
250  }
251
252  // Clobbers
253  for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
254    Writer.AddStmt(S->getClobberStringLiteral(I));
255
256  Code = serialization::STMT_GCCASM;
257}
258
259void ASTStmtWriter::VisitMSAsmStmt(MSAsmStmt *S) {
260  VisitAsmStmt(S);
261  Writer.AddSourceLocation(S->getLBraceLoc(), Record);
262  Writer.AddSourceLocation(S->getEndLoc(), Record);
263  Record.push_back(S->getNumAsmToks());
264  Writer.AddString(S->getAsmString(), Record);
265
266  // Tokens
267  for (unsigned I = 0, N = S->getNumAsmToks(); I != N; ++I) {
268    Writer.AddToken(S->getAsmToks()[I], Record);
269  }
270
271  // Clobbers
272  for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I) {
273    Writer.AddString(S->getClobber(I), Record);
274  }
275
276  // Outputs
277  for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
278    Writer.AddStmt(S->getOutputExpr(I));
279    Writer.AddString(S->getOutputConstraint(I), Record);
280  }
281
282  // Inputs
283  for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
284    Writer.AddStmt(S->getInputExpr(I));
285    Writer.AddString(S->getInputConstraint(I), Record);
286  }
287
288  Code = serialization::STMT_MSASM;
289}
290
291void ASTStmtWriter::VisitCapturedStmt(CapturedStmt *S) {
292  VisitStmt(S);
293  // NumCaptures
294  Record.push_back(std::distance(S->capture_begin(), S->capture_end()));
295
296  // CapturedDecl and captured region kind
297  Writer.AddDeclRef(S->getCapturedDecl(), Record);
298  Record.push_back(S->getCapturedRegionKind());
299
300  Writer.AddDeclRef(S->getCapturedRecordDecl(), Record);
301
302  // Capture inits
303  for (CapturedStmt::capture_init_iterator I = S->capture_init_begin(),
304                                           E = S->capture_init_end();
305       I != E; ++I)
306    Writer.AddStmt(*I);
307
308  // Body
309  Writer.AddStmt(S->getCapturedStmt());
310
311  // Captures
312  for (CapturedStmt::capture_iterator I = S->capture_begin(),
313                                      E = S->capture_end();
314       I != E; ++I) {
315    if (I->capturesThis())
316      Writer.AddDeclRef(0, Record);
317    else
318      Writer.AddDeclRef(I->getCapturedVar(), Record);
319    Record.push_back(I->getCaptureKind());
320    Writer.AddSourceLocation(I->getLocation(), Record);
321  }
322
323  Code = serialization::STMT_CAPTURED;
324}
325
326void ASTStmtWriter::VisitExpr(Expr *E) {
327  VisitStmt(E);
328  Writer.AddTypeRef(E->getType(), Record);
329  Record.push_back(E->isTypeDependent());
330  Record.push_back(E->isValueDependent());
331  Record.push_back(E->isInstantiationDependent());
332  Record.push_back(E->containsUnexpandedParameterPack());
333  Record.push_back(E->getValueKind());
334  Record.push_back(E->getObjectKind());
335}
336
337void ASTStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
338  VisitExpr(E);
339  Writer.AddSourceLocation(E->getLocation(), Record);
340  Record.push_back(E->getIdentType()); // FIXME: stable encoding
341  Code = serialization::EXPR_PREDEFINED;
342}
343
344void ASTStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
345  VisitExpr(E);
346
347  Record.push_back(E->hasQualifier());
348  Record.push_back(E->getDecl() != E->getFoundDecl());
349  Record.push_back(E->hasTemplateKWAndArgsInfo());
350  Record.push_back(E->hadMultipleCandidates());
351  Record.push_back(E->refersToEnclosingLocal());
352
353  if (E->hasTemplateKWAndArgsInfo()) {
354    unsigned NumTemplateArgs = E->getNumTemplateArgs();
355    Record.push_back(NumTemplateArgs);
356  }
357
358  DeclarationName::NameKind nk = (E->getDecl()->getDeclName().getNameKind());
359
360  if ((!E->hasTemplateKWAndArgsInfo()) && (!E->hasQualifier()) &&
361      (E->getDecl() == E->getFoundDecl()) &&
362      nk == DeclarationName::Identifier) {
363    AbbrevToUse = Writer.getDeclRefExprAbbrev();
364  }
365
366  if (E->hasQualifier())
367    Writer.AddNestedNameSpecifierLoc(E->getQualifierLoc(), Record);
368
369  if (E->getDecl() != E->getFoundDecl())
370    Writer.AddDeclRef(E->getFoundDecl(), Record);
371
372  if (E->hasTemplateKWAndArgsInfo())
373    AddTemplateKWAndArgsInfo(*E->getTemplateKWAndArgsInfo());
374
375  Writer.AddDeclRef(E->getDecl(), Record);
376  Writer.AddSourceLocation(E->getLocation(), Record);
377  Writer.AddDeclarationNameLoc(E->DNLoc, E->getDecl()->getDeclName(), Record);
378  Code = serialization::EXPR_DECL_REF;
379}
380
381void ASTStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
382  VisitExpr(E);
383  Writer.AddSourceLocation(E->getLocation(), Record);
384  Writer.AddAPInt(E->getValue(), Record);
385
386  if (E->getValue().getBitWidth() == 32) {
387    AbbrevToUse = Writer.getIntegerLiteralAbbrev();
388  }
389
390  Code = serialization::EXPR_INTEGER_LITERAL;
391}
392
393void ASTStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
394  VisitExpr(E);
395  Record.push_back(E->getRawSemantics());
396  Record.push_back(E->isExact());
397  Writer.AddAPFloat(E->getValue(), Record);
398  Writer.AddSourceLocation(E->getLocation(), Record);
399  Code = serialization::EXPR_FLOATING_LITERAL;
400}
401
402void ASTStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
403  VisitExpr(E);
404  Writer.AddStmt(E->getSubExpr());
405  Code = serialization::EXPR_IMAGINARY_LITERAL;
406}
407
408void ASTStmtWriter::VisitStringLiteral(StringLiteral *E) {
409  VisitExpr(E);
410  Record.push_back(E->getByteLength());
411  Record.push_back(E->getNumConcatenated());
412  Record.push_back(E->getKind());
413  Record.push_back(E->isPascal());
414  // FIXME: String data should be stored as a blob at the end of the
415  // StringLiteral. However, we can't do so now because we have no
416  // provision for coping with abbreviations when we're jumping around
417  // the AST file during deserialization.
418  Record.append(E->getBytes().begin(), E->getBytes().end());
419  for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
420    Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
421  Code = serialization::EXPR_STRING_LITERAL;
422}
423
424void ASTStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
425  VisitExpr(E);
426  Record.push_back(E->getValue());
427  Writer.AddSourceLocation(E->getLocation(), Record);
428  Record.push_back(E->getKind());
429
430  AbbrevToUse = Writer.getCharacterLiteralAbbrev();
431
432  Code = serialization::EXPR_CHARACTER_LITERAL;
433}
434
435void ASTStmtWriter::VisitParenExpr(ParenExpr *E) {
436  VisitExpr(E);
437  Writer.AddSourceLocation(E->getLParen(), Record);
438  Writer.AddSourceLocation(E->getRParen(), Record);
439  Writer.AddStmt(E->getSubExpr());
440  Code = serialization::EXPR_PAREN;
441}
442
443void ASTStmtWriter::VisitParenListExpr(ParenListExpr *E) {
444  VisitExpr(E);
445  Record.push_back(E->NumExprs);
446  for (unsigned i=0; i != E->NumExprs; ++i)
447    Writer.AddStmt(E->Exprs[i]);
448  Writer.AddSourceLocation(E->LParenLoc, Record);
449  Writer.AddSourceLocation(E->RParenLoc, Record);
450  Code = serialization::EXPR_PAREN_LIST;
451}
452
453void ASTStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
454  VisitExpr(E);
455  Writer.AddStmt(E->getSubExpr());
456  Record.push_back(E->getOpcode()); // FIXME: stable encoding
457  Writer.AddSourceLocation(E->getOperatorLoc(), Record);
458  Code = serialization::EXPR_UNARY_OPERATOR;
459}
460
461void ASTStmtWriter::VisitOffsetOfExpr(OffsetOfExpr *E) {
462  VisitExpr(E);
463  Record.push_back(E->getNumComponents());
464  Record.push_back(E->getNumExpressions());
465  Writer.AddSourceLocation(E->getOperatorLoc(), Record);
466  Writer.AddSourceLocation(E->getRParenLoc(), Record);
467  Writer.AddTypeSourceInfo(E->getTypeSourceInfo(), Record);
468  for (unsigned I = 0, N = E->getNumComponents(); I != N; ++I) {
469    const OffsetOfExpr::OffsetOfNode &ON = E->getComponent(I);
470    Record.push_back(ON.getKind()); // FIXME: Stable encoding
471    Writer.AddSourceLocation(ON.getSourceRange().getBegin(), Record);
472    Writer.AddSourceLocation(ON.getSourceRange().getEnd(), Record);
473    switch (ON.getKind()) {
474    case OffsetOfExpr::OffsetOfNode::Array:
475      Record.push_back(ON.getArrayExprIndex());
476      break;
477
478    case OffsetOfExpr::OffsetOfNode::Field:
479      Writer.AddDeclRef(ON.getField(), Record);
480      break;
481
482    case OffsetOfExpr::OffsetOfNode::Identifier:
483      Writer.AddIdentifierRef(ON.getFieldName(), Record);
484      break;
485
486    case OffsetOfExpr::OffsetOfNode::Base:
487      Writer.AddCXXBaseSpecifier(*ON.getBase(), Record);
488      break;
489    }
490  }
491  for (unsigned I = 0, N = E->getNumExpressions(); I != N; ++I)
492    Writer.AddStmt(E->getIndexExpr(I));
493  Code = serialization::EXPR_OFFSETOF;
494}
495
496void ASTStmtWriter::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
497  VisitExpr(E);
498  Record.push_back(E->getKind());
499  if (E->isArgumentType())
500    Writer.AddTypeSourceInfo(E->getArgumentTypeInfo(), Record);
501  else {
502    Record.push_back(0);
503    Writer.AddStmt(E->getArgumentExpr());
504  }
505  Writer.AddSourceLocation(E->getOperatorLoc(), Record);
506  Writer.AddSourceLocation(E->getRParenLoc(), Record);
507  Code = serialization::EXPR_SIZEOF_ALIGN_OF;
508}
509
510void ASTStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
511  VisitExpr(E);
512  Writer.AddStmt(E->getLHS());
513  Writer.AddStmt(E->getRHS());
514  Writer.AddSourceLocation(E->getRBracketLoc(), Record);
515  Code = serialization::EXPR_ARRAY_SUBSCRIPT;
516}
517
518void ASTStmtWriter::VisitCallExpr(CallExpr *E) {
519  VisitExpr(E);
520  Record.push_back(E->getNumArgs());
521  Writer.AddSourceLocation(E->getRParenLoc(), Record);
522  Writer.AddStmt(E->getCallee());
523  for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
524       Arg != ArgEnd; ++Arg)
525    Writer.AddStmt(*Arg);
526  Code = serialization::EXPR_CALL;
527}
528
529void ASTStmtWriter::VisitMemberExpr(MemberExpr *E) {
530  // Don't call VisitExpr, we'll write everything here.
531
532  Record.push_back(E->hasQualifier());
533  if (E->hasQualifier())
534    Writer.AddNestedNameSpecifierLoc(E->getQualifierLoc(), Record);
535
536  Record.push_back(E->HasTemplateKWAndArgsInfo);
537  if (E->HasTemplateKWAndArgsInfo) {
538    Writer.AddSourceLocation(E->getTemplateKeywordLoc(), Record);
539    unsigned NumTemplateArgs = E->getNumTemplateArgs();
540    Record.push_back(NumTemplateArgs);
541    Writer.AddSourceLocation(E->getLAngleLoc(), Record);
542    Writer.AddSourceLocation(E->getRAngleLoc(), Record);
543    for (unsigned i=0; i != NumTemplateArgs; ++i)
544      Writer.AddTemplateArgumentLoc(E->getTemplateArgs()[i], Record);
545  }
546
547  Record.push_back(E->hadMultipleCandidates());
548
549  DeclAccessPair FoundDecl = E->getFoundDecl();
550  Writer.AddDeclRef(FoundDecl.getDecl(), Record);
551  Record.push_back(FoundDecl.getAccess());
552
553  Writer.AddTypeRef(E->getType(), Record);
554  Record.push_back(E->getValueKind());
555  Record.push_back(E->getObjectKind());
556  Writer.AddStmt(E->getBase());
557  Writer.AddDeclRef(E->getMemberDecl(), Record);
558  Writer.AddSourceLocation(E->getMemberLoc(), Record);
559  Record.push_back(E->isArrow());
560  Writer.AddDeclarationNameLoc(E->MemberDNLoc,
561                               E->getMemberDecl()->getDeclName(), Record);
562  Code = serialization::EXPR_MEMBER;
563}
564
565void ASTStmtWriter::VisitObjCIsaExpr(ObjCIsaExpr *E) {
566  VisitExpr(E);
567  Writer.AddStmt(E->getBase());
568  Writer.AddSourceLocation(E->getIsaMemberLoc(), Record);
569  Writer.AddSourceLocation(E->getOpLoc(), Record);
570  Record.push_back(E->isArrow());
571  Code = serialization::EXPR_OBJC_ISA;
572}
573
574void ASTStmtWriter::
575VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
576  VisitExpr(E);
577  Writer.AddStmt(E->getSubExpr());
578  Record.push_back(E->shouldCopy());
579  Code = serialization::EXPR_OBJC_INDIRECT_COPY_RESTORE;
580}
581
582void ASTStmtWriter::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
583  VisitExplicitCastExpr(E);
584  Writer.AddSourceLocation(E->getLParenLoc(), Record);
585  Writer.AddSourceLocation(E->getBridgeKeywordLoc(), Record);
586  Record.push_back(E->getBridgeKind()); // FIXME: Stable encoding
587  Code = serialization::EXPR_OBJC_BRIDGED_CAST;
588}
589
590void ASTStmtWriter::VisitCastExpr(CastExpr *E) {
591  VisitExpr(E);
592  Record.push_back(E->path_size());
593  Writer.AddStmt(E->getSubExpr());
594  Record.push_back(E->getCastKind()); // FIXME: stable encoding
595
596  for (CastExpr::path_iterator
597         PI = E->path_begin(), PE = E->path_end(); PI != PE; ++PI)
598    Writer.AddCXXBaseSpecifier(**PI, Record);
599}
600
601void ASTStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
602  VisitExpr(E);
603  Writer.AddStmt(E->getLHS());
604  Writer.AddStmt(E->getRHS());
605  Record.push_back(E->getOpcode()); // FIXME: stable encoding
606  Writer.AddSourceLocation(E->getOperatorLoc(), Record);
607  Record.push_back(E->isFPContractable());
608  Code = serialization::EXPR_BINARY_OPERATOR;
609}
610
611void ASTStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
612  VisitBinaryOperator(E);
613  Writer.AddTypeRef(E->getComputationLHSType(), Record);
614  Writer.AddTypeRef(E->getComputationResultType(), Record);
615  Code = serialization::EXPR_COMPOUND_ASSIGN_OPERATOR;
616}
617
618void ASTStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
619  VisitExpr(E);
620  Writer.AddStmt(E->getCond());
621  Writer.AddStmt(E->getLHS());
622  Writer.AddStmt(E->getRHS());
623  Writer.AddSourceLocation(E->getQuestionLoc(), Record);
624  Writer.AddSourceLocation(E->getColonLoc(), Record);
625  Code = serialization::EXPR_CONDITIONAL_OPERATOR;
626}
627
628void
629ASTStmtWriter::VisitBinaryConditionalOperator(BinaryConditionalOperator *E) {
630  VisitExpr(E);
631  Writer.AddStmt(E->getOpaqueValue());
632  Writer.AddStmt(E->getCommon());
633  Writer.AddStmt(E->getCond());
634  Writer.AddStmt(E->getTrueExpr());
635  Writer.AddStmt(E->getFalseExpr());
636  Writer.AddSourceLocation(E->getQuestionLoc(), Record);
637  Writer.AddSourceLocation(E->getColonLoc(), Record);
638  Code = serialization::EXPR_BINARY_CONDITIONAL_OPERATOR;
639}
640
641void ASTStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
642  VisitCastExpr(E);
643  Code = serialization::EXPR_IMPLICIT_CAST;
644}
645
646void ASTStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
647  VisitCastExpr(E);
648  Writer.AddTypeSourceInfo(E->getTypeInfoAsWritten(), Record);
649}
650
651void ASTStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
652  VisitExplicitCastExpr(E);
653  Writer.AddSourceLocation(E->getLParenLoc(), Record);
654  Writer.AddSourceLocation(E->getRParenLoc(), Record);
655  Code = serialization::EXPR_CSTYLE_CAST;
656}
657
658void ASTStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
659  VisitExpr(E);
660  Writer.AddSourceLocation(E->getLParenLoc(), Record);
661  Writer.AddTypeSourceInfo(E->getTypeSourceInfo(), Record);
662  Writer.AddStmt(E->getInitializer());
663  Record.push_back(E->isFileScope());
664  Code = serialization::EXPR_COMPOUND_LITERAL;
665}
666
667void ASTStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
668  VisitExpr(E);
669  Writer.AddStmt(E->getBase());
670  Writer.AddIdentifierRef(&E->getAccessor(), Record);
671  Writer.AddSourceLocation(E->getAccessorLoc(), Record);
672  Code = serialization::EXPR_EXT_VECTOR_ELEMENT;
673}
674
675void ASTStmtWriter::VisitInitListExpr(InitListExpr *E) {
676  VisitExpr(E);
677  // NOTE: only add the (possibly null) syntactic form.
678  // No need to serialize the isSemanticForm flag and the semantic form.
679  Writer.AddStmt(E->getSyntacticForm());
680  Writer.AddSourceLocation(E->getLBraceLoc(), Record);
681  Writer.AddSourceLocation(E->getRBraceLoc(), Record);
682  bool isArrayFiller = E->ArrayFillerOrUnionFieldInit.is<Expr*>();
683  Record.push_back(isArrayFiller);
684  if (isArrayFiller)
685    Writer.AddStmt(E->getArrayFiller());
686  else
687    Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
688  Record.push_back(E->hadArrayRangeDesignator());
689  Record.push_back(E->getNumInits());
690  if (isArrayFiller) {
691    // ArrayFiller may have filled "holes" due to designated initializer.
692    // Replace them by 0 to indicate that the filler goes in that place.
693    Expr *filler = E->getArrayFiller();
694    for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
695      Writer.AddStmt(E->getInit(I) != filler ? E->getInit(I) : 0);
696  } else {
697    for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
698      Writer.AddStmt(E->getInit(I));
699  }
700  Code = serialization::EXPR_INIT_LIST;
701}
702
703void ASTStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
704  VisitExpr(E);
705  Record.push_back(E->getNumSubExprs());
706  for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
707    Writer.AddStmt(E->getSubExpr(I));
708  Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
709  Record.push_back(E->usesGNUSyntax());
710  for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
711                                             DEnd = E->designators_end();
712       D != DEnd; ++D) {
713    if (D->isFieldDesignator()) {
714      if (FieldDecl *Field = D->getField()) {
715        Record.push_back(serialization::DESIG_FIELD_DECL);
716        Writer.AddDeclRef(Field, Record);
717      } else {
718        Record.push_back(serialization::DESIG_FIELD_NAME);
719        Writer.AddIdentifierRef(D->getFieldName(), Record);
720      }
721      Writer.AddSourceLocation(D->getDotLoc(), Record);
722      Writer.AddSourceLocation(D->getFieldLoc(), Record);
723    } else if (D->isArrayDesignator()) {
724      Record.push_back(serialization::DESIG_ARRAY);
725      Record.push_back(D->getFirstExprIndex());
726      Writer.AddSourceLocation(D->getLBracketLoc(), Record);
727      Writer.AddSourceLocation(D->getRBracketLoc(), Record);
728    } else {
729      assert(D->isArrayRangeDesignator() && "Unknown designator");
730      Record.push_back(serialization::DESIG_ARRAY_RANGE);
731      Record.push_back(D->getFirstExprIndex());
732      Writer.AddSourceLocation(D->getLBracketLoc(), Record);
733      Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
734      Writer.AddSourceLocation(D->getRBracketLoc(), Record);
735    }
736  }
737  Code = serialization::EXPR_DESIGNATED_INIT;
738}
739
740void ASTStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
741  VisitExpr(E);
742  Code = serialization::EXPR_IMPLICIT_VALUE_INIT;
743}
744
745void ASTStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
746  VisitExpr(E);
747  Writer.AddStmt(E->getSubExpr());
748  Writer.AddTypeSourceInfo(E->getWrittenTypeInfo(), Record);
749  Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
750  Writer.AddSourceLocation(E->getRParenLoc(), Record);
751  Code = serialization::EXPR_VA_ARG;
752}
753
754void ASTStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) {
755  VisitExpr(E);
756  Writer.AddSourceLocation(E->getAmpAmpLoc(), Record);
757  Writer.AddSourceLocation(E->getLabelLoc(), Record);
758  Writer.AddDeclRef(E->getLabel(), Record);
759  Code = serialization::EXPR_ADDR_LABEL;
760}
761
762void ASTStmtWriter::VisitStmtExpr(StmtExpr *E) {
763  VisitExpr(E);
764  Writer.AddStmt(E->getSubStmt());
765  Writer.AddSourceLocation(E->getLParenLoc(), Record);
766  Writer.AddSourceLocation(E->getRParenLoc(), Record);
767  Code = serialization::EXPR_STMT;
768}
769
770void ASTStmtWriter::VisitChooseExpr(ChooseExpr *E) {
771  VisitExpr(E);
772  Writer.AddStmt(E->getCond());
773  Writer.AddStmt(E->getLHS());
774  Writer.AddStmt(E->getRHS());
775  Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
776  Writer.AddSourceLocation(E->getRParenLoc(), Record);
777  Record.push_back(E->isConditionDependent() ? false : E->isConditionTrue());
778  Code = serialization::EXPR_CHOOSE;
779}
780
781void ASTStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
782  VisitExpr(E);
783  Writer.AddSourceLocation(E->getTokenLocation(), Record);
784  Code = serialization::EXPR_GNU_NULL;
785}
786
787void ASTStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
788  VisitExpr(E);
789  Record.push_back(E->getNumSubExprs());
790  for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
791    Writer.AddStmt(E->getExpr(I));
792  Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
793  Writer.AddSourceLocation(E->getRParenLoc(), Record);
794  Code = serialization::EXPR_SHUFFLE_VECTOR;
795}
796
797void ASTStmtWriter::VisitBlockExpr(BlockExpr *E) {
798  VisitExpr(E);
799  Writer.AddDeclRef(E->getBlockDecl(), Record);
800  Code = serialization::EXPR_BLOCK;
801}
802
803void ASTStmtWriter::VisitGenericSelectionExpr(GenericSelectionExpr *E) {
804  VisitExpr(E);
805  Record.push_back(E->getNumAssocs());
806
807  Writer.AddStmt(E->getControllingExpr());
808  for (unsigned I = 0, N = E->getNumAssocs(); I != N; ++I) {
809    Writer.AddTypeSourceInfo(E->getAssocTypeSourceInfo(I), Record);
810    Writer.AddStmt(E->getAssocExpr(I));
811  }
812  Record.push_back(E->isResultDependent() ? -1U : E->getResultIndex());
813
814  Writer.AddSourceLocation(E->getGenericLoc(), Record);
815  Writer.AddSourceLocation(E->getDefaultLoc(), Record);
816  Writer.AddSourceLocation(E->getRParenLoc(), Record);
817  Code = serialization::EXPR_GENERIC_SELECTION;
818}
819
820void ASTStmtWriter::VisitPseudoObjectExpr(PseudoObjectExpr *E) {
821  VisitExpr(E);
822  Record.push_back(E->getNumSemanticExprs());
823
824  // Push the result index.  Currently, this needs to exactly match
825  // the encoding used internally for ResultIndex.
826  unsigned result = E->getResultExprIndex();
827  result = (result == PseudoObjectExpr::NoResult ? 0 : result + 1);
828  Record.push_back(result);
829
830  Writer.AddStmt(E->getSyntacticForm());
831  for (PseudoObjectExpr::semantics_iterator
832         i = E->semantics_begin(), e = E->semantics_end(); i != e; ++i) {
833    Writer.AddStmt(*i);
834  }
835  Code = serialization::EXPR_PSEUDO_OBJECT;
836}
837
838void ASTStmtWriter::VisitAtomicExpr(AtomicExpr *E) {
839  VisitExpr(E);
840  Record.push_back(E->getOp());
841  for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
842    Writer.AddStmt(E->getSubExprs()[I]);
843  Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
844  Writer.AddSourceLocation(E->getRParenLoc(), Record);
845  Code = serialization::EXPR_ATOMIC;
846}
847
848//===----------------------------------------------------------------------===//
849// Objective-C Expressions and Statements.
850//===----------------------------------------------------------------------===//
851
852void ASTStmtWriter::VisitObjCStringLiteral(ObjCStringLiteral *E) {
853  VisitExpr(E);
854  Writer.AddStmt(E->getString());
855  Writer.AddSourceLocation(E->getAtLoc(), Record);
856  Code = serialization::EXPR_OBJC_STRING_LITERAL;
857}
858
859void ASTStmtWriter::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
860  VisitExpr(E);
861  Writer.AddStmt(E->getSubExpr());
862  Writer.AddDeclRef(E->getBoxingMethod(), Record);
863  Writer.AddSourceRange(E->getSourceRange(), Record);
864  Code = serialization::EXPR_OBJC_BOXED_EXPRESSION;
865}
866
867void ASTStmtWriter::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
868  VisitExpr(E);
869  Record.push_back(E->getNumElements());
870  for (unsigned i = 0; i < E->getNumElements(); i++)
871    Writer.AddStmt(E->getElement(i));
872  Writer.AddDeclRef(E->getArrayWithObjectsMethod(), Record);
873  Writer.AddSourceRange(E->getSourceRange(), Record);
874  Code = serialization::EXPR_OBJC_ARRAY_LITERAL;
875}
876
877void ASTStmtWriter::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
878  VisitExpr(E);
879  Record.push_back(E->getNumElements());
880  Record.push_back(E->HasPackExpansions);
881  for (unsigned i = 0; i < E->getNumElements(); i++) {
882    ObjCDictionaryElement Element = E->getKeyValueElement(i);
883    Writer.AddStmt(Element.Key);
884    Writer.AddStmt(Element.Value);
885    if (E->HasPackExpansions) {
886      Writer.AddSourceLocation(Element.EllipsisLoc, Record);
887      unsigned NumExpansions = 0;
888      if (Element.NumExpansions)
889        NumExpansions = *Element.NumExpansions + 1;
890      Record.push_back(NumExpansions);
891    }
892  }
893
894  Writer.AddDeclRef(E->getDictWithObjectsMethod(), Record);
895  Writer.AddSourceRange(E->getSourceRange(), Record);
896  Code = serialization::EXPR_OBJC_DICTIONARY_LITERAL;
897}
898
899void ASTStmtWriter::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
900  VisitExpr(E);
901  Writer.AddTypeSourceInfo(E->getEncodedTypeSourceInfo(), Record);
902  Writer.AddSourceLocation(E->getAtLoc(), Record);
903  Writer.AddSourceLocation(E->getRParenLoc(), Record);
904  Code = serialization::EXPR_OBJC_ENCODE;
905}
906
907void ASTStmtWriter::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
908  VisitExpr(E);
909  Writer.AddSelectorRef(E->getSelector(), Record);
910  Writer.AddSourceLocation(E->getAtLoc(), Record);
911  Writer.AddSourceLocation(E->getRParenLoc(), Record);
912  Code = serialization::EXPR_OBJC_SELECTOR_EXPR;
913}
914
915void ASTStmtWriter::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
916  VisitExpr(E);
917  Writer.AddDeclRef(E->getProtocol(), Record);
918  Writer.AddSourceLocation(E->getAtLoc(), Record);
919  Writer.AddSourceLocation(E->ProtoLoc, Record);
920  Writer.AddSourceLocation(E->getRParenLoc(), Record);
921  Code = serialization::EXPR_OBJC_PROTOCOL_EXPR;
922}
923
924void ASTStmtWriter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
925  VisitExpr(E);
926  Writer.AddDeclRef(E->getDecl(), Record);
927  Writer.AddSourceLocation(E->getLocation(), Record);
928  Writer.AddSourceLocation(E->getOpLoc(), Record);
929  Writer.AddStmt(E->getBase());
930  Record.push_back(E->isArrow());
931  Record.push_back(E->isFreeIvar());
932  Code = serialization::EXPR_OBJC_IVAR_REF_EXPR;
933}
934
935void ASTStmtWriter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
936  VisitExpr(E);
937  Record.push_back(E->SetterAndMethodRefFlags.getInt());
938  Record.push_back(E->isImplicitProperty());
939  if (E->isImplicitProperty()) {
940    Writer.AddDeclRef(E->getImplicitPropertyGetter(), Record);
941    Writer.AddDeclRef(E->getImplicitPropertySetter(), Record);
942  } else {
943    Writer.AddDeclRef(E->getExplicitProperty(), Record);
944  }
945  Writer.AddSourceLocation(E->getLocation(), Record);
946  Writer.AddSourceLocation(E->getReceiverLocation(), Record);
947  if (E->isObjectReceiver()) {
948    Record.push_back(0);
949    Writer.AddStmt(E->getBase());
950  } else if (E->isSuperReceiver()) {
951    Record.push_back(1);
952    Writer.AddTypeRef(E->getSuperReceiverType(), Record);
953  } else {
954    Record.push_back(2);
955    Writer.AddDeclRef(E->getClassReceiver(), Record);
956  }
957
958  Code = serialization::EXPR_OBJC_PROPERTY_REF_EXPR;
959}
960
961void ASTStmtWriter::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *E) {
962  VisitExpr(E);
963  Writer.AddSourceLocation(E->getRBracket(), Record);
964  Writer.AddStmt(E->getBaseExpr());
965  Writer.AddStmt(E->getKeyExpr());
966  Writer.AddDeclRef(E->getAtIndexMethodDecl(), Record);
967  Writer.AddDeclRef(E->setAtIndexMethodDecl(), Record);
968
969  Code = serialization::EXPR_OBJC_SUBSCRIPT_REF_EXPR;
970}
971
972void ASTStmtWriter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
973  VisitExpr(E);
974  Record.push_back(E->getNumArgs());
975  Record.push_back(E->getNumStoredSelLocs());
976  Record.push_back(E->SelLocsKind);
977  Record.push_back(E->isDelegateInitCall());
978  Record.push_back(E->IsImplicit);
979  Record.push_back((unsigned)E->getReceiverKind()); // FIXME: stable encoding
980  switch (E->getReceiverKind()) {
981  case ObjCMessageExpr::Instance:
982    Writer.AddStmt(E->getInstanceReceiver());
983    break;
984
985  case ObjCMessageExpr::Class:
986    Writer.AddTypeSourceInfo(E->getClassReceiverTypeInfo(), Record);
987    break;
988
989  case ObjCMessageExpr::SuperClass:
990  case ObjCMessageExpr::SuperInstance:
991    Writer.AddTypeRef(E->getSuperType(), Record);
992    Writer.AddSourceLocation(E->getSuperLoc(), Record);
993    break;
994  }
995
996  if (E->getMethodDecl()) {
997    Record.push_back(1);
998    Writer.AddDeclRef(E->getMethodDecl(), Record);
999  } else {
1000    Record.push_back(0);
1001    Writer.AddSelectorRef(E->getSelector(), Record);
1002  }
1003
1004  Writer.AddSourceLocation(E->getLeftLoc(), Record);
1005  Writer.AddSourceLocation(E->getRightLoc(), Record);
1006
1007  for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
1008       Arg != ArgEnd; ++Arg)
1009    Writer.AddStmt(*Arg);
1010
1011  SourceLocation *Locs = E->getStoredSelLocs();
1012  for (unsigned i = 0, e = E->getNumStoredSelLocs(); i != e; ++i)
1013    Writer.AddSourceLocation(Locs[i], Record);
1014
1015  Code = serialization::EXPR_OBJC_MESSAGE_EXPR;
1016}
1017
1018void ASTStmtWriter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
1019  VisitStmt(S);
1020  Writer.AddStmt(S->getElement());
1021  Writer.AddStmt(S->getCollection());
1022  Writer.AddStmt(S->getBody());
1023  Writer.AddSourceLocation(S->getForLoc(), Record);
1024  Writer.AddSourceLocation(S->getRParenLoc(), Record);
1025  Code = serialization::STMT_OBJC_FOR_COLLECTION;
1026}
1027
1028void ASTStmtWriter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
1029  Writer.AddStmt(S->getCatchBody());
1030  Writer.AddDeclRef(S->getCatchParamDecl(), Record);
1031  Writer.AddSourceLocation(S->getAtCatchLoc(), Record);
1032  Writer.AddSourceLocation(S->getRParenLoc(), Record);
1033  Code = serialization::STMT_OBJC_CATCH;
1034}
1035
1036void ASTStmtWriter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
1037  Writer.AddStmt(S->getFinallyBody());
1038  Writer.AddSourceLocation(S->getAtFinallyLoc(), Record);
1039  Code = serialization::STMT_OBJC_FINALLY;
1040}
1041
1042void ASTStmtWriter::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S) {
1043  Writer.AddStmt(S->getSubStmt());
1044  Writer.AddSourceLocation(S->getAtLoc(), Record);
1045  Code = serialization::STMT_OBJC_AUTORELEASE_POOL;
1046}
1047
1048void ASTStmtWriter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
1049  Record.push_back(S->getNumCatchStmts());
1050  Record.push_back(S->getFinallyStmt() != 0);
1051  Writer.AddStmt(S->getTryBody());
1052  for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I)
1053    Writer.AddStmt(S->getCatchStmt(I));
1054  if (S->getFinallyStmt())
1055    Writer.AddStmt(S->getFinallyStmt());
1056  Writer.AddSourceLocation(S->getAtTryLoc(), Record);
1057  Code = serialization::STMT_OBJC_AT_TRY;
1058}
1059
1060void ASTStmtWriter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S) {
1061  Writer.AddStmt(S->getSynchExpr());
1062  Writer.AddStmt(S->getSynchBody());
1063  Writer.AddSourceLocation(S->getAtSynchronizedLoc(), Record);
1064  Code = serialization::STMT_OBJC_AT_SYNCHRONIZED;
1065}
1066
1067void ASTStmtWriter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
1068  Writer.AddStmt(S->getThrowExpr());
1069  Writer.AddSourceLocation(S->getThrowLoc(), Record);
1070  Code = serialization::STMT_OBJC_AT_THROW;
1071}
1072
1073void ASTStmtWriter::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *E) {
1074  VisitExpr(E);
1075  Record.push_back(E->getValue());
1076  Writer.AddSourceLocation(E->getLocation(), Record);
1077  Code = serialization::EXPR_OBJC_BOOL_LITERAL;
1078}
1079
1080//===----------------------------------------------------------------------===//
1081// C++ Expressions and Statements.
1082//===----------------------------------------------------------------------===//
1083
1084void ASTStmtWriter::VisitCXXCatchStmt(CXXCatchStmt *S) {
1085  VisitStmt(S);
1086  Writer.AddSourceLocation(S->getCatchLoc(), Record);
1087  Writer.AddDeclRef(S->getExceptionDecl(), Record);
1088  Writer.AddStmt(S->getHandlerBlock());
1089  Code = serialization::STMT_CXX_CATCH;
1090}
1091
1092void ASTStmtWriter::VisitCXXTryStmt(CXXTryStmt *S) {
1093  VisitStmt(S);
1094  Record.push_back(S->getNumHandlers());
1095  Writer.AddSourceLocation(S->getTryLoc(), Record);
1096  Writer.AddStmt(S->getTryBlock());
1097  for (unsigned i = 0, e = S->getNumHandlers(); i != e; ++i)
1098    Writer.AddStmt(S->getHandler(i));
1099  Code = serialization::STMT_CXX_TRY;
1100}
1101
1102void ASTStmtWriter::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
1103  VisitStmt(S);
1104  Writer.AddSourceLocation(S->getForLoc(), Record);
1105  Writer.AddSourceLocation(S->getColonLoc(), Record);
1106  Writer.AddSourceLocation(S->getRParenLoc(), Record);
1107  Writer.AddStmt(S->getRangeStmt());
1108  Writer.AddStmt(S->getBeginEndStmt());
1109  Writer.AddStmt(S->getCond());
1110  Writer.AddStmt(S->getInc());
1111  Writer.AddStmt(S->getLoopVarStmt());
1112  Writer.AddStmt(S->getBody());
1113  Code = serialization::STMT_CXX_FOR_RANGE;
1114}
1115
1116void ASTStmtWriter::VisitMSDependentExistsStmt(MSDependentExistsStmt *S) {
1117  VisitStmt(S);
1118  Writer.AddSourceLocation(S->getKeywordLoc(), Record);
1119  Record.push_back(S->isIfExists());
1120  Writer.AddNestedNameSpecifierLoc(S->getQualifierLoc(), Record);
1121  Writer.AddDeclarationNameInfo(S->getNameInfo(), Record);
1122  Writer.AddStmt(S->getSubStmt());
1123  Code = serialization::STMT_MS_DEPENDENT_EXISTS;
1124}
1125
1126void ASTStmtWriter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) {
1127  VisitCallExpr(E);
1128  Record.push_back(E->getOperator());
1129  Writer.AddSourceRange(E->Range, Record);
1130  Record.push_back(E->isFPContractable());
1131  Code = serialization::EXPR_CXX_OPERATOR_CALL;
1132}
1133
1134void ASTStmtWriter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
1135  VisitCallExpr(E);
1136  Code = serialization::EXPR_CXX_MEMBER_CALL;
1137}
1138
1139void ASTStmtWriter::VisitCXXConstructExpr(CXXConstructExpr *E) {
1140  VisitExpr(E);
1141  Record.push_back(E->getNumArgs());
1142  for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
1143    Writer.AddStmt(E->getArg(I));
1144  Writer.AddDeclRef(E->getConstructor(), Record);
1145  Writer.AddSourceLocation(E->getLocation(), Record);
1146  Record.push_back(E->isElidable());
1147  Record.push_back(E->hadMultipleCandidates());
1148  Record.push_back(E->isListInitialization());
1149  Record.push_back(E->requiresZeroInitialization());
1150  Record.push_back(E->getConstructionKind()); // FIXME: stable encoding
1151  Writer.AddSourceRange(E->getParenRange(), Record);
1152  Code = serialization::EXPR_CXX_CONSTRUCT;
1153}
1154
1155void ASTStmtWriter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) {
1156  VisitCXXConstructExpr(E);
1157  Writer.AddTypeSourceInfo(E->getTypeSourceInfo(), Record);
1158  Code = serialization::EXPR_CXX_TEMPORARY_OBJECT;
1159}
1160
1161void ASTStmtWriter::VisitLambdaExpr(LambdaExpr *E) {
1162  VisitExpr(E);
1163  Record.push_back(E->NumCaptures);
1164  unsigned NumArrayIndexVars = 0;
1165  if (E->HasArrayIndexVars)
1166    NumArrayIndexVars = E->getArrayIndexStarts()[E->NumCaptures];
1167  Record.push_back(NumArrayIndexVars);
1168  Writer.AddSourceRange(E->IntroducerRange, Record);
1169  Record.push_back(E->CaptureDefault); // FIXME: stable encoding
1170  Writer.AddSourceLocation(E->CaptureDefaultLoc, Record);
1171  Record.push_back(E->ExplicitParams);
1172  Record.push_back(E->ExplicitResultType);
1173  Writer.AddSourceLocation(E->ClosingBrace, Record);
1174
1175  // Add capture initializers.
1176  for (LambdaExpr::capture_init_iterator C = E->capture_init_begin(),
1177                                      CEnd = E->capture_init_end();
1178       C != CEnd; ++C) {
1179    Writer.AddStmt(*C);
1180  }
1181
1182  // Add array index variables, if any.
1183  if (NumArrayIndexVars) {
1184    Record.append(E->getArrayIndexStarts(),
1185                  E->getArrayIndexStarts() + E->NumCaptures + 1);
1186    VarDecl **ArrayIndexVars = E->getArrayIndexVars();
1187    for (unsigned I = 0; I != NumArrayIndexVars; ++I)
1188      Writer.AddDeclRef(ArrayIndexVars[I], Record);
1189  }
1190
1191  Code = serialization::EXPR_LAMBDA;
1192}
1193
1194void ASTStmtWriter::VisitCXXStdInitializerListExpr(CXXStdInitializerListExpr *E) {
1195  VisitExpr(E);
1196  Writer.AddStmt(E->getSubExpr());
1197  Code = serialization::EXPR_CXX_STD_INITIALIZER_LIST;
1198}
1199
1200void ASTStmtWriter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
1201  VisitExplicitCastExpr(E);
1202  Writer.AddSourceRange(SourceRange(E->getOperatorLoc(), E->getRParenLoc()),
1203                        Record);
1204  Writer.AddSourceRange(E->getAngleBrackets(), Record);
1205}
1206
1207void ASTStmtWriter::VisitCXXStaticCastExpr(CXXStaticCastExpr *E) {
1208  VisitCXXNamedCastExpr(E);
1209  Code = serialization::EXPR_CXX_STATIC_CAST;
1210}
1211
1212void ASTStmtWriter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *E) {
1213  VisitCXXNamedCastExpr(E);
1214  Code = serialization::EXPR_CXX_DYNAMIC_CAST;
1215}
1216
1217void ASTStmtWriter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *E) {
1218  VisitCXXNamedCastExpr(E);
1219  Code = serialization::EXPR_CXX_REINTERPRET_CAST;
1220}
1221
1222void ASTStmtWriter::VisitCXXConstCastExpr(CXXConstCastExpr *E) {
1223  VisitCXXNamedCastExpr(E);
1224  Code = serialization::EXPR_CXX_CONST_CAST;
1225}
1226
1227void ASTStmtWriter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E) {
1228  VisitExplicitCastExpr(E);
1229  Writer.AddSourceLocation(E->getLParenLoc(), Record);
1230  Writer.AddSourceLocation(E->getRParenLoc(), Record);
1231  Code = serialization::EXPR_CXX_FUNCTIONAL_CAST;
1232}
1233
1234void ASTStmtWriter::VisitUserDefinedLiteral(UserDefinedLiteral *E) {
1235  VisitCallExpr(E);
1236  Writer.AddSourceLocation(E->UDSuffixLoc, Record);
1237  Code = serialization::EXPR_USER_DEFINED_LITERAL;
1238}
1239
1240void ASTStmtWriter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
1241  VisitExpr(E);
1242  Record.push_back(E->getValue());
1243  Writer.AddSourceLocation(E->getLocation(), Record);
1244  Code = serialization::EXPR_CXX_BOOL_LITERAL;
1245}
1246
1247void ASTStmtWriter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
1248  VisitExpr(E);
1249  Writer.AddSourceLocation(E->getLocation(), Record);
1250  Code = serialization::EXPR_CXX_NULL_PTR_LITERAL;
1251}
1252
1253void ASTStmtWriter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
1254  VisitExpr(E);
1255  Writer.AddSourceRange(E->getSourceRange(), Record);
1256  if (E->isTypeOperand()) {
1257    Writer.AddTypeSourceInfo(E->getTypeOperandSourceInfo(), Record);
1258    Code = serialization::EXPR_CXX_TYPEID_TYPE;
1259  } else {
1260    Writer.AddStmt(E->getExprOperand());
1261    Code = serialization::EXPR_CXX_TYPEID_EXPR;
1262  }
1263}
1264
1265void ASTStmtWriter::VisitCXXThisExpr(CXXThisExpr *E) {
1266  VisitExpr(E);
1267  Writer.AddSourceLocation(E->getLocation(), Record);
1268  Record.push_back(E->isImplicit());
1269  Code = serialization::EXPR_CXX_THIS;
1270}
1271
1272void ASTStmtWriter::VisitCXXThrowExpr(CXXThrowExpr *E) {
1273  VisitExpr(E);
1274  Writer.AddSourceLocation(E->getThrowLoc(), Record);
1275  Writer.AddStmt(E->getSubExpr());
1276  Record.push_back(E->isThrownVariableInScope());
1277  Code = serialization::EXPR_CXX_THROW;
1278}
1279
1280void ASTStmtWriter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
1281  VisitExpr(E);
1282
1283  bool HasOtherExprStored = E->Param.getInt();
1284  // Store these first, the reader reads them before creation.
1285  Record.push_back(HasOtherExprStored);
1286  if (HasOtherExprStored)
1287    Writer.AddStmt(E->getExpr());
1288  Writer.AddDeclRef(E->getParam(), Record);
1289  Writer.AddSourceLocation(E->getUsedLocation(), Record);
1290
1291  Code = serialization::EXPR_CXX_DEFAULT_ARG;
1292}
1293
1294void ASTStmtWriter::VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) {
1295  VisitExpr(E);
1296  Writer.AddDeclRef(E->getField(), Record);
1297  Writer.AddSourceLocation(E->getExprLoc(), Record);
1298  Code = serialization::EXPR_CXX_DEFAULT_INIT;
1299}
1300
1301void ASTStmtWriter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
1302  VisitExpr(E);
1303  Writer.AddCXXTemporary(E->getTemporary(), Record);
1304  Writer.AddStmt(E->getSubExpr());
1305  Code = serialization::EXPR_CXX_BIND_TEMPORARY;
1306}
1307
1308void ASTStmtWriter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
1309  VisitExpr(E);
1310  Writer.AddTypeSourceInfo(E->getTypeSourceInfo(), Record);
1311  Writer.AddSourceLocation(E->getRParenLoc(), Record);
1312  Code = serialization::EXPR_CXX_SCALAR_VALUE_INIT;
1313}
1314
1315void ASTStmtWriter::VisitCXXNewExpr(CXXNewExpr *E) {
1316  VisitExpr(E);
1317  Record.push_back(E->isGlobalNew());
1318  Record.push_back(E->isArray());
1319  Record.push_back(E->doesUsualArrayDeleteWantSize());
1320  Record.push_back(E->getNumPlacementArgs());
1321  Record.push_back(E->StoredInitializationStyle);
1322  Writer.AddDeclRef(E->getOperatorNew(), Record);
1323  Writer.AddDeclRef(E->getOperatorDelete(), Record);
1324  Writer.AddTypeSourceInfo(E->getAllocatedTypeSourceInfo(), Record);
1325  Writer.AddSourceRange(E->getTypeIdParens(), Record);
1326  Writer.AddSourceRange(E->getSourceRange(), Record);
1327  Writer.AddSourceRange(E->getDirectInitRange(), Record);
1328  for (CXXNewExpr::arg_iterator I = E->raw_arg_begin(), e = E->raw_arg_end();
1329       I != e; ++I)
1330    Writer.AddStmt(*I);
1331
1332  Code = serialization::EXPR_CXX_NEW;
1333}
1334
1335void ASTStmtWriter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1336  VisitExpr(E);
1337  Record.push_back(E->isGlobalDelete());
1338  Record.push_back(E->isArrayForm());
1339  Record.push_back(E->isArrayFormAsWritten());
1340  Record.push_back(E->doesUsualArrayDeleteWantSize());
1341  Writer.AddDeclRef(E->getOperatorDelete(), Record);
1342  Writer.AddStmt(E->getArgument());
1343  Writer.AddSourceLocation(E->getSourceRange().getBegin(), Record);
1344
1345  Code = serialization::EXPR_CXX_DELETE;
1346}
1347
1348void ASTStmtWriter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1349  VisitExpr(E);
1350
1351  Writer.AddStmt(E->getBase());
1352  Record.push_back(E->isArrow());
1353  Writer.AddSourceLocation(E->getOperatorLoc(), Record);
1354  Writer.AddNestedNameSpecifierLoc(E->getQualifierLoc(), Record);
1355  Writer.AddTypeSourceInfo(E->getScopeTypeInfo(), Record);
1356  Writer.AddSourceLocation(E->getColonColonLoc(), Record);
1357  Writer.AddSourceLocation(E->getTildeLoc(), Record);
1358
1359  // PseudoDestructorTypeStorage.
1360  Writer.AddIdentifierRef(E->getDestroyedTypeIdentifier(), Record);
1361  if (E->getDestroyedTypeIdentifier())
1362    Writer.AddSourceLocation(E->getDestroyedTypeLoc(), Record);
1363  else
1364    Writer.AddTypeSourceInfo(E->getDestroyedTypeInfo(), Record);
1365
1366  Code = serialization::EXPR_CXX_PSEUDO_DESTRUCTOR;
1367}
1368
1369void ASTStmtWriter::VisitExprWithCleanups(ExprWithCleanups *E) {
1370  VisitExpr(E);
1371  Record.push_back(E->getNumObjects());
1372  for (unsigned i = 0, e = E->getNumObjects(); i != e; ++i)
1373    Writer.AddDeclRef(E->getObject(i), Record);
1374
1375  Writer.AddStmt(E->getSubExpr());
1376  Code = serialization::EXPR_EXPR_WITH_CLEANUPS;
1377}
1378
1379void
1380ASTStmtWriter::VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E){
1381  VisitExpr(E);
1382
1383  // Don't emit anything here, HasTemplateKWAndArgsInfo must be
1384  // emitted first.
1385
1386  Record.push_back(E->HasTemplateKWAndArgsInfo);
1387  if (E->HasTemplateKWAndArgsInfo) {
1388    const ASTTemplateKWAndArgsInfo &Args = *E->getTemplateKWAndArgsInfo();
1389    Record.push_back(Args.NumTemplateArgs);
1390    AddTemplateKWAndArgsInfo(Args);
1391  }
1392
1393  if (!E->isImplicitAccess())
1394    Writer.AddStmt(E->getBase());
1395  else
1396    Writer.AddStmt(0);
1397  Writer.AddTypeRef(E->getBaseType(), Record);
1398  Record.push_back(E->isArrow());
1399  Writer.AddSourceLocation(E->getOperatorLoc(), Record);
1400  Writer.AddNestedNameSpecifierLoc(E->getQualifierLoc(), Record);
1401  Writer.AddDeclRef(E->getFirstQualifierFoundInScope(), Record);
1402  Writer.AddDeclarationNameInfo(E->MemberNameInfo, Record);
1403  Code = serialization::EXPR_CXX_DEPENDENT_SCOPE_MEMBER;
1404}
1405
1406void
1407ASTStmtWriter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
1408  VisitExpr(E);
1409
1410  // Don't emit anything here, HasTemplateKWAndArgsInfo must be
1411  // emitted first.
1412
1413  Record.push_back(E->HasTemplateKWAndArgsInfo);
1414  if (E->HasTemplateKWAndArgsInfo) {
1415    const ASTTemplateKWAndArgsInfo &Args = *E->getTemplateKWAndArgsInfo();
1416    Record.push_back(Args.NumTemplateArgs);
1417    AddTemplateKWAndArgsInfo(Args);
1418  }
1419
1420  Writer.AddNestedNameSpecifierLoc(E->getQualifierLoc(), Record);
1421  Writer.AddDeclarationNameInfo(E->NameInfo, Record);
1422  Code = serialization::EXPR_CXX_DEPENDENT_SCOPE_DECL_REF;
1423}
1424
1425void
1426ASTStmtWriter::VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E) {
1427  VisitExpr(E);
1428  Record.push_back(E->arg_size());
1429  for (CXXUnresolvedConstructExpr::arg_iterator
1430         ArgI = E->arg_begin(), ArgE = E->arg_end(); ArgI != ArgE; ++ArgI)
1431    Writer.AddStmt(*ArgI);
1432  Writer.AddTypeSourceInfo(E->getTypeSourceInfo(), Record);
1433  Writer.AddSourceLocation(E->getLParenLoc(), Record);
1434  Writer.AddSourceLocation(E->getRParenLoc(), Record);
1435  Code = serialization::EXPR_CXX_UNRESOLVED_CONSTRUCT;
1436}
1437
1438void ASTStmtWriter::VisitOverloadExpr(OverloadExpr *E) {
1439  VisitExpr(E);
1440
1441  // Don't emit anything here, HasTemplateKWAndArgsInfo must be
1442  // emitted first.
1443
1444  Record.push_back(E->HasTemplateKWAndArgsInfo);
1445  if (E->HasTemplateKWAndArgsInfo) {
1446    const ASTTemplateKWAndArgsInfo &Args = *E->getTemplateKWAndArgsInfo();
1447    Record.push_back(Args.NumTemplateArgs);
1448    AddTemplateKWAndArgsInfo(Args);
1449  }
1450
1451  Record.push_back(E->getNumDecls());
1452  for (OverloadExpr::decls_iterator
1453         OvI = E->decls_begin(), OvE = E->decls_end(); OvI != OvE; ++OvI) {
1454    Writer.AddDeclRef(OvI.getDecl(), Record);
1455    Record.push_back(OvI.getAccess());
1456  }
1457
1458  Writer.AddDeclarationNameInfo(E->NameInfo, Record);
1459  Writer.AddNestedNameSpecifierLoc(E->getQualifierLoc(), Record);
1460}
1461
1462void ASTStmtWriter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
1463  VisitOverloadExpr(E);
1464  Record.push_back(E->isArrow());
1465  Record.push_back(E->hasUnresolvedUsing());
1466  Writer.AddStmt(!E->isImplicitAccess() ? E->getBase() : 0);
1467  Writer.AddTypeRef(E->getBaseType(), Record);
1468  Writer.AddSourceLocation(E->getOperatorLoc(), Record);
1469  Code = serialization::EXPR_CXX_UNRESOLVED_MEMBER;
1470}
1471
1472void ASTStmtWriter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
1473  VisitOverloadExpr(E);
1474  Record.push_back(E->requiresADL());
1475  Record.push_back(E->isOverloaded());
1476  Writer.AddDeclRef(E->getNamingClass(), Record);
1477  Code = serialization::EXPR_CXX_UNRESOLVED_LOOKUP;
1478}
1479
1480void ASTStmtWriter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1481  VisitExpr(E);
1482  Record.push_back(E->getTrait());
1483  Record.push_back(E->getValue());
1484  Writer.AddSourceRange(E->getSourceRange(), Record);
1485  Writer.AddTypeSourceInfo(E->getQueriedTypeSourceInfo(), Record);
1486  Code = serialization::EXPR_CXX_UNARY_TYPE_TRAIT;
1487}
1488
1489void ASTStmtWriter::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) {
1490  VisitExpr(E);
1491  Record.push_back(E->getTrait());
1492  Record.push_back(E->getValue());
1493  Writer.AddSourceRange(E->getSourceRange(), Record);
1494  Writer.AddTypeSourceInfo(E->getLhsTypeSourceInfo(), Record);
1495  Writer.AddTypeSourceInfo(E->getRhsTypeSourceInfo(), Record);
1496  Code = serialization::EXPR_BINARY_TYPE_TRAIT;
1497}
1498
1499void ASTStmtWriter::VisitTypeTraitExpr(TypeTraitExpr *E) {
1500  VisitExpr(E);
1501  Record.push_back(E->TypeTraitExprBits.NumArgs);
1502  Record.push_back(E->TypeTraitExprBits.Kind); // FIXME: Stable encoding
1503  Record.push_back(E->TypeTraitExprBits.Value);
1504  for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
1505    Writer.AddTypeSourceInfo(E->getArg(I), Record);
1506  Code = serialization::EXPR_TYPE_TRAIT;
1507}
1508
1509void ASTStmtWriter::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
1510  VisitExpr(E);
1511  Record.push_back(E->getTrait());
1512  Record.push_back(E->getValue());
1513  Writer.AddSourceRange(E->getSourceRange(), Record);
1514  Writer.AddTypeSourceInfo(E->getQueriedTypeSourceInfo(), Record);
1515  Code = serialization::EXPR_ARRAY_TYPE_TRAIT;
1516}
1517
1518void ASTStmtWriter::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
1519  VisitExpr(E);
1520  Record.push_back(E->getTrait());
1521  Record.push_back(E->getValue());
1522  Writer.AddSourceRange(E->getSourceRange(), Record);
1523  Writer.AddStmt(E->getQueriedExpression());
1524  Code = serialization::EXPR_CXX_EXPRESSION_TRAIT;
1525}
1526
1527void ASTStmtWriter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1528  VisitExpr(E);
1529  Record.push_back(E->getValue());
1530  Writer.AddSourceRange(E->getSourceRange(), Record);
1531  Writer.AddStmt(E->getOperand());
1532  Code = serialization::EXPR_CXX_NOEXCEPT;
1533}
1534
1535void ASTStmtWriter::VisitPackExpansionExpr(PackExpansionExpr *E) {
1536  VisitExpr(E);
1537  Writer.AddSourceLocation(E->getEllipsisLoc(), Record);
1538  Record.push_back(E->NumExpansions);
1539  Writer.AddStmt(E->getPattern());
1540  Code = serialization::EXPR_PACK_EXPANSION;
1541}
1542
1543void ASTStmtWriter::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
1544  VisitExpr(E);
1545  Writer.AddSourceLocation(E->OperatorLoc, Record);
1546  Writer.AddSourceLocation(E->PackLoc, Record);
1547  Writer.AddSourceLocation(E->RParenLoc, Record);
1548  Record.push_back(E->Length);
1549  Writer.AddDeclRef(E->Pack, Record);
1550  Code = serialization::EXPR_SIZEOF_PACK;
1551}
1552
1553void ASTStmtWriter::VisitSubstNonTypeTemplateParmExpr(
1554                                              SubstNonTypeTemplateParmExpr *E) {
1555  VisitExpr(E);
1556  Writer.AddDeclRef(E->getParameter(), Record);
1557  Writer.AddSourceLocation(E->getNameLoc(), Record);
1558  Writer.AddStmt(E->getReplacement());
1559  Code = serialization::EXPR_SUBST_NON_TYPE_TEMPLATE_PARM;
1560}
1561
1562void ASTStmtWriter::VisitSubstNonTypeTemplateParmPackExpr(
1563                                          SubstNonTypeTemplateParmPackExpr *E) {
1564  VisitExpr(E);
1565  Writer.AddDeclRef(E->getParameterPack(), Record);
1566  Writer.AddTemplateArgument(E->getArgumentPack(), Record);
1567  Writer.AddSourceLocation(E->getParameterPackLocation(), Record);
1568  Code = serialization::EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK;
1569}
1570
1571void ASTStmtWriter::VisitFunctionParmPackExpr(FunctionParmPackExpr *E) {
1572  VisitExpr(E);
1573  Record.push_back(E->getNumExpansions());
1574  Writer.AddDeclRef(E->getParameterPack(), Record);
1575  Writer.AddSourceLocation(E->getParameterPackLocation(), Record);
1576  for (FunctionParmPackExpr::iterator I = E->begin(), End = E->end();
1577       I != End; ++I)
1578    Writer.AddDeclRef(*I, Record);
1579  Code = serialization::EXPR_FUNCTION_PARM_PACK;
1580}
1581
1582void ASTStmtWriter::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
1583  VisitExpr(E);
1584  Writer.AddStmt(E->Temporary);
1585  Writer.AddDeclRef(E->ExtendingDecl, Record);
1586  Code = serialization::EXPR_MATERIALIZE_TEMPORARY;
1587}
1588
1589void ASTStmtWriter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
1590  VisitExpr(E);
1591  Writer.AddStmt(E->getSourceExpr());
1592  Writer.AddSourceLocation(E->getLocation(), Record);
1593  Code = serialization::EXPR_OPAQUE_VALUE;
1594}
1595
1596//===----------------------------------------------------------------------===//
1597// CUDA Expressions and Statements.
1598//===----------------------------------------------------------------------===//
1599
1600void ASTStmtWriter::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *E) {
1601  VisitCallExpr(E);
1602  Writer.AddStmt(E->getConfig());
1603  Code = serialization::EXPR_CUDA_KERNEL_CALL;
1604}
1605
1606//===----------------------------------------------------------------------===//
1607// OpenCL Expressions and Statements.
1608//===----------------------------------------------------------------------===//
1609void ASTStmtWriter::VisitAsTypeExpr(AsTypeExpr *E) {
1610  VisitExpr(E);
1611  Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
1612  Writer.AddSourceLocation(E->getRParenLoc(), Record);
1613  Writer.AddStmt(E->getSrcExpr());
1614  Code = serialization::EXPR_ASTYPE;
1615}
1616
1617//===----------------------------------------------------------------------===//
1618// Microsoft Expressions and Statements.
1619//===----------------------------------------------------------------------===//
1620void ASTStmtWriter::VisitMSPropertyRefExpr(MSPropertyRefExpr *E) {
1621  VisitExpr(E);
1622  Record.push_back(E->isArrow());
1623  Writer.AddStmt(E->getBaseExpr());
1624  Writer.AddNestedNameSpecifierLoc(E->getQualifierLoc(), Record);
1625  Writer.AddSourceLocation(E->getMemberLoc(), Record);
1626  Writer.AddDeclRef(E->getPropertyDecl(), Record);
1627  Code = serialization::EXPR_CXX_PROPERTY_REF_EXPR;
1628}
1629
1630void ASTStmtWriter::VisitCXXUuidofExpr(CXXUuidofExpr *E) {
1631  VisitExpr(E);
1632  Writer.AddSourceRange(E->getSourceRange(), Record);
1633  if (E->isTypeOperand()) {
1634    Writer.AddTypeSourceInfo(E->getTypeOperandSourceInfo(), Record);
1635    Code = serialization::EXPR_CXX_UUIDOF_TYPE;
1636  } else {
1637    Writer.AddStmt(E->getExprOperand());
1638    Code = serialization::EXPR_CXX_UUIDOF_EXPR;
1639  }
1640}
1641
1642void ASTStmtWriter::VisitSEHExceptStmt(SEHExceptStmt *S) {
1643  VisitStmt(S);
1644  Writer.AddSourceLocation(S->getExceptLoc(), Record);
1645  Writer.AddStmt(S->getFilterExpr());
1646  Writer.AddStmt(S->getBlock());
1647  Code = serialization::STMT_SEH_EXCEPT;
1648}
1649
1650void ASTStmtWriter::VisitSEHFinallyStmt(SEHFinallyStmt *S) {
1651  VisitStmt(S);
1652  Writer.AddSourceLocation(S->getFinallyLoc(), Record);
1653  Writer.AddStmt(S->getBlock());
1654  Code = serialization::STMT_SEH_FINALLY;
1655}
1656
1657void ASTStmtWriter::VisitSEHTryStmt(SEHTryStmt *S) {
1658  VisitStmt(S);
1659  Record.push_back(S->getIsCXXTry());
1660  Writer.AddSourceLocation(S->getTryLoc(), Record);
1661  Writer.AddStmt(S->getTryBlock());
1662  Writer.AddStmt(S->getHandler());
1663  Code = serialization::STMT_SEH_TRY;
1664}
1665
1666//===----------------------------------------------------------------------===//
1667// OpenMP Clauses.
1668//===----------------------------------------------------------------------===//
1669
1670namespace clang {
1671class OMPClauseWriter : public OMPClauseVisitor<OMPClauseWriter> {
1672  ASTStmtWriter *Writer;
1673  ASTWriter::RecordData &Record;
1674public:
1675  OMPClauseWriter(ASTStmtWriter *W, ASTWriter::RecordData &Record)
1676    : Writer(W), Record(Record) { }
1677#define OPENMP_CLAUSE(Name, Class)    \
1678  void Visit##Class(Class *S);
1679#include "clang/Basic/OpenMPKinds.def"
1680  void writeClause(OMPClause *C);
1681};
1682}
1683
1684void OMPClauseWriter::writeClause(OMPClause *C) {
1685  Record.push_back(C->getClauseKind());
1686  Visit(C);
1687  Writer->Writer.AddSourceLocation(C->getLocStart(), Record);
1688  Writer->Writer.AddSourceLocation(C->getLocEnd(), Record);
1689}
1690
1691void OMPClauseWriter::VisitOMPDefaultClause(OMPDefaultClause *C) {
1692  Record.push_back(C->getDefaultKind());
1693  Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
1694  Writer->Writer.AddSourceLocation(C->getDefaultKindKwLoc(), Record);
1695}
1696
1697void OMPClauseWriter::VisitOMPPrivateClause(OMPPrivateClause *C) {
1698  Record.push_back(C->varlist_size());
1699  Writer->Writer.AddSourceLocation(C->getLParenLoc(), Record);
1700  for (OMPVarList<OMPPrivateClause>::varlist_iterator I = C->varlist_begin(),
1701                                                      E = C->varlist_end();
1702       I != E; ++I)
1703    Writer->Writer.AddStmt(*I);
1704}
1705
1706//===----------------------------------------------------------------------===//
1707// OpenMP Directives.
1708//===----------------------------------------------------------------------===//
1709void ASTStmtWriter::VisitOMPExecutableDirective(OMPExecutableDirective *E) {
1710  VisitStmt(E);
1711  Record.push_back(E->getNumClauses());
1712  Writer.AddSourceLocation(E->getLocStart(), Record);
1713  Writer.AddSourceLocation(E->getLocEnd(), Record);
1714  OMPClauseWriter ClauseWriter(this, Record);
1715  for (unsigned i = 0; i < E->getNumClauses(); ++i) {
1716    ClauseWriter.writeClause(E->getClause(i));
1717  }
1718  Writer.AddStmt(E->getAssociatedStmt());
1719}
1720
1721void ASTStmtWriter::VisitOMPParallelDirective(OMPParallelDirective *D) {
1722  VisitOMPExecutableDirective(D);
1723  Code = serialization::STMT_OMP_PARALLEL_DIRECTIVE;
1724}
1725
1726//===----------------------------------------------------------------------===//
1727// ASTWriter Implementation
1728//===----------------------------------------------------------------------===//
1729
1730unsigned ASTWriter::RecordSwitchCaseID(SwitchCase *S) {
1731  assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
1732         "SwitchCase recorded twice");
1733  unsigned NextID = SwitchCaseIDs.size();
1734  SwitchCaseIDs[S] = NextID;
1735  return NextID;
1736}
1737
1738unsigned ASTWriter::getSwitchCaseID(SwitchCase *S) {
1739  assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
1740         "SwitchCase hasn't been seen yet");
1741  return SwitchCaseIDs[S];
1742}
1743
1744void ASTWriter::ClearSwitchCaseIDs() {
1745  SwitchCaseIDs.clear();
1746}
1747
1748/// \brief Write the given substatement or subexpression to the
1749/// bitstream.
1750void ASTWriter::WriteSubStmt(Stmt *S,
1751                             llvm::DenseMap<Stmt *, uint64_t> &SubStmtEntries,
1752                             llvm::DenseSet<Stmt *> &ParentStmts) {
1753  RecordData Record;
1754  ASTStmtWriter Writer(*this, Record);
1755  ++NumStatements;
1756
1757  if (!S) {
1758    Stream.EmitRecord(serialization::STMT_NULL_PTR, Record);
1759    return;
1760  }
1761
1762  llvm::DenseMap<Stmt *, uint64_t>::iterator I = SubStmtEntries.find(S);
1763  if (I != SubStmtEntries.end()) {
1764    Record.push_back(I->second);
1765    Stream.EmitRecord(serialization::STMT_REF_PTR, Record);
1766    return;
1767  }
1768
1769#ifndef NDEBUG
1770  assert(!ParentStmts.count(S) && "There is a Stmt cycle!");
1771
1772  struct ParentStmtInserterRAII {
1773    Stmt *S;
1774    llvm::DenseSet<Stmt *> &ParentStmts;
1775
1776    ParentStmtInserterRAII(Stmt *S, llvm::DenseSet<Stmt *> &ParentStmts)
1777      : S(S), ParentStmts(ParentStmts) {
1778      ParentStmts.insert(S);
1779    }
1780    ~ParentStmtInserterRAII() {
1781      ParentStmts.erase(S);
1782    }
1783  };
1784
1785  ParentStmtInserterRAII ParentStmtInserter(S, ParentStmts);
1786#endif
1787
1788  // Redirect ASTWriter::AddStmt to collect sub stmts.
1789  SmallVector<Stmt *, 16> SubStmts;
1790  CollectedStmts = &SubStmts;
1791
1792  Writer.Code = serialization::STMT_NULL_PTR;
1793  Writer.AbbrevToUse = 0;
1794  Writer.Visit(S);
1795
1796#ifndef NDEBUG
1797  if (Writer.Code == serialization::STMT_NULL_PTR) {
1798    SourceManager &SrcMgr
1799      = DeclIDs.begin()->first->getASTContext().getSourceManager();
1800    S->dump(SrcMgr);
1801    llvm_unreachable("Unhandled sub statement writing AST file");
1802  }
1803#endif
1804
1805  // Revert ASTWriter::AddStmt.
1806  CollectedStmts = &StmtsToEmit;
1807
1808  // Write the sub stmts in reverse order, last to first. When reading them back
1809  // we will read them in correct order by "pop"ing them from the Stmts stack.
1810  // This simplifies reading and allows to store a variable number of sub stmts
1811  // without knowing it in advance.
1812  while (!SubStmts.empty())
1813    WriteSubStmt(SubStmts.pop_back_val(), SubStmtEntries, ParentStmts);
1814
1815  Stream.EmitRecord(Writer.Code, Record, Writer.AbbrevToUse);
1816
1817  SubStmtEntries[S] = Stream.GetCurrentBitNo();
1818}
1819
1820/// \brief Flush all of the statements that have been added to the
1821/// queue via AddStmt().
1822void ASTWriter::FlushStmts() {
1823  RecordData Record;
1824
1825  // We expect to be the only consumer of the two temporary statement maps,
1826  // assert that they are empty.
1827  assert(SubStmtEntries.empty() && "unexpected entries in sub stmt map");
1828  assert(ParentStmts.empty() && "unexpected entries in parent stmt map");
1829
1830  for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
1831    WriteSubStmt(StmtsToEmit[I], SubStmtEntries, ParentStmts);
1832
1833    assert(N == StmtsToEmit.size() &&
1834           "Substatement written via AddStmt rather than WriteSubStmt!");
1835
1836    // Note that we are at the end of a full expression. Any
1837    // expression records that follow this one are part of a different
1838    // expression.
1839    Stream.EmitRecord(serialization::STMT_STOP, Record);
1840
1841    SubStmtEntries.clear();
1842    ParentStmts.clear();
1843  }
1844
1845  StmtsToEmit.clear();
1846}
1847