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