1 //===--- StmtCXX.cpp - Classes for representing C++ statements ------------===//
3 // 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
7 //===----------------------------------------------------------------------===//
9 // This file implements the subclesses of Stmt class declared in StmtCXX.h
11 //===----------------------------------------------------------------------===//
13 #include "clang/AST/StmtCXX.h"
15 #include "clang/AST/ASTContext.h"
17 using namespace clang;
19 QualType CXXCatchStmt::getCaughtType() const {
21 return ExceptionDecl->getType();
25 CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, SourceLocation tryLoc,
26 Stmt *tryBlock, ArrayRef<Stmt *> handlers) {
27 const size_t Size = totalSizeToAlloc<Stmt *>(handlers.size() + 1);
28 void *Mem = C.Allocate(Size, alignof(CXXTryStmt));
29 return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers);
32 CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, EmptyShell Empty,
33 unsigned numHandlers) {
34 const size_t Size = totalSizeToAlloc<Stmt *>(numHandlers + 1);
35 void *Mem = C.Allocate(Size, alignof(CXXTryStmt));
36 return new (Mem) CXXTryStmt(Empty, numHandlers);
39 CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
40 ArrayRef<Stmt *> handlers)
41 : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(handlers.size()) {
42 Stmt **Stmts = getStmts();
44 std::copy(handlers.begin(), handlers.end(), Stmts + 1);
47 CXXForRangeStmt::CXXForRangeStmt(Stmt *Init, DeclStmt *Range,
48 DeclStmt *BeginStmt, DeclStmt *EndStmt,
49 Expr *Cond, Expr *Inc, DeclStmt *LoopVar,
50 Stmt *Body, SourceLocation FL,
51 SourceLocation CAL, SourceLocation CL,
53 : Stmt(CXXForRangeStmtClass), ForLoc(FL), CoawaitLoc(CAL), ColonLoc(CL),
55 SubExprs[INIT] = Init;
56 SubExprs[RANGE] = Range;
57 SubExprs[BEGINSTMT] = BeginStmt;
58 SubExprs[ENDSTMT] = EndStmt;
59 SubExprs[COND] = Cond;
61 SubExprs[LOOPVAR] = LoopVar;
62 SubExprs[BODY] = Body;
65 Expr *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();
72 const Expr *CXXForRangeStmt::getRangeInit() const {
73 return const_cast<CXXForRangeStmt *>(this)->getRangeInit();
76 VarDecl *CXXForRangeStmt::getLoopVariable() {
77 Decl *LV = cast<DeclStmt>(getLoopVarStmt())->getSingleDecl();
78 assert(LV && "No loop variable in CXXForRangeStmt");
79 return cast<VarDecl>(LV);
82 const VarDecl *CXXForRangeStmt::getLoopVariable() const {
83 return const_cast<CXXForRangeStmt *>(this)->getLoopVariable();
86 CoroutineBodyStmt *CoroutineBodyStmt::Create(
87 const ASTContext &C, CoroutineBodyStmt::CtorArgs const &Args) {
88 std::size_t Size = totalSizeToAlloc<Stmt *>(
89 CoroutineBodyStmt::FirstParamMove + Args.ParamMoves.size());
91 void *Mem = C.Allocate(Size, alignof(CoroutineBodyStmt));
92 return new (Mem) CoroutineBodyStmt(Args);
95 CoroutineBodyStmt *CoroutineBodyStmt::Create(const ASTContext &C, EmptyShell,
97 std::size_t Size = totalSizeToAlloc<Stmt *>(
98 CoroutineBodyStmt::FirstParamMove + NumParams);
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));
109 CoroutineBodyStmt::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;
121 SubStmts[CoroutineBodyStmt::ResultDecl] = Args.ResultDecl;
122 SubStmts[CoroutineBodyStmt::ReturnStmt] = Args.ReturnStmt;
123 SubStmts[CoroutineBodyStmt::ReturnStmtOnAllocFailure] =
124 Args.ReturnStmtOnAllocFailure;
125 std::copy(Args.ParamMoves.begin(), Args.ParamMoves.end(),
126 const_cast<Stmt **>(getParamMoves().data()));