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