Clang Project

clang_source_code/include/clang/AST/StmtObjC.h
1//===--- StmtObjC.h - Classes for representing ObjC statements --*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9/// \file
10/// Defines the Objective-C statement AST node classes.
11
12#ifndef LLVM_CLANG_AST_STMTOBJC_H
13#define LLVM_CLANG_AST_STMTOBJC_H
14
15#include "clang/AST/Stmt.h"
16#include "llvm/Support/Compiler.h"
17
18namespace clang {
19
20/// Represents Objective-C's collection statement.
21///
22/// This is represented as 'for (element 'in' collection-expression)' stmt.
23class ObjCForCollectionStmt : public Stmt {
24  enum { ELEMCOLLECTIONBODYEND_EXPR };
25  StmtSubExprs[END_EXPR]; // SubExprs[ELEM] is an expression or declstmt.
26  SourceLocation ForLoc;
27  SourceLocation RParenLoc;
28public:
29  ObjCForCollectionStmt(Stmt *ElemExpr *CollectStmt *Body,
30                        SourceLocation FCLSourceLocation RPL);
31  explicit ObjCForCollectionStmt(EmptyShell Empty) :
32    Stmt(ObjCForCollectionStmtClass, Empty) { }
33
34  Stmt *getElement() { return SubExprs[ELEM]; }
35  Expr *getCollection() {
36    return reinterpret_cast<Expr*>(SubExprs[COLLECTION]);
37  }
38  Stmt *getBody() { return SubExprs[BODY]; }
39
40  const Stmt *getElement() const { return SubExprs[ELEM]; }
41  const Expr *getCollection() const {
42    return reinterpret_cast<Expr*>(SubExprs[COLLECTION]);
43  }
44  const Stmt *getBody() const { return SubExprs[BODY]; }
45
46  void setElement(Stmt *S) { SubExprs[ELEM] = S; }
47  void setCollection(Expr *E) {
48    SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(E);
49  }
50  void setBody(Stmt *S) { SubExprs[BODY] = S; }
51
52  SourceLocation getForLoc() const { return ForLoc; }
53  void setForLoc(SourceLocation Loc) { ForLoc = Loc; }
54  SourceLocation getRParenLoc() const { return RParenLoc; }
55  void setRParenLoc(SourceLocation Loc) { RParenLoc = Loc; }
56
57  SourceLocation getBeginLoc() const LLVM_READONLY { return ForLoc; }
58  SourceLocation getEndLoc() const LLVM_READONLY {
59    return SubExprs[BODY]->getEndLoc();
60  }
61
62  static bool classof(const Stmt *T) {
63    return T->getStmtClass() == ObjCForCollectionStmtClass;
64  }
65
66  // Iterators
67  child_range children() {
68    return child_range(&SubExprs[0], &SubExprs[END_EXPR]);
69  }
70};
71
72/// Represents Objective-C's \@catch statement.
73class ObjCAtCatchStmt : public Stmt {
74private:
75  VarDecl *ExceptionDecl;
76  Stmt *Body;
77  SourceLocation AtCatchLocRParenLoc;
78
79public:
80  ObjCAtCatchStmt(SourceLocation atCatchLocSourceLocation rparenloc,
81                  VarDecl *catchVarDecl,
82                  Stmt *atCatchStmt)
83    : Stmt(ObjCAtCatchStmtClass), ExceptionDecl(catchVarDecl),
84    Body(atCatchStmt), AtCatchLoc(atCatchLoc), RParenLoc(rparenloc) { }
85
86  explicit ObjCAtCatchStmt(EmptyShell Empty) :
87    Stmt(ObjCAtCatchStmtClass, Empty) { }
88
89  const Stmt *getCatchBody() const { return Body; }
90  Stmt *getCatchBody() { return Body; }
91  void setCatchBody(Stmt *S) { Body = S; }
92
93  const VarDecl *getCatchParamDecl() const {
94    return ExceptionDecl;
95  }
96  VarDecl *getCatchParamDecl() {
97    return ExceptionDecl;
98  }
99  void setCatchParamDecl(VarDecl *D) { ExceptionDecl = D; }
100
101  SourceLocation getAtCatchLoc() const { return AtCatchLoc; }
102  void setAtCatchLoc(SourceLocation Loc) { AtCatchLoc = Loc; }
103  SourceLocation getRParenLoc() const { return RParenLoc; }
104  void setRParenLoc(SourceLocation Loc) { RParenLoc = Loc; }
105
106  SourceLocation getBeginLoc() const LLVM_READONLY { return AtCatchLoc; }
107  SourceLocation getEndLoc() const LLVM_READONLY { return Body->getEndLoc(); }
108
109  bool hasEllipsis() const { return getCatchParamDecl() == nullptr; }
110
111  static bool classof(const Stmt *T) {
112    return T->getStmtClass() == ObjCAtCatchStmtClass;
113  }
114
115  child_range children() { return child_range(&Body, &Body + 1); }
116};
117
118/// Represents Objective-C's \@finally statement
119class ObjCAtFinallyStmt : public Stmt {
120  SourceLocation AtFinallyLoc;
121  Stmt *AtFinallyStmt;
122
123public:
124  ObjCAtFinallyStmt(SourceLocation atFinallyLocStmt *atFinallyStmt)
125      : Stmt(ObjCAtFinallyStmtClass), AtFinallyLoc(atFinallyLoc),
126        AtFinallyStmt(atFinallyStmt) {}
127
128  explicit ObjCAtFinallyStmt(EmptyShell Empty) :
129    Stmt(ObjCAtFinallyStmtClass, Empty) { }
130
131  const Stmt *getFinallyBody() const { return AtFinallyStmt; }
132  Stmt *getFinallyBody() { return AtFinallyStmt; }
133  void setFinallyBody(Stmt *S) { AtFinallyStmt = S; }
134
135  SourceLocation getBeginLoc() const LLVM_READONLY { return AtFinallyLoc; }
136  SourceLocation getEndLoc() const LLVM_READONLY {
137    return AtFinallyStmt->getEndLoc();
138  }
139
140  SourceLocation getAtFinallyLoc() const { return AtFinallyLoc; }
141  void setAtFinallyLoc(SourceLocation Loc) { AtFinallyLoc = Loc; }
142
143  static bool classof(const Stmt *T) {
144    return T->getStmtClass() == ObjCAtFinallyStmtClass;
145  }
146
147  child_range children() {
148    return child_range(&AtFinallyStmt, &AtFinallyStmt+1);
149  }
150};
151
152/// Represents Objective-C's \@try ... \@catch ... \@finally statement.
153class ObjCAtTryStmt : public Stmt {
154private:
155  // The location of the @ in the \@try.
156  SourceLocation AtTryLoc;
157
158  // The number of catch blocks in this statement.
159  unsigned NumCatchStmts : 16;
160
161  // Whether this statement has a \@finally statement.
162  bool HasFinally : 1;
163
164  /// Retrieve the statements that are stored after this \@try statement.
165  ///
166  /// The order of the statements in memory follows the order in the source,
167  /// with the \@try body first, followed by the \@catch statements (if any)
168  /// and, finally, the \@finally (if it exists).
169  Stmt **getStmts() { return reinterpret_cast<Stmt **> (this + 1); }
170  const Stmtconst *getStmts() const {
171    return reinterpret_cast<const Stmt * const*> (this + 1);
172  }
173
174  ObjCAtTryStmt(SourceLocation atTryLocStmt *atTryStmt,
175                Stmt **CatchStmtsunsigned NumCatchStmts,
176                Stmt *atFinallyStmt);
177
178  explicit ObjCAtTryStmt(EmptyShell Emptyunsigned NumCatchStmts,
179                         bool HasFinally)
180    : Stmt(ObjCAtTryStmtClass, Empty), NumCatchStmts(NumCatchStmts),
181      HasFinally(HasFinally) { }
182
183public:
184  static ObjCAtTryStmt *Create(const ASTContext &Context,
185                               SourceLocation atTryLocStmt *atTryStmt,
186                               Stmt **CatchStmtsunsigned NumCatchStmts,
187                               Stmt *atFinallyStmt);
188  static ObjCAtTryStmt *CreateEmpty(const ASTContext &Context,
189                                    unsigned NumCatchStmtsbool HasFinally);
190
191  /// Retrieve the location of the @ in the \@try.
192  SourceLocation getAtTryLoc() const { return AtTryLoc; }
193  void setAtTryLoc(SourceLocation Loc) { AtTryLoc = Loc; }
194
195  /// Retrieve the \@try body.
196  const Stmt *getTryBody() const { return getStmts()[0]; }
197  Stmt *getTryBody() { return getStmts()[0]; }
198  void setTryBody(Stmt *S) { getStmts()[0] = S; }
199
200  /// Retrieve the number of \@catch statements in this try-catch-finally
201  /// block.
202  unsigned getNumCatchStmts() const { return NumCatchStmts; }
203
204  /// Retrieve a \@catch statement.
205  const ObjCAtCatchStmt *getCatchStmt(unsigned Iconst {
206     (0) . __assert_fail ("I < NumCatchStmts && \"Out-of-bounds @catch index\"", "/home/seafit/code_projects/clang_source/clang/include/clang/AST/StmtObjC.h", 206, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(I < NumCatchStmts && "Out-of-bounds @catch index");
207    return cast_or_null<ObjCAtCatchStmt>(getStmts()[I + 1]);
208  }
209
210  /// Retrieve a \@catch statement.
211  ObjCAtCatchStmt *getCatchStmt(unsigned I) {
212     (0) . __assert_fail ("I < NumCatchStmts && \"Out-of-bounds @catch index\"", "/home/seafit/code_projects/clang_source/clang/include/clang/AST/StmtObjC.h", 212, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(I < NumCatchStmts && "Out-of-bounds @catch index");
213    return cast_or_null<ObjCAtCatchStmt>(getStmts()[I + 1]);
214  }
215
216  /// Set a particular catch statement.
217  void setCatchStmt(unsigned IObjCAtCatchStmt *S) {
218     (0) . __assert_fail ("I < NumCatchStmts && \"Out-of-bounds @catch index\"", "/home/seafit/code_projects/clang_source/clang/include/clang/AST/StmtObjC.h", 218, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(I < NumCatchStmts && "Out-of-bounds @catch index");
219    getStmts()[I + 1] = S;
220  }
221
222  /// Retrieve the \@finally statement, if any.
223  const ObjCAtFinallyStmt *getFinallyStmt() const {
224    if (!HasFinally)
225      return nullptr;
226
227    return cast_or_null<ObjCAtFinallyStmt>(getStmts()[1 + NumCatchStmts]);
228  }
229  ObjCAtFinallyStmt *getFinallyStmt() {
230    if (!HasFinally)
231      return nullptr;
232
233    return cast_or_null<ObjCAtFinallyStmt>(getStmts()[1 + NumCatchStmts]);
234  }
235  void setFinallyStmt(Stmt *S) {
236     (0) . __assert_fail ("HasFinally && \"@try does not have a @finally slot!\"", "/home/seafit/code_projects/clang_source/clang/include/clang/AST/StmtObjC.h", 236, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(HasFinally && "@try does not have a @finally slot!");
237    getStmts()[1 + NumCatchStmts] = S;
238  }
239
240  SourceLocation getBeginLoc() const LLVM_READONLY { return AtTryLoc; }
241  SourceLocation getEndLoc() const LLVM_READONLY;
242
243  static bool classof(const Stmt *T) {
244    return T->getStmtClass() == ObjCAtTryStmtClass;
245  }
246
247  child_range children() {
248    return child_range(getStmts(),
249                       getStmts() + 1 + NumCatchStmts + HasFinally);
250  }
251};
252
253/// Represents Objective-C's \@synchronized statement.
254///
255/// Example:
256/// \code
257///   @synchronized (sem) {
258///     do-something;
259///   }
260/// \endcode
261class ObjCAtSynchronizedStmt : public Stmt {
262private:
263  SourceLocation AtSynchronizedLoc;
264  enum { SYNC_EXPRSYNC_BODYEND_EXPR };
265  StmtSubStmts[END_EXPR];
266
267public:
268  ObjCAtSynchronizedStmt(SourceLocation atSynchronizedLocStmt *synchExpr,
269                         Stmt *synchBody)
270  : Stmt(ObjCAtSynchronizedStmtClass) {
271    SubStmts[SYNC_EXPR] = synchExpr;
272    SubStmts[SYNC_BODY] = synchBody;
273    AtSynchronizedLoc = atSynchronizedLoc;
274  }
275  explicit ObjCAtSynchronizedStmt(EmptyShell Empty) :
276    Stmt(ObjCAtSynchronizedStmtClass, Empty) { }
277
278  SourceLocation getAtSynchronizedLoc() const { return AtSynchronizedLoc; }
279  void setAtSynchronizedLoc(SourceLocation Loc) { AtSynchronizedLoc = Loc; }
280
281  const CompoundStmt *getSynchBody() const {
282    return reinterpret_cast<CompoundStmt*>(SubStmts[SYNC_BODY]);
283  }
284  CompoundStmt *getSynchBody() {
285    return reinterpret_cast<CompoundStmt*>(SubStmts[SYNC_BODY]);
286  }
287  void setSynchBody(Stmt *S) { SubStmts[SYNC_BODY] = S; }
288
289  const Expr *getSynchExpr() const {
290    return reinterpret_cast<Expr*>(SubStmts[SYNC_EXPR]);
291  }
292  Expr *getSynchExpr() {
293    return reinterpret_cast<Expr*>(SubStmts[SYNC_EXPR]);
294  }
295  void setSynchExpr(Stmt *S) { SubStmts[SYNC_EXPR] = S; }
296
297  SourceLocation getBeginLoc() const LLVM_READONLY { return AtSynchronizedLoc; }
298  SourceLocation getEndLoc() const LLVM_READONLY {
299    return getSynchBody()->getEndLoc();
300  }
301
302  static bool classof(const Stmt *T) {
303    return T->getStmtClass() == ObjCAtSynchronizedStmtClass;
304  }
305
306  child_range children() {
307    return child_range(&SubStmts[0], &SubStmts[0]+END_EXPR);
308  }
309};
310
311/// Represents Objective-C's \@throw statement.
312class ObjCAtThrowStmt : public Stmt {
313  SourceLocation AtThrowLoc;
314  Stmt *Throw;
315
316public:
317  ObjCAtThrowStmt(SourceLocation atThrowLocStmt *throwExpr)
318  : Stmt(ObjCAtThrowStmtClass), Throw(throwExpr) {
319    AtThrowLoc = atThrowLoc;
320  }
321  explicit ObjCAtThrowStmt(EmptyShell Empty) :
322    Stmt(ObjCAtThrowStmtClass, Empty) { }
323
324  const Expr *getThrowExpr() const { return reinterpret_cast<Expr*>(Throw); }
325  Expr *getThrowExpr() { return reinterpret_cast<Expr*>(Throw); }
326  void setThrowExpr(Stmt *S) { Throw = S; }
327
328  SourceLocation getThrowLoc() const LLVM_READONLY { return AtThrowLoc; }
329  void setThrowLoc(SourceLocation Loc) { AtThrowLoc = Loc; }
330
331  SourceLocation getBeginLoc() const LLVM_READONLY { return AtThrowLoc; }
332  SourceLocation getEndLoc() const LLVM_READONLY {
333    return Throw ? Throw->getEndLoc() : AtThrowLoc;
334  }
335
336  static bool classof(const Stmt *T) {
337    return T->getStmtClass() == ObjCAtThrowStmtClass;
338  }
339
340  child_range children() { return child_range(&Throw, &Throw+1); }
341};
342
343/// Represents Objective-C's \@autoreleasepool Statement
344class ObjCAutoreleasePoolStmt : public Stmt {
345  SourceLocation AtLoc;
346  Stmt *SubStmt;
347
348public:
349  ObjCAutoreleasePoolStmt(SourceLocation atLocStmt *subStmt)
350      : Stmt(ObjCAutoreleasePoolStmtClass), AtLoc(atLoc), SubStmt(subStmt) {}
351
352  explicit ObjCAutoreleasePoolStmt(EmptyShell Empty) :
353    Stmt(ObjCAutoreleasePoolStmtClass, Empty) { }
354
355  const Stmt *getSubStmt() const { return SubStmt; }
356  Stmt *getSubStmt() { return SubStmt; }
357  void setSubStmt(Stmt *S) { SubStmt = S; }
358
359  SourceLocation getBeginLoc() const LLVM_READONLY { return AtLoc; }
360  SourceLocation getEndLoc() const LLVM_READONLY {
361    return SubStmt->getEndLoc();
362  }
363
364  SourceLocation getAtLoc() const { return AtLoc; }
365  void setAtLoc(SourceLocation Loc) { AtLoc = Loc; }
366
367  static bool classof(const Stmt *T) {
368    return T->getStmtClass() == ObjCAutoreleasePoolStmtClass;
369  }
370
371  child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
372};
373
374}  // end namespace clang
375
376#endif
377
clang::ObjCForCollectionStmt::SubExprs
clang::ObjCForCollectionStmt::ForLoc
clang::ObjCForCollectionStmt::RParenLoc
clang::ObjCForCollectionStmt::getElement
clang::ObjCForCollectionStmt::getCollection
clang::ObjCForCollectionStmt::getBody
clang::ObjCForCollectionStmt::getElement
clang::ObjCForCollectionStmt::getCollection
clang::ObjCForCollectionStmt::getBody
clang::ObjCForCollectionStmt::setElement
clang::ObjCForCollectionStmt::setCollection
clang::ObjCForCollectionStmt::setBody
clang::ObjCForCollectionStmt::getForLoc
clang::ObjCForCollectionStmt::setForLoc
clang::ObjCForCollectionStmt::getRParenLoc
clang::ObjCForCollectionStmt::setRParenLoc
clang::ObjCForCollectionStmt::getBeginLoc
clang::ObjCAtCatchStmt::ExceptionDecl
clang::ObjCAtCatchStmt::Body
clang::ObjCAtCatchStmt::AtCatchLoc
clang::ObjCAtCatchStmt::RParenLoc
clang::ObjCAtCatchStmt::getCatchBody
clang::ObjCAtCatchStmt::getCatchBody
clang::ObjCAtCatchStmt::setCatchBody
clang::ObjCAtCatchStmt::getCatchParamDecl
clang::ObjCAtCatchStmt::getCatchParamDecl
clang::ObjCAtCatchStmt::setCatchParamDecl
clang::ObjCAtCatchStmt::getAtCatchLoc
clang::ObjCAtCatchStmt::setAtCatchLoc
clang::ObjCAtCatchStmt::getRParenLoc
clang::ObjCAtCatchStmt::setRParenLoc
clang::ObjCAtCatchStmt::getBeginLoc
clang::ObjCAtFinallyStmt::AtFinallyLoc
clang::ObjCAtFinallyStmt::AtFinallyStmt
clang::ObjCAtFinallyStmt::getFinallyBody
clang::ObjCAtFinallyStmt::getFinallyBody
clang::ObjCAtFinallyStmt::setFinallyBody
clang::ObjCAtFinallyStmt::getBeginLoc
clang::ObjCAtTryStmt::AtTryLoc
clang::ObjCAtTryStmt::NumCatchStmts
clang::ObjCAtTryStmt::HasFinally
clang::ObjCAtTryStmt::getStmts
clang::ObjCAtTryStmt::getStmts
clang::ObjCAtTryStmt::Create
clang::ObjCAtTryStmt::CreateEmpty
clang::ObjCAtTryStmt::getAtTryLoc
clang::ObjCAtTryStmt::setAtTryLoc
clang::ObjCAtTryStmt::getTryBody
clang::ObjCAtTryStmt::getTryBody
clang::ObjCAtTryStmt::setTryBody
clang::ObjCAtTryStmt::getNumCatchStmts
clang::ObjCAtTryStmt::getCatchStmt
clang::ObjCAtTryStmt::getCatchStmt
clang::ObjCAtTryStmt::setCatchStmt
clang::ObjCAtTryStmt::getFinallyStmt
clang::ObjCAtTryStmt::getFinallyStmt
clang::ObjCAtTryStmt::setFinallyStmt
clang::ObjCAtTryStmt::getBeginLoc
clang::ObjCAtTryStmt::classof
clang::ObjCAtTryStmt::children
clang::ObjCAtSynchronizedStmt::AtSynchronizedLoc
clang::ObjCAtSynchronizedStmt::SubStmts
clang::ObjCAtSynchronizedStmt::getAtSynchronizedLoc
clang::ObjCAtSynchronizedStmt::setAtSynchronizedLoc
clang::ObjCAtSynchronizedStmt::getSynchBody
clang::ObjCAtSynchronizedStmt::getSynchBody
clang::ObjCAtSynchronizedStmt::setSynchBody
clang::ObjCAtSynchronizedStmt::getSynchExpr
clang::ObjCAtSynchronizedStmt::getSynchExpr
clang::ObjCAtSynchronizedStmt::setSynchExpr
clang::ObjCAtSynchronizedStmt::getBeginLoc
clang::ObjCAtThrowStmt::AtThrowLoc
clang::ObjCAtThrowStmt::Throw
clang::ObjCAtThrowStmt::getThrowExpr
clang::ObjCAtThrowStmt::getThrowExpr
clang::ObjCAtThrowStmt::setThrowExpr
clang::ObjCAtThrowStmt::getThrowLoc
clang::ObjCAutoreleasePoolStmt::AtLoc
clang::ObjCAutoreleasePoolStmt::SubStmt
clang::ObjCAutoreleasePoolStmt::getSubStmt
clang::ObjCAutoreleasePoolStmt::getSubStmt
clang::ObjCAutoreleasePoolStmt::setSubStmt
clang::ObjCAutoreleasePoolStmt::getBeginLoc