]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/AST/StmtCXX.h
MFV: r342867
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / include / clang / AST / StmtCXX.h
1 //===--- StmtCXX.h - Classes for representing C++ statements ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the C++ statement AST node classes.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CLANG_AST_STMTCXX_H
15 #define LLVM_CLANG_AST_STMTCXX_H
16
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"
22
23 namespace clang {
24
25 class VarDecl;
26
27 /// CXXCatchStmt - This represents a C++ catch block.
28 ///
29 class CXXCatchStmt : public Stmt {
30   SourceLocation CatchLoc;
31   /// The exception-declaration of the type.
32   VarDecl *ExceptionDecl;
33   /// The handler block.
34   Stmt *HandlerBlock;
35
36 public:
37   CXXCatchStmt(SourceLocation catchLoc, VarDecl *exDecl, Stmt *handlerBlock)
38   : Stmt(CXXCatchStmtClass), CatchLoc(catchLoc), ExceptionDecl(exDecl),
39     HandlerBlock(handlerBlock) {}
40
41   CXXCatchStmt(EmptyShell Empty)
42   : Stmt(CXXCatchStmtClass), ExceptionDecl(nullptr), HandlerBlock(nullptr) {}
43
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();
49   }
50
51   SourceLocation getCatchLoc() const { return CatchLoc; }
52   VarDecl *getExceptionDecl() const { return ExceptionDecl; }
53   QualType getCaughtType() const;
54   Stmt *getHandlerBlock() const { return HandlerBlock; }
55
56   static bool classof(const Stmt *T) {
57     return T->getStmtClass() == CXXCatchStmtClass;
58   }
59
60   child_range children() { return child_range(&HandlerBlock, &HandlerBlock+1); }
61
62   friend class ASTStmtReader;
63 };
64
65 /// CXXTryStmt - A C++ try block, including all handlers.
66 ///
67 class CXXTryStmt final : public Stmt,
68                          private llvm::TrailingObjects<CXXTryStmt, Stmt *> {
69
70   friend TrailingObjects;
71   friend class ASTStmtReader;
72
73   SourceLocation TryLoc;
74   unsigned NumHandlers;
75   size_t numTrailingObjects(OverloadToken<Stmt *>) const { return NumHandlers; }
76
77   CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock, ArrayRef<Stmt*> handlers);
78   CXXTryStmt(EmptyShell Empty, unsigned numHandlers)
79     : Stmt(CXXTryStmtClass), NumHandlers(numHandlers) { }
80
81   Stmt *const *getStmts() const { return getTrailingObjects<Stmt *>(); }
82   Stmt **getStmts() { return getTrailingObjects<Stmt *>(); }
83
84 public:
85   static CXXTryStmt *Create(const ASTContext &C, SourceLocation tryLoc,
86                             Stmt *tryBlock, ArrayRef<Stmt*> handlers);
87
88   static CXXTryStmt *Create(const ASTContext &C, EmptyShell Empty,
89                             unsigned numHandlers);
90
91   SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
92   SourceLocation getBeginLoc() const LLVM_READONLY { return getTryLoc(); }
93   SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
94
95   SourceLocation getTryLoc() const { return TryLoc; }
96   SourceLocation getEndLoc() const {
97     return getStmts()[NumHandlers]->getLocEnd();
98   }
99
100   CompoundStmt *getTryBlock() {
101     return cast<CompoundStmt>(getStmts()[0]);
102   }
103   const CompoundStmt *getTryBlock() const {
104     return cast<CompoundStmt>(getStmts()[0]);
105   }
106
107   unsigned getNumHandlers() const { return NumHandlers; }
108   CXXCatchStmt *getHandler(unsigned i) {
109     return cast<CXXCatchStmt>(getStmts()[i + 1]);
110   }
111   const CXXCatchStmt *getHandler(unsigned i) const {
112     return cast<CXXCatchStmt>(getStmts()[i + 1]);
113   }
114
115   static bool classof(const Stmt *T) {
116     return T->getStmtClass() == CXXTryStmtClass;
117   }
118
119   child_range children() {
120     return child_range(getStmts(), getStmts() + getNumHandlers() + 1);
121   }
122 };
123
124 /// CXXForRangeStmt - This represents C++0x [stmt.ranged]'s ranged for
125 /// statement, represented as 'for (range-declarator : range-expression)'.
126 ///
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.
135   Stmt *SubExprs[END];
136   SourceLocation CoawaitLoc;
137   SourceLocation ColonLoc;
138   SourceLocation RParenLoc;
139
140   friend class ASTStmtReader;
141 public:
142   CXXForRangeStmt(DeclStmt *Range, DeclStmt *Begin, DeclStmt *End,
143                   Expr *Cond, Expr *Inc, DeclStmt *LoopVar, Stmt *Body,
144                   SourceLocation FL, SourceLocation CAL, SourceLocation CL,
145                   SourceLocation RPL);
146   CXXForRangeStmt(EmptyShell Empty) : Stmt(CXXForRangeStmtClass, Empty) { }
147
148
149   VarDecl *getLoopVariable();
150   Expr *getRangeInit();
151
152   const VarDecl *getLoopVariable() const;
153   const Expr *getRangeInit() const;
154
155
156   DeclStmt *getRangeStmt() { return cast<DeclStmt>(SubExprs[RANGE]); }
157   DeclStmt *getBeginStmt() {
158     return cast_or_null<DeclStmt>(SubExprs[BEGINSTMT]);
159   }
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]; }
165
166   const DeclStmt *getRangeStmt() const {
167     return cast<DeclStmt>(SubExprs[RANGE]);
168   }
169   const DeclStmt *getBeginStmt() const {
170     return cast_or_null<DeclStmt>(SubExprs[BEGINSTMT]);
171   }
172   const DeclStmt *getEndStmt() const {
173     return cast_or_null<DeclStmt>(SubExprs[ENDSTMT]);
174   }
175   const Expr *getCond() const {
176     return cast_or_null<Expr>(SubExprs[COND]);
177   }
178   const Expr *getInc() const {
179     return cast_or_null<Expr>(SubExprs[INC]);
180   }
181   const DeclStmt *getLoopVarStmt() const {
182     return cast<DeclStmt>(SubExprs[LOOPVAR]);
183   }
184   const Stmt *getBody() const { return SubExprs[BODY]; }
185
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; }
194
195   SourceLocation getForLoc() const { return ForLoc; }
196   SourceLocation getCoawaitLoc() const { return CoawaitLoc; }
197   SourceLocation getColonLoc() const { return ColonLoc; }
198   SourceLocation getRParenLoc() const { return RParenLoc; }
199
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();
205   }
206
207   static bool classof(const Stmt *T) {
208     return T->getStmtClass() == CXXForRangeStmtClass;
209   }
210
211   // Iterators
212   child_range children() {
213     return child_range(&SubExprs[0], &SubExprs[END]);
214   }
215 };
216
217 /// Representation of a Microsoft __if_exists or __if_not_exists
218 /// statement with a dependent name.
219 ///
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
222 /// example:
223 ///
224 /// \code
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.
229 ///   }
230 /// }
231 /// \endcode
232 ///
233 /// Similarly, the __if_not_exists statement can be used to include the
234 /// statements when a particular name does not exist.
235 ///
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;
246   bool IsIfExists;
247   NestedNameSpecifierLoc QualifierLoc;
248   DeclarationNameInfo NameInfo;
249   Stmt *SubStmt;
250
251   friend class ASTReader;
252   friend class ASTStmtReader;
253
254 public:
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)) { }
263
264   /// Retrieve the location of the __if_exists or __if_not_exists
265   /// keyword.
266   SourceLocation getKeywordLoc() const { return KeywordLoc; }
267
268   /// Determine whether this is an __if_exists statement.
269   bool isIfExists() const { return IsIfExists; }
270
271   /// Determine whether this is an __if_exists statement.
272   bool isIfNotExists() const { return !IsIfExists; }
273
274   /// Retrieve the nested-name-specifier that qualifies this name, if
275   /// any.
276   NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
277
278   /// Retrieve the name of the entity we're testing for, along with
279   /// location information
280   DeclarationNameInfo getNameInfo() const { return NameInfo; }
281
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);
286   }
287
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();
293   }
294
295   child_range children() {
296     return child_range(&SubStmt, &SubStmt+1);
297   }
298
299   static bool classof(const Stmt *T) {
300     return T->getStmtClass() == MSDependentExistsStmtClass;
301   }
302 };
303
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
308     : public Stmt,
309       private llvm::TrailingObjects<CoroutineBodyStmt, Stmt *> {
310   enum SubStmt {
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.
324   };
325   unsigned NumParams;
326
327   friend class ASTStmtReader;
328   friend class ASTReader;
329   friend TrailingObjects;
330
331   Stmt **getStoredStmts() { return getTrailingObjects<Stmt *>(); }
332
333   Stmt *const *getStoredStmts() const { return getTrailingObjects<Stmt *>(); }
334
335 public:
336
337   struct CtorArgs {
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;
351   };
352
353 private:
354
355   CoroutineBodyStmt(CtorArgs const& Args);
356
357 public:
358   static CoroutineBodyStmt *Create(const ASTContext &C, CtorArgs const &Args);
359   static CoroutineBodyStmt *Create(const ASTContext &C, EmptyShell,
360                                    unsigned NumParams);
361
362   bool hasDependentPromiseType() const {
363     return getPromiseDecl()->getType()->isDependentType();
364   }
365
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];
370   }
371
372   Stmt *getPromiseDeclStmt() const {
373     return getStoredStmts()[SubStmt::Promise];
374   }
375   VarDecl *getPromiseDecl() const {
376     return cast<VarDecl>(cast<DeclStmt>(getPromiseDeclStmt())->getSingleDecl());
377   }
378
379   Stmt *getInitSuspendStmt() const {
380     return getStoredStmts()[SubStmt::InitSuspend];
381   }
382   Stmt *getFinalSuspendStmt() const {
383     return getStoredStmts()[SubStmt::FinalSuspend];
384   }
385
386   Stmt *getExceptionHandler() const {
387     return getStoredStmts()[SubStmt::OnException];
388   }
389   Stmt *getFallthroughHandler() const {
390     return getStoredStmts()[SubStmt::OnFallthrough];
391   }
392
393   Expr *getAllocate() const {
394     return cast_or_null<Expr>(getStoredStmts()[SubStmt::Allocate]);
395   }
396   Expr *getDeallocate() const {
397     return cast_or_null<Expr>(getStoredStmts()[SubStmt::Deallocate]);
398   }
399   Expr *getReturnValueInit() const {
400     return cast<Expr>(getStoredStmts()[SubStmt::ReturnValue]);
401   }
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];
406   }
407   ArrayRef<Stmt const *> getParamMoves() const {
408     return {getStoredStmts() + SubStmt::FirstParamMove, NumParams};
409   }
410
411   SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
412   SourceLocation getBeginLoc() const LLVM_READONLY {
413     return getBody() ? getBody()->getLocStart()
414             : getPromiseDecl()->getLocStart();
415   }
416   SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
417   SourceLocation getEndLoc() const LLVM_READONLY {
418     return getBody() ? getBody()->getLocEnd() : getPromiseDecl()->getLocEnd();
419   }
420
421   child_range children() {
422     return child_range(getStoredStmts(),
423                        getStoredStmts() + SubStmt::FirstParamMove + NumParams);
424   }
425
426   static bool classof(const Stmt *T) {
427     return T->getStmtClass() == CoroutineBodyStmtClass;
428   }
429 };
430
431 /// Represents a 'co_return' statement in the C++ Coroutines TS.
432 ///
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.
436 ///
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;
444
445   enum SubStmt { Operand, PromiseCall, Count };
446   Stmt *SubStmts[SubStmt::Count];
447
448   bool IsImplicit : 1;
449
450   friend class ASTStmtReader;
451 public:
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;
458   }
459
460   CoreturnStmt(EmptyShell) : CoreturnStmt({}, {}, {}) {}
461
462   SourceLocation getKeywordLoc() const { return CoreturnLoc; }
463
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]); }
467
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]);
473   }
474
475   bool isImplicit() const { return IsImplicit; }
476   void setIsImplicit(bool value = true) { IsImplicit = value; }
477
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();
483   }
484
485   child_range children() {
486     if (!getOperand())
487       return child_range(SubStmts + SubStmt::PromiseCall,
488                          SubStmts + SubStmt::Count);
489     return child_range(SubStmts, SubStmts + SubStmt::Count);
490   }
491
492   static bool classof(const Stmt *T) {
493     return T->getStmtClass() == CoreturnStmtClass;
494   }
495 };
496
497 }  // end namespace clang
498
499 #endif