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 getBeginLoc(); }
45 SourceLocation getBeginLoc() const LLVM_READONLY { return CatchLoc; }
46 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
47 SourceLocation getEndLoc() const LLVM_READONLY {
48 return HandlerBlock->getLocEnd();
51 SourceLocation getCatchLoc() const { return CatchLoc; }
52 VarDecl *getExceptionDecl() const { return ExceptionDecl; }
53 QualType getCaughtType() const;
54 Stmt *getHandlerBlock() const { return HandlerBlock; }
56 static bool classof(const Stmt *T) {
57 return T->getStmtClass() == CXXCatchStmtClass;
60 child_range children() { return child_range(&HandlerBlock, &HandlerBlock+1); }
62 friend class ASTStmtReader;
65 /// CXXTryStmt - A C++ try block, including all handlers.
67 class CXXTryStmt final : public Stmt,
68 private llvm::TrailingObjects<CXXTryStmt, Stmt *> {
70 friend TrailingObjects;
71 friend class ASTStmtReader;
73 SourceLocation TryLoc;
75 size_t numTrailingObjects(OverloadToken<Stmt *>) const { return NumHandlers; }
77 CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock, ArrayRef<Stmt*> handlers);
78 CXXTryStmt(EmptyShell Empty, unsigned numHandlers)
79 : Stmt(CXXTryStmtClass), NumHandlers(numHandlers) { }
81 Stmt *const *getStmts() const { return getTrailingObjects<Stmt *>(); }
82 Stmt **getStmts() { return getTrailingObjects<Stmt *>(); }
85 static CXXTryStmt *Create(const ASTContext &C, SourceLocation tryLoc,
86 Stmt *tryBlock, ArrayRef<Stmt*> handlers);
88 static CXXTryStmt *Create(const ASTContext &C, EmptyShell Empty,
89 unsigned numHandlers);
91 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
92 SourceLocation getBeginLoc() const LLVM_READONLY { return getTryLoc(); }
93 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
95 SourceLocation getTryLoc() const { return TryLoc; }
96 SourceLocation getEndLoc() const {
97 return getStmts()[NumHandlers]->getLocEnd();
100 CompoundStmt *getTryBlock() {
101 return cast<CompoundStmt>(getStmts()[0]);
103 const CompoundStmt *getTryBlock() const {
104 return cast<CompoundStmt>(getStmts()[0]);
107 unsigned getNumHandlers() const { return NumHandlers; }
108 CXXCatchStmt *getHandler(unsigned i) {
109 return cast<CXXCatchStmt>(getStmts()[i + 1]);
111 const CXXCatchStmt *getHandler(unsigned i) const {
112 return cast<CXXCatchStmt>(getStmts()[i + 1]);
115 static bool classof(const Stmt *T) {
116 return T->getStmtClass() == CXXTryStmtClass;
119 child_range children() {
120 return child_range(getStmts(), getStmts() + getNumHandlers() + 1);
124 /// CXXForRangeStmt - This represents C++0x [stmt.ranged]'s ranged for
125 /// statement, represented as 'for (range-declarator : range-expression)'.
127 /// This is stored in a partially-desugared form to allow full semantic
128 /// analysis of the constituent components. The original syntactic components
129 /// can be extracted using getLoopVariable and getRangeInit.
130 class CXXForRangeStmt : public Stmt {
131 SourceLocation ForLoc;
132 enum { RANGE, BEGINSTMT, ENDSTMT, COND, INC, LOOPVAR, BODY, END };
133 // SubExprs[RANGE] is an expression or declstmt.
134 // SubExprs[COND] and SubExprs[INC] are expressions.
136 SourceLocation CoawaitLoc;
137 SourceLocation ColonLoc;
138 SourceLocation RParenLoc;
140 friend class ASTStmtReader;
142 CXXForRangeStmt(DeclStmt *Range, DeclStmt *Begin, DeclStmt *End,
143 Expr *Cond, Expr *Inc, DeclStmt *LoopVar, Stmt *Body,
144 SourceLocation FL, SourceLocation CAL, SourceLocation CL,
146 CXXForRangeStmt(EmptyShell Empty) : Stmt(CXXForRangeStmtClass, Empty) { }
149 VarDecl *getLoopVariable();
150 Expr *getRangeInit();
152 const VarDecl *getLoopVariable() const;
153 const Expr *getRangeInit() const;
156 DeclStmt *getRangeStmt() { return cast<DeclStmt>(SubExprs[RANGE]); }
157 DeclStmt *getBeginStmt() {
158 return cast_or_null<DeclStmt>(SubExprs[BEGINSTMT]);
160 DeclStmt *getEndStmt() { return cast_or_null<DeclStmt>(SubExprs[ENDSTMT]); }
161 Expr *getCond() { return cast_or_null<Expr>(SubExprs[COND]); }
162 Expr *getInc() { return cast_or_null<Expr>(SubExprs[INC]); }
163 DeclStmt *getLoopVarStmt() { return cast<DeclStmt>(SubExprs[LOOPVAR]); }
164 Stmt *getBody() { return SubExprs[BODY]; }
166 const DeclStmt *getRangeStmt() const {
167 return cast<DeclStmt>(SubExprs[RANGE]);
169 const DeclStmt *getBeginStmt() const {
170 return cast_or_null<DeclStmt>(SubExprs[BEGINSTMT]);
172 const DeclStmt *getEndStmt() const {
173 return cast_or_null<DeclStmt>(SubExprs[ENDSTMT]);
175 const Expr *getCond() const {
176 return cast_or_null<Expr>(SubExprs[COND]);
178 const Expr *getInc() const {
179 return cast_or_null<Expr>(SubExprs[INC]);
181 const DeclStmt *getLoopVarStmt() const {
182 return cast<DeclStmt>(SubExprs[LOOPVAR]);
184 const Stmt *getBody() const { return SubExprs[BODY]; }
186 void setRangeInit(Expr *E) { SubExprs[RANGE] = reinterpret_cast<Stmt*>(E); }
187 void setRangeStmt(Stmt *S) { SubExprs[RANGE] = S; }
188 void setBeginStmt(Stmt *S) { SubExprs[BEGINSTMT] = S; }
189 void setEndStmt(Stmt *S) { SubExprs[ENDSTMT] = S; }
190 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
191 void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
192 void setLoopVarStmt(Stmt *S) { SubExprs[LOOPVAR] = S; }
193 void setBody(Stmt *S) { SubExprs[BODY] = S; }
195 SourceLocation getForLoc() const { return ForLoc; }
196 SourceLocation getCoawaitLoc() const { return CoawaitLoc; }
197 SourceLocation getColonLoc() const { return ColonLoc; }
198 SourceLocation getRParenLoc() const { return RParenLoc; }
200 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
201 SourceLocation getBeginLoc() const LLVM_READONLY { return ForLoc; }
202 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
203 SourceLocation getEndLoc() const LLVM_READONLY {
204 return SubExprs[BODY]->getLocEnd();
207 static bool classof(const Stmt *T) {
208 return T->getStmtClass() == CXXForRangeStmtClass;
212 child_range children() {
213 return child_range(&SubExprs[0], &SubExprs[END]);
217 /// Representation of a Microsoft __if_exists or __if_not_exists
218 /// statement with a dependent name.
220 /// The __if_exists statement can be used to include a sequence of statements
221 /// in the program only when a particular dependent name does not exist. For
225 /// template<typename T>
226 /// void call_foo(T &t) {
227 /// __if_exists (T::foo) {
228 /// t.foo(); // okay: only called when T::foo exists.
233 /// Similarly, the __if_not_exists statement can be used to include the
234 /// statements when a particular name does not exist.
236 /// Note that this statement only captures __if_exists and __if_not_exists
237 /// statements whose name is dependent. All non-dependent cases are handled
238 /// directly in the parser, so that they don't introduce a new scope. Clang
239 /// introduces scopes in the dependent case to keep names inside the compound
240 /// statement from leaking out into the surround statements, which would
241 /// compromise the template instantiation model. This behavior differs from
242 /// Visual C++ (which never introduces a scope), but is a fairly reasonable
243 /// approximation of the VC++ behavior.
244 class MSDependentExistsStmt : public Stmt {
245 SourceLocation KeywordLoc;
247 NestedNameSpecifierLoc QualifierLoc;
248 DeclarationNameInfo NameInfo;
251 friend class ASTReader;
252 friend class ASTStmtReader;
255 MSDependentExistsStmt(SourceLocation KeywordLoc, bool IsIfExists,
256 NestedNameSpecifierLoc QualifierLoc,
257 DeclarationNameInfo NameInfo,
258 CompoundStmt *SubStmt)
259 : Stmt(MSDependentExistsStmtClass),
260 KeywordLoc(KeywordLoc), IsIfExists(IsIfExists),
261 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
262 SubStmt(reinterpret_cast<Stmt *>(SubStmt)) { }
264 /// Retrieve the location of the __if_exists or __if_not_exists
266 SourceLocation getKeywordLoc() const { return KeywordLoc; }
268 /// Determine whether this is an __if_exists statement.
269 bool isIfExists() const { return IsIfExists; }
271 /// Determine whether this is an __if_exists statement.
272 bool isIfNotExists() const { return !IsIfExists; }
274 /// Retrieve the nested-name-specifier that qualifies this name, if
276 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
278 /// Retrieve the name of the entity we're testing for, along with
279 /// location information
280 DeclarationNameInfo getNameInfo() const { return NameInfo; }
282 /// Retrieve the compound statement that will be included in the
283 /// program only if the existence of the symbol matches the initial keyword.
284 CompoundStmt *getSubStmt() const {
285 return reinterpret_cast<CompoundStmt *>(SubStmt);
288 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
289 SourceLocation getBeginLoc() const LLVM_READONLY { return KeywordLoc; }
290 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
291 SourceLocation getEndLoc() const LLVM_READONLY {
292 return SubStmt->getLocEnd();
295 child_range children() {
296 return child_range(&SubStmt, &SubStmt+1);
299 static bool classof(const Stmt *T) {
300 return T->getStmtClass() == MSDependentExistsStmtClass;
304 /// Represents the body of a coroutine. This wraps the normal function
305 /// body and holds the additional semantic context required to set up and tear
306 /// down the coroutine frame.
307 class CoroutineBodyStmt final
309 private llvm::TrailingObjects<CoroutineBodyStmt, Stmt *> {
311 Body, ///< The body of the coroutine.
312 Promise, ///< The promise statement.
313 InitSuspend, ///< The initial suspend statement, run before the body.
314 FinalSuspend, ///< The final suspend statement, run after the body.
315 OnException, ///< Handler for exceptions thrown in the body.
316 OnFallthrough, ///< Handler for control flow falling off the body.
317 Allocate, ///< Coroutine frame memory allocation.
318 Deallocate, ///< Coroutine frame memory deallocation.
319 ReturnValue, ///< Return value for thunk function: p.get_return_object().
320 ResultDecl, ///< Declaration holding the result of get_return_object.
321 ReturnStmt, ///< Return statement for the thunk function.
322 ReturnStmtOnAllocFailure, ///< Return statement if allocation failed.
323 FirstParamMove ///< First offset for move construction of parameter copies.
327 friend class ASTStmtReader;
328 friend class ASTReader;
329 friend TrailingObjects;
331 Stmt **getStoredStmts() { return getTrailingObjects<Stmt *>(); }
333 Stmt *const *getStoredStmts() const { return getTrailingObjects<Stmt *>(); }
338 Stmt *Body = nullptr;
339 Stmt *Promise = nullptr;
340 Expr *InitialSuspend = nullptr;
341 Expr *FinalSuspend = nullptr;
342 Stmt *OnException = nullptr;
343 Stmt *OnFallthrough = nullptr;
344 Expr *Allocate = nullptr;
345 Expr *Deallocate = nullptr;
346 Expr *ReturnValue = nullptr;
347 Stmt *ResultDecl = nullptr;
348 Stmt *ReturnStmt = nullptr;
349 Stmt *ReturnStmtOnAllocFailure = nullptr;
350 ArrayRef<Stmt *> ParamMoves;
355 CoroutineBodyStmt(CtorArgs const& Args);
358 static CoroutineBodyStmt *Create(const ASTContext &C, CtorArgs const &Args);
359 static CoroutineBodyStmt *Create(const ASTContext &C, EmptyShell,
362 bool hasDependentPromiseType() const {
363 return getPromiseDecl()->getType()->isDependentType();
366 /// Retrieve the body of the coroutine as written. This will be either
367 /// a CompoundStmt or a TryStmt.
368 Stmt *getBody() const {
369 return getStoredStmts()[SubStmt::Body];
372 Stmt *getPromiseDeclStmt() const {
373 return getStoredStmts()[SubStmt::Promise];
375 VarDecl *getPromiseDecl() const {
376 return cast<VarDecl>(cast<DeclStmt>(getPromiseDeclStmt())->getSingleDecl());
379 Stmt *getInitSuspendStmt() const {
380 return getStoredStmts()[SubStmt::InitSuspend];
382 Stmt *getFinalSuspendStmt() const {
383 return getStoredStmts()[SubStmt::FinalSuspend];
386 Stmt *getExceptionHandler() const {
387 return getStoredStmts()[SubStmt::OnException];
389 Stmt *getFallthroughHandler() const {
390 return getStoredStmts()[SubStmt::OnFallthrough];
393 Expr *getAllocate() const {
394 return cast_or_null<Expr>(getStoredStmts()[SubStmt::Allocate]);
396 Expr *getDeallocate() const {
397 return cast_or_null<Expr>(getStoredStmts()[SubStmt::Deallocate]);
399 Expr *getReturnValueInit() const {
400 return cast<Expr>(getStoredStmts()[SubStmt::ReturnValue]);
402 Stmt *getResultDecl() const { return getStoredStmts()[SubStmt::ResultDecl]; }
403 Stmt *getReturnStmt() const { return getStoredStmts()[SubStmt::ReturnStmt]; }
404 Stmt *getReturnStmtOnAllocFailure() const {
405 return getStoredStmts()[SubStmt::ReturnStmtOnAllocFailure];
407 ArrayRef<Stmt const *> getParamMoves() const {
408 return {getStoredStmts() + SubStmt::FirstParamMove, NumParams};
411 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
412 SourceLocation getBeginLoc() const LLVM_READONLY {
413 return getBody() ? getBody()->getLocStart()
414 : getPromiseDecl()->getLocStart();
416 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
417 SourceLocation getEndLoc() const LLVM_READONLY {
418 return getBody() ? getBody()->getLocEnd() : getPromiseDecl()->getLocEnd();
421 child_range children() {
422 return child_range(getStoredStmts(),
423 getStoredStmts() + SubStmt::FirstParamMove + NumParams);
426 static bool classof(const Stmt *T) {
427 return T->getStmtClass() == CoroutineBodyStmtClass;
431 /// Represents a 'co_return' statement in the C++ Coroutines TS.
433 /// This statament models the initialization of the coroutine promise
434 /// (encapsulating the eventual notional return value) from an expression
435 /// (or braced-init-list), followed by termination of the coroutine.
437 /// This initialization is modeled by the evaluation of the operand
438 /// followed by a call to one of:
439 /// <promise>.return_value(<operand>)
440 /// <promise>.return_void()
441 /// which we name the "promise call".
442 class CoreturnStmt : public Stmt {
443 SourceLocation CoreturnLoc;
445 enum SubStmt { Operand, PromiseCall, Count };
446 Stmt *SubStmts[SubStmt::Count];
450 friend class ASTStmtReader;
452 CoreturnStmt(SourceLocation CoreturnLoc, Stmt *Operand, Stmt *PromiseCall,
453 bool IsImplicit = false)
454 : Stmt(CoreturnStmtClass), CoreturnLoc(CoreturnLoc),
455 IsImplicit(IsImplicit) {
456 SubStmts[SubStmt::Operand] = Operand;
457 SubStmts[SubStmt::PromiseCall] = PromiseCall;
460 CoreturnStmt(EmptyShell) : CoreturnStmt({}, {}, {}) {}
462 SourceLocation getKeywordLoc() const { return CoreturnLoc; }
464 /// Retrieve the operand of the 'co_return' statement. Will be nullptr
465 /// if none was specified.
466 Expr *getOperand() const { return static_cast<Expr*>(SubStmts[Operand]); }
468 /// Retrieve the promise call that results from this 'co_return'
469 /// statement. Will be nullptr if either the coroutine has not yet been
470 /// finalized or the coroutine has no eventual return type.
471 Expr *getPromiseCall() const {
472 return static_cast<Expr*>(SubStmts[PromiseCall]);
475 bool isImplicit() const { return IsImplicit; }
476 void setIsImplicit(bool value = true) { IsImplicit = value; }
478 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
479 SourceLocation getBeginLoc() const LLVM_READONLY { return CoreturnLoc; }
480 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
481 SourceLocation getEndLoc() const LLVM_READONLY {
482 return getOperand() ? getOperand()->getLocEnd() : getLocStart();
485 child_range children() {
487 return child_range(SubStmts + SubStmt::PromiseCall,
488 SubStmts + SubStmt::Count);
489 return child_range(SubStmts, SubStmts + SubStmt::Count);
492 static bool classof(const Stmt *T) {
493 return T->getStmtClass() == CoreturnStmtClass;
497 } // end namespace clang