StmtCXX.cpp revision 321369
1//===--- StmtCXX.cpp - Classes for representing C++ statements ------------===//
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 the subclesses of Stmt class declared in StmtCXX.h
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/StmtCXX.h"
15
16#include "clang/AST/ASTContext.h"
17
18using namespace clang;
19
20QualType CXXCatchStmt::getCaughtType() const {
21  if (ExceptionDecl)
22    return ExceptionDecl->getType();
23  return QualType();
24}
25
26CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, SourceLocation tryLoc,
27                               Stmt *tryBlock, ArrayRef<Stmt *> handlers) {
28  std::size_t Size = sizeof(CXXTryStmt);
29  Size += ((handlers.size() + 1) * sizeof(Stmt *));
30
31  void *Mem = C.Allocate(Size, alignof(CXXTryStmt));
32  return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers);
33}
34
35CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, EmptyShell Empty,
36                               unsigned numHandlers) {
37  std::size_t Size = sizeof(CXXTryStmt);
38  Size += ((numHandlers + 1) * sizeof(Stmt *));
39
40  void *Mem = C.Allocate(Size, alignof(CXXTryStmt));
41  return new (Mem) CXXTryStmt(Empty, numHandlers);
42}
43
44CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
45                       ArrayRef<Stmt *> handlers)
46    : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(handlers.size()) {
47  Stmt **Stmts = reinterpret_cast<Stmt **>(this + 1);
48  Stmts[0] = tryBlock;
49  std::copy(handlers.begin(), handlers.end(), Stmts + 1);
50}
51
52CXXForRangeStmt::CXXForRangeStmt(DeclStmt *Range,
53                                 DeclStmt *BeginStmt, DeclStmt *EndStmt,
54                                 Expr *Cond, Expr *Inc, DeclStmt *LoopVar,
55                                 Stmt *Body, SourceLocation FL,
56                                 SourceLocation CAL, SourceLocation CL,
57                                 SourceLocation RPL)
58    : Stmt(CXXForRangeStmtClass), ForLoc(FL), CoawaitLoc(CAL), ColonLoc(CL),
59      RParenLoc(RPL) {
60  SubExprs[RANGE] = Range;
61  SubExprs[BEGINSTMT] = BeginStmt;
62  SubExprs[ENDSTMT] = EndStmt;
63  SubExprs[COND] = Cond;
64  SubExprs[INC] = Inc;
65  SubExprs[LOOPVAR] = LoopVar;
66  SubExprs[BODY] = Body;
67}
68
69Expr *CXXForRangeStmt::getRangeInit() {
70  DeclStmt *RangeStmt = getRangeStmt();
71  VarDecl *RangeDecl = dyn_cast_or_null<VarDecl>(RangeStmt->getSingleDecl());
72  assert(RangeDecl && "for-range should have a single var decl");
73  return RangeDecl->getInit();
74}
75
76const Expr *CXXForRangeStmt::getRangeInit() const {
77  return const_cast<CXXForRangeStmt *>(this)->getRangeInit();
78}
79
80VarDecl *CXXForRangeStmt::getLoopVariable() {
81  Decl *LV = cast<DeclStmt>(getLoopVarStmt())->getSingleDecl();
82  assert(LV && "No loop variable in CXXForRangeStmt");
83  return cast<VarDecl>(LV);
84}
85
86const VarDecl *CXXForRangeStmt::getLoopVariable() const {
87  return const_cast<CXXForRangeStmt *>(this)->getLoopVariable();
88}
89
90CoroutineBodyStmt *CoroutineBodyStmt::Create(
91    const ASTContext &C, CoroutineBodyStmt::CtorArgs const &Args) {
92  std::size_t Size = totalSizeToAlloc<Stmt *>(
93      CoroutineBodyStmt::FirstParamMove + Args.ParamMoves.size());
94
95  void *Mem = C.Allocate(Size, alignof(CoroutineBodyStmt));
96  return new (Mem) CoroutineBodyStmt(Args);
97}
98
99CoroutineBodyStmt::CoroutineBodyStmt(CoroutineBodyStmt::CtorArgs const &Args)
100    : Stmt(CoroutineBodyStmtClass), NumParams(Args.ParamMoves.size()) {
101  Stmt **SubStmts = getStoredStmts();
102  SubStmts[CoroutineBodyStmt::Body] = Args.Body;
103  SubStmts[CoroutineBodyStmt::Promise] = Args.Promise;
104  SubStmts[CoroutineBodyStmt::InitSuspend] = Args.InitialSuspend;
105  SubStmts[CoroutineBodyStmt::FinalSuspend] = Args.FinalSuspend;
106  SubStmts[CoroutineBodyStmt::OnException] = Args.OnException;
107  SubStmts[CoroutineBodyStmt::OnFallthrough] = Args.OnFallthrough;
108  SubStmts[CoroutineBodyStmt::Allocate] = Args.Allocate;
109  SubStmts[CoroutineBodyStmt::Deallocate] = Args.Deallocate;
110  SubStmts[CoroutineBodyStmt::ReturnValue] = Args.ReturnValue;
111  SubStmts[CoroutineBodyStmt::ResultDecl] = Args.ResultDecl;
112  SubStmts[CoroutineBodyStmt::ReturnStmt] = Args.ReturnStmt;
113  SubStmts[CoroutineBodyStmt::ReturnStmtOnAllocFailure] =
114      Args.ReturnStmtOnAllocFailure;
115  std::copy(Args.ParamMoves.begin(), Args.ParamMoves.end(),
116            const_cast<Stmt **>(getParamMoves().data()));
117}
118