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 getBeginLoc() const LLVM_READONLY { return CatchLoc; }
45 SourceLocation getEndLoc() const LLVM_READONLY {
46 return HandlerBlock->getEndLoc();
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 getBeginLoc() const LLVM_READONLY { return getTryLoc(); }
91 SourceLocation getTryLoc() const { return TryLoc; }
92 SourceLocation getEndLoc() const {
93 return getStmts()[NumHandlers]->getEndLoc();
96 CompoundStmt *getTryBlock() {
97 return cast<CompoundStmt>(getStmts()[0]);
99 const CompoundStmt *getTryBlock() const {
100 return cast<CompoundStmt>(getStmts()[0]);
103 unsigned getNumHandlers() const { return NumHandlers; }
104 CXXCatchStmt *getHandler(unsigned i) {
105 return cast<CXXCatchStmt>(getStmts()[i + 1]);
107 const CXXCatchStmt *getHandler(unsigned i) const {
108 return cast<CXXCatchStmt>(getStmts()[i + 1]);
111 static bool classof(const Stmt *T) {
112 return T->getStmtClass() == CXXTryStmtClass;
115 child_range children() {
116 return child_range(getStmts(), getStmts() + getNumHandlers() + 1);
120 /// CXXForRangeStmt - This represents C++0x [stmt.ranged]'s ranged for
121 /// statement, represented as 'for (range-declarator : range-expression)'
122 /// or 'for (init-statement 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 { INIT, 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(Stmt *InitStmt, DeclStmt *Range, DeclStmt *Begin,
140 DeclStmt *End, Expr *Cond, Expr *Inc, DeclStmt *LoopVar,
141 Stmt *Body, SourceLocation FL, SourceLocation CAL,
142 SourceLocation CL, SourceLocation RPL);
143 CXXForRangeStmt(EmptyShell Empty) : Stmt(CXXForRangeStmtClass, Empty) { }
145 Stmt *getInit() { return SubExprs[INIT]; }
146 VarDecl *getLoopVariable();
147 Expr *getRangeInit();
149 const Stmt *getInit() const { return SubExprs[INIT]; }
150 const VarDecl *getLoopVariable() const;
151 const Expr *getRangeInit() const;
154 DeclStmt *getRangeStmt() { return cast<DeclStmt>(SubExprs[RANGE]); }
155 DeclStmt *getBeginStmt() {
156 return cast_or_null<DeclStmt>(SubExprs[BEGINSTMT]);
158 DeclStmt *getEndStmt() { return cast_or_null<DeclStmt>(SubExprs[ENDSTMT]); }
159 Expr *getCond() { return cast_or_null<Expr>(SubExprs[COND]); }
160 Expr *getInc() { return cast_or_null<Expr>(SubExprs[INC]); }
161 DeclStmt *getLoopVarStmt() { return cast<DeclStmt>(SubExprs[LOOPVAR]); }
162 Stmt *getBody() { return SubExprs[BODY]; }
164 const DeclStmt *getRangeStmt() const {
165 return cast<DeclStmt>(SubExprs[RANGE]);
167 const DeclStmt *getBeginStmt() const {
168 return cast_or_null<DeclStmt>(SubExprs[BEGINSTMT]);
170 const DeclStmt *getEndStmt() const {
171 return cast_or_null<DeclStmt>(SubExprs[ENDSTMT]);
173 const Expr *getCond() const {
174 return cast_or_null<Expr>(SubExprs[COND]);
176 const Expr *getInc() const {
177 return cast_or_null<Expr>(SubExprs[INC]);
179 const DeclStmt *getLoopVarStmt() const {
180 return cast<DeclStmt>(SubExprs[LOOPVAR]);
182 const Stmt *getBody() const { return SubExprs[BODY]; }
184 void setInit(Stmt *S) { SubExprs[INIT] = S; }
185 void setRangeInit(Expr *E) { SubExprs[RANGE] = reinterpret_cast<Stmt*>(E); }
186 void setRangeStmt(Stmt *S) { SubExprs[RANGE] = S; }
187 void setBeginStmt(Stmt *S) { SubExprs[BEGINSTMT] = S; }
188 void setEndStmt(Stmt *S) { SubExprs[ENDSTMT] = S; }
189 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
190 void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
191 void setLoopVarStmt(Stmt *S) { SubExprs[LOOPVAR] = S; }
192 void setBody(Stmt *S) { SubExprs[BODY] = S; }
194 SourceLocation getForLoc() const { return ForLoc; }
195 SourceLocation getCoawaitLoc() const { return CoawaitLoc; }
196 SourceLocation getColonLoc() const { return ColonLoc; }
197 SourceLocation getRParenLoc() const { return RParenLoc; }
199 SourceLocation getBeginLoc() const LLVM_READONLY { return ForLoc; }
200 SourceLocation getEndLoc() const LLVM_READONLY {
201 return SubExprs[BODY]->getEndLoc();
204 static bool classof(const Stmt *T) {
205 return T->getStmtClass() == CXXForRangeStmtClass;
209 child_range children() {
210 return child_range(&SubExprs[0], &SubExprs[END]);
214 /// Representation of a Microsoft __if_exists or __if_not_exists
215 /// statement with a dependent name.
217 /// The __if_exists statement can be used to include a sequence of statements
218 /// in the program only when a particular dependent name does not exist. For
222 /// template<typename T>
223 /// void call_foo(T &t) {
224 /// __if_exists (T::foo) {
225 /// t.foo(); // okay: only called when T::foo exists.
230 /// Similarly, the __if_not_exists statement can be used to include the
231 /// statements when a particular name does not exist.
233 /// Note that this statement only captures __if_exists and __if_not_exists
234 /// statements whose name is dependent. All non-dependent cases are handled
235 /// directly in the parser, so that they don't introduce a new scope. Clang
236 /// introduces scopes in the dependent case to keep names inside the compound
237 /// statement from leaking out into the surround statements, which would
238 /// compromise the template instantiation model. This behavior differs from
239 /// Visual C++ (which never introduces a scope), but is a fairly reasonable
240 /// approximation of the VC++ behavior.
241 class MSDependentExistsStmt : public Stmt {
242 SourceLocation KeywordLoc;
244 NestedNameSpecifierLoc QualifierLoc;
245 DeclarationNameInfo NameInfo;
248 friend class ASTReader;
249 friend class ASTStmtReader;
252 MSDependentExistsStmt(SourceLocation KeywordLoc, bool IsIfExists,
253 NestedNameSpecifierLoc QualifierLoc,
254 DeclarationNameInfo NameInfo,
255 CompoundStmt *SubStmt)
256 : Stmt(MSDependentExistsStmtClass),
257 KeywordLoc(KeywordLoc), IsIfExists(IsIfExists),
258 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
259 SubStmt(reinterpret_cast<Stmt *>(SubStmt)) { }
261 /// Retrieve the location of the __if_exists or __if_not_exists
263 SourceLocation getKeywordLoc() const { return KeywordLoc; }
265 /// Determine whether this is an __if_exists statement.
266 bool isIfExists() const { return IsIfExists; }
268 /// Determine whether this is an __if_exists statement.
269 bool isIfNotExists() const { return !IsIfExists; }
271 /// Retrieve the nested-name-specifier that qualifies this name, if
273 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
275 /// Retrieve the name of the entity we're testing for, along with
276 /// location information
277 DeclarationNameInfo getNameInfo() const { return NameInfo; }
279 /// Retrieve the compound statement that will be included in the
280 /// program only if the existence of the symbol matches the initial keyword.
281 CompoundStmt *getSubStmt() const {
282 return reinterpret_cast<CompoundStmt *>(SubStmt);
285 SourceLocation getBeginLoc() const LLVM_READONLY { return KeywordLoc; }
286 SourceLocation getEndLoc() const LLVM_READONLY {
287 return SubStmt->getEndLoc();
290 child_range children() {
291 return child_range(&SubStmt, &SubStmt+1);
294 static bool classof(const Stmt *T) {
295 return T->getStmtClass() == MSDependentExistsStmtClass;
299 /// Represents the body of a coroutine. This wraps the normal function
300 /// body and holds the additional semantic context required to set up and tear
301 /// down the coroutine frame.
302 class CoroutineBodyStmt final
304 private llvm::TrailingObjects<CoroutineBodyStmt, Stmt *> {
306 Body, ///< The body of the coroutine.
307 Promise, ///< The promise statement.
308 InitSuspend, ///< The initial suspend statement, run before the body.
309 FinalSuspend, ///< The final suspend statement, run after the body.
310 OnException, ///< Handler for exceptions thrown in the body.
311 OnFallthrough, ///< Handler for control flow falling off the body.
312 Allocate, ///< Coroutine frame memory allocation.
313 Deallocate, ///< Coroutine frame memory deallocation.
314 ReturnValue, ///< Return value for thunk function: p.get_return_object().
315 ResultDecl, ///< Declaration holding the result of get_return_object.
316 ReturnStmt, ///< Return statement for the thunk function.
317 ReturnStmtOnAllocFailure, ///< Return statement if allocation failed.
318 FirstParamMove ///< First offset for move construction of parameter copies.
322 friend class ASTStmtReader;
323 friend class ASTReader;
324 friend TrailingObjects;
326 Stmt **getStoredStmts() { return getTrailingObjects<Stmt *>(); }
328 Stmt *const *getStoredStmts() const { return getTrailingObjects<Stmt *>(); }
333 Stmt *Body = nullptr;
334 Stmt *Promise = nullptr;
335 Expr *InitialSuspend = nullptr;
336 Expr *FinalSuspend = nullptr;
337 Stmt *OnException = nullptr;
338 Stmt *OnFallthrough = nullptr;
339 Expr *Allocate = nullptr;
340 Expr *Deallocate = nullptr;
341 Expr *ReturnValue = nullptr;
342 Stmt *ResultDecl = nullptr;
343 Stmt *ReturnStmt = nullptr;
344 Stmt *ReturnStmtOnAllocFailure = nullptr;
345 ArrayRef<Stmt *> ParamMoves;
350 CoroutineBodyStmt(CtorArgs const& Args);
353 static CoroutineBodyStmt *Create(const ASTContext &C, CtorArgs const &Args);
354 static CoroutineBodyStmt *Create(const ASTContext &C, EmptyShell,
357 bool hasDependentPromiseType() const {
358 return getPromiseDecl()->getType()->isDependentType();
361 /// Retrieve the body of the coroutine as written. This will be either
362 /// a CompoundStmt or a TryStmt.
363 Stmt *getBody() const {
364 return getStoredStmts()[SubStmt::Body];
367 Stmt *getPromiseDeclStmt() const {
368 return getStoredStmts()[SubStmt::Promise];
370 VarDecl *getPromiseDecl() const {
371 return cast<VarDecl>(cast<DeclStmt>(getPromiseDeclStmt())->getSingleDecl());
374 Stmt *getInitSuspendStmt() const {
375 return getStoredStmts()[SubStmt::InitSuspend];
377 Stmt *getFinalSuspendStmt() const {
378 return getStoredStmts()[SubStmt::FinalSuspend];
381 Stmt *getExceptionHandler() const {
382 return getStoredStmts()[SubStmt::OnException];
384 Stmt *getFallthroughHandler() const {
385 return getStoredStmts()[SubStmt::OnFallthrough];
388 Expr *getAllocate() const {
389 return cast_or_null<Expr>(getStoredStmts()[SubStmt::Allocate]);
391 Expr *getDeallocate() const {
392 return cast_or_null<Expr>(getStoredStmts()[SubStmt::Deallocate]);
394 Expr *getReturnValueInit() const {
395 return cast<Expr>(getStoredStmts()[SubStmt::ReturnValue]);
397 Stmt *getResultDecl() const { return getStoredStmts()[SubStmt::ResultDecl]; }
398 Stmt *getReturnStmt() const { return getStoredStmts()[SubStmt::ReturnStmt]; }
399 Stmt *getReturnStmtOnAllocFailure() const {
400 return getStoredStmts()[SubStmt::ReturnStmtOnAllocFailure];
402 ArrayRef<Stmt const *> getParamMoves() const {
403 return {getStoredStmts() + SubStmt::FirstParamMove, NumParams};
406 SourceLocation getBeginLoc() const LLVM_READONLY {
407 return getBody() ? getBody()->getBeginLoc()
408 : getPromiseDecl()->getBeginLoc();
410 SourceLocation getEndLoc() const LLVM_READONLY {
411 return getBody() ? getBody()->getEndLoc() : getPromiseDecl()->getEndLoc();
414 child_range children() {
415 return child_range(getStoredStmts(),
416 getStoredStmts() + SubStmt::FirstParamMove + NumParams);
419 static bool classof(const Stmt *T) {
420 return T->getStmtClass() == CoroutineBodyStmtClass;
424 /// Represents a 'co_return' statement in the C++ Coroutines TS.
426 /// This statament models the initialization of the coroutine promise
427 /// (encapsulating the eventual notional return value) from an expression
428 /// (or braced-init-list), followed by termination of the coroutine.
430 /// This initialization is modeled by the evaluation of the operand
431 /// followed by a call to one of:
432 /// <promise>.return_value(<operand>)
433 /// <promise>.return_void()
434 /// which we name the "promise call".
435 class CoreturnStmt : public Stmt {
436 SourceLocation CoreturnLoc;
438 enum SubStmt { Operand, PromiseCall, Count };
439 Stmt *SubStmts[SubStmt::Count];
443 friend class ASTStmtReader;
445 CoreturnStmt(SourceLocation CoreturnLoc, Stmt *Operand, Stmt *PromiseCall,
446 bool IsImplicit = false)
447 : Stmt(CoreturnStmtClass), CoreturnLoc(CoreturnLoc),
448 IsImplicit(IsImplicit) {
449 SubStmts[SubStmt::Operand] = Operand;
450 SubStmts[SubStmt::PromiseCall] = PromiseCall;
453 CoreturnStmt(EmptyShell) : CoreturnStmt({}, {}, {}) {}
455 SourceLocation getKeywordLoc() const { return CoreturnLoc; }
457 /// Retrieve the operand of the 'co_return' statement. Will be nullptr
458 /// if none was specified.
459 Expr *getOperand() const { return static_cast<Expr*>(SubStmts[Operand]); }
461 /// Retrieve the promise call that results from this 'co_return'
462 /// statement. Will be nullptr if either the coroutine has not yet been
463 /// finalized or the coroutine has no eventual return type.
464 Expr *getPromiseCall() const {
465 return static_cast<Expr*>(SubStmts[PromiseCall]);
468 bool isImplicit() const { return IsImplicit; }
469 void setIsImplicit(bool value = true) { IsImplicit = value; }
471 SourceLocation getBeginLoc() const LLVM_READONLY { return CoreturnLoc; }
472 SourceLocation getEndLoc() const LLVM_READONLY {
473 return getOperand() ? getOperand()->getEndLoc() : getBeginLoc();
476 child_range children() {
478 return child_range(SubStmts + SubStmt::PromiseCall,
479 SubStmts + SubStmt::Count);
480 return child_range(SubStmts, SubStmts + SubStmt::Count);
483 static bool classof(const Stmt *T) {
484 return T->getStmtClass() == CoreturnStmtClass;
488 } // end namespace clang