ASTWriterStmt.cpp revision f89e55ab1bfb3ea997f8b02997c611a02254eb2d
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//  This file implements serialization for Statements and Expressions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Serialization/ASTWriter.h"
15#include "clang/AST/DeclCXX.h"
16#include "clang/AST/DeclObjC.h"
17#include "clang/AST/StmtVisitor.h"
18#include "llvm/Bitcode/BitstreamWriter.h"
19using namespace clang;
20
21//===----------------------------------------------------------------------===//
22// Statement/expression serialization
23//===----------------------------------------------------------------------===//
24
25namespace clang {
26  class ASTStmtWriter : public StmtVisitor<ASTStmtWriter, void> {
27    ASTWriter &Writer;
28    ASTWriter::RecordData &Record;
29
30  public:
31    serialization::StmtCode Code;
32
33    ASTStmtWriter(ASTWriter &Writer, ASTWriter::RecordData &Record)
34      : Writer(Writer), Record(Record) { }
35
36    void
37    AddExplicitTemplateArgumentList(const ExplicitTemplateArgumentList &Args);
38
39    void VisitStmt(Stmt *S);
40    void VisitNullStmt(NullStmt *S);
41    void VisitCompoundStmt(CompoundStmt *S);
42    void VisitSwitchCase(SwitchCase *S);
43    void VisitCaseStmt(CaseStmt *S);
44    void VisitDefaultStmt(DefaultStmt *S);
45    void VisitLabelStmt(LabelStmt *S);
46    void VisitIfStmt(IfStmt *S);
47    void VisitSwitchStmt(SwitchStmt *S);
48    void VisitWhileStmt(WhileStmt *S);
49    void VisitDoStmt(DoStmt *S);
50    void VisitForStmt(ForStmt *S);
51    void VisitGotoStmt(GotoStmt *S);
52    void VisitIndirectGotoStmt(IndirectGotoStmt *S);
53    void VisitContinueStmt(ContinueStmt *S);
54    void VisitBreakStmt(BreakStmt *S);
55    void VisitReturnStmt(ReturnStmt *S);
56    void VisitDeclStmt(DeclStmt *S);
57    void VisitAsmStmt(AsmStmt *S);
58    void VisitExpr(Expr *E);
59    void VisitPredefinedExpr(PredefinedExpr *E);
60    void VisitDeclRefExpr(DeclRefExpr *E);
61    void VisitIntegerLiteral(IntegerLiteral *E);
62    void VisitFloatingLiteral(FloatingLiteral *E);
63    void VisitImaginaryLiteral(ImaginaryLiteral *E);
64    void VisitStringLiteral(StringLiteral *E);
65    void VisitCharacterLiteral(CharacterLiteral *E);
66    void VisitParenExpr(ParenExpr *E);
67    void VisitParenListExpr(ParenListExpr *E);
68    void VisitUnaryOperator(UnaryOperator *E);
69    void VisitOffsetOfExpr(OffsetOfExpr *E);
70    void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
71    void VisitArraySubscriptExpr(ArraySubscriptExpr *E);
72    void VisitCallExpr(CallExpr *E);
73    void VisitMemberExpr(MemberExpr *E);
74    void VisitCastExpr(CastExpr *E);
75    void VisitBinaryOperator(BinaryOperator *E);
76    void VisitCompoundAssignOperator(CompoundAssignOperator *E);
77    void VisitConditionalOperator(ConditionalOperator *E);
78    void VisitImplicitCastExpr(ImplicitCastExpr *E);
79    void VisitExplicitCastExpr(ExplicitCastExpr *E);
80    void VisitCStyleCastExpr(CStyleCastExpr *E);
81    void VisitCompoundLiteralExpr(CompoundLiteralExpr *E);
82    void VisitExtVectorElementExpr(ExtVectorElementExpr *E);
83    void VisitInitListExpr(InitListExpr *E);
84    void VisitDesignatedInitExpr(DesignatedInitExpr *E);
85    void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E);
86    void VisitVAArgExpr(VAArgExpr *E);
87    void VisitAddrLabelExpr(AddrLabelExpr *E);
88    void VisitStmtExpr(StmtExpr *E);
89    void VisitTypesCompatibleExpr(TypesCompatibleExpr *E);
90    void VisitChooseExpr(ChooseExpr *E);
91    void VisitGNUNullExpr(GNUNullExpr *E);
92    void VisitShuffleVectorExpr(ShuffleVectorExpr *E);
93    void VisitBlockExpr(BlockExpr *E);
94    void VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
95
96    // Objective-C Expressions
97    void VisitObjCStringLiteral(ObjCStringLiteral *E);
98    void VisitObjCEncodeExpr(ObjCEncodeExpr *E);
99    void VisitObjCSelectorExpr(ObjCSelectorExpr *E);
100    void VisitObjCProtocolExpr(ObjCProtocolExpr *E);
101    void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E);
102    void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E);
103    void VisitObjCImplicitSetterGetterRefExpr(
104                        ObjCImplicitSetterGetterRefExpr *E);
105    void VisitObjCMessageExpr(ObjCMessageExpr *E);
106    void VisitObjCIsaExpr(ObjCIsaExpr *E);
107
108    // Objective-C Statements
109    void VisitObjCForCollectionStmt(ObjCForCollectionStmt *);
110    void VisitObjCAtCatchStmt(ObjCAtCatchStmt *);
111    void VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *);
112    void VisitObjCAtTryStmt(ObjCAtTryStmt *);
113    void VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *);
114    void VisitObjCAtThrowStmt(ObjCAtThrowStmt *);
115
116    // C++ Statements
117    void VisitCXXCatchStmt(CXXCatchStmt *S);
118    void VisitCXXTryStmt(CXXTryStmt *S);
119
120    void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E);
121    void VisitCXXMemberCallExpr(CXXMemberCallExpr *E);
122    void VisitCXXConstructExpr(CXXConstructExpr *E);
123    void VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E);
124    void VisitCXXNamedCastExpr(CXXNamedCastExpr *E);
125    void VisitCXXStaticCastExpr(CXXStaticCastExpr *E);
126    void VisitCXXDynamicCastExpr(CXXDynamicCastExpr *E);
127    void VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *E);
128    void VisitCXXConstCastExpr(CXXConstCastExpr *E);
129    void VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E);
130    void VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E);
131    void VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E);
132    void VisitCXXTypeidExpr(CXXTypeidExpr *E);
133    void VisitCXXUuidofExpr(CXXUuidofExpr *E);
134    void VisitCXXThisExpr(CXXThisExpr *E);
135    void VisitCXXThrowExpr(CXXThrowExpr *E);
136    void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E);
137    void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E);
138
139    void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E);
140    void VisitCXXNewExpr(CXXNewExpr *E);
141    void VisitCXXDeleteExpr(CXXDeleteExpr *E);
142    void VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E);
143
144    void VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E);
145    void VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E);
146    void VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E);
147    void VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E);
148
149    void VisitOverloadExpr(OverloadExpr *E);
150    void VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E);
151    void VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E);
152
153    void VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E);
154    void VisitCXXNoexceptExpr(CXXNoexceptExpr *E);
155
156    void VisitOpaqueValueExpr(OpaqueValueExpr *E);
157  };
158}
159
160void ASTStmtWriter::
161AddExplicitTemplateArgumentList(const ExplicitTemplateArgumentList &Args) {
162  Writer.AddSourceLocation(Args.LAngleLoc, Record);
163  Writer.AddSourceLocation(Args.RAngleLoc, Record);
164  for (unsigned i=0; i != Args.NumTemplateArgs; ++i)
165    Writer.AddTemplateArgumentLoc(Args.getTemplateArgs()[i], Record);
166}
167
168void ASTStmtWriter::VisitStmt(Stmt *S) {
169}
170
171void ASTStmtWriter::VisitNullStmt(NullStmt *S) {
172  VisitStmt(S);
173  Writer.AddSourceLocation(S->getSemiLoc(), Record);
174  Code = serialization::STMT_NULL;
175}
176
177void ASTStmtWriter::VisitCompoundStmt(CompoundStmt *S) {
178  VisitStmt(S);
179  Record.push_back(S->size());
180  for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end();
181       CS != CSEnd; ++CS)
182    Writer.AddStmt(*CS);
183  Writer.AddSourceLocation(S->getLBracLoc(), Record);
184  Writer.AddSourceLocation(S->getRBracLoc(), Record);
185  Code = serialization::STMT_COMPOUND;
186}
187
188void ASTStmtWriter::VisitSwitchCase(SwitchCase *S) {
189  VisitStmt(S);
190  Record.push_back(Writer.getSwitchCaseID(S));
191}
192
193void ASTStmtWriter::VisitCaseStmt(CaseStmt *S) {
194  VisitSwitchCase(S);
195  Writer.AddStmt(S->getLHS());
196  Writer.AddStmt(S->getRHS());
197  Writer.AddStmt(S->getSubStmt());
198  Writer.AddSourceLocation(S->getCaseLoc(), Record);
199  Writer.AddSourceLocation(S->getEllipsisLoc(), Record);
200  Writer.AddSourceLocation(S->getColonLoc(), Record);
201  Code = serialization::STMT_CASE;
202}
203
204void ASTStmtWriter::VisitDefaultStmt(DefaultStmt *S) {
205  VisitSwitchCase(S);
206  Writer.AddStmt(S->getSubStmt());
207  Writer.AddSourceLocation(S->getDefaultLoc(), Record);
208  Writer.AddSourceLocation(S->getColonLoc(), Record);
209  Code = serialization::STMT_DEFAULT;
210}
211
212void ASTStmtWriter::VisitLabelStmt(LabelStmt *S) {
213  VisitStmt(S);
214  Writer.AddIdentifierRef(S->getID(), Record);
215  Writer.AddStmt(S->getSubStmt());
216  Writer.AddSourceLocation(S->getIdentLoc(), Record);
217  Record.push_back(S->isUsed());
218  Record.push_back(S->HasUnusedAttribute());
219  Record.push_back(Writer.GetLabelID(S));
220  Code = serialization::STMT_LABEL;
221}
222
223void ASTStmtWriter::VisitIfStmt(IfStmt *S) {
224  VisitStmt(S);
225  Writer.AddDeclRef(S->getConditionVariable(), Record);
226  Writer.AddStmt(S->getCond());
227  Writer.AddStmt(S->getThen());
228  Writer.AddStmt(S->getElse());
229  Writer.AddSourceLocation(S->getIfLoc(), Record);
230  Writer.AddSourceLocation(S->getElseLoc(), Record);
231  Code = serialization::STMT_IF;
232}
233
234void ASTStmtWriter::VisitSwitchStmt(SwitchStmt *S) {
235  VisitStmt(S);
236  Writer.AddDeclRef(S->getConditionVariable(), Record);
237  Writer.AddStmt(S->getCond());
238  Writer.AddStmt(S->getBody());
239  Writer.AddSourceLocation(S->getSwitchLoc(), Record);
240  Record.push_back(S->isAllEnumCasesCovered());
241  for (SwitchCase *SC = S->getSwitchCaseList(); SC;
242       SC = SC->getNextSwitchCase())
243    Record.push_back(Writer.RecordSwitchCaseID(SC));
244  Code = serialization::STMT_SWITCH;
245}
246
247void ASTStmtWriter::VisitWhileStmt(WhileStmt *S) {
248  VisitStmt(S);
249  Writer.AddDeclRef(S->getConditionVariable(), Record);
250  Writer.AddStmt(S->getCond());
251  Writer.AddStmt(S->getBody());
252  Writer.AddSourceLocation(S->getWhileLoc(), Record);
253  Code = serialization::STMT_WHILE;
254}
255
256void ASTStmtWriter::VisitDoStmt(DoStmt *S) {
257  VisitStmt(S);
258  Writer.AddStmt(S->getCond());
259  Writer.AddStmt(S->getBody());
260  Writer.AddSourceLocation(S->getDoLoc(), Record);
261  Writer.AddSourceLocation(S->getWhileLoc(), Record);
262  Writer.AddSourceLocation(S->getRParenLoc(), Record);
263  Code = serialization::STMT_DO;
264}
265
266void ASTStmtWriter::VisitForStmt(ForStmt *S) {
267  VisitStmt(S);
268  Writer.AddStmt(S->getInit());
269  Writer.AddStmt(S->getCond());
270  Writer.AddDeclRef(S->getConditionVariable(), Record);
271  Writer.AddStmt(S->getInc());
272  Writer.AddStmt(S->getBody());
273  Writer.AddSourceLocation(S->getForLoc(), Record);
274  Writer.AddSourceLocation(S->getLParenLoc(), Record);
275  Writer.AddSourceLocation(S->getRParenLoc(), Record);
276  Code = serialization::STMT_FOR;
277}
278
279void ASTStmtWriter::VisitGotoStmt(GotoStmt *S) {
280  VisitStmt(S);
281  Record.push_back(Writer.GetLabelID(S->getLabel()));
282  Writer.AddSourceLocation(S->getGotoLoc(), Record);
283  Writer.AddSourceLocation(S->getLabelLoc(), Record);
284  Code = serialization::STMT_GOTO;
285}
286
287void ASTStmtWriter::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
288  VisitStmt(S);
289  Writer.AddSourceLocation(S->getGotoLoc(), Record);
290  Writer.AddSourceLocation(S->getStarLoc(), Record);
291  Writer.AddStmt(S->getTarget());
292  Code = serialization::STMT_INDIRECT_GOTO;
293}
294
295void ASTStmtWriter::VisitContinueStmt(ContinueStmt *S) {
296  VisitStmt(S);
297  Writer.AddSourceLocation(S->getContinueLoc(), Record);
298  Code = serialization::STMT_CONTINUE;
299}
300
301void ASTStmtWriter::VisitBreakStmt(BreakStmt *S) {
302  VisitStmt(S);
303  Writer.AddSourceLocation(S->getBreakLoc(), Record);
304  Code = serialization::STMT_BREAK;
305}
306
307void ASTStmtWriter::VisitReturnStmt(ReturnStmt *S) {
308  VisitStmt(S);
309  Writer.AddStmt(S->getRetValue());
310  Writer.AddSourceLocation(S->getReturnLoc(), Record);
311  Writer.AddDeclRef(S->getNRVOCandidate(), Record);
312  Code = serialization::STMT_RETURN;
313}
314
315void ASTStmtWriter::VisitDeclStmt(DeclStmt *S) {
316  VisitStmt(S);
317  Writer.AddSourceLocation(S->getStartLoc(), Record);
318  Writer.AddSourceLocation(S->getEndLoc(), Record);
319  DeclGroupRef DG = S->getDeclGroup();
320  for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D)
321    Writer.AddDeclRef(*D, Record);
322  Code = serialization::STMT_DECL;
323}
324
325void ASTStmtWriter::VisitAsmStmt(AsmStmt *S) {
326  VisitStmt(S);
327  Record.push_back(S->getNumOutputs());
328  Record.push_back(S->getNumInputs());
329  Record.push_back(S->getNumClobbers());
330  Writer.AddSourceLocation(S->getAsmLoc(), Record);
331  Writer.AddSourceLocation(S->getRParenLoc(), Record);
332  Record.push_back(S->isVolatile());
333  Record.push_back(S->isSimple());
334  Record.push_back(S->isMSAsm());
335  Writer.AddStmt(S->getAsmString());
336
337  // Outputs
338  for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) {
339    Writer.AddIdentifierRef(S->getOutputIdentifier(I), Record);
340    Writer.AddStmt(S->getOutputConstraintLiteral(I));
341    Writer.AddStmt(S->getOutputExpr(I));
342  }
343
344  // Inputs
345  for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) {
346    Writer.AddIdentifierRef(S->getInputIdentifier(I), Record);
347    Writer.AddStmt(S->getInputConstraintLiteral(I));
348    Writer.AddStmt(S->getInputExpr(I));
349  }
350
351  // Clobbers
352  for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I)
353    Writer.AddStmt(S->getClobber(I));
354
355  Code = serialization::STMT_ASM;
356}
357
358void ASTStmtWriter::VisitExpr(Expr *E) {
359  VisitStmt(E);
360  Writer.AddTypeRef(E->getType(), Record);
361  Record.push_back(E->isTypeDependent());
362  Record.push_back(E->isValueDependent());
363  Record.push_back(E->getValueKind());
364  Record.push_back(E->getObjectKind());
365}
366
367void ASTStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) {
368  VisitExpr(E);
369  Writer.AddSourceLocation(E->getLocation(), Record);
370  Record.push_back(E->getIdentType()); // FIXME: stable encoding
371  Code = serialization::EXPR_PREDEFINED;
372}
373
374void ASTStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) {
375  VisitExpr(E);
376
377  Record.push_back(E->hasQualifier());
378  unsigned NumTemplateArgs = E->getNumTemplateArgs();
379  assert((NumTemplateArgs != 0) == E->hasExplicitTemplateArgs() &&
380         "Template args list with no args ?");
381  Record.push_back(NumTemplateArgs);
382
383  if (E->hasQualifier()) {
384    Writer.AddNestedNameSpecifier(E->getQualifier(), Record);
385    Writer.AddSourceRange(E->getQualifierRange(), Record);
386  }
387
388  if (NumTemplateArgs)
389    AddExplicitTemplateArgumentList(E->getExplicitTemplateArgs());
390
391  Writer.AddDeclRef(E->getDecl(), Record);
392  Writer.AddSourceLocation(E->getLocation(), Record);
393  Writer.AddDeclarationNameLoc(E->DNLoc, E->getDecl()->getDeclName(), Record);
394  Code = serialization::EXPR_DECL_REF;
395}
396
397void ASTStmtWriter::VisitIntegerLiteral(IntegerLiteral *E) {
398  VisitExpr(E);
399  Writer.AddSourceLocation(E->getLocation(), Record);
400  Writer.AddAPInt(E->getValue(), Record);
401  Code = serialization::EXPR_INTEGER_LITERAL;
402}
403
404void ASTStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) {
405  VisitExpr(E);
406  Writer.AddAPFloat(E->getValue(), Record);
407  Record.push_back(E->isExact());
408  Writer.AddSourceLocation(E->getLocation(), Record);
409  Code = serialization::EXPR_FLOATING_LITERAL;
410}
411
412void ASTStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) {
413  VisitExpr(E);
414  Writer.AddStmt(E->getSubExpr());
415  Code = serialization::EXPR_IMAGINARY_LITERAL;
416}
417
418void ASTStmtWriter::VisitStringLiteral(StringLiteral *E) {
419  VisitExpr(E);
420  Record.push_back(E->getByteLength());
421  Record.push_back(E->getNumConcatenated());
422  Record.push_back(E->isWide());
423  // FIXME: String data should be stored as a blob at the end of the
424  // StringLiteral. However, we can't do so now because we have no
425  // provision for coping with abbreviations when we're jumping around
426  // the AST file during deserialization.
427  Record.append(E->getString().begin(), E->getString().end());
428  for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
429    Writer.AddSourceLocation(E->getStrTokenLoc(I), Record);
430  Code = serialization::EXPR_STRING_LITERAL;
431}
432
433void ASTStmtWriter::VisitCharacterLiteral(CharacterLiteral *E) {
434  VisitExpr(E);
435  Record.push_back(E->getValue());
436  Writer.AddSourceLocation(E->getLocation(), Record);
437  Record.push_back(E->isWide());
438  Code = serialization::EXPR_CHARACTER_LITERAL;
439}
440
441void ASTStmtWriter::VisitParenExpr(ParenExpr *E) {
442  VisitExpr(E);
443  Writer.AddSourceLocation(E->getLParen(), Record);
444  Writer.AddSourceLocation(E->getRParen(), Record);
445  Writer.AddStmt(E->getSubExpr());
446  Code = serialization::EXPR_PAREN;
447}
448
449void ASTStmtWriter::VisitParenListExpr(ParenListExpr *E) {
450  VisitExpr(E);
451  Record.push_back(E->NumExprs);
452  for (unsigned i=0; i != E->NumExprs; ++i)
453    Writer.AddStmt(E->Exprs[i]);
454  Writer.AddSourceLocation(E->LParenLoc, Record);
455  Writer.AddSourceLocation(E->RParenLoc, Record);
456  Code = serialization::EXPR_PAREN_LIST;
457}
458
459void ASTStmtWriter::VisitUnaryOperator(UnaryOperator *E) {
460  VisitExpr(E);
461  Writer.AddStmt(E->getSubExpr());
462  Record.push_back(E->getOpcode()); // FIXME: stable encoding
463  Writer.AddSourceLocation(E->getOperatorLoc(), Record);
464  Code = serialization::EXPR_UNARY_OPERATOR;
465}
466
467void ASTStmtWriter::VisitOffsetOfExpr(OffsetOfExpr *E) {
468  VisitExpr(E);
469  Record.push_back(E->getNumComponents());
470  Record.push_back(E->getNumExpressions());
471  Writer.AddSourceLocation(E->getOperatorLoc(), Record);
472  Writer.AddSourceLocation(E->getRParenLoc(), Record);
473  Writer.AddTypeSourceInfo(E->getTypeSourceInfo(), Record);
474  for (unsigned I = 0, N = E->getNumComponents(); I != N; ++I) {
475    const OffsetOfExpr::OffsetOfNode &ON = E->getComponent(I);
476    Record.push_back(ON.getKind()); // FIXME: Stable encoding
477    Writer.AddSourceLocation(ON.getRange().getBegin(), Record);
478    Writer.AddSourceLocation(ON.getRange().getEnd(), Record);
479    switch (ON.getKind()) {
480    case OffsetOfExpr::OffsetOfNode::Array:
481      Record.push_back(ON.getArrayExprIndex());
482      break;
483
484    case OffsetOfExpr::OffsetOfNode::Field:
485      Writer.AddDeclRef(ON.getField(), Record);
486      break;
487
488    case OffsetOfExpr::OffsetOfNode::Identifier:
489      Writer.AddIdentifierRef(ON.getFieldName(), Record);
490      break;
491
492    case OffsetOfExpr::OffsetOfNode::Base:
493      Writer.AddCXXBaseSpecifier(*ON.getBase(), Record);
494      break;
495    }
496  }
497  for (unsigned I = 0, N = E->getNumExpressions(); I != N; ++I)
498    Writer.AddStmt(E->getIndexExpr(I));
499  Code = serialization::EXPR_OFFSETOF;
500}
501
502void ASTStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
503  VisitExpr(E);
504  Record.push_back(E->isSizeOf());
505  if (E->isArgumentType())
506    Writer.AddTypeSourceInfo(E->getArgumentTypeInfo(), Record);
507  else {
508    Record.push_back(0);
509    Writer.AddStmt(E->getArgumentExpr());
510  }
511  Writer.AddSourceLocation(E->getOperatorLoc(), Record);
512  Writer.AddSourceLocation(E->getRParenLoc(), Record);
513  Code = serialization::EXPR_SIZEOF_ALIGN_OF;
514}
515
516void ASTStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
517  VisitExpr(E);
518  Writer.AddStmt(E->getLHS());
519  Writer.AddStmt(E->getRHS());
520  Writer.AddSourceLocation(E->getRBracketLoc(), Record);
521  Code = serialization::EXPR_ARRAY_SUBSCRIPT;
522}
523
524void ASTStmtWriter::VisitCallExpr(CallExpr *E) {
525  VisitExpr(E);
526  Record.push_back(E->getNumArgs());
527  Writer.AddSourceLocation(E->getRParenLoc(), Record);
528  Writer.AddStmt(E->getCallee());
529  for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
530       Arg != ArgEnd; ++Arg)
531    Writer.AddStmt(*Arg);
532  Code = serialization::EXPR_CALL;
533}
534
535void ASTStmtWriter::VisitMemberExpr(MemberExpr *E) {
536  // Don't call VisitExpr, we'll write everything here.
537
538  Record.push_back(E->hasQualifier());
539  if (E->hasQualifier()) {
540    Writer.AddNestedNameSpecifier(E->getQualifier(), Record);
541    Writer.AddSourceRange(E->getQualifierRange(), Record);
542  }
543
544  unsigned NumTemplateArgs = E->getNumTemplateArgs();
545  assert((NumTemplateArgs != 0) == E->hasExplicitTemplateArgs() &&
546         "Template args list with no args ?");
547  Record.push_back(NumTemplateArgs);
548  if (NumTemplateArgs) {
549    Writer.AddSourceLocation(E->getLAngleLoc(), Record);
550    Writer.AddSourceLocation(E->getRAngleLoc(), Record);
551    for (unsigned i=0; i != NumTemplateArgs; ++i)
552      Writer.AddTemplateArgumentLoc(E->getTemplateArgs()[i], Record);
553  }
554
555  DeclAccessPair FoundDecl = E->getFoundDecl();
556  Writer.AddDeclRef(FoundDecl.getDecl(), Record);
557  Record.push_back(FoundDecl.getAccess());
558
559  Writer.AddTypeRef(E->getType(), Record);
560  Record.push_back(E->getValueKind());
561  Record.push_back(E->getObjectKind());
562  Writer.AddStmt(E->getBase());
563  Writer.AddDeclRef(E->getMemberDecl(), Record);
564  Writer.AddSourceLocation(E->getMemberLoc(), Record);
565  Record.push_back(E->isArrow());
566  Writer.AddDeclarationNameLoc(E->MemberDNLoc,
567                               E->getMemberDecl()->getDeclName(), Record);
568  Code = serialization::EXPR_MEMBER;
569}
570
571void ASTStmtWriter::VisitObjCIsaExpr(ObjCIsaExpr *E) {
572  VisitExpr(E);
573  Writer.AddStmt(E->getBase());
574  Writer.AddSourceLocation(E->getIsaMemberLoc(), Record);
575  Record.push_back(E->isArrow());
576  Code = serialization::EXPR_OBJC_ISA;
577}
578
579void ASTStmtWriter::VisitCastExpr(CastExpr *E) {
580  VisitExpr(E);
581  Record.push_back(E->path_size());
582  Writer.AddStmt(E->getSubExpr());
583  Record.push_back(E->getCastKind()); // FIXME: stable encoding
584
585  for (CastExpr::path_iterator
586         PI = E->path_begin(), PE = E->path_end(); PI != PE; ++PI)
587    Writer.AddCXXBaseSpecifier(**PI, Record);
588}
589
590void ASTStmtWriter::VisitBinaryOperator(BinaryOperator *E) {
591  VisitExpr(E);
592  Writer.AddStmt(E->getLHS());
593  Writer.AddStmt(E->getRHS());
594  Record.push_back(E->getOpcode()); // FIXME: stable encoding
595  Writer.AddSourceLocation(E->getOperatorLoc(), Record);
596  Code = serialization::EXPR_BINARY_OPERATOR;
597}
598
599void ASTStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
600  VisitBinaryOperator(E);
601  Writer.AddTypeRef(E->getComputationLHSType(), Record);
602  Writer.AddTypeRef(E->getComputationResultType(), Record);
603  Code = serialization::EXPR_COMPOUND_ASSIGN_OPERATOR;
604}
605
606void ASTStmtWriter::VisitConditionalOperator(ConditionalOperator *E) {
607  VisitExpr(E);
608  Writer.AddStmt(E->getCond());
609  Writer.AddStmt(E->getLHS());
610  Writer.AddStmt(E->getRHS());
611  Writer.AddStmt(E->getSAVE());
612  Writer.AddSourceLocation(E->getQuestionLoc(), Record);
613  Writer.AddSourceLocation(E->getColonLoc(), Record);
614  Code = serialization::EXPR_CONDITIONAL_OPERATOR;
615}
616
617void ASTStmtWriter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
618  VisitCastExpr(E);
619  Code = serialization::EXPR_IMPLICIT_CAST;
620}
621
622void ASTStmtWriter::VisitExplicitCastExpr(ExplicitCastExpr *E) {
623  VisitCastExpr(E);
624  Writer.AddTypeSourceInfo(E->getTypeInfoAsWritten(), Record);
625}
626
627void ASTStmtWriter::VisitCStyleCastExpr(CStyleCastExpr *E) {
628  VisitExplicitCastExpr(E);
629  Writer.AddSourceLocation(E->getLParenLoc(), Record);
630  Writer.AddSourceLocation(E->getRParenLoc(), Record);
631  Code = serialization::EXPR_CSTYLE_CAST;
632}
633
634void ASTStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
635  VisitExpr(E);
636  Writer.AddSourceLocation(E->getLParenLoc(), Record);
637  Writer.AddTypeSourceInfo(E->getTypeSourceInfo(), Record);
638  Writer.AddStmt(E->getInitializer());
639  Record.push_back(E->isFileScope());
640  Code = serialization::EXPR_COMPOUND_LITERAL;
641}
642
643void ASTStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
644  VisitExpr(E);
645  Writer.AddStmt(E->getBase());
646  Writer.AddIdentifierRef(&E->getAccessor(), Record);
647  Writer.AddSourceLocation(E->getAccessorLoc(), Record);
648  Code = serialization::EXPR_EXT_VECTOR_ELEMENT;
649}
650
651void ASTStmtWriter::VisitInitListExpr(InitListExpr *E) {
652  VisitExpr(E);
653  Record.push_back(E->getNumInits());
654  for (unsigned I = 0, N = E->getNumInits(); I != N; ++I)
655    Writer.AddStmt(E->getInit(I));
656  Writer.AddStmt(E->getSyntacticForm());
657  Writer.AddSourceLocation(E->getLBraceLoc(), Record);
658  Writer.AddSourceLocation(E->getRBraceLoc(), Record);
659  Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record);
660  Record.push_back(E->hadArrayRangeDesignator());
661  Code = serialization::EXPR_INIT_LIST;
662}
663
664void ASTStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
665  VisitExpr(E);
666  Record.push_back(E->getNumSubExprs());
667  for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
668    Writer.AddStmt(E->getSubExpr(I));
669  Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record);
670  Record.push_back(E->usesGNUSyntax());
671  for (DesignatedInitExpr::designators_iterator D = E->designators_begin(),
672                                             DEnd = E->designators_end();
673       D != DEnd; ++D) {
674    if (D->isFieldDesignator()) {
675      if (FieldDecl *Field = D->getField()) {
676        Record.push_back(serialization::DESIG_FIELD_DECL);
677        Writer.AddDeclRef(Field, Record);
678      } else {
679        Record.push_back(serialization::DESIG_FIELD_NAME);
680        Writer.AddIdentifierRef(D->getFieldName(), Record);
681      }
682      Writer.AddSourceLocation(D->getDotLoc(), Record);
683      Writer.AddSourceLocation(D->getFieldLoc(), Record);
684    } else if (D->isArrayDesignator()) {
685      Record.push_back(serialization::DESIG_ARRAY);
686      Record.push_back(D->getFirstExprIndex());
687      Writer.AddSourceLocation(D->getLBracketLoc(), Record);
688      Writer.AddSourceLocation(D->getRBracketLoc(), Record);
689    } else {
690      assert(D->isArrayRangeDesignator() && "Unknown designator");
691      Record.push_back(serialization::DESIG_ARRAY_RANGE);
692      Record.push_back(D->getFirstExprIndex());
693      Writer.AddSourceLocation(D->getLBracketLoc(), Record);
694      Writer.AddSourceLocation(D->getEllipsisLoc(), Record);
695      Writer.AddSourceLocation(D->getRBracketLoc(), Record);
696    }
697  }
698  Code = serialization::EXPR_DESIGNATED_INIT;
699}
700
701void ASTStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
702  VisitExpr(E);
703  Code = serialization::EXPR_IMPLICIT_VALUE_INIT;
704}
705
706void ASTStmtWriter::VisitVAArgExpr(VAArgExpr *E) {
707  VisitExpr(E);
708  Writer.AddStmt(E->getSubExpr());
709  Writer.AddTypeSourceInfo(E->getWrittenTypeInfo(), Record);
710  Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
711  Writer.AddSourceLocation(E->getRParenLoc(), Record);
712  Code = serialization::EXPR_VA_ARG;
713}
714
715void ASTStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) {
716  VisitExpr(E);
717  Writer.AddSourceLocation(E->getAmpAmpLoc(), Record);
718  Writer.AddSourceLocation(E->getLabelLoc(), Record);
719  Record.push_back(Writer.GetLabelID(E->getLabel()));
720  Code = serialization::EXPR_ADDR_LABEL;
721}
722
723void ASTStmtWriter::VisitStmtExpr(StmtExpr *E) {
724  VisitExpr(E);
725  Writer.AddStmt(E->getSubStmt());
726  Writer.AddSourceLocation(E->getLParenLoc(), Record);
727  Writer.AddSourceLocation(E->getRParenLoc(), Record);
728  Code = serialization::EXPR_STMT;
729}
730
731void ASTStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) {
732  VisitExpr(E);
733  Writer.AddTypeSourceInfo(E->getArgTInfo1(), Record);
734  Writer.AddTypeSourceInfo(E->getArgTInfo2(), Record);
735  Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
736  Writer.AddSourceLocation(E->getRParenLoc(), Record);
737  Code = serialization::EXPR_TYPES_COMPATIBLE;
738}
739
740void ASTStmtWriter::VisitChooseExpr(ChooseExpr *E) {
741  VisitExpr(E);
742  Writer.AddStmt(E->getCond());
743  Writer.AddStmt(E->getLHS());
744  Writer.AddStmt(E->getRHS());
745  Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
746  Writer.AddSourceLocation(E->getRParenLoc(), Record);
747  Code = serialization::EXPR_CHOOSE;
748}
749
750void ASTStmtWriter::VisitGNUNullExpr(GNUNullExpr *E) {
751  VisitExpr(E);
752  Writer.AddSourceLocation(E->getTokenLocation(), Record);
753  Code = serialization::EXPR_GNU_NULL;
754}
755
756void ASTStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
757  VisitExpr(E);
758  Record.push_back(E->getNumSubExprs());
759  for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I)
760    Writer.AddStmt(E->getExpr(I));
761  Writer.AddSourceLocation(E->getBuiltinLoc(), Record);
762  Writer.AddSourceLocation(E->getRParenLoc(), Record);
763  Code = serialization::EXPR_SHUFFLE_VECTOR;
764}
765
766void ASTStmtWriter::VisitBlockExpr(BlockExpr *E) {
767  VisitExpr(E);
768  Writer.AddDeclRef(E->getBlockDecl(), Record);
769  Record.push_back(E->hasBlockDeclRefExprs());
770  Code = serialization::EXPR_BLOCK;
771}
772
773void ASTStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
774  VisitExpr(E);
775  Writer.AddDeclRef(E->getDecl(), Record);
776  Writer.AddSourceLocation(E->getLocation(), Record);
777  Record.push_back(E->isByRef());
778  Record.push_back(E->isConstQualAdded());
779  Writer.AddStmt(E->getCopyConstructorExpr());
780  Code = serialization::EXPR_BLOCK_DECL_REF;
781}
782
783//===----------------------------------------------------------------------===//
784// Objective-C Expressions and Statements.
785//===----------------------------------------------------------------------===//
786
787void ASTStmtWriter::VisitObjCStringLiteral(ObjCStringLiteral *E) {
788  VisitExpr(E);
789  Writer.AddStmt(E->getString());
790  Writer.AddSourceLocation(E->getAtLoc(), Record);
791  Code = serialization::EXPR_OBJC_STRING_LITERAL;
792}
793
794void ASTStmtWriter::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
795  VisitExpr(E);
796  Writer.AddTypeSourceInfo(E->getEncodedTypeSourceInfo(), Record);
797  Writer.AddSourceLocation(E->getAtLoc(), Record);
798  Writer.AddSourceLocation(E->getRParenLoc(), Record);
799  Code = serialization::EXPR_OBJC_ENCODE;
800}
801
802void ASTStmtWriter::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
803  VisitExpr(E);
804  Writer.AddSelectorRef(E->getSelector(), Record);
805  Writer.AddSourceLocation(E->getAtLoc(), Record);
806  Writer.AddSourceLocation(E->getRParenLoc(), Record);
807  Code = serialization::EXPR_OBJC_SELECTOR_EXPR;
808}
809
810void ASTStmtWriter::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
811  VisitExpr(E);
812  Writer.AddDeclRef(E->getProtocol(), Record);
813  Writer.AddSourceLocation(E->getAtLoc(), Record);
814  Writer.AddSourceLocation(E->getRParenLoc(), Record);
815  Code = serialization::EXPR_OBJC_PROTOCOL_EXPR;
816}
817
818void ASTStmtWriter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
819  VisitExpr(E);
820  Writer.AddDeclRef(E->getDecl(), Record);
821  Writer.AddSourceLocation(E->getLocation(), Record);
822  Writer.AddStmt(E->getBase());
823  Record.push_back(E->isArrow());
824  Record.push_back(E->isFreeIvar());
825  Code = serialization::EXPR_OBJC_IVAR_REF_EXPR;
826}
827
828void ASTStmtWriter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
829  VisitExpr(E);
830  Writer.AddDeclRef(E->getProperty(), Record);
831  Writer.AddSourceLocation(E->getLocation(), Record);
832  Writer.AddSourceLocation(E->getSuperLocation(), Record);
833  if (E->isSuperReceiver())
834    Writer.AddTypeRef(E->getSuperType(), Record);
835  else
836    Writer.AddStmt(E->getBase());
837
838  Code = serialization::EXPR_OBJC_PROPERTY_REF_EXPR;
839}
840
841void ASTStmtWriter::VisitObjCImplicitSetterGetterRefExpr(
842                                  ObjCImplicitSetterGetterRefExpr *E) {
843  VisitExpr(E);
844  Writer.AddDeclRef(E->getGetterMethod(), Record);
845  Writer.AddDeclRef(E->getSetterMethod(), Record);
846
847  // NOTE: InterfaceDecl and Base are mutually exclusive.
848  Writer.AddDeclRef(E->getInterfaceDecl(), Record);
849  Writer.AddStmt(E->getBase());
850  Writer.AddSourceLocation(E->getLocation(), Record);
851  Writer.AddSourceLocation(E->getClassLoc(), Record);
852  Writer.AddSourceLocation(E->getSuperLocation(), Record);
853  Writer.AddTypeRef(E->getSuperType(), Record);
854  Code = serialization::EXPR_OBJC_KVC_REF_EXPR;
855}
856
857void ASTStmtWriter::VisitObjCMessageExpr(ObjCMessageExpr *E) {
858  VisitExpr(E);
859  Record.push_back(E->getNumArgs());
860  Record.push_back((unsigned)E->getReceiverKind()); // FIXME: stable encoding
861  switch (E->getReceiverKind()) {
862  case ObjCMessageExpr::Instance:
863    Writer.AddStmt(E->getInstanceReceiver());
864    break;
865
866  case ObjCMessageExpr::Class:
867    Writer.AddTypeSourceInfo(E->getClassReceiverTypeInfo(), Record);
868    break;
869
870  case ObjCMessageExpr::SuperClass:
871  case ObjCMessageExpr::SuperInstance:
872    Writer.AddTypeRef(E->getSuperType(), Record);
873    Writer.AddSourceLocation(E->getSuperLoc(), Record);
874    break;
875  }
876
877  if (E->getMethodDecl()) {
878    Record.push_back(1);
879    Writer.AddDeclRef(E->getMethodDecl(), Record);
880  } else {
881    Record.push_back(0);
882    Writer.AddSelectorRef(E->getSelector(), Record);
883  }
884
885  Writer.AddSourceLocation(E->getLeftLoc(), Record);
886  Writer.AddSourceLocation(E->getRightLoc(), Record);
887
888  for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end();
889       Arg != ArgEnd; ++Arg)
890    Writer.AddStmt(*Arg);
891  Code = serialization::EXPR_OBJC_MESSAGE_EXPR;
892}
893
894void ASTStmtWriter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
895  VisitStmt(S);
896  Writer.AddStmt(S->getElement());
897  Writer.AddStmt(S->getCollection());
898  Writer.AddStmt(S->getBody());
899  Writer.AddSourceLocation(S->getForLoc(), Record);
900  Writer.AddSourceLocation(S->getRParenLoc(), Record);
901  Code = serialization::STMT_OBJC_FOR_COLLECTION;
902}
903
904void ASTStmtWriter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
905  Writer.AddStmt(S->getCatchBody());
906  Writer.AddDeclRef(S->getCatchParamDecl(), Record);
907  Writer.AddSourceLocation(S->getAtCatchLoc(), Record);
908  Writer.AddSourceLocation(S->getRParenLoc(), Record);
909  Code = serialization::STMT_OBJC_CATCH;
910}
911
912void ASTStmtWriter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
913  Writer.AddStmt(S->getFinallyBody());
914  Writer.AddSourceLocation(S->getAtFinallyLoc(), Record);
915  Code = serialization::STMT_OBJC_FINALLY;
916}
917
918void ASTStmtWriter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
919  Record.push_back(S->getNumCatchStmts());
920  Record.push_back(S->getFinallyStmt() != 0);
921  Writer.AddStmt(S->getTryBody());
922  for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I)
923    Writer.AddStmt(S->getCatchStmt(I));
924  if (S->getFinallyStmt())
925    Writer.AddStmt(S->getFinallyStmt());
926  Writer.AddSourceLocation(S->getAtTryLoc(), Record);
927  Code = serialization::STMT_OBJC_AT_TRY;
928}
929
930void ASTStmtWriter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S) {
931  Writer.AddStmt(S->getSynchExpr());
932  Writer.AddStmt(S->getSynchBody());
933  Writer.AddSourceLocation(S->getAtSynchronizedLoc(), Record);
934  Code = serialization::STMT_OBJC_AT_SYNCHRONIZED;
935}
936
937void ASTStmtWriter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
938  Writer.AddStmt(S->getThrowExpr());
939  Writer.AddSourceLocation(S->getThrowLoc(), Record);
940  Code = serialization::STMT_OBJC_AT_THROW;
941}
942
943//===----------------------------------------------------------------------===//
944// C++ Expressions and Statements.
945//===----------------------------------------------------------------------===//
946
947void ASTStmtWriter::VisitCXXCatchStmt(CXXCatchStmt *S) {
948  VisitStmt(S);
949  Writer.AddSourceLocation(S->getCatchLoc(), Record);
950  Writer.AddDeclRef(S->getExceptionDecl(), Record);
951  Writer.AddStmt(S->getHandlerBlock());
952  Code = serialization::STMT_CXX_CATCH;
953}
954
955void ASTStmtWriter::VisitCXXTryStmt(CXXTryStmt *S) {
956  VisitStmt(S);
957  Record.push_back(S->getNumHandlers());
958  Writer.AddSourceLocation(S->getTryLoc(), Record);
959  Writer.AddStmt(S->getTryBlock());
960  for (unsigned i = 0, e = S->getNumHandlers(); i != e; ++i)
961    Writer.AddStmt(S->getHandler(i));
962  Code = serialization::STMT_CXX_TRY;
963}
964
965void ASTStmtWriter::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) {
966  VisitCallExpr(E);
967  Record.push_back(E->getOperator());
968  Code = serialization::EXPR_CXX_OPERATOR_CALL;
969}
970
971void ASTStmtWriter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
972  VisitCallExpr(E);
973  Code = serialization::EXPR_CXX_MEMBER_CALL;
974}
975
976void ASTStmtWriter::VisitCXXConstructExpr(CXXConstructExpr *E) {
977  VisitExpr(E);
978  Record.push_back(E->getNumArgs());
979  for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
980    Writer.AddStmt(E->getArg(I));
981  Writer.AddDeclRef(E->getConstructor(), Record);
982  Writer.AddSourceLocation(E->getLocation(), Record);
983  Record.push_back(E->isElidable());
984  Record.push_back(E->requiresZeroInitialization());
985  Record.push_back(E->getConstructionKind()); // FIXME: stable encoding
986  Writer.AddSourceRange(E->getParenRange(), Record);
987  Code = serialization::EXPR_CXX_CONSTRUCT;
988}
989
990void ASTStmtWriter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) {
991  VisitCXXConstructExpr(E);
992  Writer.AddTypeSourceInfo(E->getTypeSourceInfo(), Record);
993  Code = serialization::EXPR_CXX_TEMPORARY_OBJECT;
994}
995
996void ASTStmtWriter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
997  VisitExplicitCastExpr(E);
998  Writer.AddSourceLocation(E->getOperatorLoc(), Record);
999}
1000
1001void ASTStmtWriter::VisitCXXStaticCastExpr(CXXStaticCastExpr *E) {
1002  VisitCXXNamedCastExpr(E);
1003  Code = serialization::EXPR_CXX_STATIC_CAST;
1004}
1005
1006void ASTStmtWriter::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *E) {
1007  VisitCXXNamedCastExpr(E);
1008  Code = serialization::EXPR_CXX_DYNAMIC_CAST;
1009}
1010
1011void ASTStmtWriter::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *E) {
1012  VisitCXXNamedCastExpr(E);
1013  Code = serialization::EXPR_CXX_REINTERPRET_CAST;
1014}
1015
1016void ASTStmtWriter::VisitCXXConstCastExpr(CXXConstCastExpr *E) {
1017  VisitCXXNamedCastExpr(E);
1018  Code = serialization::EXPR_CXX_CONST_CAST;
1019}
1020
1021void ASTStmtWriter::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E) {
1022  VisitExplicitCastExpr(E);
1023  Writer.AddSourceLocation(E->getTypeBeginLoc(), Record);
1024  Writer.AddSourceLocation(E->getRParenLoc(), Record);
1025  Code = serialization::EXPR_CXX_FUNCTIONAL_CAST;
1026}
1027
1028void ASTStmtWriter::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
1029  VisitExpr(E);
1030  Record.push_back(E->getValue());
1031  Writer.AddSourceLocation(E->getLocation(), Record);
1032  Code = serialization::EXPR_CXX_BOOL_LITERAL;
1033}
1034
1035void ASTStmtWriter::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
1036  VisitExpr(E);
1037  Writer.AddSourceLocation(E->getLocation(), Record);
1038  Code = serialization::EXPR_CXX_NULL_PTR_LITERAL;
1039}
1040
1041void ASTStmtWriter::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
1042  VisitExpr(E);
1043  Writer.AddSourceRange(E->getSourceRange(), Record);
1044  if (E->isTypeOperand()) {
1045    Writer.AddTypeSourceInfo(E->getTypeOperandSourceInfo(), Record);
1046    Code = serialization::EXPR_CXX_TYPEID_TYPE;
1047  } else {
1048    Writer.AddStmt(E->getExprOperand());
1049    Code = serialization::EXPR_CXX_TYPEID_EXPR;
1050  }
1051}
1052
1053void ASTStmtWriter::VisitCXXUuidofExpr(CXXUuidofExpr *E) {
1054  VisitExpr(E);
1055  Writer.AddSourceRange(E->getSourceRange(), Record);
1056  if (E->isTypeOperand()) {
1057    Writer.AddTypeSourceInfo(E->getTypeOperandSourceInfo(), Record);
1058    Code = serialization::EXPR_CXX_UUIDOF_TYPE;
1059  } else {
1060    Writer.AddStmt(E->getExprOperand());
1061    Code = serialization::EXPR_CXX_UUIDOF_EXPR;
1062  }
1063}
1064
1065void ASTStmtWriter::VisitCXXThisExpr(CXXThisExpr *E) {
1066  VisitExpr(E);
1067  Writer.AddSourceLocation(E->getLocation(), Record);
1068  Record.push_back(E->isImplicit());
1069  Code = serialization::EXPR_CXX_THIS;
1070}
1071
1072void ASTStmtWriter::VisitCXXThrowExpr(CXXThrowExpr *E) {
1073  VisitExpr(E);
1074  Writer.AddSourceLocation(E->getThrowLoc(), Record);
1075  Writer.AddStmt(E->getSubExpr());
1076  Code = serialization::EXPR_CXX_THROW;
1077}
1078
1079void ASTStmtWriter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
1080  VisitExpr(E);
1081
1082  bool HasOtherExprStored = E->Param.getInt();
1083  // Store these first, the reader reads them before creation.
1084  Record.push_back(HasOtherExprStored);
1085  if (HasOtherExprStored)
1086    Writer.AddStmt(E->getExpr());
1087  Writer.AddDeclRef(E->getParam(), Record);
1088  Writer.AddSourceLocation(E->getUsedLocation(), Record);
1089
1090  Code = serialization::EXPR_CXX_DEFAULT_ARG;
1091}
1092
1093void ASTStmtWriter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
1094  VisitExpr(E);
1095  Writer.AddCXXTemporary(E->getTemporary(), Record);
1096  Writer.AddStmt(E->getSubExpr());
1097  Code = serialization::EXPR_CXX_BIND_TEMPORARY;
1098}
1099
1100void ASTStmtWriter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
1101  VisitExpr(E);
1102  Writer.AddTypeSourceInfo(E->getTypeSourceInfo(), Record);
1103  Writer.AddSourceLocation(E->getRParenLoc(), Record);
1104  Code = serialization::EXPR_CXX_SCALAR_VALUE_INIT;
1105}
1106
1107void ASTStmtWriter::VisitCXXNewExpr(CXXNewExpr *E) {
1108  VisitExpr(E);
1109  Record.push_back(E->isGlobalNew());
1110  Record.push_back(E->hasInitializer());
1111  Record.push_back(E->isArray());
1112  Record.push_back(E->getNumPlacementArgs());
1113  Record.push_back(E->getNumConstructorArgs());
1114  Writer.AddDeclRef(E->getOperatorNew(), Record);
1115  Writer.AddDeclRef(E->getOperatorDelete(), Record);
1116  Writer.AddDeclRef(E->getConstructor(), Record);
1117  Writer.AddTypeSourceInfo(E->getAllocatedTypeSourceInfo(), Record);
1118  Writer.AddSourceRange(E->getTypeIdParens(), Record);
1119  Writer.AddSourceLocation(E->getStartLoc(), Record);
1120  Writer.AddSourceLocation(E->getEndLoc(), Record);
1121  Writer.AddSourceLocation(E->getConstructorLParen(), Record);
1122  Writer.AddSourceLocation(E->getConstructorRParen(), Record);
1123  for (CXXNewExpr::arg_iterator I = E->raw_arg_begin(), e = E->raw_arg_end();
1124       I != e; ++I)
1125    Writer.AddStmt(*I);
1126
1127  Code = serialization::EXPR_CXX_NEW;
1128}
1129
1130void ASTStmtWriter::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1131  VisitExpr(E);
1132  Record.push_back(E->isGlobalDelete());
1133  Record.push_back(E->isArrayForm());
1134  Record.push_back(E->isArrayFormAsWritten());
1135  Writer.AddDeclRef(E->getOperatorDelete(), Record);
1136  Writer.AddStmt(E->getArgument());
1137  Writer.AddSourceLocation(E->getSourceRange().getBegin(), Record);
1138
1139  Code = serialization::EXPR_CXX_DELETE;
1140}
1141
1142void ASTStmtWriter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1143  VisitExpr(E);
1144
1145  Writer.AddStmt(E->getBase());
1146  Record.push_back(E->isArrow());
1147  Writer.AddSourceLocation(E->getOperatorLoc(), Record);
1148  Writer.AddNestedNameSpecifier(E->getQualifier(), Record);
1149  Writer.AddSourceRange(E->getQualifierRange(), Record);
1150  Writer.AddTypeSourceInfo(E->getScopeTypeInfo(), Record);
1151  Writer.AddSourceLocation(E->getColonColonLoc(), Record);
1152  Writer.AddSourceLocation(E->getTildeLoc(), Record);
1153
1154  // PseudoDestructorTypeStorage.
1155  Writer.AddIdentifierRef(E->getDestroyedTypeIdentifier(), Record);
1156  if (E->getDestroyedTypeIdentifier())
1157    Writer.AddSourceLocation(E->getDestroyedTypeLoc(), Record);
1158  else
1159    Writer.AddTypeSourceInfo(E->getDestroyedTypeInfo(), Record);
1160
1161  Code = serialization::EXPR_CXX_PSEUDO_DESTRUCTOR;
1162}
1163
1164void ASTStmtWriter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) {
1165  VisitExpr(E);
1166  Record.push_back(E->getNumTemporaries());
1167  for (unsigned i = 0, e = E->getNumTemporaries(); i != e; ++i)
1168    Writer.AddCXXTemporary(E->getTemporary(i), Record);
1169
1170  Writer.AddStmt(E->getSubExpr());
1171  Code = serialization::EXPR_CXX_EXPR_WITH_TEMPORARIES;
1172}
1173
1174void
1175ASTStmtWriter::VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E){
1176  VisitExpr(E);
1177
1178  // Don't emit anything here, NumTemplateArgs must be emitted first.
1179
1180  if (E->hasExplicitTemplateArgs()) {
1181    const ExplicitTemplateArgumentList &Args = E->getExplicitTemplateArgs();
1182    assert(Args.NumTemplateArgs &&
1183           "Num of template args was zero! AST reading will mess up!");
1184    Record.push_back(Args.NumTemplateArgs);
1185    AddExplicitTemplateArgumentList(Args);
1186  } else {
1187    Record.push_back(0);
1188  }
1189
1190  if (!E->isImplicitAccess())
1191    Writer.AddStmt(E->getBase());
1192  else
1193    Writer.AddStmt(0);
1194  Writer.AddTypeRef(E->getBaseType(), Record);
1195  Record.push_back(E->isArrow());
1196  Writer.AddSourceLocation(E->getOperatorLoc(), Record);
1197  Writer.AddNestedNameSpecifier(E->getQualifier(), Record);
1198  Writer.AddSourceRange(E->getQualifierRange(), Record);
1199  Writer.AddDeclRef(E->getFirstQualifierFoundInScope(), Record);
1200  Writer.AddDeclarationNameInfo(E->MemberNameInfo, Record);
1201  Code = serialization::EXPR_CXX_DEPENDENT_SCOPE_MEMBER;
1202}
1203
1204void
1205ASTStmtWriter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
1206  VisitExpr(E);
1207
1208  // Don't emit anything here, NumTemplateArgs must be emitted first.
1209
1210  if (E->hasExplicitTemplateArgs()) {
1211    const ExplicitTemplateArgumentList &Args = E->getExplicitTemplateArgs();
1212    assert(Args.NumTemplateArgs &&
1213           "Num of template args was zero! AST reading will mess up!");
1214    Record.push_back(Args.NumTemplateArgs);
1215    AddExplicitTemplateArgumentList(Args);
1216  } else {
1217    Record.push_back(0);
1218  }
1219
1220  Writer.AddDeclarationNameInfo(E->NameInfo, Record);
1221  Writer.AddSourceRange(E->getQualifierRange(), Record);
1222  Writer.AddNestedNameSpecifier(E->getQualifier(), Record);
1223  Code = serialization::EXPR_CXX_DEPENDENT_SCOPE_DECL_REF;
1224}
1225
1226void
1227ASTStmtWriter::VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E) {
1228  VisitExpr(E);
1229  Record.push_back(E->arg_size());
1230  for (CXXUnresolvedConstructExpr::arg_iterator
1231         ArgI = E->arg_begin(), ArgE = E->arg_end(); ArgI != ArgE; ++ArgI)
1232    Writer.AddStmt(*ArgI);
1233  Writer.AddTypeSourceInfo(E->getTypeSourceInfo(), Record);
1234  Writer.AddSourceLocation(E->getLParenLoc(), Record);
1235  Writer.AddSourceLocation(E->getRParenLoc(), Record);
1236  Code = serialization::EXPR_CXX_UNRESOLVED_CONSTRUCT;
1237}
1238
1239void ASTStmtWriter::VisitOverloadExpr(OverloadExpr *E) {
1240  VisitExpr(E);
1241
1242  // Don't emit anything here, NumTemplateArgs must be emitted first.
1243
1244  if (E->hasExplicitTemplateArgs()) {
1245    const ExplicitTemplateArgumentList &Args = E->getExplicitTemplateArgs();
1246    assert(Args.NumTemplateArgs &&
1247           "Num of template args was zero! AST reading will mess up!");
1248    Record.push_back(Args.NumTemplateArgs);
1249    AddExplicitTemplateArgumentList(Args);
1250  } else {
1251    Record.push_back(0);
1252  }
1253
1254  Record.push_back(E->getNumDecls());
1255  for (OverloadExpr::decls_iterator
1256         OvI = E->decls_begin(), OvE = E->decls_end(); OvI != OvE; ++OvI) {
1257    Writer.AddDeclRef(OvI.getDecl(), Record);
1258    Record.push_back(OvI.getAccess());
1259  }
1260
1261  Writer.AddDeclarationNameInfo(E->NameInfo, Record);
1262  Writer.AddNestedNameSpecifier(E->getQualifier(), Record);
1263  Writer.AddSourceRange(E->getQualifierRange(), Record);
1264}
1265
1266void ASTStmtWriter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
1267  VisitOverloadExpr(E);
1268  Record.push_back(E->isArrow());
1269  Record.push_back(E->hasUnresolvedUsing());
1270  Writer.AddStmt(!E->isImplicitAccess() ? E->getBase() : 0);
1271  Writer.AddTypeRef(E->getBaseType(), Record);
1272  Writer.AddSourceLocation(E->getOperatorLoc(), Record);
1273  Code = serialization::EXPR_CXX_UNRESOLVED_MEMBER;
1274}
1275
1276void ASTStmtWriter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
1277  VisitOverloadExpr(E);
1278  Record.push_back(E->requiresADL());
1279  Record.push_back(E->isOverloaded());
1280  Writer.AddDeclRef(E->getNamingClass(), Record);
1281  Code = serialization::EXPR_CXX_UNRESOLVED_LOOKUP;
1282}
1283
1284void ASTStmtWriter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1285  VisitExpr(E);
1286  Record.push_back(E->getTrait());
1287  Record.push_back(E->getValue());
1288  Writer.AddSourceRange(E->getSourceRange(), Record);
1289  Writer.AddTypeSourceInfo(E->getQueriedTypeSourceInfo(), Record);
1290  Code = serialization::EXPR_CXX_UNARY_TYPE_TRAIT;
1291}
1292
1293void ASTStmtWriter::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1294  VisitExpr(E);
1295  Record.push_back(E->getValue());
1296  Writer.AddSourceRange(E->getSourceRange(), Record);
1297  Writer.AddStmt(E->getOperand());
1298  Code = serialization::EXPR_CXX_NOEXCEPT;
1299}
1300
1301void ASTStmtWriter::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
1302  VisitExpr(E);
1303  Code = serialization::EXPR_OPAQUE_VALUE;
1304}
1305
1306//===----------------------------------------------------------------------===//
1307// ASTWriter Implementation
1308//===----------------------------------------------------------------------===//
1309
1310unsigned ASTWriter::RecordSwitchCaseID(SwitchCase *S) {
1311  assert(SwitchCaseIDs.find(S) == SwitchCaseIDs.end() &&
1312         "SwitchCase recorded twice");
1313  unsigned NextID = SwitchCaseIDs.size();
1314  SwitchCaseIDs[S] = NextID;
1315  return NextID;
1316}
1317
1318unsigned ASTWriter::getSwitchCaseID(SwitchCase *S) {
1319  assert(SwitchCaseIDs.find(S) != SwitchCaseIDs.end() &&
1320         "SwitchCase hasn't been seen yet");
1321  return SwitchCaseIDs[S];
1322}
1323
1324void ASTWriter::ClearSwitchCaseIDs() {
1325  SwitchCaseIDs.clear();
1326}
1327
1328/// \brief Retrieve the ID for the given label statement, which may
1329/// or may not have been emitted yet.
1330unsigned ASTWriter::GetLabelID(LabelStmt *S) {
1331  std::map<LabelStmt *, unsigned>::iterator Pos = LabelIDs.find(S);
1332  if (Pos != LabelIDs.end())
1333    return Pos->second;
1334
1335  unsigned NextID = LabelIDs.size();
1336  LabelIDs[S] = NextID;
1337  return NextID;
1338}
1339
1340/// \brief Write the given substatement or subexpression to the
1341/// bitstream.
1342void ASTWriter::WriteSubStmt(Stmt *S) {
1343  RecordData Record;
1344  ASTStmtWriter Writer(*this, Record);
1345  ++NumStatements;
1346
1347  if (!S) {
1348    Stream.EmitRecord(serialization::STMT_NULL_PTR, Record);
1349    return;
1350  }
1351
1352  // Redirect ASTWriter::AddStmt to collect sub stmts.
1353  llvm::SmallVector<Stmt *, 16> SubStmts;
1354  CollectedStmts = &SubStmts;
1355
1356  Writer.Code = serialization::STMT_NULL_PTR;
1357  Writer.Visit(S);
1358
1359#ifndef NDEBUG
1360  if (Writer.Code == serialization::STMT_NULL_PTR) {
1361    SourceManager &SrcMgr
1362      = DeclIDs.begin()->first->getASTContext().getSourceManager();
1363    S->dump(SrcMgr);
1364    assert(0 && "Unhandled sub statement writing AST file");
1365  }
1366#endif
1367
1368  // Revert ASTWriter::AddStmt.
1369  CollectedStmts = &StmtsToEmit;
1370
1371  // Write the sub stmts in reverse order, last to first. When reading them back
1372  // we will read them in correct order by "pop"ing them from the Stmts stack.
1373  // This simplifies reading and allows to store a variable number of sub stmts
1374  // without knowing it in advance.
1375  while (!SubStmts.empty())
1376    WriteSubStmt(SubStmts.pop_back_val());
1377
1378  Stream.EmitRecord(Writer.Code, Record);
1379}
1380
1381/// \brief Flush all of the statements that have been added to the
1382/// queue via AddStmt().
1383void ASTWriter::FlushStmts() {
1384  RecordData Record;
1385
1386  for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) {
1387    WriteSubStmt(StmtsToEmit[I]);
1388
1389    assert(N == StmtsToEmit.size() &&
1390           "Substatement writen via AddStmt rather than WriteSubStmt!");
1391
1392    // Note that we are at the end of a full expression. Any
1393    // expression records that follow this one are part of a different
1394    // expression.
1395    Stream.EmitRecord(serialization::STMT_STOP, Record);
1396  }
1397
1398  StmtsToEmit.clear();
1399}
1400