James Y Knight | b8bfd96 | 2015-10-02 13:41:04 +0000 | [diff] [blame] | 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 | |
| 18 | using namespace clang; |
| 19 | |
| 20 | QualType CXXCatchStmt::getCaughtType() const { |
| 21 | if (ExceptionDecl) |
| 22 | return ExceptionDecl->getType(); |
| 23 | return QualType(); |
| 24 | } |
| 25 | |
| 26 | CXXTryStmt *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 | |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 31 | void *Mem = C.Allocate(Size, alignof(CXXTryStmt)); |
James Y Knight | b8bfd96 | 2015-10-02 13:41:04 +0000 | [diff] [blame] | 32 | return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers); |
| 33 | } |
| 34 | |
| 35 | CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, EmptyShell Empty, |
| 36 | unsigned numHandlers) { |
| 37 | std::size_t Size = sizeof(CXXTryStmt); |
| 38 | Size += ((numHandlers + 1) * sizeof(Stmt *)); |
| 39 | |
Benjamin Kramer | c3f8925 | 2016-10-20 14:27:22 +0000 | [diff] [blame] | 40 | void *Mem = C.Allocate(Size, alignof(CXXTryStmt)); |
James Y Knight | b8bfd96 | 2015-10-02 13:41:04 +0000 | [diff] [blame] | 41 | return new (Mem) CXXTryStmt(Empty, numHandlers); |
| 42 | } |
| 43 | |
| 44 | CXXTryStmt::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 | |
Richard Smith | 01694c3 | 2016-03-20 10:33:40 +0000 | [diff] [blame] | 52 | CXXForRangeStmt::CXXForRangeStmt(DeclStmt *Range, |
| 53 | DeclStmt *BeginStmt, DeclStmt *EndStmt, |
James Y Knight | b8bfd96 | 2015-10-02 13:41:04 +0000 | [diff] [blame] | 54 | Expr *Cond, Expr *Inc, DeclStmt *LoopVar, |
| 55 | Stmt *Body, SourceLocation FL, |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 56 | SourceLocation CAL, SourceLocation CL, |
| 57 | SourceLocation RPL) |
| 58 | : Stmt(CXXForRangeStmtClass), ForLoc(FL), CoawaitLoc(CAL), ColonLoc(CL), |
| 59 | RParenLoc(RPL) { |
James Y Knight | b8bfd96 | 2015-10-02 13:41:04 +0000 | [diff] [blame] | 60 | SubExprs[RANGE] = Range; |
Richard Smith | 01694c3 | 2016-03-20 10:33:40 +0000 | [diff] [blame] | 61 | SubExprs[BEGINSTMT] = BeginStmt; |
| 62 | SubExprs[ENDSTMT] = EndStmt; |
James Y Knight | b8bfd96 | 2015-10-02 13:41:04 +0000 | [diff] [blame] | 63 | SubExprs[COND] = Cond; |
| 64 | SubExprs[INC] = Inc; |
| 65 | SubExprs[LOOPVAR] = LoopVar; |
| 66 | SubExprs[BODY] = Body; |
| 67 | } |
| 68 | |
| 69 | Expr *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 | |
| 76 | const Expr *CXXForRangeStmt::getRangeInit() const { |
| 77 | return const_cast<CXXForRangeStmt *>(this)->getRangeInit(); |
| 78 | } |
| 79 | |
| 80 | VarDecl *CXXForRangeStmt::getLoopVariable() { |
| 81 | Decl *LV = cast<DeclStmt>(getLoopVarStmt())->getSingleDecl(); |
| 82 | assert(LV && "No loop variable in CXXForRangeStmt"); |
| 83 | return cast<VarDecl>(LV); |
| 84 | } |
| 85 | |
| 86 | const VarDecl *CXXForRangeStmt::getLoopVariable() const { |
| 87 | return const_cast<CXXForRangeStmt *>(this)->getLoopVariable(); |
| 88 | } |
Gor Nishanov | bbe1c07 | 2017-02-13 05:05:02 +0000 | [diff] [blame] | 89 | |
| 90 | CoroutineBodyStmt *CoroutineBodyStmt::Create( |
Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 91 | const ASTContext &C, CoroutineBodyStmt::CtorArgs const &Args) { |
Gor Nishanov | bbe1c07 | 2017-02-13 05:05:02 +0000 | [diff] [blame] | 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 | |
Gor Nishanov | f5ecb5e | 2017-07-25 18:01:49 +0000 | [diff] [blame] | 99 | CoroutineBodyStmt *CoroutineBodyStmt::Create(const ASTContext &C, EmptyShell, |
| 100 | unsigned NumParams) { |
| 101 | std::size_t Size = totalSizeToAlloc<Stmt *>( |
| 102 | CoroutineBodyStmt::FirstParamMove + NumParams); |
| 103 | |
| 104 | void *Mem = C.Allocate(Size, alignof(CoroutineBodyStmt)); |
| 105 | auto *Result = new (Mem) CoroutineBodyStmt(CtorArgs()); |
| 106 | Result->NumParams = NumParams; |
| 107 | auto *ParamBegin = Result->getStoredStmts() + SubStmt::FirstParamMove; |
| 108 | std::uninitialized_fill(ParamBegin, ParamBegin + NumParams, |
| 109 | static_cast<Stmt *>(nullptr)); |
| 110 | return Result; |
| 111 | } |
| 112 | |
Gor Nishanov | bbe1c07 | 2017-02-13 05:05:02 +0000 | [diff] [blame] | 113 | CoroutineBodyStmt::CoroutineBodyStmt(CoroutineBodyStmt::CtorArgs const &Args) |
| 114 | : Stmt(CoroutineBodyStmtClass), NumParams(Args.ParamMoves.size()) { |
| 115 | Stmt **SubStmts = getStoredStmts(); |
| 116 | SubStmts[CoroutineBodyStmt::Body] = Args.Body; |
| 117 | SubStmts[CoroutineBodyStmt::Promise] = Args.Promise; |
| 118 | SubStmts[CoroutineBodyStmt::InitSuspend] = Args.InitialSuspend; |
| 119 | SubStmts[CoroutineBodyStmt::FinalSuspend] = Args.FinalSuspend; |
| 120 | SubStmts[CoroutineBodyStmt::OnException] = Args.OnException; |
| 121 | SubStmts[CoroutineBodyStmt::OnFallthrough] = Args.OnFallthrough; |
| 122 | SubStmts[CoroutineBodyStmt::Allocate] = Args.Allocate; |
| 123 | SubStmts[CoroutineBodyStmt::Deallocate] = Args.Deallocate; |
| 124 | SubStmts[CoroutineBodyStmt::ReturnValue] = Args.ReturnValue; |
Gor Nishanov | 6a47068 | 2017-05-22 20:22:23 +0000 | [diff] [blame] | 125 | SubStmts[CoroutineBodyStmt::ResultDecl] = Args.ResultDecl; |
| 126 | SubStmts[CoroutineBodyStmt::ReturnStmt] = Args.ReturnStmt; |
Gor Nishanov | 3aa9eb3 | 2017-03-27 23:36:59 +0000 | [diff] [blame] | 127 | SubStmts[CoroutineBodyStmt::ReturnStmtOnAllocFailure] = |
| 128 | Args.ReturnStmtOnAllocFailure; |
Gor Nishanov | bbe1c07 | 2017-02-13 05:05:02 +0000 | [diff] [blame] | 129 | std::copy(Args.ParamMoves.begin(), Args.ParamMoves.end(), |
| 130 | const_cast<Stmt **>(getParamMoves().data())); |
Eric Fiselier | bee782b | 2017-04-03 19:21:00 +0000 | [diff] [blame] | 131 | } |