1 //===--- StmtCXX.h - Classes for representing C++ statements ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the C++ statement AST node classes.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_STMTCXX_H
15 #define LLVM_CLANG_AST_STMTCXX_H
17 #include "clang/AST/DeclarationName.h"
18 #include "clang/AST/Expr.h"
19 #include "clang/AST/NestedNameSpecifier.h"
20 #include "clang/AST/Stmt.h"
21 #include "llvm/Support/Compiler.h"
27 /// CXXCatchStmt - This represents a C++ catch block.
29 class CXXCatchStmt : public Stmt {
30 SourceLocation CatchLoc;
31 /// The exception-declaration of the type.
32 VarDecl *ExceptionDecl;
33 /// The handler block.
37 CXXCatchStmt(SourceLocation catchLoc, VarDecl *exDecl, Stmt *handlerBlock)
38 : Stmt(CXXCatchStmtClass), CatchLoc(catchLoc), ExceptionDecl(exDecl),
39 HandlerBlock(handlerBlock) {}
41 CXXCatchStmt(EmptyShell Empty)
42 : Stmt(CXXCatchStmtClass), ExceptionDecl(nullptr), HandlerBlock(nullptr) {}
44 SourceLocation getLocStart() const LLVM_READONLY { return CatchLoc; }
45 SourceLocation getLocEnd() const LLVM_READONLY {
46 return HandlerBlock->getLocEnd();
49 SourceLocation getCatchLoc() const { return CatchLoc; }
50 VarDecl *getExceptionDecl() const { return ExceptionDecl; }
51 QualType getCaughtType() const;
52 Stmt *getHandlerBlock() const { return HandlerBlock; }
54 static bool classof(const Stmt *T) {
55 return T->getStmtClass() == CXXCatchStmtClass;
58 child_range children() { return child_range(&HandlerBlock, &HandlerBlock+1); }
60 friend class ASTStmtReader;
63 /// CXXTryStmt - A C++ try block, including all handlers.
65 class CXXTryStmt final : public Stmt,
66 private llvm::TrailingObjects<CXXTryStmt, Stmt *> {
68 friend TrailingObjects;
69 friend class ASTStmtReader;
71 SourceLocation TryLoc;
73 size_t numTrailingObjects(OverloadToken<Stmt *>) const { return NumHandlers; }
75 CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock, ArrayRef<Stmt*> handlers);
76 CXXTryStmt(EmptyShell Empty, unsigned numHandlers)
77 : Stmt(CXXTryStmtClass), NumHandlers(numHandlers) { }
79 Stmt *const *getStmts() const { return getTrailingObjects<Stmt *>(); }
80 Stmt **getStmts() { return getTrailingObjects<Stmt *>(); }
83 static CXXTryStmt *Create(const ASTContext &C, SourceLocation tryLoc,
84 Stmt *tryBlock, ArrayRef<Stmt*> handlers);
86 static CXXTryStmt *Create(const ASTContext &C, EmptyShell Empty,
87 unsigned numHandlers);
89 SourceLocation getLocStart() const LLVM_READONLY { return getTryLoc(); }
90 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
92 SourceLocation getTryLoc() const { return TryLoc; }
93 SourceLocation getEndLoc() const {
94 return getStmts()[NumHandlers]->getLocEnd();
97 CompoundStmt *getTryBlock() {
98 return cast<CompoundStmt>(getStmts()[0]);
100 const CompoundStmt *getTryBlock() const {
101 return cast<CompoundStmt>(getStmts()[0]);
104 unsigned getNumHandlers() const { return NumHandlers; }
105 CXXCatchStmt *getHandler(unsigned i) {
106 return cast<CXXCatchStmt>(getStmts()[i + 1]);
108 const CXXCatchStmt *getHandler(unsigned i) const {
109 return cast<CXXCatchStmt>(getStmts()[i + 1]);
112 static bool classof(const Stmt *T) {
113 return T->getStmtClass() == CXXTryStmtClass;
116 child_range children() {
117 return child_range(getStmts(), getStmts() + getNumHandlers() + 1);
121 /// CXXForRangeStmt - This represents C++0x [stmt.ranged]'s ranged for
122 /// statement, represented as 'for (range-declarator : range-expression)'.
124 /// This is stored in a partially-desugared form to allow full semantic
125 /// analysis of the constituent components. The original syntactic components
126 /// can be extracted using getLoopVariable and getRangeInit.
127 class CXXForRangeStmt : public Stmt {
128 SourceLocation ForLoc;
129 enum { RANGE, BEGINSTMT, ENDSTMT, COND, INC, LOOPVAR, BODY, END };
130 // SubExprs[RANGE] is an expression or declstmt.
131 // SubExprs[COND] and SubExprs[INC] are expressions.
133 SourceLocation CoawaitLoc;
134 SourceLocation ColonLoc;
135 SourceLocation RParenLoc;
137 friend class ASTStmtReader;
139 CXXForRangeStmt(DeclStmt *Range, DeclStmt *Begin, DeclStmt *End,
140 Expr *Cond, Expr *Inc, DeclStmt *LoopVar, Stmt *Body,
141 SourceLocation FL, SourceLocation CAL, SourceLocation CL,
143 CXXForRangeStmt(EmptyShell Empty) : Stmt(CXXForRangeStmtClass, Empty) { }
146 VarDecl *getLoopVariable();
147 Expr *getRangeInit();
149 const VarDecl *getLoopVariable() const;
150 const Expr *getRangeInit() const;
153 DeclStmt *getRangeStmt() { return cast<DeclStmt>(SubExprs[RANGE]); }
154 DeclStmt *getBeginStmt() {
155 return cast_or_null<DeclStmt>(SubExprs[BEGINSTMT]);
157 DeclStmt *getEndStmt() { return cast_or_null<DeclStmt>(SubExprs[ENDSTMT]); }
158 Expr *getCond() { return cast_or_null<Expr>(SubExprs[COND]); }
159 Expr *getInc() { return cast_or_null<Expr>(SubExprs[INC]); }
160 DeclStmt *getLoopVarStmt() { return cast<DeclStmt>(SubExprs[LOOPVAR]); }
161 Stmt *getBody() { return SubExprs[BODY]; }
163 const DeclStmt *getRangeStmt() const {
164 return cast<DeclStmt>(SubExprs[RANGE]);
166 const DeclStmt *getBeginStmt() const {
167 return cast_or_null<DeclStmt>(SubExprs[BEGINSTMT]);
169 const DeclStmt *getEndStmt() const {
170 return cast_or_null<DeclStmt>(SubExprs[ENDSTMT]);
172 const Expr *getCond() const {
173 return cast_or_null<Expr>(SubExprs[COND]);
175 const Expr *getInc() const {
176 return cast_or_null<Expr>(SubExprs[INC]);
178 const DeclStmt *getLoopVarStmt() const {
179 return cast<DeclStmt>(SubExprs[LOOPVAR]);
181 const Stmt *getBody() const { return SubExprs[BODY]; }
183 void setRangeInit(Expr *E) { SubExprs[RANGE] = reinterpret_cast<Stmt*>(E); }
184 void setRangeStmt(Stmt *S) { SubExprs[RANGE] = S; }
185 void setBeginStmt(Stmt *S) { SubExprs[BEGINSTMT] = S; }
186 void setEndStmt(Stmt *S) { SubExprs[ENDSTMT] = S; }
187 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
188 void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
189 void setLoopVarStmt(Stmt *S) { SubExprs[LOOPVAR] = S; }
190 void setBody(Stmt *S) { SubExprs[BODY] = S; }
192 SourceLocation getForLoc() const { return ForLoc; }
193 SourceLocation getCoawaitLoc() const { return CoawaitLoc; }
194 SourceLocation getColonLoc() const { return ColonLoc; }
195 SourceLocation getRParenLoc() const { return RParenLoc; }
197 SourceLocation getLocStart() const LLVM_READONLY { return ForLoc; }
198 SourceLocation getLocEnd() const LLVM_READONLY {
199 return SubExprs[BODY]->getLocEnd();
202 static bool classof(const Stmt *T) {
203 return T->getStmtClass() == CXXForRangeStmtClass;
207 child_range children() {
208 return child_range(&SubExprs[0], &SubExprs[END]);
212 /// Representation of a Microsoft __if_exists or __if_not_exists
213 /// statement with a dependent name.
215 /// The __if_exists statement can be used to include a sequence of statements
216 /// in the program only when a particular dependent name does not exist. For
220 /// template<typename T>
221 /// void call_foo(T &t) {
222 /// __if_exists (T::foo) {
223 /// t.foo(); // okay: only called when T::foo exists.
228 /// Similarly, the __if_not_exists statement can be used to include the
229 /// statements when a particular name does not exist.
231 /// Note that this statement only captures __if_exists and __if_not_exists
232 /// statements whose name is dependent. All non-dependent cases are handled
233 /// directly in the parser, so that they don't introduce a new scope. Clang
234 /// introduces scopes in the dependent case to keep names inside the compound
235 /// statement from leaking out into the surround statements, which would
236 /// compromise the template instantiation model. This behavior differs from
237 /// Visual C++ (which never introduces a scope), but is a fairly reasonable
238 /// approximation of the VC++ behavior.
239 class MSDependentExistsStmt : public Stmt {
240 SourceLocation KeywordLoc;
242 NestedNameSpecifierLoc QualifierLoc;
243 DeclarationNameInfo NameInfo;
246 friend class ASTReader;
247 friend class ASTStmtReader;
250 MSDependentExistsStmt(SourceLocation KeywordLoc, bool IsIfExists,
251 NestedNameSpecifierLoc QualifierLoc,
252 DeclarationNameInfo NameInfo,
253 CompoundStmt *SubStmt)
254 : Stmt(MSDependentExistsStmtClass),
255 KeywordLoc(KeywordLoc), IsIfExists(IsIfExists),
256 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
257 SubStmt(reinterpret_cast<Stmt *>(SubStmt)) { }
259 /// Retrieve the location of the __if_exists or __if_not_exists
261 SourceLocation getKeywordLoc() const { return KeywordLoc; }
263 /// Determine whether this is an __if_exists statement.
264 bool isIfExists() const { return IsIfExists; }
266 /// Determine whether this is an __if_exists statement.
267 bool isIfNotExists() const { return !IsIfExists; }
269 /// Retrieve the nested-name-specifier that qualifies this name, if
271 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
273 /// Retrieve the name of the entity we're testing for, along with
274 /// location information
275 DeclarationNameInfo getNameInfo() const { return NameInfo; }
277 /// Retrieve the compound statement that will be included in the
278 /// program only if the existence of the symbol matches the initial keyword.
279 CompoundStmt *getSubStmt() const {
280 return reinterpret_cast<CompoundStmt *>(SubStmt);
283 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
284 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
286 child_range children() {
287 return child_range(&SubStmt, &SubStmt+1);
290 static bool classof(const Stmt *T) {
291 return T->getStmtClass() == MSDependentExistsStmtClass;
295 /// Represents the body of a coroutine. This wraps the normal function
296 /// body and holds the additional semantic context required to set up and tear
297 /// down the coroutine frame.
298 class CoroutineBodyStmt final
300 private llvm::TrailingObjects<CoroutineBodyStmt, Stmt *> {
302 Body, ///< The body of the coroutine.
303 Promise, ///< The promise statement.
304 InitSuspend, ///< The initial suspend statement, run before the body.
305 FinalSuspend, ///< The final suspend statement, run after the body.
306 OnException, ///< Handler for exceptions thrown in the body.
307 OnFallthrough, ///< Handler for control flow falling off the body.
308 Allocate, ///< Coroutine frame memory allocation.
309 Deallocate, ///< Coroutine frame memory deallocation.
310 ReturnValue, ///< Return value for thunk function: p.get_return_object().
311 ResultDecl, ///< Declaration holding the result of get_return_object.
312 ReturnStmt, ///< Return statement for the thunk function.
313 ReturnStmtOnAllocFailure, ///< Return statement if allocation failed.
314 FirstParamMove ///< First offset for move construction of parameter copies.
318 friend class ASTStmtReader;
319 friend class ASTReader;
320 friend TrailingObjects;
322 Stmt **getStoredStmts() { return getTrailingObjects<Stmt *>(); }
324 Stmt *const *getStoredStmts() const { return getTrailingObjects<Stmt *>(); }
329 Stmt *Body = nullptr;
330 Stmt *Promise = nullptr;
331 Expr *InitialSuspend = nullptr;
332 Expr *FinalSuspend = nullptr;
333 Stmt *OnException = nullptr;
334 Stmt *OnFallthrough = nullptr;
335 Expr *Allocate = nullptr;
336 Expr *Deallocate = nullptr;
337 Expr *ReturnValue = nullptr;
338 Stmt *ResultDecl = nullptr;
339 Stmt *ReturnStmt = nullptr;
340 Stmt *ReturnStmtOnAllocFailure = nullptr;
341 ArrayRef<Stmt *> ParamMoves;
346 CoroutineBodyStmt(CtorArgs const& Args);
349 static CoroutineBodyStmt *Create(const ASTContext &C, CtorArgs const &Args);
350 static CoroutineBodyStmt *Create(const ASTContext &C, EmptyShell,
353 bool hasDependentPromiseType() const {
354 return getPromiseDecl()->getType()->isDependentType();
357 /// Retrieve the body of the coroutine as written. This will be either
358 /// a CompoundStmt or a TryStmt.
359 Stmt *getBody() const {
360 return getStoredStmts()[SubStmt::Body];
363 Stmt *getPromiseDeclStmt() const {
364 return getStoredStmts()[SubStmt::Promise];
366 VarDecl *getPromiseDecl() const {
367 return cast<VarDecl>(cast<DeclStmt>(getPromiseDeclStmt())->getSingleDecl());
370 Stmt *getInitSuspendStmt() const {
371 return getStoredStmts()[SubStmt::InitSuspend];
373 Stmt *getFinalSuspendStmt() const {
374 return getStoredStmts()[SubStmt::FinalSuspend];
377 Stmt *getExceptionHandler() const {
378 return getStoredStmts()[SubStmt::OnException];
380 Stmt *getFallthroughHandler() const {
381 return getStoredStmts()[SubStmt::OnFallthrough];
384 Expr *getAllocate() const {
385 return cast_or_null<Expr>(getStoredStmts()[SubStmt::Allocate]);
387 Expr *getDeallocate() const {
388 return cast_or_null<Expr>(getStoredStmts()[SubStmt::Deallocate]);
390 Expr *getReturnValueInit() const {
391 return cast<Expr>(getStoredStmts()[SubStmt::ReturnValue]);
393 Stmt *getResultDecl() const { return getStoredStmts()[SubStmt::ResultDecl]; }
394 Stmt *getReturnStmt() const { return getStoredStmts()[SubStmt::ReturnStmt]; }
395 Stmt *getReturnStmtOnAllocFailure() const {
396 return getStoredStmts()[SubStmt::ReturnStmtOnAllocFailure];
398 ArrayRef<Stmt const *> getParamMoves() const {
399 return {getStoredStmts() + SubStmt::FirstParamMove, NumParams};
402 SourceLocation getLocStart() const LLVM_READONLY {
403 return getBody() ? getBody()->getLocStart()
404 : getPromiseDecl()->getLocStart();
406 SourceLocation getLocEnd() const LLVM_READONLY {
407 return getBody() ? getBody()->getLocEnd() : getPromiseDecl()->getLocEnd();
410 child_range children() {
411 return child_range(getStoredStmts(),
412 getStoredStmts() + SubStmt::FirstParamMove + NumParams);
415 static bool classof(const Stmt *T) {
416 return T->getStmtClass() == CoroutineBodyStmtClass;
420 /// Represents a 'co_return' statement in the C++ Coroutines TS.
422 /// This statament models the initialization of the coroutine promise
423 /// (encapsulating the eventual notional return value) from an expression
424 /// (or braced-init-list), followed by termination of the coroutine.
426 /// This initialization is modeled by the evaluation of the operand
427 /// followed by a call to one of:
428 /// <promise>.return_value(<operand>)
429 /// <promise>.return_void()
430 /// which we name the "promise call".
431 class CoreturnStmt : public Stmt {
432 SourceLocation CoreturnLoc;
434 enum SubStmt { Operand, PromiseCall, Count };
435 Stmt *SubStmts[SubStmt::Count];
439 friend class ASTStmtReader;
441 CoreturnStmt(SourceLocation CoreturnLoc, Stmt *Operand, Stmt *PromiseCall,
442 bool IsImplicit = false)
443 : Stmt(CoreturnStmtClass), CoreturnLoc(CoreturnLoc),
444 IsImplicit(IsImplicit) {
445 SubStmts[SubStmt::Operand] = Operand;
446 SubStmts[SubStmt::PromiseCall] = PromiseCall;
449 CoreturnStmt(EmptyShell) : CoreturnStmt({}, {}, {}) {}
451 SourceLocation getKeywordLoc() const { return CoreturnLoc; }
453 /// Retrieve the operand of the 'co_return' statement. Will be nullptr
454 /// if none was specified.
455 Expr *getOperand() const { return static_cast<Expr*>(SubStmts[Operand]); }
457 /// Retrieve the promise call that results from this 'co_return'
458 /// statement. Will be nullptr if either the coroutine has not yet been
459 /// finalized or the coroutine has no eventual return type.
460 Expr *getPromiseCall() const {
461 return static_cast<Expr*>(SubStmts[PromiseCall]);
464 bool isImplicit() const { return IsImplicit; }
465 void setIsImplicit(bool value = true) { IsImplicit = value; }
467 SourceLocation getLocStart() const LLVM_READONLY { return CoreturnLoc; }
468 SourceLocation getLocEnd() const LLVM_READONLY {
469 return getOperand() ? getOperand()->getLocEnd() : getLocStart();
472 child_range children() {
474 return child_range(SubStmts + SubStmt::PromiseCall,
475 SubStmts + SubStmt::Count);
476 return child_range(SubStmts, SubStmts + SubStmt::Count);
479 static bool classof(const Stmt *T) {
480 return T->getStmtClass() == CoreturnStmtClass;
484 } // end namespace clang