blob: 12367f8fd54b367282ca65ab3168055b1ff6cdca [file] [log] [blame]
James Y Knightb8bfd962015-10-02 13:41:04 +00001//===--- 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) {
Benjamin Kramerc0a0fb32018-07-23 12:45:24 +000028 const size_t Size = totalSizeToAlloc<Stmt *>(handlers.size() + 1);
Benjamin Kramerc3f89252016-10-20 14:27:22 +000029 void *Mem = C.Allocate(Size, alignof(CXXTryStmt));
James Y Knightb8bfd962015-10-02 13:41:04 +000030 return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers);
31}
32
33CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, EmptyShell Empty,
34 unsigned numHandlers) {
Benjamin Kramerc0a0fb32018-07-23 12:45:24 +000035 const size_t Size = totalSizeToAlloc<Stmt *>(numHandlers + 1);
Benjamin Kramerc3f89252016-10-20 14:27:22 +000036 void *Mem = C.Allocate(Size, alignof(CXXTryStmt));
James Y Knightb8bfd962015-10-02 13:41:04 +000037 return new (Mem) CXXTryStmt(Empty, numHandlers);
38}
39
40CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
41 ArrayRef<Stmt *> handlers)
42 : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(handlers.size()) {
Benjamin Kramerc0a0fb32018-07-23 12:45:24 +000043 Stmt **Stmts = getStmts();
James Y Knightb8bfd962015-10-02 13:41:04 +000044 Stmts[0] = tryBlock;
45 std::copy(handlers.begin(), handlers.end(), Stmts + 1);
46}
47
Richard Smith8baa5002018-09-28 18:44:09 +000048CXXForRangeStmt::CXXForRangeStmt(Stmt *Init, DeclStmt *Range,
Richard Smith01694c32016-03-20 10:33:40 +000049 DeclStmt *BeginStmt, DeclStmt *EndStmt,
James Y Knightb8bfd962015-10-02 13:41:04 +000050 Expr *Cond, Expr *Inc, DeclStmt *LoopVar,
51 Stmt *Body, SourceLocation FL,
Richard Smith9f690bd2015-10-27 06:02:45 +000052 SourceLocation CAL, SourceLocation CL,
53 SourceLocation RPL)
54 : Stmt(CXXForRangeStmtClass), ForLoc(FL), CoawaitLoc(CAL), ColonLoc(CL),
55 RParenLoc(RPL) {
Richard Smith8baa5002018-09-28 18:44:09 +000056 SubExprs[INIT] = Init;
James Y Knightb8bfd962015-10-02 13:41:04 +000057 SubExprs[RANGE] = Range;
Richard Smith01694c32016-03-20 10:33:40 +000058 SubExprs[BEGINSTMT] = BeginStmt;
59 SubExprs[ENDSTMT] = EndStmt;
James Y Knightb8bfd962015-10-02 13:41:04 +000060 SubExprs[COND] = Cond;
61 SubExprs[INC] = Inc;
62 SubExprs[LOOPVAR] = LoopVar;
63 SubExprs[BODY] = Body;
64}
65
66Expr *CXXForRangeStmt::getRangeInit() {
67 DeclStmt *RangeStmt = getRangeStmt();
68 VarDecl *RangeDecl = dyn_cast_or_null<VarDecl>(RangeStmt->getSingleDecl());
69 assert(RangeDecl && "for-range should have a single var decl");
70 return RangeDecl->getInit();
71}
72
73const Expr *CXXForRangeStmt::getRangeInit() const {
74 return const_cast<CXXForRangeStmt *>(this)->getRangeInit();
75}
76
77VarDecl *CXXForRangeStmt::getLoopVariable() {
78 Decl *LV = cast<DeclStmt>(getLoopVarStmt())->getSingleDecl();
79 assert(LV && "No loop variable in CXXForRangeStmt");
80 return cast<VarDecl>(LV);
81}
82
83const VarDecl *CXXForRangeStmt::getLoopVariable() const {
84 return const_cast<CXXForRangeStmt *>(this)->getLoopVariable();
85}
Gor Nishanovbbe1c072017-02-13 05:05:02 +000086
87CoroutineBodyStmt *CoroutineBodyStmt::Create(
Gor Nishanov6a470682017-05-22 20:22:23 +000088 const ASTContext &C, CoroutineBodyStmt::CtorArgs const &Args) {
Gor Nishanovbbe1c072017-02-13 05:05:02 +000089 std::size_t Size = totalSizeToAlloc<Stmt *>(
90 CoroutineBodyStmt::FirstParamMove + Args.ParamMoves.size());
91
92 void *Mem = C.Allocate(Size, alignof(CoroutineBodyStmt));
93 return new (Mem) CoroutineBodyStmt(Args);
94}
95
Gor Nishanovf5ecb5e2017-07-25 18:01:49 +000096CoroutineBodyStmt *CoroutineBodyStmt::Create(const ASTContext &C, EmptyShell,
97 unsigned NumParams) {
98 std::size_t Size = totalSizeToAlloc<Stmt *>(
99 CoroutineBodyStmt::FirstParamMove + NumParams);
100
101 void *Mem = C.Allocate(Size, alignof(CoroutineBodyStmt));
102 auto *Result = new (Mem) CoroutineBodyStmt(CtorArgs());
103 Result->NumParams = NumParams;
104 auto *ParamBegin = Result->getStoredStmts() + SubStmt::FirstParamMove;
105 std::uninitialized_fill(ParamBegin, ParamBegin + NumParams,
106 static_cast<Stmt *>(nullptr));
107 return Result;
108}
109
Gor Nishanovbbe1c072017-02-13 05:05:02 +0000110CoroutineBodyStmt::CoroutineBodyStmt(CoroutineBodyStmt::CtorArgs const &Args)
111 : Stmt(CoroutineBodyStmtClass), NumParams(Args.ParamMoves.size()) {
112 Stmt **SubStmts = getStoredStmts();
113 SubStmts[CoroutineBodyStmt::Body] = Args.Body;
114 SubStmts[CoroutineBodyStmt::Promise] = Args.Promise;
115 SubStmts[CoroutineBodyStmt::InitSuspend] = Args.InitialSuspend;
116 SubStmts[CoroutineBodyStmt::FinalSuspend] = Args.FinalSuspend;
117 SubStmts[CoroutineBodyStmt::OnException] = Args.OnException;
118 SubStmts[CoroutineBodyStmt::OnFallthrough] = Args.OnFallthrough;
119 SubStmts[CoroutineBodyStmt::Allocate] = Args.Allocate;
120 SubStmts[CoroutineBodyStmt::Deallocate] = Args.Deallocate;
121 SubStmts[CoroutineBodyStmt::ReturnValue] = Args.ReturnValue;
Gor Nishanov6a470682017-05-22 20:22:23 +0000122 SubStmts[CoroutineBodyStmt::ResultDecl] = Args.ResultDecl;
123 SubStmts[CoroutineBodyStmt::ReturnStmt] = Args.ReturnStmt;
Gor Nishanov3aa9eb32017-03-27 23:36:59 +0000124 SubStmts[CoroutineBodyStmt::ReturnStmtOnAllocFailure] =
125 Args.ReturnStmtOnAllocFailure;
Gor Nishanovbbe1c072017-02-13 05:05:02 +0000126 std::copy(Args.ParamMoves.begin(), Args.ParamMoves.end(),
127 const_cast<Stmt **>(getParamMoves().data()));
Eric Fiselierbee782b2017-04-03 19:21:00 +0000128}