blob: 060d090fc06ac75a632565c27d89efe9cbb23e02 [file] [log] [blame]
James Y Knightb8bfd962015-10-02 13:41:04 +00001//===--- StmtCXX.cpp - Classes for representing C++ statements ------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
James Y Knightb8bfd962015-10-02 13:41:04 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the subclesses of Stmt class declared in StmtCXX.h
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/AST/StmtCXX.h"
14
15#include "clang/AST/ASTContext.h"
16
17using namespace clang;
18
19QualType CXXCatchStmt::getCaughtType() const {
20 if (ExceptionDecl)
21 return ExceptionDecl->getType();
22 return QualType();
23}
24
25CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, SourceLocation tryLoc,
26 Stmt *tryBlock, ArrayRef<Stmt *> handlers) {
Benjamin Kramerc0a0fb32018-07-23 12:45:24 +000027 const size_t Size = totalSizeToAlloc<Stmt *>(handlers.size() + 1);
Benjamin Kramerc3f89252016-10-20 14:27:22 +000028 void *Mem = C.Allocate(Size, alignof(CXXTryStmt));
James Y Knightb8bfd962015-10-02 13:41:04 +000029 return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers);
30}
31
32CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, EmptyShell Empty,
33 unsigned numHandlers) {
Benjamin Kramerc0a0fb32018-07-23 12:45:24 +000034 const size_t Size = totalSizeToAlloc<Stmt *>(numHandlers + 1);
Benjamin Kramerc3f89252016-10-20 14:27:22 +000035 void *Mem = C.Allocate(Size, alignof(CXXTryStmt));
James Y Knightb8bfd962015-10-02 13:41:04 +000036 return new (Mem) CXXTryStmt(Empty, numHandlers);
37}
38
39CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
40 ArrayRef<Stmt *> handlers)
41 : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(handlers.size()) {
Benjamin Kramerc0a0fb32018-07-23 12:45:24 +000042 Stmt **Stmts = getStmts();
James Y Knightb8bfd962015-10-02 13:41:04 +000043 Stmts[0] = tryBlock;
44 std::copy(handlers.begin(), handlers.end(), Stmts + 1);
45}
46
Richard Smith8baa5002018-09-28 18:44:09 +000047CXXForRangeStmt::CXXForRangeStmt(Stmt *Init, DeclStmt *Range,
Richard Smith01694c32016-03-20 10:33:40 +000048 DeclStmt *BeginStmt, DeclStmt *EndStmt,
James Y Knightb8bfd962015-10-02 13:41:04 +000049 Expr *Cond, Expr *Inc, DeclStmt *LoopVar,
50 Stmt *Body, SourceLocation FL,
Richard Smith9f690bd2015-10-27 06:02:45 +000051 SourceLocation CAL, SourceLocation CL,
52 SourceLocation RPL)
53 : Stmt(CXXForRangeStmtClass), ForLoc(FL), CoawaitLoc(CAL), ColonLoc(CL),
54 RParenLoc(RPL) {
Richard Smith8baa5002018-09-28 18:44:09 +000055 SubExprs[INIT] = Init;
James Y Knightb8bfd962015-10-02 13:41:04 +000056 SubExprs[RANGE] = Range;
Richard Smith01694c32016-03-20 10:33:40 +000057 SubExprs[BEGINSTMT] = BeginStmt;
58 SubExprs[ENDSTMT] = EndStmt;
James Y Knightb8bfd962015-10-02 13:41:04 +000059 SubExprs[COND] = Cond;
60 SubExprs[INC] = Inc;
61 SubExprs[LOOPVAR] = LoopVar;
62 SubExprs[BODY] = Body;
63}
64
65Expr *CXXForRangeStmt::getRangeInit() {
66 DeclStmt *RangeStmt = getRangeStmt();
67 VarDecl *RangeDecl = dyn_cast_or_null<VarDecl>(RangeStmt->getSingleDecl());
68 assert(RangeDecl && "for-range should have a single var decl");
69 return RangeDecl->getInit();
70}
71
72const Expr *CXXForRangeStmt::getRangeInit() const {
73 return const_cast<CXXForRangeStmt *>(this)->getRangeInit();
74}
75
76VarDecl *CXXForRangeStmt::getLoopVariable() {
77 Decl *LV = cast<DeclStmt>(getLoopVarStmt())->getSingleDecl();
78 assert(LV && "No loop variable in CXXForRangeStmt");
79 return cast<VarDecl>(LV);
80}
81
82const VarDecl *CXXForRangeStmt::getLoopVariable() const {
83 return const_cast<CXXForRangeStmt *>(this)->getLoopVariable();
84}
Gor Nishanovbbe1c072017-02-13 05:05:02 +000085
86CoroutineBodyStmt *CoroutineBodyStmt::Create(
Gor Nishanov6a470682017-05-22 20:22:23 +000087 const ASTContext &C, CoroutineBodyStmt::CtorArgs const &Args) {
Gor Nishanovbbe1c072017-02-13 05:05:02 +000088 std::size_t Size = totalSizeToAlloc<Stmt *>(
89 CoroutineBodyStmt::FirstParamMove + Args.ParamMoves.size());
90
91 void *Mem = C.Allocate(Size, alignof(CoroutineBodyStmt));
92 return new (Mem) CoroutineBodyStmt(Args);
93}
94
Gor Nishanovf5ecb5e2017-07-25 18:01:49 +000095CoroutineBodyStmt *CoroutineBodyStmt::Create(const ASTContext &C, EmptyShell,
96 unsigned NumParams) {
97 std::size_t Size = totalSizeToAlloc<Stmt *>(
98 CoroutineBodyStmt::FirstParamMove + NumParams);
99
100 void *Mem = C.Allocate(Size, alignof(CoroutineBodyStmt));
101 auto *Result = new (Mem) CoroutineBodyStmt(CtorArgs());
102 Result->NumParams = NumParams;
103 auto *ParamBegin = Result->getStoredStmts() + SubStmt::FirstParamMove;
104 std::uninitialized_fill(ParamBegin, ParamBegin + NumParams,
105 static_cast<Stmt *>(nullptr));
106 return Result;
107}
108
Gor Nishanovbbe1c072017-02-13 05:05:02 +0000109CoroutineBodyStmt::CoroutineBodyStmt(CoroutineBodyStmt::CtorArgs const &Args)
110 : Stmt(CoroutineBodyStmtClass), NumParams(Args.ParamMoves.size()) {
111 Stmt **SubStmts = getStoredStmts();
112 SubStmts[CoroutineBodyStmt::Body] = Args.Body;
113 SubStmts[CoroutineBodyStmt::Promise] = Args.Promise;
114 SubStmts[CoroutineBodyStmt::InitSuspend] = Args.InitialSuspend;
115 SubStmts[CoroutineBodyStmt::FinalSuspend] = Args.FinalSuspend;
116 SubStmts[CoroutineBodyStmt::OnException] = Args.OnException;
117 SubStmts[CoroutineBodyStmt::OnFallthrough] = Args.OnFallthrough;
118 SubStmts[CoroutineBodyStmt::Allocate] = Args.Allocate;
119 SubStmts[CoroutineBodyStmt::Deallocate] = Args.Deallocate;
120 SubStmts[CoroutineBodyStmt::ReturnValue] = Args.ReturnValue;
Gor Nishanov6a470682017-05-22 20:22:23 +0000121 SubStmts[CoroutineBodyStmt::ResultDecl] = Args.ResultDecl;
122 SubStmts[CoroutineBodyStmt::ReturnStmt] = Args.ReturnStmt;
Gor Nishanov3aa9eb32017-03-27 23:36:59 +0000123 SubStmts[CoroutineBodyStmt::ReturnStmtOnAllocFailure] =
124 Args.ReturnStmtOnAllocFailure;
Gor Nishanovbbe1c072017-02-13 05:05:02 +0000125 std::copy(Args.ParamMoves.begin(), Args.ParamMoves.end(),
126 const_cast<Stmt **>(getParamMoves().data()));
Eric Fiselierbee782b2017-04-03 19:21:00 +0000127}