clang 10.0.0git

StmtCXX.cpp
Go to the documentation of this file.
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 QualType CXXCatchStmt::getCaughtType() const {
20 if (ExceptionDecl)
21 return ExceptionDecl->getType();
22 return QualType();
23 }
24
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);
30 }
31
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);
37 }
38
39 CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
40 ArrayRef<Stmt *> handlers)
41 : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(handlers.size()) {
42 Stmt **Stmts = getStmts();
43 Stmts[0] = tryBlock;
44 std::copy(handlers.begin(), handlers.end(), Stmts + 1);
45 }
46
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,
52 SourceLocation RPL)
53 : Stmt(CXXForRangeStmtClass), ForLoc(FL), CoawaitLoc(CAL), ColonLoc(CL),
54 RParenLoc(RPL) {
55 SubExprs[INIT] = Init;
56 SubExprs[RANGE] = Range;
57 SubExprs[BEGINSTMT] = BeginStmt;
58 SubExprs[ENDSTMT] = EndStmt;
59 SubExprs[COND] = Cond;
60 SubExprs[INC] = Inc;
61 SubExprs[LOOPVAR] = LoopVar;
62 SubExprs[BODY] = Body;
63 }
64
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();
70 }
71
72 const Expr *CXXForRangeStmt::getRangeInit() const {
73 return const_cast<CXXForRangeStmt *>(this)->getRangeInit();
74 }
75
76 VarDecl *CXXForRangeStmt::getLoopVariable() {
77 Decl *LV = cast<DeclStmt>(getLoopVarStmt())->getSingleDecl();
78 assert(LV && "No loop variable in CXXForRangeStmt");
79 return cast<VarDecl>(LV);
80 }
81
82 const VarDecl *CXXForRangeStmt::getLoopVariable() const {
83 return const_cast<CXXForRangeStmt *>(this)->getLoopVariable();
84 }
85
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());
90
91 void *Mem = C.Allocate(Size, alignof(CoroutineBodyStmt));
92 return new (Mem) CoroutineBodyStmt(Args);
93 }
94
95 CoroutineBodyStmt *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
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()));
127 }
ASTContext.h
Defines the clang::ASTContext interface.
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:654
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:66
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:88
clang::CoroutineBodyStmt::CtorArgs::Allocate
Expr * Allocate
Definition: StmtCXX.h:354
clang::VarDecl
Represents a variable declaration or definition.
Definition: Decl.h:820
clang::CoroutineBodyStmt::CtorArgs::ReturnStmt
Stmt * ReturnStmt
Definition: StmtCXX.h:358
clang::CoroutineBodyStmt::CtorArgs::Body
Stmt * Body
Definition: StmtCXX.h:348
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:168
clang::CXXCatchStmt::getCaughtType
QualType getCaughtType() const
Definition: StmtCXX.cpp:19
size_t
__SIZE_TYPE__ size_t
The unsigned integer type of the result of the sizeof operator.
Definition: opencl-c-base.h:40
StmtCXX.h
clang::CoroutineBodyStmt::CtorArgs::ReturnStmtOnAllocFailure
Stmt * ReturnStmtOnAllocFailure
Definition: StmtCXX.h:359
clang::CXXForRangeStmt
CXXForRangeStmt - This represents C++0x [stmt.ranged]&#39;s ranged for statement, represented as &#39;for (ra...
Definition: StmtCXX.h:134
llvm::ArrayRef
Definition: LLVM.h:31
clang::CoroutineBodyStmt::CtorArgs::InitialSuspend
Expr * InitialSuspend
Definition: StmtCXX.h:350
clang::Expr
This represents one expression.
Definition: Expr.h:108
clang::CXXTryStmt
CXXTryStmt - A C++ try block, including all handlers.
Definition: StmtCXX.h:68
clang::Stmt::Stmt
Stmt()=delete
clang::ObjCSubstitutionContext::Result
The result type of a method or function.
clang::CoroutineBodyStmt::Create
static CoroutineBodyStmt * Create(const ASTContext &C, CtorArgs const &Args)
Definition: StmtCXX.cpp:86
clang::CoroutineBodyStmt::CtorArgs::Promise
Stmt * Promise
Definition: StmtCXX.h:349
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:86
clang::DeclStmt
DeclStmt - Adaptor class for mixing declarations with statements and expressions. ...
Definition: Stmt.h:1225
clang::CoroutineBodyStmt::CtorArgs
Definition: StmtCXX.h:347
clang::CoroutineBodyStmt::CtorArgs::ParamMoves
ArrayRef< Stmt * > ParamMoves
Definition: StmtCXX.h:360
clang::CoroutineBodyStmt::CtorArgs::ReturnValue
Expr * ReturnValue
Definition: StmtCXX.h:356
clang::CoroutineBodyStmt::CtorArgs::FinalSuspend
Expr * FinalSuspend
Definition: StmtCXX.h:351
clang::CXXForRangeStmt::getRangeInit
Expr * getRangeInit()
Definition: StmtCXX.cpp:65
clang::Stmt::EmptyShell
A placeholder type used to construct an empty shell of a type, that will be filled in later (e...
Definition: Stmt.h:1056
clang::ASTContext::Allocate
void * Allocate(size_t Size, unsigned Align=8) const
Definition: ASTContext.h:686
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:21
clang::VarDecl::getInit
const Expr * getInit() const
Definition: Decl.h:1229
clang::DeclStmt::getSingleDecl
const Decl * getSingleDecl() const
Definition: Stmt.h:1240
clang::CXXForRangeStmt::CXXForRangeStmt
CXXForRangeStmt(Stmt *InitStmt, DeclStmt *Range, DeclStmt *Begin, DeclStmt *End, Expr *Cond, Expr *Inc, DeclStmt *LoopVar, Stmt *Body, SourceLocation FL, SourceLocation CAL, SourceLocation CL, SourceLocation RPL)
Definition: StmtCXX.cpp:47
clang::CoroutineBodyStmt
Represents the body of a coroutine.
Definition: StmtCXX.h:317
clang::CXXForRangeStmt::getRangeStmt
DeclStmt * getRangeStmt()
Definition: StmtCXX.h:161
clang::CXXTryStmt::Create
static CXXTryStmt * Create(const ASTContext &C, SourceLocation tryLoc, Stmt *tryBlock, ArrayRef< Stmt *> handlers)
Definition: StmtCXX.cpp:25
clang::CXXForRangeStmt::getLoopVariable
VarDecl * getLoopVariable()
Definition: StmtCXX.cpp:76
clang::AttrSyntax::C
clang::CoroutineBodyStmt::CtorArgs::OnFallthrough
Stmt * OnFallthrough
Definition: StmtCXX.h:353
clang::CoroutineBodyStmt::CtorArgs::ResultDecl
Stmt * ResultDecl
Definition: StmtCXX.h:357
clang::CXXForRangeStmt::getLoopVarStmt
DeclStmt * getLoopVarStmt()
Definition: StmtCXX.h:168
clang::ValueDecl::getType
QualType getType() const
Definition: Decl.h:630
clang::CoroutineBodyStmt::CtorArgs::OnException
Stmt * OnException
Definition: StmtCXX.h:352
ColonLoc
SourceLocation ColonLoc
Location of &#39;:&#39;.
Definition: OpenMPClause.h:107
clang::CoroutineBodyStmt::CtorArgs::Deallocate
Expr * Deallocate
Definition: StmtCXX.h:355