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