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