]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/clang/AST/StmtCXX.h
Vendor import of clang trunk r338150:
[FreeBSD/FreeBSD.git] / 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 CatchLoc; }
45   SourceLocation getLocEnd() const LLVM_READONLY {
46     return HandlerBlock->getLocEnd();
47   }
48
49   SourceLocation getCatchLoc() const { return CatchLoc; }
50   VarDecl *getExceptionDecl() const { return ExceptionDecl; }
51   QualType getCaughtType() const;
52   Stmt *getHandlerBlock() const { return HandlerBlock; }
53
54   static bool classof(const Stmt *T) {
55     return T->getStmtClass() == CXXCatchStmtClass;
56   }
57
58   child_range children() { return child_range(&HandlerBlock, &HandlerBlock+1); }
59
60   friend class ASTStmtReader;
61 };
62
63 /// CXXTryStmt - A C++ try block, including all handlers.
64 ///
65 class CXXTryStmt final : public Stmt,
66                          private llvm::TrailingObjects<CXXTryStmt, Stmt *> {
67
68   friend TrailingObjects;
69   friend class ASTStmtReader;
70
71   SourceLocation TryLoc;
72   unsigned NumHandlers;
73   size_t numTrailingObjects(OverloadToken<Stmt *>) const { return NumHandlers; }
74
75   CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock, ArrayRef<Stmt*> handlers);
76   CXXTryStmt(EmptyShell Empty, unsigned numHandlers)
77     : Stmt(CXXTryStmtClass), NumHandlers(numHandlers) { }
78
79   Stmt *const *getStmts() const { return getTrailingObjects<Stmt *>(); }
80   Stmt **getStmts() { return getTrailingObjects<Stmt *>(); }
81
82 public:
83   static CXXTryStmt *Create(const ASTContext &C, SourceLocation tryLoc,
84                             Stmt *tryBlock, ArrayRef<Stmt*> handlers);
85
86   static CXXTryStmt *Create(const ASTContext &C, EmptyShell Empty,
87                             unsigned numHandlers);
88
89   SourceLocation getLocStart() const LLVM_READONLY { return getTryLoc(); }
90   SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
91
92   SourceLocation getTryLoc() const { return TryLoc; }
93   SourceLocation getEndLoc() const {
94     return getStmts()[NumHandlers]->getLocEnd();
95   }
96
97   CompoundStmt *getTryBlock() {
98     return cast<CompoundStmt>(getStmts()[0]);
99   }
100   const CompoundStmt *getTryBlock() const {
101     return cast<CompoundStmt>(getStmts()[0]);
102   }
103
104   unsigned getNumHandlers() const { return NumHandlers; }
105   CXXCatchStmt *getHandler(unsigned i) {
106     return cast<CXXCatchStmt>(getStmts()[i + 1]);
107   }
108   const CXXCatchStmt *getHandler(unsigned i) const {
109     return cast<CXXCatchStmt>(getStmts()[i + 1]);
110   }
111
112   static bool classof(const Stmt *T) {
113     return T->getStmtClass() == CXXTryStmtClass;
114   }
115
116   child_range children() {
117     return child_range(getStmts(), getStmts() + getNumHandlers() + 1);
118   }
119 };
120
121 /// CXXForRangeStmt - This represents C++0x [stmt.ranged]'s ranged for
122 /// statement, represented as 'for (range-declarator : range-expression)'.
123 ///
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.
132   Stmt *SubExprs[END];
133   SourceLocation CoawaitLoc;
134   SourceLocation ColonLoc;
135   SourceLocation RParenLoc;
136
137   friend class ASTStmtReader;
138 public:
139   CXXForRangeStmt(DeclStmt *Range, DeclStmt *Begin, DeclStmt *End,
140                   Expr *Cond, Expr *Inc, DeclStmt *LoopVar, Stmt *Body,
141                   SourceLocation FL, SourceLocation CAL, SourceLocation CL,
142                   SourceLocation RPL);
143   CXXForRangeStmt(EmptyShell Empty) : Stmt(CXXForRangeStmtClass, Empty) { }
144
145
146   VarDecl *getLoopVariable();
147   Expr *getRangeInit();
148
149   const VarDecl *getLoopVariable() const;
150   const Expr *getRangeInit() const;
151
152
153   DeclStmt *getRangeStmt() { return cast<DeclStmt>(SubExprs[RANGE]); }
154   DeclStmt *getBeginStmt() {
155     return cast_or_null<DeclStmt>(SubExprs[BEGINSTMT]);
156   }
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]; }
162
163   const DeclStmt *getRangeStmt() const {
164     return cast<DeclStmt>(SubExprs[RANGE]);
165   }
166   const DeclStmt *getBeginStmt() const {
167     return cast_or_null<DeclStmt>(SubExprs[BEGINSTMT]);
168   }
169   const DeclStmt *getEndStmt() const {
170     return cast_or_null<DeclStmt>(SubExprs[ENDSTMT]);
171   }
172   const Expr *getCond() const {
173     return cast_or_null<Expr>(SubExprs[COND]);
174   }
175   const Expr *getInc() const {
176     return cast_or_null<Expr>(SubExprs[INC]);
177   }
178   const DeclStmt *getLoopVarStmt() const {
179     return cast<DeclStmt>(SubExprs[LOOPVAR]);
180   }
181   const Stmt *getBody() const { return SubExprs[BODY]; }
182
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; }
191
192   SourceLocation getForLoc() const { return ForLoc; }
193   SourceLocation getCoawaitLoc() const { return CoawaitLoc; }
194   SourceLocation getColonLoc() const { return ColonLoc; }
195   SourceLocation getRParenLoc() const { return RParenLoc; }
196
197   SourceLocation getLocStart() const LLVM_READONLY { return ForLoc; }
198   SourceLocation getLocEnd() const LLVM_READONLY {
199     return SubExprs[BODY]->getLocEnd();
200   }
201
202   static bool classof(const Stmt *T) {
203     return T->getStmtClass() == CXXForRangeStmtClass;
204   }
205
206   // Iterators
207   child_range children() {
208     return child_range(&SubExprs[0], &SubExprs[END]);
209   }
210 };
211
212 /// Representation of a Microsoft __if_exists or __if_not_exists
213 /// statement with a dependent name.
214 ///
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
217 /// example:
218 ///
219 /// \code
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.
224 ///   }
225 /// }
226 /// \endcode
227 ///
228 /// Similarly, the __if_not_exists statement can be used to include the
229 /// statements when a particular name does not exist.
230 ///
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;
241   bool IsIfExists;
242   NestedNameSpecifierLoc QualifierLoc;
243   DeclarationNameInfo NameInfo;
244   Stmt *SubStmt;
245
246   friend class ASTReader;
247   friend class ASTStmtReader;
248
249 public:
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)) { }
258
259   /// Retrieve the location of the __if_exists or __if_not_exists
260   /// keyword.
261   SourceLocation getKeywordLoc() const { return KeywordLoc; }
262
263   /// Determine whether this is an __if_exists statement.
264   bool isIfExists() const { return IsIfExists; }
265
266   /// Determine whether this is an __if_exists statement.
267   bool isIfNotExists() const { return !IsIfExists; }
268
269   /// Retrieve the nested-name-specifier that qualifies this name, if
270   /// any.
271   NestedNameSpecifierLoc getQualifierLoc() const { return QualifierLoc; }
272
273   /// Retrieve the name of the entity we're testing for, along with
274   /// location information
275   DeclarationNameInfo getNameInfo() const { return NameInfo; }
276
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);
281   }
282
283   SourceLocation getLocStart() const LLVM_READONLY { return KeywordLoc; }
284   SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();}
285
286   child_range children() {
287     return child_range(&SubStmt, &SubStmt+1);
288   }
289
290   static bool classof(const Stmt *T) {
291     return T->getStmtClass() == MSDependentExistsStmtClass;
292   }
293 };
294
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
299     : public Stmt,
300       private llvm::TrailingObjects<CoroutineBodyStmt, Stmt *> {
301   enum SubStmt {
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.
315   };
316   unsigned NumParams;
317
318   friend class ASTStmtReader;
319   friend class ASTReader;
320   friend TrailingObjects;
321
322   Stmt **getStoredStmts() { return getTrailingObjects<Stmt *>(); }
323
324   Stmt *const *getStoredStmts() const { return getTrailingObjects<Stmt *>(); }
325
326 public:
327
328   struct CtorArgs {
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;
342   };
343
344 private:
345
346   CoroutineBodyStmt(CtorArgs const& Args);
347
348 public:
349   static CoroutineBodyStmt *Create(const ASTContext &C, CtorArgs const &Args);
350   static CoroutineBodyStmt *Create(const ASTContext &C, EmptyShell,
351                                    unsigned NumParams);
352
353   bool hasDependentPromiseType() const {
354     return getPromiseDecl()->getType()->isDependentType();
355   }
356
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];
361   }
362
363   Stmt *getPromiseDeclStmt() const {
364     return getStoredStmts()[SubStmt::Promise];
365   }
366   VarDecl *getPromiseDecl() const {
367     return cast<VarDecl>(cast<DeclStmt>(getPromiseDeclStmt())->getSingleDecl());
368   }
369
370   Stmt *getInitSuspendStmt() const {
371     return getStoredStmts()[SubStmt::InitSuspend];
372   }
373   Stmt *getFinalSuspendStmt() const {
374     return getStoredStmts()[SubStmt::FinalSuspend];
375   }
376
377   Stmt *getExceptionHandler() const {
378     return getStoredStmts()[SubStmt::OnException];
379   }
380   Stmt *getFallthroughHandler() const {
381     return getStoredStmts()[SubStmt::OnFallthrough];
382   }
383
384   Expr *getAllocate() const {
385     return cast_or_null<Expr>(getStoredStmts()[SubStmt::Allocate]);
386   }
387   Expr *getDeallocate() const {
388     return cast_or_null<Expr>(getStoredStmts()[SubStmt::Deallocate]);
389   }
390   Expr *getReturnValueInit() const {
391     return cast<Expr>(getStoredStmts()[SubStmt::ReturnValue]);
392   }
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];
397   }
398   ArrayRef<Stmt const *> getParamMoves() const {
399     return {getStoredStmts() + SubStmt::FirstParamMove, NumParams};
400   }
401
402   SourceLocation getLocStart() const LLVM_READONLY {
403     return getBody() ? getBody()->getLocStart()
404             : getPromiseDecl()->getLocStart();
405   }
406   SourceLocation getLocEnd() const LLVM_READONLY {
407     return getBody() ? getBody()->getLocEnd() : getPromiseDecl()->getLocEnd();
408   }
409
410   child_range children() {
411     return child_range(getStoredStmts(),
412                        getStoredStmts() + SubStmt::FirstParamMove + NumParams);
413   }
414
415   static bool classof(const Stmt *T) {
416     return T->getStmtClass() == CoroutineBodyStmtClass;
417   }
418 };
419
420 /// Represents a 'co_return' statement in the C++ Coroutines TS.
421 ///
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.
425 ///
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;
433
434   enum SubStmt { Operand, PromiseCall, Count };
435   Stmt *SubStmts[SubStmt::Count];
436
437   bool IsImplicit : 1;
438
439   friend class ASTStmtReader;
440 public:
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;
447   }
448
449   CoreturnStmt(EmptyShell) : CoreturnStmt({}, {}, {}) {}
450
451   SourceLocation getKeywordLoc() const { return CoreturnLoc; }
452
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]); }
456
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]);
462   }
463
464   bool isImplicit() const { return IsImplicit; }
465   void setIsImplicit(bool value = true) { IsImplicit = value; }
466
467   SourceLocation getLocStart() const LLVM_READONLY { return CoreturnLoc; }
468   SourceLocation getLocEnd() const LLVM_READONLY {
469     return getOperand() ? getOperand()->getLocEnd() : getLocStart();
470   }
471
472   child_range children() {
473     if (!getOperand())
474       return child_range(SubStmts + SubStmt::PromiseCall,
475                          SubStmts + SubStmt::Count);
476     return child_range(SubStmts, SubStmts + SubStmt::Count);
477   }
478
479   static bool classof(const Stmt *T) {
480     return T->getStmtClass() == CoreturnStmtClass;
481   }
482 };
483
484 }  // end namespace clang
485
486 #endif