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 : public Stmt {
66 SourceLocation TryLoc;
69 CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock, ArrayRef<Stmt*> handlers);
71 CXXTryStmt(EmptyShell Empty, unsigned numHandlers)
72 : Stmt(CXXTryStmtClass), NumHandlers(numHandlers) { }
74 Stmt const * const *getStmts() const {
75 return reinterpret_cast<Stmt const * const*>(this + 1);
78 return reinterpret_cast<Stmt **>(this + 1);
82 static CXXTryStmt *Create(const ASTContext &C, SourceLocation tryLoc,
83 Stmt *tryBlock, ArrayRef<Stmt*> handlers);
85 static CXXTryStmt *Create(const ASTContext &C, EmptyShell Empty,
86 unsigned numHandlers);
88 SourceLocation getLocStart() const LLVM_READONLY { return getTryLoc(); }
89 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
91 SourceLocation getTryLoc() const { return TryLoc; }
92 SourceLocation getEndLoc() const {
93 return getStmts()[NumHandlers]->getLocEnd();
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);
119 friend class ASTStmtReader;
122 /// CXXForRangeStmt - This represents C++0x [stmt.ranged]'s ranged for
123 /// statement, represented as 'for (range-declarator : range-expression)'.
125 /// This is stored in a partially-desugared form to allow full semantic
126 /// analysis of the constituent components. The original syntactic components
127 /// can be extracted using getLoopVariable and getRangeInit.
128 class CXXForRangeStmt : public Stmt {
129 SourceLocation ForLoc;
130 enum { RANGE, BEGINSTMT, ENDSTMT, COND, INC, LOOPVAR, BODY, END };
131 // SubExprs[RANGE] is an expression or declstmt.
132 // SubExprs[COND] and SubExprs[INC] are expressions.
134 SourceLocation CoawaitLoc;
135 SourceLocation ColonLoc;
136 SourceLocation RParenLoc;
138 friend class ASTStmtReader;
140 CXXForRangeStmt(DeclStmt *Range, DeclStmt *Begin, DeclStmt *End,
141 Expr *Cond, Expr *Inc, DeclStmt *LoopVar, Stmt *Body,
142 SourceLocation FL, SourceLocation CAL, SourceLocation CL,
144 CXXForRangeStmt(EmptyShell Empty) : Stmt(CXXForRangeStmtClass, Empty) { }
147 VarDecl *getLoopVariable();
148 Expr *getRangeInit();
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 setRangeInit(Expr *E) { SubExprs[RANGE] = reinterpret_cast<Stmt*>(E); }
185 void setRangeStmt(Stmt *S) { SubExprs[RANGE] = S; }
186 void setBeginStmt(Stmt *S) { SubExprs[BEGINSTMT] = S; }
187 void setEndStmt(Stmt *S) { SubExprs[ENDSTMT] = S; }
188 void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
189 void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
190 void setLoopVarStmt(Stmt *S) { SubExprs[LOOPVAR] = S; }
191 void setBody(Stmt *S) { SubExprs[BODY] = S; }
193 SourceLocation getForLoc() const { return ForLoc; }
194 SourceLocation getCoawaitLoc() const { return CoawaitLoc; }
195 SourceLocation getColonLoc() const { return ColonLoc; }
196 SourceLocation getRParenLoc() const { return RParenLoc; }
198 SourceLocation getLocStart() const LLVM_READONLY { return ForLoc; }
199 SourceLocation getLocEnd() const LLVM_READONLY {
200 return SubExprs[BODY]->getLocEnd();
203 static bool classof(const Stmt *T) {
204 return T->getStmtClass() == CXXForRangeStmtClass;
208 child_range children() {
209 return child_range(&SubExprs[0], &SubExprs[END]);
213 /// \brief Representation of a Microsoft __if_exists or __if_not_exists
214 /// statement with a dependent name.
216 /// The __if_exists statement can be used to include a sequence of statements
217 /// in the program only when a particular dependent name does not exist. For
221 /// template<typename T>
222 /// void call_foo(T &t) {
223 /// __if_exists (T::foo) {
224 /// t.foo(); // okay: only called when T::foo exists.
229 /// Similarly, the __if_not_exists statement can be used to include the
230 /// statements when a particular name does not exist.
232 /// Note that this statement only captures __if_exists and __if_not_exists
233 /// statements whose name is dependent. All non-dependent cases are handled
234 /// directly in the parser, so that they don't introduce a new scope. Clang
235 /// introduces scopes in the dependent case to keep names inside the compound
236 /// statement from leaking out into the surround statements, which would
237 /// compromise the template instantiation model. This behavior differs from
238 /// Visual C++ (which never introduces a scope), but is a fairly reasonable
239 /// approximation of the VC++ behavior.
240 class MSDependentExistsStmt : public Stmt {
241 SourceLocation KeywordLoc;
243 NestedNameSpecifierLoc QualifierLoc;
244 DeclarationNameInfo NameInfo;
247 friend class ASTReader;
248 friend class ASTStmtReader;
251 MSDependentExistsStmt(SourceLocation KeywordLoc, bool IsIfExists,
252 NestedNameSpecifierLoc QualifierLoc,
253 DeclarationNameInfo NameInfo,
254 CompoundStmt *SubStmt)
255 : Stmt(MSDependentExistsStmtClass),
256 KeywordLoc(KeywordLoc), IsIfExists(IsIfExists),
257 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
258 SubStmt(reinterpret_cast<Stmt *>(SubStmt)) { }
260 /// \brief Retrieve the location of the __if_exists or __if_not_exists
262 SourceLocation getKeywordLoc() const { return KeywordLoc; }
264 /// \brief Determine whether this is an __if_exists statement.
265 bool isIfExists() const { return IsIfExists; }
267 /// \brief Determine whether this is an __if_exists statement.
268 bool isIfNotExists() const { return !IsIfExists; }
270 /// \brief Retrieve the nested-name-specifier that qualifies this name, if
272 NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
274 /// \brief Retrieve the name of the entity we're testing for, along with
275 /// location information
276 DeclarationNameInfo getNameInfo() const { return NameInfo; }
278 /// \brief Retrieve the compound statement that will be included in the
279 /// program only if the existence of the symbol matches the initial keyword.
280 CompoundStmt *getSubStmt() const {
281 return reinterpret_cast<CompoundStmt *>(SubStmt);
284 SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
285 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
287 child_range children() {
288 return child_range(&SubStmt, &SubStmt+1);
291 static bool classof(const Stmt *T) {
292 return T->getStmtClass() == MSDependentExistsStmtClass;
296 /// \brief Represents the body of a coroutine. This wraps the normal function
297 /// body and holds the additional semantic context required to set up and tear
298 /// down the coroutine frame.
299 class CoroutineBodyStmt final
301 private llvm::TrailingObjects<CoroutineBodyStmt, Stmt *> {
303 Body, ///< The body of the coroutine.
304 Promise, ///< The promise statement.
305 InitSuspend, ///< The initial suspend statement, run before the body.
306 FinalSuspend, ///< The final suspend statement, run after the body.
307 OnException, ///< Handler for exceptions thrown in the body.
308 OnFallthrough, ///< Handler for control flow falling off the body.
309 Allocate, ///< Coroutine frame memory allocation.
310 Deallocate, ///< Coroutine frame memory deallocation.
311 ReturnValue, ///< Return value for thunk function: p.get_return_object().
312 ResultDecl, ///< Declaration holding the result of get_return_object.
313 ReturnStmt, ///< Return statement for the thunk function.
314 ReturnStmtOnAllocFailure, ///< Return statement if allocation failed.
315 FirstParamMove ///< First offset for move construction of parameter copies.
319 friend class ASTStmtReader;
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);
351 bool hasDependentPromiseType() const {
352 return getPromiseDecl()->getType()->isDependentType();
355 /// \brief Retrieve the body of the coroutine as written. This will be either
356 /// a CompoundStmt or a TryStmt.
357 Stmt *getBody() const {
358 return getStoredStmts()[SubStmt::Body];
361 Stmt *getPromiseDeclStmt() const {
362 return getStoredStmts()[SubStmt::Promise];
364 VarDecl *getPromiseDecl() const {
365 return cast<VarDecl>(cast<DeclStmt>(getPromiseDeclStmt())->getSingleDecl());
368 Stmt *getInitSuspendStmt() const {
369 return getStoredStmts()[SubStmt::InitSuspend];
371 Stmt *getFinalSuspendStmt() const {
372 return getStoredStmts()[SubStmt::FinalSuspend];
375 Stmt *getExceptionHandler() const {
376 return getStoredStmts()[SubStmt::OnException];
378 Stmt *getFallthroughHandler() const {
379 return getStoredStmts()[SubStmt::OnFallthrough];
382 Expr *getAllocate() const {
383 return cast_or_null<Expr>(getStoredStmts()[SubStmt::Allocate]);
385 Expr *getDeallocate() const {
386 return cast_or_null<Expr>(getStoredStmts()[SubStmt::Deallocate]);
388 Expr *getReturnValueInit() const {
389 return cast<Expr>(getStoredStmts()[SubStmt::ReturnValue]);
391 Stmt *getResultDecl() const { return getStoredStmts()[SubStmt::ResultDecl]; }
392 Stmt *getReturnStmt() const { return getStoredStmts()[SubStmt::ReturnStmt]; }
393 Stmt *getReturnStmtOnAllocFailure() const {
394 return getStoredStmts()[SubStmt::ReturnStmtOnAllocFailure];
396 ArrayRef<Stmt const *> getParamMoves() const {
397 return {getStoredStmts() + SubStmt::FirstParamMove, NumParams};
400 SourceLocation getLocStart() const LLVM_READONLY {
401 return getBody() ? getBody()->getLocStart()
402 : getPromiseDecl()->getLocStart();
404 SourceLocation getLocEnd() const LLVM_READONLY {
405 return getBody() ? getBody()->getLocEnd() : getPromiseDecl()->getLocEnd();
408 child_range children() {
409 return child_range(getStoredStmts(),
410 getStoredStmts() + SubStmt::FirstParamMove + NumParams);
413 static bool classof(const Stmt *T) {
414 return T->getStmtClass() == CoroutineBodyStmtClass;
418 /// \brief Represents a 'co_return' statement in the C++ Coroutines TS.
420 /// This statament models the initialization of the coroutine promise
421 /// (encapsulating the eventual notional return value) from an expression
422 /// (or braced-init-list), followed by termination of the coroutine.
424 /// This initialization is modeled by the evaluation of the operand
425 /// followed by a call to one of:
426 /// <promise>.return_value(<operand>)
427 /// <promise>.return_void()
428 /// which we name the "promise call".
429 class CoreturnStmt : public Stmt {
430 SourceLocation CoreturnLoc;
432 enum SubStmt { Operand, PromiseCall, Count };
433 Stmt *SubStmts[SubStmt::Count];
437 friend class ASTStmtReader;
439 CoreturnStmt(SourceLocation CoreturnLoc, Stmt *Operand, Stmt *PromiseCall,
440 bool IsImplicit = false)
441 : Stmt(CoreturnStmtClass), CoreturnLoc(CoreturnLoc),
442 IsImplicit(IsImplicit) {
443 SubStmts[SubStmt::Operand] = Operand;
444 SubStmts[SubStmt::PromiseCall] = PromiseCall;
447 SourceLocation getKeywordLoc() const { return CoreturnLoc; }
449 /// \brief Retrieve the operand of the 'co_return' statement. Will be nullptr
450 /// if none was specified.
451 Expr *getOperand() const { return static_cast<Expr*>(SubStmts[Operand]); }
453 /// \brief Retrieve the promise call that results from this 'co_return'
454 /// statement. Will be nullptr if either the coroutine has not yet been
455 /// finalized or the coroutine has no eventual return type.
456 Expr *getPromiseCall() const {
457 return static_cast<Expr*>(SubStmts[PromiseCall]);
460 bool isImplicit() const { return IsImplicit; }
461 void setIsImplicit(bool value = true) { IsImplicit = value; }
463 SourceLocation getLocStart() const LLVM_READONLY { return CoreturnLoc; }
464 SourceLocation getLocEnd() const LLVM_READONLY {
465 return getOperand() ? getOperand()->getLocEnd() : getLocStart();
468 child_range children() {
470 return child_range(SubStmts + SubStmt::PromiseCall,
471 SubStmts + SubStmt::Count);
472 return child_range(SubStmts, SubStmts + SubStmt::Count);
475 static bool classof(const Stmt *T) {
476 return T->getStmtClass() == CoreturnStmtClass;
480 } // end namespace clang