1//===--- ASTReaderStmt.cpp - Stmt/Expr Deserialization ----------*- C++ -*-===//
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// Statement/expression deserialization.  This implements the
11// ASTReader::ReadStmt method.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Serialization/ASTReader.h"
16#include "clang/AST/ASTContext.h"
17#include "clang/AST/DeclCXX.h"
18#include "clang/AST/DeclTemplate.h"
19#include "clang/AST/StmtVisitor.h"
20#include "llvm/ADT/SmallString.h"
21using namespace clang;
22using namespace clang::serialization;
23
24namespace clang {
25
26  class ASTStmtReader : public StmtVisitor<ASTStmtReader> {
27    typedef ASTReader::RecordData RecordData;
28
29    ASTReader &Reader;
30    ModuleFile &F;
31    llvm::BitstreamCursor &DeclsCursor;
32    const ASTReader::RecordData &Record;
33    unsigned &Idx;
34
35    SourceLocation ReadSourceLocation(const RecordData &R, unsigned &I) {
36      return Reader.ReadSourceLocation(F, R, I);
37    }
38
39    SourceRange ReadSourceRange(const RecordData &R, unsigned &I) {
40      return Reader.ReadSourceRange(F, R, I);
41    }
42
43    TypeSourceInfo *GetTypeSourceInfo(const RecordData &R, unsigned &I) {
44      return Reader.GetTypeSourceInfo(F, R, I);
45    }
46
47    serialization::DeclID ReadDeclID(const RecordData &R, unsigned &I) {
48      return Reader.ReadDeclID(F, R, I);
49    }
50
51    Decl *ReadDecl(const RecordData &R, unsigned &I) {
52      return Reader.ReadDecl(F, R, I);
53    }
54
55    template<typename T>
56    T *ReadDeclAs(const RecordData &R, unsigned &I) {
57      return Reader.ReadDeclAs<T>(F, R, I);
58    }
59
60    void ReadDeclarationNameLoc(DeclarationNameLoc &DNLoc, DeclarationName Name,
61                                const ASTReader::RecordData &R, unsigned &I) {
62      Reader.ReadDeclarationNameLoc(F, DNLoc, Name, R, I);
63    }
64
65    void ReadDeclarationNameInfo(DeclarationNameInfo &NameInfo,
66                                const ASTReader::RecordData &R, unsigned &I) {
67      Reader.ReadDeclarationNameInfo(F, NameInfo, R, I);
68    }
69
70  public:
71    ASTStmtReader(ASTReader &Reader, ModuleFile &F,
72                  llvm::BitstreamCursor &Cursor,
73                  const ASTReader::RecordData &Record, unsigned &Idx)
74      : Reader(Reader), F(F), DeclsCursor(Cursor), Record(Record), Idx(Idx) { }
75
76    /// \brief The number of record fields required for the Stmt class
77    /// itself.
78    static const unsigned NumStmtFields = 0;
79
80    /// \brief The number of record fields required for the Expr class
81    /// itself.
82    static const unsigned NumExprFields = NumStmtFields + 7;
83
84    /// \brief Read and initialize a ExplicitTemplateArgumentList structure.
85    void ReadTemplateKWAndArgsInfo(ASTTemplateKWAndArgsInfo &Args,
86                                   unsigned NumTemplateArgs);
87    /// \brief Read and initialize a ExplicitTemplateArgumentList structure.
88    void ReadExplicitTemplateArgumentList(ASTTemplateArgumentListInfo &ArgList,
89                                          unsigned NumTemplateArgs);
90
91    void VisitStmt(Stmt *S);
92#define STMT(Type, Base) \
93    void Visit##Type(Type *);
94#include "clang/AST/StmtNodes.inc"
95  };
96}
97
98void ASTStmtReader::
99ReadTemplateKWAndArgsInfo(ASTTemplateKWAndArgsInfo &Args,
100                          unsigned NumTemplateArgs) {
101  SourceLocation TemplateKWLoc = ReadSourceLocation(Record, Idx);
102  TemplateArgumentListInfo ArgInfo;
103  ArgInfo.setLAngleLoc(ReadSourceLocation(Record, Idx));
104  ArgInfo.setRAngleLoc(ReadSourceLocation(Record, Idx));
105  for (unsigned i = 0; i != NumTemplateArgs; ++i)
106    ArgInfo.addArgument(
107        Reader.ReadTemplateArgumentLoc(F, Record, Idx));
108  Args.initializeFrom(TemplateKWLoc, ArgInfo);
109}
110
111void ASTStmtReader::VisitStmt(Stmt *S) {
112  assert(Idx == NumStmtFields && "Incorrect statement field count");
113}
114
115void ASTStmtReader::VisitNullStmt(NullStmt *S) {
116  VisitStmt(S);
117  S->setSemiLoc(ReadSourceLocation(Record, Idx));
118  S->HasLeadingEmptyMacro = Record[Idx++];
119}
120
121void ASTStmtReader::VisitCompoundStmt(CompoundStmt *S) {
122  VisitStmt(S);
123  SmallVector<Stmt *, 16> Stmts;
124  unsigned NumStmts = Record[Idx++];
125  while (NumStmts--)
126    Stmts.push_back(Reader.ReadSubStmt());
127  S->setStmts(Reader.getContext(), Stmts.data(), Stmts.size());
128  S->setLBracLoc(ReadSourceLocation(Record, Idx));
129  S->setRBracLoc(ReadSourceLocation(Record, Idx));
130}
131
132void ASTStmtReader::VisitSwitchCase(SwitchCase *S) {
133  VisitStmt(S);
134  Reader.RecordSwitchCaseID(S, Record[Idx++]);
135}
136
137void ASTStmtReader::VisitCaseStmt(CaseStmt *S) {
138  VisitSwitchCase(S);
139  S->setLHS(Reader.ReadSubExpr());
140  S->setRHS(Reader.ReadSubExpr());
141  S->setSubStmt(Reader.ReadSubStmt());
142  S->setCaseLoc(ReadSourceLocation(Record, Idx));
143  S->setEllipsisLoc(ReadSourceLocation(Record, Idx));
144  S->setColonLoc(ReadSourceLocation(Record, Idx));
145}
146
147void ASTStmtReader::VisitDefaultStmt(DefaultStmt *S) {
148  VisitSwitchCase(S);
149  S->setSubStmt(Reader.ReadSubStmt());
150  S->setDefaultLoc(ReadSourceLocation(Record, Idx));
151  S->setColonLoc(ReadSourceLocation(Record, Idx));
152}
153
154void ASTStmtReader::VisitLabelStmt(LabelStmt *S) {
155  VisitStmt(S);
156  LabelDecl *LD = ReadDeclAs<LabelDecl>(Record, Idx);
157  LD->setStmt(S);
158  S->setDecl(LD);
159  S->setSubStmt(Reader.ReadSubStmt());
160  S->setIdentLoc(ReadSourceLocation(Record, Idx));
161}
162
163void ASTStmtReader::VisitAttributedStmt(AttributedStmt *S) {
164  VisitStmt(S);
165  uint64_t NumAttrs = Record[Idx++];
166  AttrVec Attrs;
167  Reader.ReadAttributes(F, Attrs, Record, Idx);
168  (void)NumAttrs;
169  assert(NumAttrs == S->NumAttrs);
170  assert(NumAttrs == Attrs.size());
171  std::copy(Attrs.begin(), Attrs.end(), S->Attrs);
172  S->SubStmt = Reader.ReadSubStmt();
173  S->AttrLoc = ReadSourceLocation(Record, Idx);
174}
175
176void ASTStmtReader::VisitIfStmt(IfStmt *S) {
177  VisitStmt(S);
178  S->setConditionVariable(Reader.getContext(),
179                          ReadDeclAs<VarDecl>(Record, Idx));
180  S->setCond(Reader.ReadSubExpr());
181  S->setThen(Reader.ReadSubStmt());
182  S->setElse(Reader.ReadSubStmt());
183  S->setIfLoc(ReadSourceLocation(Record, Idx));
184  S->setElseLoc(ReadSourceLocation(Record, Idx));
185}
186
187void ASTStmtReader::VisitSwitchStmt(SwitchStmt *S) {
188  VisitStmt(S);
189  S->setConditionVariable(Reader.getContext(),
190                          ReadDeclAs<VarDecl>(Record, Idx));
191  S->setCond(Reader.ReadSubExpr());
192  S->setBody(Reader.ReadSubStmt());
193  S->setSwitchLoc(ReadSourceLocation(Record, Idx));
194  if (Record[Idx++])
195    S->setAllEnumCasesCovered();
196
197  SwitchCase *PrevSC = 0;
198  for (unsigned N = Record.size(); Idx != N; ++Idx) {
199    SwitchCase *SC = Reader.getSwitchCaseWithID(Record[Idx]);
200    if (PrevSC)
201      PrevSC->setNextSwitchCase(SC);
202    else
203      S->setSwitchCaseList(SC);
204
205    PrevSC = SC;
206  }
207}
208
209void ASTStmtReader::VisitWhileStmt(WhileStmt *S) {
210  VisitStmt(S);
211  S->setConditionVariable(Reader.getContext(),
212                          ReadDeclAs<VarDecl>(Record, Idx));
213
214  S->setCond(Reader.ReadSubExpr());
215  S->setBody(Reader.ReadSubStmt());
216  S->setWhileLoc(ReadSourceLocation(Record, Idx));
217}
218
219void ASTStmtReader::VisitDoStmt(DoStmt *S) {
220  VisitStmt(S);
221  S->setCond(Reader.ReadSubExpr());
222  S->setBody(Reader.ReadSubStmt());
223  S->setDoLoc(ReadSourceLocation(Record, Idx));
224  S->setWhileLoc(ReadSourceLocation(Record, Idx));
225  S->setRParenLoc(ReadSourceLocation(Record, Idx));
226}
227
228void ASTStmtReader::VisitForStmt(ForStmt *S) {
229  VisitStmt(S);
230  S->setInit(Reader.ReadSubStmt());
231  S->setCond(Reader.ReadSubExpr());
232  S->setConditionVariable(Reader.getContext(),
233                          ReadDeclAs<VarDecl>(Record, Idx));
234  S->setInc(Reader.ReadSubExpr());
235  S->setBody(Reader.ReadSubStmt());
236  S->setForLoc(ReadSourceLocation(Record, Idx));
237  S->setLParenLoc(ReadSourceLocation(Record, Idx));
238  S->setRParenLoc(ReadSourceLocation(Record, Idx));
239}
240
241void ASTStmtReader::VisitGotoStmt(GotoStmt *S) {
242  VisitStmt(S);
243  S->setLabel(ReadDeclAs<LabelDecl>(Record, Idx));
244  S->setGotoLoc(ReadSourceLocation(Record, Idx));
245  S->setLabelLoc(ReadSourceLocation(Record, Idx));
246}
247
248void ASTStmtReader::VisitIndirectGotoStmt(IndirectGotoStmt *S) {
249  VisitStmt(S);
250  S->setGotoLoc(ReadSourceLocation(Record, Idx));
251  S->setStarLoc(ReadSourceLocation(Record, Idx));
252  S->setTarget(Reader.ReadSubExpr());
253}
254
255void ASTStmtReader::VisitContinueStmt(ContinueStmt *S) {
256  VisitStmt(S);
257  S->setContinueLoc(ReadSourceLocation(Record, Idx));
258}
259
260void ASTStmtReader::VisitBreakStmt(BreakStmt *S) {
261  VisitStmt(S);
262  S->setBreakLoc(ReadSourceLocation(Record, Idx));
263}
264
265void ASTStmtReader::VisitReturnStmt(ReturnStmt *S) {
266  VisitStmt(S);
267  S->setRetValue(Reader.ReadSubExpr());
268  S->setReturnLoc(ReadSourceLocation(Record, Idx));
269  S->setNRVOCandidate(ReadDeclAs<VarDecl>(Record, Idx));
270}
271
272void ASTStmtReader::VisitDeclStmt(DeclStmt *S) {
273  VisitStmt(S);
274  S->setStartLoc(ReadSourceLocation(Record, Idx));
275  S->setEndLoc(ReadSourceLocation(Record, Idx));
276
277  if (Idx + 1 == Record.size()) {
278    // Single declaration
279    S->setDeclGroup(DeclGroupRef(ReadDecl(Record, Idx)));
280  } else {
281    SmallVector<Decl *, 16> Decls;
282    Decls.reserve(Record.size() - Idx);
283    for (unsigned N = Record.size(); Idx != N; )
284      Decls.push_back(ReadDecl(Record, Idx));
285    S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Reader.getContext(),
286                                                   Decls.data(),
287                                                   Decls.size())));
288  }
289}
290
291void ASTStmtReader::VisitGCCAsmStmt(GCCAsmStmt *S) {
292  VisitStmt(S);
293  unsigned NumOutputs = Record[Idx++];
294  unsigned NumInputs = Record[Idx++];
295  unsigned NumClobbers = Record[Idx++];
296  S->setAsmLoc(ReadSourceLocation(Record, Idx));
297  S->setRParenLoc(ReadSourceLocation(Record, Idx));
298  S->setVolatile(Record[Idx++]);
299  S->setSimple(Record[Idx++]);
300
301  S->setAsmString(cast_or_null<StringLiteral>(Reader.ReadSubStmt()));
302
303  // Outputs and inputs
304  SmallVector<IdentifierInfo *, 16> Names;
305  SmallVector<StringLiteral*, 16> Constraints;
306  SmallVector<Stmt*, 16> Exprs;
307  for (unsigned I = 0, N = NumOutputs + NumInputs; I != N; ++I) {
308    Names.push_back(Reader.GetIdentifierInfo(F, Record, Idx));
309    Constraints.push_back(cast_or_null<StringLiteral>(Reader.ReadSubStmt()));
310    Exprs.push_back(Reader.ReadSubStmt());
311  }
312
313  // Constraints
314  SmallVector<StringLiteral*, 16> Clobbers;
315  for (unsigned I = 0; I != NumClobbers; ++I)
316    Clobbers.push_back(cast_or_null<StringLiteral>(Reader.ReadSubStmt()));
317
318  S->setOutputsAndInputsAndClobbers(Reader.getContext(),
319                                    Names.data(), Constraints.data(),
320                                    Exprs.data(), NumOutputs, NumInputs,
321                                    Clobbers.data(), NumClobbers);
322}
323
324void ASTStmtReader::VisitMSAsmStmt(MSAsmStmt *S) {
325  // FIXME: Statement reader not yet implemented for MS style inline asm.
326  VisitStmt(S);
327}
328
329void ASTStmtReader::VisitExpr(Expr *E) {
330  VisitStmt(E);
331  E->setType(Reader.readType(F, Record, Idx));
332  E->setTypeDependent(Record[Idx++]);
333  E->setValueDependent(Record[Idx++]);
334  E->setInstantiationDependent(Record[Idx++]);
335  E->ExprBits.ContainsUnexpandedParameterPack = Record[Idx++];
336  E->setValueKind(static_cast<ExprValueKind>(Record[Idx++]));
337  E->setObjectKind(static_cast<ExprObjectKind>(Record[Idx++]));
338  assert(Idx == NumExprFields && "Incorrect expression field count");
339}
340
341void ASTStmtReader::VisitPredefinedExpr(PredefinedExpr *E) {
342  VisitExpr(E);
343  E->setLocation(ReadSourceLocation(Record, Idx));
344  E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]);
345}
346
347void ASTStmtReader::VisitDeclRefExpr(DeclRefExpr *E) {
348  VisitExpr(E);
349
350  E->DeclRefExprBits.HasQualifier = Record[Idx++];
351  E->DeclRefExprBits.HasFoundDecl = Record[Idx++];
352  E->DeclRefExprBits.HasTemplateKWAndArgsInfo = Record[Idx++];
353  E->DeclRefExprBits.HadMultipleCandidates = Record[Idx++];
354  E->DeclRefExprBits.RefersToEnclosingLocal = Record[Idx++];
355  unsigned NumTemplateArgs = 0;
356  if (E->hasTemplateKWAndArgsInfo())
357    NumTemplateArgs = Record[Idx++];
358
359  if (E->hasQualifier())
360    E->getInternalQualifierLoc()
361      = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx);
362
363  if (E->hasFoundDecl())
364    E->getInternalFoundDecl() = ReadDeclAs<NamedDecl>(Record, Idx);
365
366  if (E->hasTemplateKWAndArgsInfo())
367    ReadTemplateKWAndArgsInfo(*E->getTemplateKWAndArgsInfo(),
368                              NumTemplateArgs);
369
370  E->setDecl(ReadDeclAs<ValueDecl>(Record, Idx));
371  E->setLocation(ReadSourceLocation(Record, Idx));
372  ReadDeclarationNameLoc(E->DNLoc, E->getDecl()->getDeclName(), Record, Idx);
373}
374
375void ASTStmtReader::VisitIntegerLiteral(IntegerLiteral *E) {
376  VisitExpr(E);
377  E->setLocation(ReadSourceLocation(Record, Idx));
378  E->setValue(Reader.getContext(), Reader.ReadAPInt(Record, Idx));
379}
380
381void ASTStmtReader::VisitFloatingLiteral(FloatingLiteral *E) {
382  VisitExpr(E);
383  E->setValue(Reader.getContext(), Reader.ReadAPFloat(Record, Idx));
384  E->setExact(Record[Idx++]);
385  E->setLocation(ReadSourceLocation(Record, Idx));
386}
387
388void ASTStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) {
389  VisitExpr(E);
390  E->setSubExpr(Reader.ReadSubExpr());
391}
392
393void ASTStmtReader::VisitStringLiteral(StringLiteral *E) {
394  VisitExpr(E);
395  unsigned Len = Record[Idx++];
396  assert(Record[Idx] == E->getNumConcatenated() &&
397         "Wrong number of concatenated tokens!");
398  ++Idx;
399  StringLiteral::StringKind kind =
400        static_cast<StringLiteral::StringKind>(Record[Idx++]);
401  bool isPascal = Record[Idx++];
402
403  // Read string data
404  SmallString<16> Str(&Record[Idx], &Record[Idx] + Len);
405  E->setString(Reader.getContext(), Str.str(), kind, isPascal);
406  Idx += Len;
407
408  // Read source locations
409  for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I)
410    E->setStrTokenLoc(I, ReadSourceLocation(Record, Idx));
411}
412
413void ASTStmtReader::VisitCharacterLiteral(CharacterLiteral *E) {
414  VisitExpr(E);
415  E->setValue(Record[Idx++]);
416  E->setLocation(ReadSourceLocation(Record, Idx));
417  E->setKind(static_cast<CharacterLiteral::CharacterKind>(Record[Idx++]));
418}
419
420void ASTStmtReader::VisitParenExpr(ParenExpr *E) {
421  VisitExpr(E);
422  E->setLParen(ReadSourceLocation(Record, Idx));
423  E->setRParen(ReadSourceLocation(Record, Idx));
424  E->setSubExpr(Reader.ReadSubExpr());
425}
426
427void ASTStmtReader::VisitParenListExpr(ParenListExpr *E) {
428  VisitExpr(E);
429  unsigned NumExprs = Record[Idx++];
430  E->Exprs = new (Reader.getContext()) Stmt*[NumExprs];
431  for (unsigned i = 0; i != NumExprs; ++i)
432    E->Exprs[i] = Reader.ReadSubStmt();
433  E->NumExprs = NumExprs;
434  E->LParenLoc = ReadSourceLocation(Record, Idx);
435  E->RParenLoc = ReadSourceLocation(Record, Idx);
436}
437
438void ASTStmtReader::VisitUnaryOperator(UnaryOperator *E) {
439  VisitExpr(E);
440  E->setSubExpr(Reader.ReadSubExpr());
441  E->setOpcode((UnaryOperator::Opcode)Record[Idx++]);
442  E->setOperatorLoc(ReadSourceLocation(Record, Idx));
443}
444
445void ASTStmtReader::VisitOffsetOfExpr(OffsetOfExpr *E) {
446  typedef OffsetOfExpr::OffsetOfNode Node;
447  VisitExpr(E);
448  assert(E->getNumComponents() == Record[Idx]);
449  ++Idx;
450  assert(E->getNumExpressions() == Record[Idx]);
451  ++Idx;
452  E->setOperatorLoc(ReadSourceLocation(Record, Idx));
453  E->setRParenLoc(ReadSourceLocation(Record, Idx));
454  E->setTypeSourceInfo(GetTypeSourceInfo(Record, Idx));
455  for (unsigned I = 0, N = E->getNumComponents(); I != N; ++I) {
456    Node::Kind Kind = static_cast<Node::Kind>(Record[Idx++]);
457    SourceLocation Start = ReadSourceLocation(Record, Idx);
458    SourceLocation End = ReadSourceLocation(Record, Idx);
459    switch (Kind) {
460    case Node::Array:
461      E->setComponent(I, Node(Start, Record[Idx++], End));
462      break;
463
464    case Node::Field:
465      E->setComponent(I, Node(Start, ReadDeclAs<FieldDecl>(Record, Idx), End));
466      break;
467
468    case Node::Identifier:
469      E->setComponent(I,
470                      Node(Start,
471                           Reader.GetIdentifierInfo(F, Record, Idx),
472                           End));
473      break;
474
475    case Node::Base: {
476      CXXBaseSpecifier *Base = new (Reader.getContext()) CXXBaseSpecifier();
477      *Base = Reader.ReadCXXBaseSpecifier(F, Record, Idx);
478      E->setComponent(I, Node(Base));
479      break;
480    }
481    }
482  }
483
484  for (unsigned I = 0, N = E->getNumExpressions(); I != N; ++I)
485    E->setIndexExpr(I, Reader.ReadSubExpr());
486}
487
488void ASTStmtReader::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
489  VisitExpr(E);
490  E->setKind(static_cast<UnaryExprOrTypeTrait>(Record[Idx++]));
491  if (Record[Idx] == 0) {
492    E->setArgument(Reader.ReadSubExpr());
493    ++Idx;
494  } else {
495    E->setArgument(GetTypeSourceInfo(Record, Idx));
496  }
497  E->setOperatorLoc(ReadSourceLocation(Record, Idx));
498  E->setRParenLoc(ReadSourceLocation(Record, Idx));
499}
500
501void ASTStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
502  VisitExpr(E);
503  E->setLHS(Reader.ReadSubExpr());
504  E->setRHS(Reader.ReadSubExpr());
505  E->setRBracketLoc(ReadSourceLocation(Record, Idx));
506}
507
508void ASTStmtReader::VisitCallExpr(CallExpr *E) {
509  VisitExpr(E);
510  E->setNumArgs(Reader.getContext(), Record[Idx++]);
511  E->setRParenLoc(ReadSourceLocation(Record, Idx));
512  E->setCallee(Reader.ReadSubExpr());
513  for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
514    E->setArg(I, Reader.ReadSubExpr());
515}
516
517void ASTStmtReader::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
518  VisitCallExpr(E);
519}
520
521void ASTStmtReader::VisitMemberExpr(MemberExpr *E) {
522  // Don't call VisitExpr, this is fully initialized at creation.
523  assert(E->getStmtClass() == Stmt::MemberExprClass &&
524         "It's a subclass, we must advance Idx!");
525}
526
527void ASTStmtReader::VisitObjCIsaExpr(ObjCIsaExpr *E) {
528  VisitExpr(E);
529  E->setBase(Reader.ReadSubExpr());
530  E->setIsaMemberLoc(ReadSourceLocation(Record, Idx));
531  E->setArrow(Record[Idx++]);
532}
533
534void ASTStmtReader::
535VisitObjCIndirectCopyRestoreExpr(ObjCIndirectCopyRestoreExpr *E) {
536  VisitExpr(E);
537  E->Operand = Reader.ReadSubExpr();
538  E->setShouldCopy(Record[Idx++]);
539}
540
541void ASTStmtReader::VisitObjCBridgedCastExpr(ObjCBridgedCastExpr *E) {
542  VisitExplicitCastExpr(E);
543  E->LParenLoc = ReadSourceLocation(Record, Idx);
544  E->BridgeKeywordLoc = ReadSourceLocation(Record, Idx);
545  E->Kind = Record[Idx++];
546}
547
548void ASTStmtReader::VisitCastExpr(CastExpr *E) {
549  VisitExpr(E);
550  unsigned NumBaseSpecs = Record[Idx++];
551  assert(NumBaseSpecs == E->path_size());
552  E->setSubExpr(Reader.ReadSubExpr());
553  E->setCastKind((CastExpr::CastKind)Record[Idx++]);
554  CastExpr::path_iterator BaseI = E->path_begin();
555  while (NumBaseSpecs--) {
556    CXXBaseSpecifier *BaseSpec = new (Reader.getContext()) CXXBaseSpecifier;
557    *BaseSpec = Reader.ReadCXXBaseSpecifier(F, Record, Idx);
558    *BaseI++ = BaseSpec;
559  }
560}
561
562void ASTStmtReader::VisitBinaryOperator(BinaryOperator *E) {
563  VisitExpr(E);
564  E->setLHS(Reader.ReadSubExpr());
565  E->setRHS(Reader.ReadSubExpr());
566  E->setOpcode((BinaryOperator::Opcode)Record[Idx++]);
567  E->setOperatorLoc(ReadSourceLocation(Record, Idx));
568}
569
570void ASTStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) {
571  VisitBinaryOperator(E);
572  E->setComputationLHSType(Reader.readType(F, Record, Idx));
573  E->setComputationResultType(Reader.readType(F, Record, Idx));
574}
575
576void ASTStmtReader::VisitConditionalOperator(ConditionalOperator *E) {
577  VisitExpr(E);
578  E->SubExprs[ConditionalOperator::COND] = Reader.ReadSubExpr();
579  E->SubExprs[ConditionalOperator::LHS] = Reader.ReadSubExpr();
580  E->SubExprs[ConditionalOperator::RHS] = Reader.ReadSubExpr();
581  E->QuestionLoc = ReadSourceLocation(Record, Idx);
582  E->ColonLoc = ReadSourceLocation(Record, Idx);
583}
584
585void
586ASTStmtReader::VisitBinaryConditionalOperator(BinaryConditionalOperator *E) {
587  VisitExpr(E);
588  E->OpaqueValue = cast<OpaqueValueExpr>(Reader.ReadSubExpr());
589  E->SubExprs[BinaryConditionalOperator::COMMON] = Reader.ReadSubExpr();
590  E->SubExprs[BinaryConditionalOperator::COND] = Reader.ReadSubExpr();
591  E->SubExprs[BinaryConditionalOperator::LHS] = Reader.ReadSubExpr();
592  E->SubExprs[BinaryConditionalOperator::RHS] = Reader.ReadSubExpr();
593  E->QuestionLoc = ReadSourceLocation(Record, Idx);
594  E->ColonLoc = ReadSourceLocation(Record, Idx);
595}
596
597void ASTStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) {
598  VisitCastExpr(E);
599}
600
601void ASTStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) {
602  VisitCastExpr(E);
603  E->setTypeInfoAsWritten(GetTypeSourceInfo(Record, Idx));
604}
605
606void ASTStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) {
607  VisitExplicitCastExpr(E);
608  E->setLParenLoc(ReadSourceLocation(Record, Idx));
609  E->setRParenLoc(ReadSourceLocation(Record, Idx));
610}
611
612void ASTStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
613  VisitExpr(E);
614  E->setLParenLoc(ReadSourceLocation(Record, Idx));
615  E->setTypeSourceInfo(GetTypeSourceInfo(Record, Idx));
616  E->setInitializer(Reader.ReadSubExpr());
617  E->setFileScope(Record[Idx++]);
618}
619
620void ASTStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) {
621  VisitExpr(E);
622  E->setBase(Reader.ReadSubExpr());
623  E->setAccessor(Reader.GetIdentifierInfo(F, Record, Idx));
624  E->setAccessorLoc(ReadSourceLocation(Record, Idx));
625}
626
627void ASTStmtReader::VisitInitListExpr(InitListExpr *E) {
628  VisitExpr(E);
629  E->setSyntacticForm(cast_or_null<InitListExpr>(Reader.ReadSubStmt()));
630  E->setLBraceLoc(ReadSourceLocation(Record, Idx));
631  E->setRBraceLoc(ReadSourceLocation(Record, Idx));
632  bool isArrayFiller = Record[Idx++];
633  Expr *filler = 0;
634  if (isArrayFiller) {
635    filler = Reader.ReadSubExpr();
636    E->ArrayFillerOrUnionFieldInit = filler;
637  } else
638    E->ArrayFillerOrUnionFieldInit = ReadDeclAs<FieldDecl>(Record, Idx);
639  E->sawArrayRangeDesignator(Record[Idx++]);
640  E->setInitializesStdInitializerList(Record[Idx++]);
641  unsigned NumInits = Record[Idx++];
642  E->reserveInits(Reader.getContext(), NumInits);
643  if (isArrayFiller) {
644    for (unsigned I = 0; I != NumInits; ++I) {
645      Expr *init = Reader.ReadSubExpr();
646      E->updateInit(Reader.getContext(), I, init ? init : filler);
647    }
648  } else {
649    for (unsigned I = 0; I != NumInits; ++I)
650      E->updateInit(Reader.getContext(), I, Reader.ReadSubExpr());
651  }
652}
653
654void ASTStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) {
655  typedef DesignatedInitExpr::Designator Designator;
656
657  VisitExpr(E);
658  unsigned NumSubExprs = Record[Idx++];
659  assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs");
660  for (unsigned I = 0; I != NumSubExprs; ++I)
661    E->setSubExpr(I, Reader.ReadSubExpr());
662  E->setEqualOrColonLoc(ReadSourceLocation(Record, Idx));
663  E->setGNUSyntax(Record[Idx++]);
664
665  SmallVector<Designator, 4> Designators;
666  while (Idx < Record.size()) {
667    switch ((DesignatorTypes)Record[Idx++]) {
668    case DESIG_FIELD_DECL: {
669      FieldDecl *Field = ReadDeclAs<FieldDecl>(Record, Idx);
670      SourceLocation DotLoc
671        = ReadSourceLocation(Record, Idx);
672      SourceLocation FieldLoc
673        = ReadSourceLocation(Record, Idx);
674      Designators.push_back(Designator(Field->getIdentifier(), DotLoc,
675                                       FieldLoc));
676      Designators.back().setField(Field);
677      break;
678    }
679
680    case DESIG_FIELD_NAME: {
681      const IdentifierInfo *Name = Reader.GetIdentifierInfo(F, Record, Idx);
682      SourceLocation DotLoc
683        = ReadSourceLocation(Record, Idx);
684      SourceLocation FieldLoc
685        = ReadSourceLocation(Record, Idx);
686      Designators.push_back(Designator(Name, DotLoc, FieldLoc));
687      break;
688    }
689
690    case DESIG_ARRAY: {
691      unsigned Index = Record[Idx++];
692      SourceLocation LBracketLoc
693        = ReadSourceLocation(Record, Idx);
694      SourceLocation RBracketLoc
695        = ReadSourceLocation(Record, Idx);
696      Designators.push_back(Designator(Index, LBracketLoc, RBracketLoc));
697      break;
698    }
699
700    case DESIG_ARRAY_RANGE: {
701      unsigned Index = Record[Idx++];
702      SourceLocation LBracketLoc
703        = ReadSourceLocation(Record, Idx);
704      SourceLocation EllipsisLoc
705        = ReadSourceLocation(Record, Idx);
706      SourceLocation RBracketLoc
707        = ReadSourceLocation(Record, Idx);
708      Designators.push_back(Designator(Index, LBracketLoc, EllipsisLoc,
709                                       RBracketLoc));
710      break;
711    }
712    }
713  }
714  E->setDesignators(Reader.getContext(),
715                    Designators.data(), Designators.size());
716}
717
718void ASTStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) {
719  VisitExpr(E);
720}
721
722void ASTStmtReader::VisitVAArgExpr(VAArgExpr *E) {
723  VisitExpr(E);
724  E->setSubExpr(Reader.ReadSubExpr());
725  E->setWrittenTypeInfo(GetTypeSourceInfo(Record, Idx));
726  E->setBuiltinLoc(ReadSourceLocation(Record, Idx));
727  E->setRParenLoc(ReadSourceLocation(Record, Idx));
728}
729
730void ASTStmtReader::VisitAddrLabelExpr(AddrLabelExpr *E) {
731  VisitExpr(E);
732  E->setAmpAmpLoc(ReadSourceLocation(Record, Idx));
733  E->setLabelLoc(ReadSourceLocation(Record, Idx));
734  E->setLabel(ReadDeclAs<LabelDecl>(Record, Idx));
735}
736
737void ASTStmtReader::VisitStmtExpr(StmtExpr *E) {
738  VisitExpr(E);
739  E->setLParenLoc(ReadSourceLocation(Record, Idx));
740  E->setRParenLoc(ReadSourceLocation(Record, Idx));
741  E->setSubStmt(cast_or_null<CompoundStmt>(Reader.ReadSubStmt()));
742}
743
744void ASTStmtReader::VisitChooseExpr(ChooseExpr *E) {
745  VisitExpr(E);
746  E->setCond(Reader.ReadSubExpr());
747  E->setLHS(Reader.ReadSubExpr());
748  E->setRHS(Reader.ReadSubExpr());
749  E->setBuiltinLoc(ReadSourceLocation(Record, Idx));
750  E->setRParenLoc(ReadSourceLocation(Record, Idx));
751}
752
753void ASTStmtReader::VisitGNUNullExpr(GNUNullExpr *E) {
754  VisitExpr(E);
755  E->setTokenLocation(ReadSourceLocation(Record, Idx));
756}
757
758void ASTStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) {
759  VisitExpr(E);
760  SmallVector<Expr *, 16> Exprs;
761  unsigned NumExprs = Record[Idx++];
762  while (NumExprs--)
763    Exprs.push_back(Reader.ReadSubExpr());
764  E->setExprs(Reader.getContext(), Exprs.data(), Exprs.size());
765  E->setBuiltinLoc(ReadSourceLocation(Record, Idx));
766  E->setRParenLoc(ReadSourceLocation(Record, Idx));
767}
768
769void ASTStmtReader::VisitBlockExpr(BlockExpr *E) {
770  VisitExpr(E);
771  E->setBlockDecl(ReadDeclAs<BlockDecl>(Record, Idx));
772}
773
774void ASTStmtReader::VisitGenericSelectionExpr(GenericSelectionExpr *E) {
775  VisitExpr(E);
776  E->NumAssocs = Record[Idx++];
777  E->AssocTypes = new (Reader.getContext()) TypeSourceInfo*[E->NumAssocs];
778  E->SubExprs =
779   new(Reader.getContext()) Stmt*[GenericSelectionExpr::END_EXPR+E->NumAssocs];
780
781  E->SubExprs[GenericSelectionExpr::CONTROLLING] = Reader.ReadSubExpr();
782  for (unsigned I = 0, N = E->getNumAssocs(); I != N; ++I) {
783    E->AssocTypes[I] = GetTypeSourceInfo(Record, Idx);
784    E->SubExprs[GenericSelectionExpr::END_EXPR+I] = Reader.ReadSubExpr();
785  }
786  E->ResultIndex = Record[Idx++];
787
788  E->GenericLoc = ReadSourceLocation(Record, Idx);
789  E->DefaultLoc = ReadSourceLocation(Record, Idx);
790  E->RParenLoc = ReadSourceLocation(Record, Idx);
791}
792
793void ASTStmtReader::VisitPseudoObjectExpr(PseudoObjectExpr *E) {
794  VisitExpr(E);
795  unsigned numSemanticExprs = Record[Idx++];
796  assert(numSemanticExprs + 1 == E->PseudoObjectExprBits.NumSubExprs);
797  E->PseudoObjectExprBits.ResultIndex = Record[Idx++];
798
799  // Read the syntactic expression.
800  E->getSubExprsBuffer()[0] = Reader.ReadSubExpr();
801
802  // Read all the semantic expressions.
803  for (unsigned i = 0; i != numSemanticExprs; ++i) {
804    Expr *subExpr = Reader.ReadSubExpr();
805    E->getSubExprsBuffer()[i+1] = subExpr;
806  }
807}
808
809void ASTStmtReader::VisitAtomicExpr(AtomicExpr *E) {
810  VisitExpr(E);
811  E->Op = AtomicExpr::AtomicOp(Record[Idx++]);
812  E->NumSubExprs = AtomicExpr::getNumSubExprs(E->Op);
813  for (unsigned I = 0; I != E->NumSubExprs; ++I)
814    E->SubExprs[I] = Reader.ReadSubExpr();
815  E->BuiltinLoc = ReadSourceLocation(Record, Idx);
816  E->RParenLoc = ReadSourceLocation(Record, Idx);
817}
818
819//===----------------------------------------------------------------------===//
820// Objective-C Expressions and Statements
821
822void ASTStmtReader::VisitObjCStringLiteral(ObjCStringLiteral *E) {
823  VisitExpr(E);
824  E->setString(cast<StringLiteral>(Reader.ReadSubStmt()));
825  E->setAtLoc(ReadSourceLocation(Record, Idx));
826}
827
828void ASTStmtReader::VisitObjCBoxedExpr(ObjCBoxedExpr *E) {
829  VisitExpr(E);
830  // could be one of several IntegerLiteral, FloatLiteral, etc.
831  E->SubExpr = Reader.ReadSubStmt();
832  E->BoxingMethod = ReadDeclAs<ObjCMethodDecl>(Record, Idx);
833  E->Range = ReadSourceRange(Record, Idx);
834}
835
836void ASTStmtReader::VisitObjCArrayLiteral(ObjCArrayLiteral *E) {
837  VisitExpr(E);
838  unsigned NumElements = Record[Idx++];
839  assert(NumElements == E->getNumElements() && "Wrong number of elements");
840  Expr **Elements = E->getElements();
841  for (unsigned I = 0, N = NumElements; I != N; ++I)
842    Elements[I] = Reader.ReadSubExpr();
843  E->ArrayWithObjectsMethod = ReadDeclAs<ObjCMethodDecl>(Record, Idx);
844  E->Range = ReadSourceRange(Record, Idx);
845}
846
847void ASTStmtReader::VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) {
848  VisitExpr(E);
849  unsigned NumElements = Record[Idx++];
850  assert(NumElements == E->getNumElements() && "Wrong number of elements");
851  bool HasPackExpansions = Record[Idx++];
852  assert(HasPackExpansions == E->HasPackExpansions &&"Pack expansion mismatch");
853  ObjCDictionaryLiteral::KeyValuePair *KeyValues = E->getKeyValues();
854  ObjCDictionaryLiteral::ExpansionData *Expansions = E->getExpansionData();
855  for (unsigned I = 0; I != NumElements; ++I) {
856    KeyValues[I].Key = Reader.ReadSubExpr();
857    KeyValues[I].Value = Reader.ReadSubExpr();
858    if (HasPackExpansions) {
859      Expansions[I].EllipsisLoc = ReadSourceLocation(Record, Idx);
860      Expansions[I].NumExpansionsPlusOne = Record[Idx++];
861    }
862  }
863  E->DictWithObjectsMethod = ReadDeclAs<ObjCMethodDecl>(Record, Idx);
864  E->Range = ReadSourceRange(Record, Idx);
865}
866
867void ASTStmtReader::VisitObjCEncodeExpr(ObjCEncodeExpr *E) {
868  VisitExpr(E);
869  E->setEncodedTypeSourceInfo(GetTypeSourceInfo(Record, Idx));
870  E->setAtLoc(ReadSourceLocation(Record, Idx));
871  E->setRParenLoc(ReadSourceLocation(Record, Idx));
872}
873
874void ASTStmtReader::VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
875  VisitExpr(E);
876  E->setSelector(Reader.ReadSelector(F, Record, Idx));
877  E->setAtLoc(ReadSourceLocation(Record, Idx));
878  E->setRParenLoc(ReadSourceLocation(Record, Idx));
879}
880
881void ASTStmtReader::VisitObjCProtocolExpr(ObjCProtocolExpr *E) {
882  VisitExpr(E);
883  E->setProtocol(ReadDeclAs<ObjCProtocolDecl>(Record, Idx));
884  E->setAtLoc(ReadSourceLocation(Record, Idx));
885  E->ProtoLoc = ReadSourceLocation(Record, Idx);
886  E->setRParenLoc(ReadSourceLocation(Record, Idx));
887}
888
889void ASTStmtReader::VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
890  VisitExpr(E);
891  E->setDecl(ReadDeclAs<ObjCIvarDecl>(Record, Idx));
892  E->setLocation(ReadSourceLocation(Record, Idx));
893  E->setBase(Reader.ReadSubExpr());
894  E->setIsArrow(Record[Idx++]);
895  E->setIsFreeIvar(Record[Idx++]);
896}
897
898void ASTStmtReader::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) {
899  VisitExpr(E);
900  unsigned MethodRefFlags = Record[Idx++];
901  bool Implicit = Record[Idx++] != 0;
902  if (Implicit) {
903    ObjCMethodDecl *Getter = ReadDeclAs<ObjCMethodDecl>(Record, Idx);
904    ObjCMethodDecl *Setter = ReadDeclAs<ObjCMethodDecl>(Record, Idx);
905    E->setImplicitProperty(Getter, Setter, MethodRefFlags);
906  } else {
907    E->setExplicitProperty(ReadDeclAs<ObjCPropertyDecl>(Record, Idx),
908                           MethodRefFlags);
909  }
910  E->setLocation(ReadSourceLocation(Record, Idx));
911  E->setReceiverLocation(ReadSourceLocation(Record, Idx));
912  switch (Record[Idx++]) {
913  case 0:
914    E->setBase(Reader.ReadSubExpr());
915    break;
916  case 1:
917    E->setSuperReceiver(Reader.readType(F, Record, Idx));
918    break;
919  case 2:
920    E->setClassReceiver(ReadDeclAs<ObjCInterfaceDecl>(Record, Idx));
921    break;
922  }
923}
924
925void ASTStmtReader::VisitObjCSubscriptRefExpr(ObjCSubscriptRefExpr *E) {
926  VisitExpr(E);
927  E->setRBracket(ReadSourceLocation(Record, Idx));
928  E->setBaseExpr(Reader.ReadSubExpr());
929  E->setKeyExpr(Reader.ReadSubExpr());
930  E->GetAtIndexMethodDecl = ReadDeclAs<ObjCMethodDecl>(Record, Idx);
931  E->SetAtIndexMethodDecl = ReadDeclAs<ObjCMethodDecl>(Record, Idx);
932}
933
934void ASTStmtReader::VisitObjCMessageExpr(ObjCMessageExpr *E) {
935  VisitExpr(E);
936  assert(Record[Idx] == E->getNumArgs());
937  ++Idx;
938  unsigned NumStoredSelLocs = Record[Idx++];
939  E->SelLocsKind = Record[Idx++];
940  E->setDelegateInitCall(Record[Idx++]);
941  E->IsImplicit = Record[Idx++];
942  ObjCMessageExpr::ReceiverKind Kind
943    = static_cast<ObjCMessageExpr::ReceiverKind>(Record[Idx++]);
944  switch (Kind) {
945  case ObjCMessageExpr::Instance:
946    E->setInstanceReceiver(Reader.ReadSubExpr());
947    break;
948
949  case ObjCMessageExpr::Class:
950    E->setClassReceiver(GetTypeSourceInfo(Record, Idx));
951    break;
952
953  case ObjCMessageExpr::SuperClass:
954  case ObjCMessageExpr::SuperInstance: {
955    QualType T = Reader.readType(F, Record, Idx);
956    SourceLocation SuperLoc = ReadSourceLocation(Record, Idx);
957    E->setSuper(SuperLoc, T, Kind == ObjCMessageExpr::SuperInstance);
958    break;
959  }
960  }
961
962  assert(Kind == E->getReceiverKind());
963
964  if (Record[Idx++])
965    E->setMethodDecl(ReadDeclAs<ObjCMethodDecl>(Record, Idx));
966  else
967    E->setSelector(Reader.ReadSelector(F, Record, Idx));
968
969  E->LBracLoc = ReadSourceLocation(Record, Idx);
970  E->RBracLoc = ReadSourceLocation(Record, Idx);
971
972  for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
973    E->setArg(I, Reader.ReadSubExpr());
974
975  SourceLocation *Locs = E->getStoredSelLocs();
976  for (unsigned I = 0; I != NumStoredSelLocs; ++I)
977    Locs[I] = ReadSourceLocation(Record, Idx);
978}
979
980void ASTStmtReader::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) {
981  VisitStmt(S);
982  S->setElement(Reader.ReadSubStmt());
983  S->setCollection(Reader.ReadSubExpr());
984  S->setBody(Reader.ReadSubStmt());
985  S->setForLoc(ReadSourceLocation(Record, Idx));
986  S->setRParenLoc(ReadSourceLocation(Record, Idx));
987}
988
989void ASTStmtReader::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) {
990  VisitStmt(S);
991  S->setCatchBody(Reader.ReadSubStmt());
992  S->setCatchParamDecl(ReadDeclAs<VarDecl>(Record, Idx));
993  S->setAtCatchLoc(ReadSourceLocation(Record, Idx));
994  S->setRParenLoc(ReadSourceLocation(Record, Idx));
995}
996
997void ASTStmtReader::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) {
998  VisitStmt(S);
999  S->setFinallyBody(Reader.ReadSubStmt());
1000  S->setAtFinallyLoc(ReadSourceLocation(Record, Idx));
1001}
1002
1003void ASTStmtReader::VisitObjCAutoreleasePoolStmt(ObjCAutoreleasePoolStmt *S) {
1004  VisitStmt(S);
1005  S->setSubStmt(Reader.ReadSubStmt());
1006  S->setAtLoc(ReadSourceLocation(Record, Idx));
1007}
1008
1009void ASTStmtReader::VisitObjCAtTryStmt(ObjCAtTryStmt *S) {
1010  VisitStmt(S);
1011  assert(Record[Idx] == S->getNumCatchStmts());
1012  ++Idx;
1013  bool HasFinally = Record[Idx++];
1014  S->setTryBody(Reader.ReadSubStmt());
1015  for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I)
1016    S->setCatchStmt(I, cast_or_null<ObjCAtCatchStmt>(Reader.ReadSubStmt()));
1017
1018  if (HasFinally)
1019    S->setFinallyStmt(Reader.ReadSubStmt());
1020  S->setAtTryLoc(ReadSourceLocation(Record, Idx));
1021}
1022
1023void ASTStmtReader::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S) {
1024  VisitStmt(S);
1025  S->setSynchExpr(Reader.ReadSubStmt());
1026  S->setSynchBody(Reader.ReadSubStmt());
1027  S->setAtSynchronizedLoc(ReadSourceLocation(Record, Idx));
1028}
1029
1030void ASTStmtReader::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) {
1031  VisitStmt(S);
1032  S->setThrowExpr(Reader.ReadSubStmt());
1033  S->setThrowLoc(ReadSourceLocation(Record, Idx));
1034}
1035
1036void ASTStmtReader::VisitObjCBoolLiteralExpr(ObjCBoolLiteralExpr *E) {
1037  VisitExpr(E);
1038  E->setValue(Record[Idx++]);
1039  E->setLocation(ReadSourceLocation(Record, Idx));
1040}
1041
1042//===----------------------------------------------------------------------===//
1043// C++ Expressions and Statements
1044//===----------------------------------------------------------------------===//
1045
1046void ASTStmtReader::VisitCXXCatchStmt(CXXCatchStmt *S) {
1047  VisitStmt(S);
1048  S->CatchLoc = ReadSourceLocation(Record, Idx);
1049  S->ExceptionDecl = ReadDeclAs<VarDecl>(Record, Idx);
1050  S->HandlerBlock = Reader.ReadSubStmt();
1051}
1052
1053void ASTStmtReader::VisitCXXTryStmt(CXXTryStmt *S) {
1054  VisitStmt(S);
1055  assert(Record[Idx] == S->getNumHandlers() && "NumStmtFields is wrong ?");
1056  ++Idx;
1057  S->TryLoc = ReadSourceLocation(Record, Idx);
1058  S->getStmts()[0] = Reader.ReadSubStmt();
1059  for (unsigned i = 0, e = S->getNumHandlers(); i != e; ++i)
1060    S->getStmts()[i + 1] = Reader.ReadSubStmt();
1061}
1062
1063void ASTStmtReader::VisitCXXForRangeStmt(CXXForRangeStmt *S) {
1064  VisitStmt(S);
1065  S->setForLoc(ReadSourceLocation(Record, Idx));
1066  S->setColonLoc(ReadSourceLocation(Record, Idx));
1067  S->setRParenLoc(ReadSourceLocation(Record, Idx));
1068  S->setRangeStmt(Reader.ReadSubStmt());
1069  S->setBeginEndStmt(Reader.ReadSubStmt());
1070  S->setCond(Reader.ReadSubExpr());
1071  S->setInc(Reader.ReadSubExpr());
1072  S->setLoopVarStmt(Reader.ReadSubStmt());
1073  S->setBody(Reader.ReadSubStmt());
1074}
1075
1076void ASTStmtReader::VisitMSDependentExistsStmt(MSDependentExistsStmt *S) {
1077  VisitStmt(S);
1078  S->KeywordLoc = ReadSourceLocation(Record, Idx);
1079  S->IsIfExists = Record[Idx++];
1080  S->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx);
1081  ReadDeclarationNameInfo(S->NameInfo, Record, Idx);
1082  S->SubStmt = Reader.ReadSubStmt();
1083}
1084
1085void ASTStmtReader::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) {
1086  VisitCallExpr(E);
1087  E->Operator = (OverloadedOperatorKind)Record[Idx++];
1088  E->Range = Reader.ReadSourceRange(F, Record, Idx);
1089}
1090
1091void ASTStmtReader::VisitCXXConstructExpr(CXXConstructExpr *E) {
1092  VisitExpr(E);
1093  E->NumArgs = Record[Idx++];
1094  if (E->NumArgs)
1095    E->Args = new (Reader.getContext()) Stmt*[E->NumArgs];
1096  for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
1097    E->setArg(I, Reader.ReadSubExpr());
1098  E->setConstructor(ReadDeclAs<CXXConstructorDecl>(Record, Idx));
1099  E->setLocation(ReadSourceLocation(Record, Idx));
1100  E->setElidable(Record[Idx++]);
1101  E->setHadMultipleCandidates(Record[Idx++]);
1102  E->setRequiresZeroInitialization(Record[Idx++]);
1103  E->setConstructionKind((CXXConstructExpr::ConstructionKind)Record[Idx++]);
1104  E->ParenRange = ReadSourceRange(Record, Idx);
1105}
1106
1107void ASTStmtReader::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) {
1108  VisitCXXConstructExpr(E);
1109  E->Type = GetTypeSourceInfo(Record, Idx);
1110}
1111
1112void ASTStmtReader::VisitLambdaExpr(LambdaExpr *E) {
1113  VisitExpr(E);
1114  unsigned NumCaptures = Record[Idx++];
1115  assert(NumCaptures == E->NumCaptures);(void)NumCaptures;
1116  unsigned NumArrayIndexVars = Record[Idx++];
1117  E->IntroducerRange = ReadSourceRange(Record, Idx);
1118  E->CaptureDefault = static_cast<LambdaCaptureDefault>(Record[Idx++]);
1119  E->ExplicitParams = Record[Idx++];
1120  E->ExplicitResultType = Record[Idx++];
1121  E->ClosingBrace = ReadSourceLocation(Record, Idx);
1122
1123  // Read capture initializers.
1124  for (LambdaExpr::capture_init_iterator C = E->capture_init_begin(),
1125                                      CEnd = E->capture_init_end();
1126       C != CEnd; ++C)
1127    *C = Reader.ReadSubExpr();
1128
1129  // Read array capture index variables.
1130  if (NumArrayIndexVars > 0) {
1131    unsigned *ArrayIndexStarts = E->getArrayIndexStarts();
1132    for (unsigned I = 0; I != NumCaptures + 1; ++I)
1133      ArrayIndexStarts[I] = Record[Idx++];
1134
1135    VarDecl **ArrayIndexVars = E->getArrayIndexVars();
1136    for (unsigned I = 0; I != NumArrayIndexVars; ++I)
1137      ArrayIndexVars[I] = ReadDeclAs<VarDecl>(Record, Idx);
1138  }
1139}
1140
1141void ASTStmtReader::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) {
1142  VisitExplicitCastExpr(E);
1143  SourceRange R = ReadSourceRange(Record, Idx);
1144  E->Loc = R.getBegin();
1145  E->RParenLoc = R.getEnd();
1146}
1147
1148void ASTStmtReader::VisitCXXStaticCastExpr(CXXStaticCastExpr *E) {
1149  return VisitCXXNamedCastExpr(E);
1150}
1151
1152void ASTStmtReader::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *E) {
1153  return VisitCXXNamedCastExpr(E);
1154}
1155
1156void ASTStmtReader::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *E) {
1157  return VisitCXXNamedCastExpr(E);
1158}
1159
1160void ASTStmtReader::VisitCXXConstCastExpr(CXXConstCastExpr *E) {
1161  return VisitCXXNamedCastExpr(E);
1162}
1163
1164void ASTStmtReader::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E) {
1165  VisitExplicitCastExpr(E);
1166  E->setTypeBeginLoc(ReadSourceLocation(Record, Idx));
1167  E->setRParenLoc(ReadSourceLocation(Record, Idx));
1168}
1169
1170void ASTStmtReader::VisitUserDefinedLiteral(UserDefinedLiteral *E) {
1171  VisitCallExpr(E);
1172  E->UDSuffixLoc = ReadSourceLocation(Record, Idx);
1173}
1174
1175void ASTStmtReader::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) {
1176  VisitExpr(E);
1177  E->setValue(Record[Idx++]);
1178  E->setLocation(ReadSourceLocation(Record, Idx));
1179}
1180
1181void ASTStmtReader::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) {
1182  VisitExpr(E);
1183  E->setLocation(ReadSourceLocation(Record, Idx));
1184}
1185
1186void ASTStmtReader::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
1187  VisitExpr(E);
1188  E->setSourceRange(ReadSourceRange(Record, Idx));
1189  if (E->isTypeOperand()) { // typeid(int)
1190    E->setTypeOperandSourceInfo(
1191        GetTypeSourceInfo(Record, Idx));
1192    return;
1193  }
1194
1195  // typeid(42+2)
1196  E->setExprOperand(Reader.ReadSubExpr());
1197}
1198
1199void ASTStmtReader::VisitCXXThisExpr(CXXThisExpr *E) {
1200  VisitExpr(E);
1201  E->setLocation(ReadSourceLocation(Record, Idx));
1202  E->setImplicit(Record[Idx++]);
1203}
1204
1205void ASTStmtReader::VisitCXXThrowExpr(CXXThrowExpr *E) {
1206  VisitExpr(E);
1207  E->ThrowLoc = ReadSourceLocation(Record, Idx);
1208  E->Op = Reader.ReadSubExpr();
1209  E->IsThrownVariableInScope = Record[Idx++];
1210}
1211
1212void ASTStmtReader::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
1213  VisitExpr(E);
1214
1215  assert((bool)Record[Idx] == E->Param.getInt() && "We messed up at creation ?");
1216  ++Idx; // HasOtherExprStored and SubExpr was handled during creation.
1217  E->Param.setPointer(ReadDeclAs<ParmVarDecl>(Record, Idx));
1218  E->Loc = ReadSourceLocation(Record, Idx);
1219}
1220
1221void ASTStmtReader::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
1222  VisitExpr(E);
1223  E->setTemporary(Reader.ReadCXXTemporary(F, Record, Idx));
1224  E->setSubExpr(Reader.ReadSubExpr());
1225}
1226
1227void ASTStmtReader::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) {
1228  VisitExpr(E);
1229  E->TypeInfo = GetTypeSourceInfo(Record, Idx);
1230  E->RParenLoc = ReadSourceLocation(Record, Idx);
1231}
1232
1233void ASTStmtReader::VisitCXXNewExpr(CXXNewExpr *E) {
1234  VisitExpr(E);
1235  E->GlobalNew = Record[Idx++];
1236  bool isArray = Record[Idx++];
1237  E->UsualArrayDeleteWantsSize = Record[Idx++];
1238  unsigned NumPlacementArgs = Record[Idx++];
1239  E->StoredInitializationStyle = Record[Idx++];
1240  E->setOperatorNew(ReadDeclAs<FunctionDecl>(Record, Idx));
1241  E->setOperatorDelete(ReadDeclAs<FunctionDecl>(Record, Idx));
1242  E->AllocatedTypeInfo = GetTypeSourceInfo(Record, Idx);
1243  E->TypeIdParens = ReadSourceRange(Record, Idx);
1244  E->StartLoc = ReadSourceLocation(Record, Idx);
1245  E->DirectInitRange = ReadSourceRange(Record, Idx);
1246
1247  E->AllocateArgsArray(Reader.getContext(), isArray, NumPlacementArgs,
1248                       E->StoredInitializationStyle != 0);
1249
1250  // Install all the subexpressions.
1251  for (CXXNewExpr::raw_arg_iterator I = E->raw_arg_begin(),e = E->raw_arg_end();
1252       I != e; ++I)
1253    *I = Reader.ReadSubStmt();
1254}
1255
1256void ASTStmtReader::VisitCXXDeleteExpr(CXXDeleteExpr *E) {
1257  VisitExpr(E);
1258  E->GlobalDelete = Record[Idx++];
1259  E->ArrayForm = Record[Idx++];
1260  E->ArrayFormAsWritten = Record[Idx++];
1261  E->UsualArrayDeleteWantsSize = Record[Idx++];
1262  E->OperatorDelete = ReadDeclAs<FunctionDecl>(Record, Idx);
1263  E->Argument = Reader.ReadSubExpr();
1264  E->Loc = ReadSourceLocation(Record, Idx);
1265}
1266
1267void ASTStmtReader::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) {
1268  VisitExpr(E);
1269
1270  E->Base = Reader.ReadSubExpr();
1271  E->IsArrow = Record[Idx++];
1272  E->OperatorLoc = ReadSourceLocation(Record, Idx);
1273  E->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx);
1274  E->ScopeType = GetTypeSourceInfo(Record, Idx);
1275  E->ColonColonLoc = ReadSourceLocation(Record, Idx);
1276  E->TildeLoc = ReadSourceLocation(Record, Idx);
1277
1278  IdentifierInfo *II = Reader.GetIdentifierInfo(F, Record, Idx);
1279  if (II)
1280    E->setDestroyedType(II, ReadSourceLocation(Record, Idx));
1281  else
1282    E->setDestroyedType(GetTypeSourceInfo(Record, Idx));
1283}
1284
1285void ASTStmtReader::VisitExprWithCleanups(ExprWithCleanups *E) {
1286  VisitExpr(E);
1287
1288  unsigned NumObjects = Record[Idx++];
1289  assert(NumObjects == E->getNumObjects());
1290  for (unsigned i = 0; i != NumObjects; ++i)
1291    E->getObjectsBuffer()[i] = ReadDeclAs<BlockDecl>(Record, Idx);
1292
1293  E->SubExpr = Reader.ReadSubExpr();
1294}
1295
1296void
1297ASTStmtReader::VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E){
1298  VisitExpr(E);
1299
1300  if (Record[Idx++]) // HasTemplateKWAndArgsInfo
1301    ReadTemplateKWAndArgsInfo(*E->getTemplateKWAndArgsInfo(),
1302                              /*NumTemplateArgs=*/Record[Idx++]);
1303
1304  E->Base = Reader.ReadSubExpr();
1305  E->BaseType = Reader.readType(F, Record, Idx);
1306  E->IsArrow = Record[Idx++];
1307  E->OperatorLoc = ReadSourceLocation(Record, Idx);
1308  E->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx);
1309  E->FirstQualifierFoundInScope = ReadDeclAs<NamedDecl>(Record, Idx);
1310  ReadDeclarationNameInfo(E->MemberNameInfo, Record, Idx);
1311}
1312
1313void
1314ASTStmtReader::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) {
1315  VisitExpr(E);
1316
1317  if (Record[Idx++]) // HasTemplateKWAndArgsInfo
1318    ReadTemplateKWAndArgsInfo(*E->getTemplateKWAndArgsInfo(),
1319                              /*NumTemplateArgs=*/Record[Idx++]);
1320
1321  E->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx);
1322  ReadDeclarationNameInfo(E->NameInfo, Record, Idx);
1323}
1324
1325void
1326ASTStmtReader::VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E) {
1327  VisitExpr(E);
1328  assert(Record[Idx] == E->arg_size() && "Read wrong record during creation ?");
1329  ++Idx; // NumArgs;
1330  for (unsigned I = 0, N = E->arg_size(); I != N; ++I)
1331    E->setArg(I, Reader.ReadSubExpr());
1332  E->Type = GetTypeSourceInfo(Record, Idx);
1333  E->setLParenLoc(ReadSourceLocation(Record, Idx));
1334  E->setRParenLoc(ReadSourceLocation(Record, Idx));
1335}
1336
1337void ASTStmtReader::VisitOverloadExpr(OverloadExpr *E) {
1338  VisitExpr(E);
1339
1340  if (Record[Idx++]) // HasTemplateKWAndArgsInfo
1341    ReadTemplateKWAndArgsInfo(*E->getTemplateKWAndArgsInfo(),
1342                              /*NumTemplateArgs=*/Record[Idx++]);
1343
1344  unsigned NumDecls = Record[Idx++];
1345  UnresolvedSet<8> Decls;
1346  for (unsigned i = 0; i != NumDecls; ++i) {
1347    NamedDecl *D = ReadDeclAs<NamedDecl>(Record, Idx);
1348    AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
1349    Decls.addDecl(D, AS);
1350  }
1351  E->initializeResults(Reader.getContext(), Decls.begin(), Decls.end());
1352
1353  ReadDeclarationNameInfo(E->NameInfo, Record, Idx);
1354  E->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx);
1355}
1356
1357void ASTStmtReader::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
1358  VisitOverloadExpr(E);
1359  E->IsArrow = Record[Idx++];
1360  E->HasUnresolvedUsing = Record[Idx++];
1361  E->Base = Reader.ReadSubExpr();
1362  E->BaseType = Reader.readType(F, Record, Idx);
1363  E->OperatorLoc = ReadSourceLocation(Record, Idx);
1364}
1365
1366void ASTStmtReader::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) {
1367  VisitOverloadExpr(E);
1368  E->RequiresADL = Record[Idx++];
1369  if (E->RequiresADL)
1370    E->StdIsAssociatedNamespace = Record[Idx++];
1371  E->Overloaded = Record[Idx++];
1372  E->NamingClass = ReadDeclAs<CXXRecordDecl>(Record, Idx);
1373}
1374
1375void ASTStmtReader::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) {
1376  VisitExpr(E);
1377  E->UTT = (UnaryTypeTrait)Record[Idx++];
1378  E->Value = (bool)Record[Idx++];
1379  SourceRange Range = ReadSourceRange(Record, Idx);
1380  E->Loc = Range.getBegin();
1381  E->RParen = Range.getEnd();
1382  E->QueriedType = GetTypeSourceInfo(Record, Idx);
1383}
1384
1385void ASTStmtReader::VisitBinaryTypeTraitExpr(BinaryTypeTraitExpr *E) {
1386  VisitExpr(E);
1387  E->BTT = (BinaryTypeTrait)Record[Idx++];
1388  E->Value = (bool)Record[Idx++];
1389  SourceRange Range = ReadSourceRange(Record, Idx);
1390  E->Loc = Range.getBegin();
1391  E->RParen = Range.getEnd();
1392  E->LhsType = GetTypeSourceInfo(Record, Idx);
1393  E->RhsType = GetTypeSourceInfo(Record, Idx);
1394}
1395
1396void ASTStmtReader::VisitTypeTraitExpr(TypeTraitExpr *E) {
1397  VisitExpr(E);
1398  E->TypeTraitExprBits.NumArgs = Record[Idx++];
1399  E->TypeTraitExprBits.Kind = Record[Idx++];
1400  E->TypeTraitExprBits.Value = Record[Idx++];
1401
1402  TypeSourceInfo **Args = E->getTypeSourceInfos();
1403  for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
1404    Args[I] = GetTypeSourceInfo(Record, Idx);
1405}
1406
1407void ASTStmtReader::VisitArrayTypeTraitExpr(ArrayTypeTraitExpr *E) {
1408  VisitExpr(E);
1409  E->ATT = (ArrayTypeTrait)Record[Idx++];
1410  E->Value = (unsigned int)Record[Idx++];
1411  SourceRange Range = ReadSourceRange(Record, Idx);
1412  E->Loc = Range.getBegin();
1413  E->RParen = Range.getEnd();
1414  E->QueriedType = GetTypeSourceInfo(Record, Idx);
1415}
1416
1417void ASTStmtReader::VisitExpressionTraitExpr(ExpressionTraitExpr *E) {
1418  VisitExpr(E);
1419  E->ET = (ExpressionTrait)Record[Idx++];
1420  E->Value = (bool)Record[Idx++];
1421  SourceRange Range = ReadSourceRange(Record, Idx);
1422  E->QueriedExpression = Reader.ReadSubExpr();
1423  E->Loc = Range.getBegin();
1424  E->RParen = Range.getEnd();
1425}
1426
1427void ASTStmtReader::VisitCXXNoexceptExpr(CXXNoexceptExpr *E) {
1428  VisitExpr(E);
1429  E->Value = (bool)Record[Idx++];
1430  E->Range = ReadSourceRange(Record, Idx);
1431  E->Operand = Reader.ReadSubExpr();
1432}
1433
1434void ASTStmtReader::VisitPackExpansionExpr(PackExpansionExpr *E) {
1435  VisitExpr(E);
1436  E->EllipsisLoc = ReadSourceLocation(Record, Idx);
1437  E->NumExpansions = Record[Idx++];
1438  E->Pattern = Reader.ReadSubExpr();
1439}
1440
1441void ASTStmtReader::VisitSizeOfPackExpr(SizeOfPackExpr *E) {
1442  VisitExpr(E);
1443  E->OperatorLoc = ReadSourceLocation(Record, Idx);
1444  E->PackLoc = ReadSourceLocation(Record, Idx);
1445  E->RParenLoc = ReadSourceLocation(Record, Idx);
1446  E->Length = Record[Idx++];
1447  E->Pack = ReadDeclAs<NamedDecl>(Record, Idx);
1448}
1449
1450void ASTStmtReader::VisitSubstNonTypeTemplateParmExpr(
1451                                              SubstNonTypeTemplateParmExpr *E) {
1452  VisitExpr(E);
1453  E->Param = ReadDeclAs<NonTypeTemplateParmDecl>(Record, Idx);
1454  E->NameLoc = ReadSourceLocation(Record, Idx);
1455  E->Replacement = Reader.ReadSubExpr();
1456}
1457
1458void ASTStmtReader::VisitSubstNonTypeTemplateParmPackExpr(
1459                                          SubstNonTypeTemplateParmPackExpr *E) {
1460  VisitExpr(E);
1461  E->Param = ReadDeclAs<NonTypeTemplateParmDecl>(Record, Idx);
1462  TemplateArgument ArgPack = Reader.ReadTemplateArgument(F, Record, Idx);
1463  if (ArgPack.getKind() != TemplateArgument::Pack)
1464    return;
1465
1466  E->Arguments = ArgPack.pack_begin();
1467  E->NumArguments = ArgPack.pack_size();
1468  E->NameLoc = ReadSourceLocation(Record, Idx);
1469}
1470
1471void ASTStmtReader::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr *E) {
1472  VisitExpr(E);
1473  E->Temporary = Reader.ReadSubExpr();
1474}
1475
1476void ASTStmtReader::VisitOpaqueValueExpr(OpaqueValueExpr *E) {
1477  VisitExpr(E);
1478  E->SourceExpr = Reader.ReadSubExpr();
1479  E->Loc = ReadSourceLocation(Record, Idx);
1480}
1481
1482//===----------------------------------------------------------------------===//
1483// Microsoft Expressions and Statements
1484//===----------------------------------------------------------------------===//
1485void ASTStmtReader::VisitCXXUuidofExpr(CXXUuidofExpr *E) {
1486  VisitExpr(E);
1487  E->setSourceRange(ReadSourceRange(Record, Idx));
1488  if (E->isTypeOperand()) { // __uuidof(ComType)
1489    E->setTypeOperandSourceInfo(
1490        GetTypeSourceInfo(Record, Idx));
1491    return;
1492  }
1493
1494  // __uuidof(expr)
1495  E->setExprOperand(Reader.ReadSubExpr());
1496}
1497
1498void ASTStmtReader::VisitSEHExceptStmt(SEHExceptStmt *S) {
1499  VisitStmt(S);
1500  S->Loc = ReadSourceLocation(Record, Idx);
1501  S->Children[SEHExceptStmt::FILTER_EXPR] = Reader.ReadSubStmt();
1502  S->Children[SEHExceptStmt::BLOCK] = Reader.ReadSubStmt();
1503}
1504
1505void ASTStmtReader::VisitSEHFinallyStmt(SEHFinallyStmt *S) {
1506  VisitStmt(S);
1507  S->Loc = ReadSourceLocation(Record, Idx);
1508  S->Block = Reader.ReadSubStmt();
1509}
1510
1511void ASTStmtReader::VisitSEHTryStmt(SEHTryStmt *S) {
1512  VisitStmt(S);
1513  S->IsCXXTry = Record[Idx++];
1514  S->TryLoc = ReadSourceLocation(Record, Idx);
1515  S->Children[SEHTryStmt::TRY] = Reader.ReadSubStmt();
1516  S->Children[SEHTryStmt::HANDLER] = Reader.ReadSubStmt();
1517}
1518
1519//===----------------------------------------------------------------------===//
1520// CUDA Expressions and Statements
1521//===----------------------------------------------------------------------===//
1522
1523void ASTStmtReader::VisitCUDAKernelCallExpr(CUDAKernelCallExpr *E) {
1524  VisitCallExpr(E);
1525  E->setConfig(cast<CallExpr>(Reader.ReadSubExpr()));
1526}
1527
1528//===----------------------------------------------------------------------===//
1529// OpenCL Expressions and Statements.
1530//===----------------------------------------------------------------------===//
1531void ASTStmtReader::VisitAsTypeExpr(AsTypeExpr *E) {
1532  VisitExpr(E);
1533  E->BuiltinLoc = ReadSourceLocation(Record, Idx);
1534  E->RParenLoc = ReadSourceLocation(Record, Idx);
1535  E->SrcExpr = Reader.ReadSubExpr();
1536}
1537
1538//===----------------------------------------------------------------------===//
1539// ASTReader Implementation
1540//===----------------------------------------------------------------------===//
1541
1542Stmt *ASTReader::ReadStmt(ModuleFile &F) {
1543  switch (ReadingKind) {
1544  case Read_Decl:
1545  case Read_Type:
1546    return ReadStmtFromStream(F);
1547  case Read_Stmt:
1548    return ReadSubStmt();
1549  }
1550
1551  llvm_unreachable("ReadingKind not set ?");
1552}
1553
1554Expr *ASTReader::ReadExpr(ModuleFile &F) {
1555  return cast_or_null<Expr>(ReadStmt(F));
1556}
1557
1558Expr *ASTReader::ReadSubExpr() {
1559  return cast_or_null<Expr>(ReadSubStmt());
1560}
1561
1562// Within the bitstream, expressions are stored in Reverse Polish
1563// Notation, with each of the subexpressions preceding the
1564// expression they are stored in. Subexpressions are stored from last to first.
1565// To evaluate expressions, we continue reading expressions and placing them on
1566// the stack, with expressions having operands removing those operands from the
1567// stack. Evaluation terminates when we see a STMT_STOP record, and
1568// the single remaining expression on the stack is our result.
1569Stmt *ASTReader::ReadStmtFromStream(ModuleFile &F) {
1570
1571  ReadingKindTracker ReadingKind(Read_Stmt, *this);
1572  llvm::BitstreamCursor &Cursor = F.DeclsCursor;
1573
1574  // Map of offset to previously deserialized stmt. The offset points
1575  /// just after the stmt record.
1576  llvm::DenseMap<uint64_t, Stmt *> StmtEntries;
1577
1578#ifndef NDEBUG
1579  unsigned PrevNumStmts = StmtStack.size();
1580#endif
1581
1582  RecordData Record;
1583  unsigned Idx;
1584  ASTStmtReader Reader(*this, F, Cursor, Record, Idx);
1585  Stmt::EmptyShell Empty;
1586
1587  while (true) {
1588    unsigned Code = Cursor.ReadCode();
1589    if (Code == llvm::bitc::END_BLOCK) {
1590      if (Cursor.ReadBlockEnd()) {
1591        Error("error at end of block in AST file");
1592        return 0;
1593      }
1594      break;
1595    }
1596
1597    if (Code == llvm::bitc::ENTER_SUBBLOCK) {
1598      // No known subblocks, always skip them.
1599      Cursor.ReadSubBlockID();
1600      if (Cursor.SkipBlock()) {
1601        Error("malformed block record in AST file");
1602        return 0;
1603      }
1604      continue;
1605    }
1606
1607    if (Code == llvm::bitc::DEFINE_ABBREV) {
1608      Cursor.ReadAbbrevRecord();
1609      continue;
1610    }
1611
1612    Stmt *S = 0;
1613    Idx = 0;
1614    Record.clear();
1615    bool Finished = false;
1616    bool IsStmtReference = false;
1617    switch ((StmtCode)Cursor.ReadRecord(Code, Record)) {
1618    case STMT_STOP:
1619      Finished = true;
1620      break;
1621
1622    case STMT_REF_PTR:
1623      IsStmtReference = true;
1624      assert(StmtEntries.find(Record[0]) != StmtEntries.end() &&
1625             "No stmt was recorded for this offset reference!");
1626      S = StmtEntries[Record[Idx++]];
1627      break;
1628
1629    case STMT_NULL_PTR:
1630      S = 0;
1631      break;
1632
1633    case STMT_NULL:
1634      S = new (Context) NullStmt(Empty);
1635      break;
1636
1637    case STMT_COMPOUND:
1638      S = new (Context) CompoundStmt(Empty);
1639      break;
1640
1641    case STMT_CASE:
1642      S = new (Context) CaseStmt(Empty);
1643      break;
1644
1645    case STMT_DEFAULT:
1646      S = new (Context) DefaultStmt(Empty);
1647      break;
1648
1649    case STMT_LABEL:
1650      S = new (Context) LabelStmt(Empty);
1651      break;
1652
1653    case STMT_ATTRIBUTED:
1654      S = AttributedStmt::CreateEmpty(
1655        Context,
1656        /*NumAttrs*/Record[ASTStmtReader::NumStmtFields]);
1657      break;
1658
1659    case STMT_IF:
1660      S = new (Context) IfStmt(Empty);
1661      break;
1662
1663    case STMT_SWITCH:
1664      S = new (Context) SwitchStmt(Empty);
1665      break;
1666
1667    case STMT_WHILE:
1668      S = new (Context) WhileStmt(Empty);
1669      break;
1670
1671    case STMT_DO:
1672      S = new (Context) DoStmt(Empty);
1673      break;
1674
1675    case STMT_FOR:
1676      S = new (Context) ForStmt(Empty);
1677      break;
1678
1679    case STMT_GOTO:
1680      S = new (Context) GotoStmt(Empty);
1681      break;
1682
1683    case STMT_INDIRECT_GOTO:
1684      S = new (Context) IndirectGotoStmt(Empty);
1685      break;
1686
1687    case STMT_CONTINUE:
1688      S = new (Context) ContinueStmt(Empty);
1689      break;
1690
1691    case STMT_BREAK:
1692      S = new (Context) BreakStmt(Empty);
1693      break;
1694
1695    case STMT_RETURN:
1696      S = new (Context) ReturnStmt(Empty);
1697      break;
1698
1699    case STMT_DECL:
1700      S = new (Context) DeclStmt(Empty);
1701      break;
1702
1703    case STMT_GCCASM:
1704      S = new (Context) GCCAsmStmt(Empty);
1705      break;
1706
1707    case STMT_MSASM:
1708      S = new (Context) MSAsmStmt(Empty);
1709      break;
1710
1711    case EXPR_PREDEFINED:
1712      S = new (Context) PredefinedExpr(Empty);
1713      break;
1714
1715    case EXPR_DECL_REF:
1716      S = DeclRefExpr::CreateEmpty(
1717        Context,
1718        /*HasQualifier=*/Record[ASTStmtReader::NumExprFields],
1719        /*HasFoundDecl=*/Record[ASTStmtReader::NumExprFields + 1],
1720        /*HasTemplateKWAndArgsInfo=*/Record[ASTStmtReader::NumExprFields + 2],
1721        /*NumTemplateArgs=*/Record[ASTStmtReader::NumExprFields + 2] ?
1722          Record[ASTStmtReader::NumExprFields + 5] : 0);
1723      break;
1724
1725    case EXPR_INTEGER_LITERAL:
1726      S = IntegerLiteral::Create(Context, Empty);
1727      break;
1728
1729    case EXPR_FLOATING_LITERAL:
1730      S = FloatingLiteral::Create(Context, Empty);
1731      break;
1732
1733    case EXPR_IMAGINARY_LITERAL:
1734      S = new (Context) ImaginaryLiteral(Empty);
1735      break;
1736
1737    case EXPR_STRING_LITERAL:
1738      S = StringLiteral::CreateEmpty(Context,
1739                                     Record[ASTStmtReader::NumExprFields + 1]);
1740      break;
1741
1742    case EXPR_CHARACTER_LITERAL:
1743      S = new (Context) CharacterLiteral(Empty);
1744      break;
1745
1746    case EXPR_PAREN:
1747      S = new (Context) ParenExpr(Empty);
1748      break;
1749
1750    case EXPR_PAREN_LIST:
1751      S = new (Context) ParenListExpr(Empty);
1752      break;
1753
1754    case EXPR_UNARY_OPERATOR:
1755      S = new (Context) UnaryOperator(Empty);
1756      break;
1757
1758    case EXPR_OFFSETOF:
1759      S = OffsetOfExpr::CreateEmpty(Context,
1760                                    Record[ASTStmtReader::NumExprFields],
1761                                    Record[ASTStmtReader::NumExprFields + 1]);
1762      break;
1763
1764    case EXPR_SIZEOF_ALIGN_OF:
1765      S = new (Context) UnaryExprOrTypeTraitExpr(Empty);
1766      break;
1767
1768    case EXPR_ARRAY_SUBSCRIPT:
1769      S = new (Context) ArraySubscriptExpr(Empty);
1770      break;
1771
1772    case EXPR_CALL:
1773      S = new (Context) CallExpr(Context, Stmt::CallExprClass, Empty);
1774      break;
1775
1776    case EXPR_MEMBER: {
1777      // We load everything here and fully initialize it at creation.
1778      // That way we can use MemberExpr::Create and don't have to duplicate its
1779      // logic with a MemberExpr::CreateEmpty.
1780
1781      assert(Idx == 0);
1782      NestedNameSpecifierLoc QualifierLoc;
1783      if (Record[Idx++]) { // HasQualifier.
1784        QualifierLoc = ReadNestedNameSpecifierLoc(F, Record, Idx);
1785      }
1786
1787      SourceLocation TemplateKWLoc;
1788      TemplateArgumentListInfo ArgInfo;
1789      bool HasTemplateKWAndArgsInfo = Record[Idx++];
1790      if (HasTemplateKWAndArgsInfo) {
1791        TemplateKWLoc = ReadSourceLocation(F, Record, Idx);
1792        unsigned NumTemplateArgs = Record[Idx++];
1793        ArgInfo.setLAngleLoc(ReadSourceLocation(F, Record, Idx));
1794        ArgInfo.setRAngleLoc(ReadSourceLocation(F, Record, Idx));
1795        for (unsigned i = 0; i != NumTemplateArgs; ++i)
1796          ArgInfo.addArgument(ReadTemplateArgumentLoc(F, Record, Idx));
1797      }
1798
1799      bool HadMultipleCandidates = Record[Idx++];
1800
1801      NamedDecl *FoundD = ReadDeclAs<NamedDecl>(F, Record, Idx);
1802      AccessSpecifier AS = (AccessSpecifier)Record[Idx++];
1803      DeclAccessPair FoundDecl = DeclAccessPair::make(FoundD, AS);
1804
1805      QualType T = readType(F, Record, Idx);
1806      ExprValueKind VK = static_cast<ExprValueKind>(Record[Idx++]);
1807      ExprObjectKind OK = static_cast<ExprObjectKind>(Record[Idx++]);
1808      Expr *Base = ReadSubExpr();
1809      ValueDecl *MemberD = ReadDeclAs<ValueDecl>(F, Record, Idx);
1810      SourceLocation MemberLoc = ReadSourceLocation(F, Record, Idx);
1811      DeclarationNameInfo MemberNameInfo(MemberD->getDeclName(), MemberLoc);
1812      bool IsArrow = Record[Idx++];
1813
1814      S = MemberExpr::Create(Context, Base, IsArrow, QualifierLoc,
1815                             TemplateKWLoc, MemberD, FoundDecl, MemberNameInfo,
1816                             HasTemplateKWAndArgsInfo ? &ArgInfo : 0,
1817                             T, VK, OK);
1818      ReadDeclarationNameLoc(F, cast<MemberExpr>(S)->MemberDNLoc,
1819                             MemberD->getDeclName(), Record, Idx);
1820      if (HadMultipleCandidates)
1821        cast<MemberExpr>(S)->setHadMultipleCandidates(true);
1822      break;
1823    }
1824
1825    case EXPR_BINARY_OPERATOR:
1826      S = new (Context) BinaryOperator(Empty);
1827      break;
1828
1829    case EXPR_COMPOUND_ASSIGN_OPERATOR:
1830      S = new (Context) CompoundAssignOperator(Empty);
1831      break;
1832
1833    case EXPR_CONDITIONAL_OPERATOR:
1834      S = new (Context) ConditionalOperator(Empty);
1835      break;
1836
1837    case EXPR_BINARY_CONDITIONAL_OPERATOR:
1838      S = new (Context) BinaryConditionalOperator(Empty);
1839      break;
1840
1841    case EXPR_IMPLICIT_CAST:
1842      S = ImplicitCastExpr::CreateEmpty(Context,
1843                       /*PathSize*/ Record[ASTStmtReader::NumExprFields]);
1844      break;
1845
1846    case EXPR_CSTYLE_CAST:
1847      S = CStyleCastExpr::CreateEmpty(Context,
1848                       /*PathSize*/ Record[ASTStmtReader::NumExprFields]);
1849      break;
1850
1851    case EXPR_COMPOUND_LITERAL:
1852      S = new (Context) CompoundLiteralExpr(Empty);
1853      break;
1854
1855    case EXPR_EXT_VECTOR_ELEMENT:
1856      S = new (Context) ExtVectorElementExpr(Empty);
1857      break;
1858
1859    case EXPR_INIT_LIST:
1860      S = new (Context) InitListExpr(getContext(), Empty);
1861      break;
1862
1863    case EXPR_DESIGNATED_INIT:
1864      S = DesignatedInitExpr::CreateEmpty(Context,
1865                                     Record[ASTStmtReader::NumExprFields] - 1);
1866
1867      break;
1868
1869    case EXPR_IMPLICIT_VALUE_INIT:
1870      S = new (Context) ImplicitValueInitExpr(Empty);
1871      break;
1872
1873    case EXPR_VA_ARG:
1874      S = new (Context) VAArgExpr(Empty);
1875      break;
1876
1877    case EXPR_ADDR_LABEL:
1878      S = new (Context) AddrLabelExpr(Empty);
1879      break;
1880
1881    case EXPR_STMT:
1882      S = new (Context) StmtExpr(Empty);
1883      break;
1884
1885    case EXPR_CHOOSE:
1886      S = new (Context) ChooseExpr(Empty);
1887      break;
1888
1889    case EXPR_GNU_NULL:
1890      S = new (Context) GNUNullExpr(Empty);
1891      break;
1892
1893    case EXPR_SHUFFLE_VECTOR:
1894      S = new (Context) ShuffleVectorExpr(Empty);
1895      break;
1896
1897    case EXPR_BLOCK:
1898      S = new (Context) BlockExpr(Empty);
1899      break;
1900
1901    case EXPR_GENERIC_SELECTION:
1902      S = new (Context) GenericSelectionExpr(Empty);
1903      break;
1904
1905    case EXPR_OBJC_STRING_LITERAL:
1906      S = new (Context) ObjCStringLiteral(Empty);
1907      break;
1908    case EXPR_OBJC_BOXED_EXPRESSION:
1909      S = new (Context) ObjCBoxedExpr(Empty);
1910      break;
1911    case EXPR_OBJC_ARRAY_LITERAL:
1912      S = ObjCArrayLiteral::CreateEmpty(Context,
1913                                        Record[ASTStmtReader::NumExprFields]);
1914      break;
1915    case EXPR_OBJC_DICTIONARY_LITERAL:
1916      S = ObjCDictionaryLiteral::CreateEmpty(Context,
1917            Record[ASTStmtReader::NumExprFields],
1918            Record[ASTStmtReader::NumExprFields + 1]);
1919      break;
1920    case EXPR_OBJC_ENCODE:
1921      S = new (Context) ObjCEncodeExpr(Empty);
1922      break;
1923    case EXPR_OBJC_SELECTOR_EXPR:
1924      S = new (Context) ObjCSelectorExpr(Empty);
1925      break;
1926    case EXPR_OBJC_PROTOCOL_EXPR:
1927      S = new (Context) ObjCProtocolExpr(Empty);
1928      break;
1929    case EXPR_OBJC_IVAR_REF_EXPR:
1930      S = new (Context) ObjCIvarRefExpr(Empty);
1931      break;
1932    case EXPR_OBJC_PROPERTY_REF_EXPR:
1933      S = new (Context) ObjCPropertyRefExpr(Empty);
1934      break;
1935    case EXPR_OBJC_SUBSCRIPT_REF_EXPR:
1936      S = new (Context) ObjCSubscriptRefExpr(Empty);
1937      break;
1938    case EXPR_OBJC_KVC_REF_EXPR:
1939      llvm_unreachable("mismatching AST file");
1940    case EXPR_OBJC_MESSAGE_EXPR:
1941      S = ObjCMessageExpr::CreateEmpty(Context,
1942                                     Record[ASTStmtReader::NumExprFields],
1943                                     Record[ASTStmtReader::NumExprFields + 1]);
1944      break;
1945    case EXPR_OBJC_ISA:
1946      S = new (Context) ObjCIsaExpr(Empty);
1947      break;
1948    case EXPR_OBJC_INDIRECT_COPY_RESTORE:
1949      S = new (Context) ObjCIndirectCopyRestoreExpr(Empty);
1950      break;
1951    case EXPR_OBJC_BRIDGED_CAST:
1952      S = new (Context) ObjCBridgedCastExpr(Empty);
1953      break;
1954    case STMT_OBJC_FOR_COLLECTION:
1955      S = new (Context) ObjCForCollectionStmt(Empty);
1956      break;
1957    case STMT_OBJC_CATCH:
1958      S = new (Context) ObjCAtCatchStmt(Empty);
1959      break;
1960    case STMT_OBJC_FINALLY:
1961      S = new (Context) ObjCAtFinallyStmt(Empty);
1962      break;
1963    case STMT_OBJC_AT_TRY:
1964      S = ObjCAtTryStmt::CreateEmpty(Context,
1965                                     Record[ASTStmtReader::NumStmtFields],
1966                                     Record[ASTStmtReader::NumStmtFields + 1]);
1967      break;
1968    case STMT_OBJC_AT_SYNCHRONIZED:
1969      S = new (Context) ObjCAtSynchronizedStmt(Empty);
1970      break;
1971    case STMT_OBJC_AT_THROW:
1972      S = new (Context) ObjCAtThrowStmt(Empty);
1973      break;
1974    case STMT_OBJC_AUTORELEASE_POOL:
1975      S = new (Context) ObjCAutoreleasePoolStmt(Empty);
1976      break;
1977    case EXPR_OBJC_BOOL_LITERAL:
1978      S = new (Context) ObjCBoolLiteralExpr(Empty);
1979      break;
1980    case STMT_SEH_EXCEPT:
1981      S = new (Context) SEHExceptStmt(Empty);
1982      break;
1983    case STMT_SEH_FINALLY:
1984      S = new (Context) SEHFinallyStmt(Empty);
1985      break;
1986    case STMT_SEH_TRY:
1987      S = new (Context) SEHTryStmt(Empty);
1988      break;
1989    case STMT_CXX_CATCH:
1990      S = new (Context) CXXCatchStmt(Empty);
1991      break;
1992
1993    case STMT_CXX_TRY:
1994      S = CXXTryStmt::Create(Context, Empty,
1995             /*NumHandlers=*/Record[ASTStmtReader::NumStmtFields]);
1996      break;
1997
1998    case STMT_CXX_FOR_RANGE:
1999      S = new (Context) CXXForRangeStmt(Empty);
2000      break;
2001
2002    case STMT_MS_DEPENDENT_EXISTS:
2003      S = new (Context) MSDependentExistsStmt(SourceLocation(), true,
2004                                              NestedNameSpecifierLoc(),
2005                                              DeclarationNameInfo(),
2006                                              0);
2007      break;
2008
2009    case EXPR_CXX_OPERATOR_CALL:
2010      S = new (Context) CXXOperatorCallExpr(Context, Empty);
2011      break;
2012
2013    case EXPR_CXX_MEMBER_CALL:
2014      S = new (Context) CXXMemberCallExpr(Context, Empty);
2015      break;
2016
2017    case EXPR_CXX_CONSTRUCT:
2018      S = new (Context) CXXConstructExpr(Empty);
2019      break;
2020
2021    case EXPR_CXX_TEMPORARY_OBJECT:
2022      S = new (Context) CXXTemporaryObjectExpr(Empty);
2023      break;
2024
2025    case EXPR_CXX_STATIC_CAST:
2026      S = CXXStaticCastExpr::CreateEmpty(Context,
2027                       /*PathSize*/ Record[ASTStmtReader::NumExprFields]);
2028      break;
2029
2030    case EXPR_CXX_DYNAMIC_CAST:
2031      S = CXXDynamicCastExpr::CreateEmpty(Context,
2032                       /*PathSize*/ Record[ASTStmtReader::NumExprFields]);
2033      break;
2034
2035    case EXPR_CXX_REINTERPRET_CAST:
2036      S = CXXReinterpretCastExpr::CreateEmpty(Context,
2037                       /*PathSize*/ Record[ASTStmtReader::NumExprFields]);
2038      break;
2039
2040    case EXPR_CXX_CONST_CAST:
2041      S = CXXConstCastExpr::CreateEmpty(Context);
2042      break;
2043
2044    case EXPR_CXX_FUNCTIONAL_CAST:
2045      S = CXXFunctionalCastExpr::CreateEmpty(Context,
2046                       /*PathSize*/ Record[ASTStmtReader::NumExprFields]);
2047      break;
2048
2049    case EXPR_USER_DEFINED_LITERAL:
2050      S = new (Context) UserDefinedLiteral(Context, Empty);
2051      break;
2052
2053    case EXPR_CXX_BOOL_LITERAL:
2054      S = new (Context) CXXBoolLiteralExpr(Empty);
2055      break;
2056
2057    case EXPR_CXX_NULL_PTR_LITERAL:
2058      S = new (Context) CXXNullPtrLiteralExpr(Empty);
2059      break;
2060    case EXPR_CXX_TYPEID_EXPR:
2061      S = new (Context) CXXTypeidExpr(Empty, true);
2062      break;
2063    case EXPR_CXX_TYPEID_TYPE:
2064      S = new (Context) CXXTypeidExpr(Empty, false);
2065      break;
2066    case EXPR_CXX_UUIDOF_EXPR:
2067      S = new (Context) CXXUuidofExpr(Empty, true);
2068      break;
2069    case EXPR_CXX_UUIDOF_TYPE:
2070      S = new (Context) CXXUuidofExpr(Empty, false);
2071      break;
2072    case EXPR_CXX_THIS:
2073      S = new (Context) CXXThisExpr(Empty);
2074      break;
2075    case EXPR_CXX_THROW:
2076      S = new (Context) CXXThrowExpr(Empty);
2077      break;
2078    case EXPR_CXX_DEFAULT_ARG: {
2079      bool HasOtherExprStored = Record[ASTStmtReader::NumExprFields];
2080      if (HasOtherExprStored) {
2081        Expr *SubExpr = ReadSubExpr();
2082        S = CXXDefaultArgExpr::Create(Context, SourceLocation(), 0, SubExpr);
2083      } else
2084        S = new (Context) CXXDefaultArgExpr(Empty);
2085      break;
2086    }
2087    case EXPR_CXX_BIND_TEMPORARY:
2088      S = new (Context) CXXBindTemporaryExpr(Empty);
2089      break;
2090
2091    case EXPR_CXX_SCALAR_VALUE_INIT:
2092      S = new (Context) CXXScalarValueInitExpr(Empty);
2093      break;
2094    case EXPR_CXX_NEW:
2095      S = new (Context) CXXNewExpr(Empty);
2096      break;
2097    case EXPR_CXX_DELETE:
2098      S = new (Context) CXXDeleteExpr(Empty);
2099      break;
2100    case EXPR_CXX_PSEUDO_DESTRUCTOR:
2101      S = new (Context) CXXPseudoDestructorExpr(Empty);
2102      break;
2103
2104    case EXPR_EXPR_WITH_CLEANUPS:
2105      S = ExprWithCleanups::Create(Context, Empty,
2106                                   Record[ASTStmtReader::NumExprFields]);
2107      break;
2108
2109    case EXPR_CXX_DEPENDENT_SCOPE_MEMBER:
2110      S = CXXDependentScopeMemberExpr::CreateEmpty(Context,
2111         /*HasTemplateKWAndArgsInfo=*/Record[ASTStmtReader::NumExprFields],
2112                  /*NumTemplateArgs=*/Record[ASTStmtReader::NumExprFields]
2113                                   ? Record[ASTStmtReader::NumExprFields + 1]
2114                                   : 0);
2115      break;
2116
2117    case EXPR_CXX_DEPENDENT_SCOPE_DECL_REF:
2118      S = DependentScopeDeclRefExpr::CreateEmpty(Context,
2119         /*HasTemplateKWAndArgsInfo=*/Record[ASTStmtReader::NumExprFields],
2120                  /*NumTemplateArgs=*/Record[ASTStmtReader::NumExprFields]
2121                                   ? Record[ASTStmtReader::NumExprFields + 1]
2122                                   : 0);
2123      break;
2124
2125    case EXPR_CXX_UNRESOLVED_CONSTRUCT:
2126      S = CXXUnresolvedConstructExpr::CreateEmpty(Context,
2127                              /*NumArgs=*/Record[ASTStmtReader::NumExprFields]);
2128      break;
2129
2130    case EXPR_CXX_UNRESOLVED_MEMBER:
2131      S = UnresolvedMemberExpr::CreateEmpty(Context,
2132         /*HasTemplateKWAndArgsInfo=*/Record[ASTStmtReader::NumExprFields],
2133                  /*NumTemplateArgs=*/Record[ASTStmtReader::NumExprFields]
2134                                   ? Record[ASTStmtReader::NumExprFields + 1]
2135                                   : 0);
2136      break;
2137
2138    case EXPR_CXX_UNRESOLVED_LOOKUP:
2139      S = UnresolvedLookupExpr::CreateEmpty(Context,
2140         /*HasTemplateKWAndArgsInfo=*/Record[ASTStmtReader::NumExprFields],
2141                  /*NumTemplateArgs=*/Record[ASTStmtReader::NumExprFields]
2142                                   ? Record[ASTStmtReader::NumExprFields + 1]
2143                                   : 0);
2144      break;
2145
2146    case EXPR_CXX_UNARY_TYPE_TRAIT:
2147      S = new (Context) UnaryTypeTraitExpr(Empty);
2148      break;
2149
2150    case EXPR_BINARY_TYPE_TRAIT:
2151      S = new (Context) BinaryTypeTraitExpr(Empty);
2152      break;
2153
2154    case EXPR_TYPE_TRAIT:
2155      S = TypeTraitExpr::CreateDeserialized(Context,
2156            Record[ASTStmtReader::NumExprFields]);
2157      break;
2158
2159    case EXPR_ARRAY_TYPE_TRAIT:
2160      S = new (Context) ArrayTypeTraitExpr(Empty);
2161      break;
2162
2163    case EXPR_CXX_EXPRESSION_TRAIT:
2164      S = new (Context) ExpressionTraitExpr(Empty);
2165      break;
2166
2167    case EXPR_CXX_NOEXCEPT:
2168      S = new (Context) CXXNoexceptExpr(Empty);
2169      break;
2170
2171    case EXPR_PACK_EXPANSION:
2172      S = new (Context) PackExpansionExpr(Empty);
2173      break;
2174
2175    case EXPR_SIZEOF_PACK:
2176      S = new (Context) SizeOfPackExpr(Empty);
2177      break;
2178
2179    case EXPR_SUBST_NON_TYPE_TEMPLATE_PARM:
2180      S = new (Context) SubstNonTypeTemplateParmExpr(Empty);
2181      break;
2182
2183    case EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK:
2184      S = new (Context) SubstNonTypeTemplateParmPackExpr(Empty);
2185      break;
2186
2187    case EXPR_MATERIALIZE_TEMPORARY:
2188      S = new (Context) MaterializeTemporaryExpr(Empty);
2189      break;
2190
2191    case EXPR_OPAQUE_VALUE:
2192      S = new (Context) OpaqueValueExpr(Empty);
2193      break;
2194
2195    case EXPR_CUDA_KERNEL_CALL:
2196      S = new (Context) CUDAKernelCallExpr(Context, Empty);
2197      break;
2198
2199    case EXPR_ASTYPE:
2200      S = new (Context) AsTypeExpr(Empty);
2201      break;
2202
2203    case EXPR_PSEUDO_OBJECT: {
2204      unsigned numSemanticExprs = Record[ASTStmtReader::NumExprFields];
2205      S = PseudoObjectExpr::Create(Context, Empty, numSemanticExprs);
2206      break;
2207    }
2208
2209    case EXPR_ATOMIC:
2210      S = new (Context) AtomicExpr(Empty);
2211      break;
2212
2213    case EXPR_LAMBDA: {
2214      unsigned NumCaptures = Record[ASTStmtReader::NumExprFields];
2215      unsigned NumArrayIndexVars = Record[ASTStmtReader::NumExprFields + 1];
2216      S = LambdaExpr::CreateDeserialized(Context, NumCaptures,
2217                                         NumArrayIndexVars);
2218      break;
2219    }
2220    }
2221
2222    // We hit a STMT_STOP, so we're done with this expression.
2223    if (Finished)
2224      break;
2225
2226    ++NumStatementsRead;
2227
2228    if (S && !IsStmtReference) {
2229      Reader.Visit(S);
2230      StmtEntries[Cursor.GetCurrentBitNo()] = S;
2231    }
2232
2233
2234    assert(Idx == Record.size() && "Invalid deserialization of statement");
2235    StmtStack.push_back(S);
2236  }
2237
2238#ifndef NDEBUG
2239  assert(StmtStack.size() > PrevNumStmts && "Read too many sub stmts!");
2240  assert(StmtStack.size() == PrevNumStmts + 1 && "Extra expressions on stack!");
2241#endif
2242
2243  return StmtStack.pop_back_val();
2244}
2245