1 //===--- StmtObjC.h - Classes for representing ObjC 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 //===----------------------------------------------------------------------===//
11 /// \brief Defines the Objective-C statement AST node classes.
13 #ifndef LLVM_CLANG_AST_STMTOBJC_H
14 #define LLVM_CLANG_AST_STMTOBJC_H
16 #include "clang/AST/Stmt.h"
17 #include "llvm/Support/Compiler.h"
21 /// \brief Represents Objective-C's collection statement.
23 /// This is represented as 'for (element 'in' collection-expression)' stmt.
24 class ObjCForCollectionStmt : public Stmt {
25 enum { ELEM, COLLECTION, BODY, END_EXPR };
26 Stmt* SubExprs[END_EXPR]; // SubExprs[ELEM] is an expression or declstmt.
27 SourceLocation ForLoc;
28 SourceLocation RParenLoc;
30 ObjCForCollectionStmt(Stmt *Elem, Expr *Collect, Stmt *Body,
31 SourceLocation FCL, SourceLocation RPL);
32 explicit ObjCForCollectionStmt(EmptyShell Empty) :
33 Stmt(ObjCForCollectionStmtClass, Empty) { }
35 Stmt *getElement() { return SubExprs[ELEM]; }
36 Expr *getCollection() {
37 return reinterpret_cast<Expr*>(SubExprs[COLLECTION]);
39 Stmt *getBody() { return SubExprs[BODY]; }
41 const Stmt *getElement() const { return SubExprs[ELEM]; }
42 const Expr *getCollection() const {
43 return reinterpret_cast<Expr*>(SubExprs[COLLECTION]);
45 const Stmt *getBody() const { return SubExprs[BODY]; }
47 void setElement(Stmt *S) { SubExprs[ELEM] = S; }
48 void setCollection(Expr *E) {
49 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(E);
51 void setBody(Stmt *S) { SubExprs[BODY] = S; }
53 SourceLocation getForLoc() const { return ForLoc; }
54 void setForLoc(SourceLocation Loc) { ForLoc = Loc; }
55 SourceLocation getRParenLoc() const { return RParenLoc; }
56 void setRParenLoc(SourceLocation Loc) { RParenLoc = Loc; }
58 SourceRange getSourceRange() const LLVM_READONLY {
59 return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd());
61 static bool classof(const Stmt *T) {
62 return T->getStmtClass() == ObjCForCollectionStmtClass;
66 child_range children() {
67 return child_range(&SubExprs[0], &SubExprs[END_EXPR]);
71 /// \brief Represents Objective-C's \@catch statement.
72 class ObjCAtCatchStmt : public Stmt {
74 VarDecl *ExceptionDecl;
76 SourceLocation AtCatchLoc, RParenLoc;
79 ObjCAtCatchStmt(SourceLocation atCatchLoc, SourceLocation rparenloc,
80 VarDecl *catchVarDecl,
82 : Stmt(ObjCAtCatchStmtClass), ExceptionDecl(catchVarDecl),
83 Body(atCatchStmt), AtCatchLoc(atCatchLoc), RParenLoc(rparenloc) { }
85 explicit ObjCAtCatchStmt(EmptyShell Empty) :
86 Stmt(ObjCAtCatchStmtClass, Empty) { }
88 const Stmt *getCatchBody() const { return Body; }
89 Stmt *getCatchBody() { return Body; }
90 void setCatchBody(Stmt *S) { Body = S; }
92 const VarDecl *getCatchParamDecl() const {
95 VarDecl *getCatchParamDecl() {
98 void setCatchParamDecl(VarDecl *D) { ExceptionDecl = D; }
100 SourceLocation getAtCatchLoc() const { return AtCatchLoc; }
101 void setAtCatchLoc(SourceLocation Loc) { AtCatchLoc = Loc; }
102 SourceLocation getRParenLoc() const { return RParenLoc; }
103 void setRParenLoc(SourceLocation Loc) { RParenLoc = Loc; }
105 SourceRange getSourceRange() const LLVM_READONLY {
106 return SourceRange(AtCatchLoc, Body->getLocEnd());
109 bool hasEllipsis() const { return getCatchParamDecl() == 0; }
111 static bool classof(const Stmt *T) {
112 return T->getStmtClass() == ObjCAtCatchStmtClass;
115 child_range children() { return child_range(&Body, &Body + 1); }
118 /// \brief Represents Objective-C's \@finally statement
119 class ObjCAtFinallyStmt : public Stmt {
121 SourceLocation AtFinallyLoc;
123 ObjCAtFinallyStmt(SourceLocation atFinallyLoc, Stmt *atFinallyStmt)
124 : Stmt(ObjCAtFinallyStmtClass),
125 AtFinallyStmt(atFinallyStmt), AtFinallyLoc(atFinallyLoc) {}
127 explicit ObjCAtFinallyStmt(EmptyShell Empty) :
128 Stmt(ObjCAtFinallyStmtClass, Empty) { }
130 const Stmt *getFinallyBody() const { return AtFinallyStmt; }
131 Stmt *getFinallyBody() { return AtFinallyStmt; }
132 void setFinallyBody(Stmt *S) { AtFinallyStmt = S; }
134 SourceRange getSourceRange() const LLVM_READONLY {
135 return SourceRange(AtFinallyLoc, AtFinallyStmt->getLocEnd());
138 SourceLocation getAtFinallyLoc() const { return AtFinallyLoc; }
139 void setAtFinallyLoc(SourceLocation Loc) { AtFinallyLoc = Loc; }
141 static bool classof(const Stmt *T) {
142 return T->getStmtClass() == ObjCAtFinallyStmtClass;
145 child_range children() {
146 return child_range(&AtFinallyStmt, &AtFinallyStmt+1);
150 /// \brief Represents Objective-C's \@try ... \@catch ... \@finally statement.
151 class ObjCAtTryStmt : public Stmt {
153 // The location of the @ in the \@try.
154 SourceLocation AtTryLoc;
156 // The number of catch blocks in this statement.
157 unsigned NumCatchStmts : 16;
159 // Whether this statement has a \@finally statement.
162 /// \brief Retrieve the statements that are stored after this \@try statement.
164 /// The order of the statements in memory follows the order in the source,
165 /// with the \@try body first, followed by the \@catch statements (if any)
166 /// and, finally, the \@finally (if it exists).
167 Stmt **getStmts() { return reinterpret_cast<Stmt **> (this + 1); }
168 const Stmt* const *getStmts() const {
169 return reinterpret_cast<const Stmt * const*> (this + 1);
172 ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
173 Stmt **CatchStmts, unsigned NumCatchStmts,
174 Stmt *atFinallyStmt);
176 explicit ObjCAtTryStmt(EmptyShell Empty, unsigned NumCatchStmts,
178 : Stmt(ObjCAtTryStmtClass, Empty), NumCatchStmts(NumCatchStmts),
179 HasFinally(HasFinally) { }
182 static ObjCAtTryStmt *Create(ASTContext &Context, SourceLocation atTryLoc,
184 Stmt **CatchStmts, unsigned NumCatchStmts,
185 Stmt *atFinallyStmt);
186 static ObjCAtTryStmt *CreateEmpty(ASTContext &Context,
187 unsigned NumCatchStmts,
190 /// \brief Retrieve the location of the @ in the \@try.
191 SourceLocation getAtTryLoc() const { return AtTryLoc; }
192 void setAtTryLoc(SourceLocation Loc) { AtTryLoc = Loc; }
194 /// \brief Retrieve the \@try body.
195 const Stmt *getTryBody() const { return getStmts()[0]; }
196 Stmt *getTryBody() { return getStmts()[0]; }
197 void setTryBody(Stmt *S) { getStmts()[0] = S; }
199 /// \brief Retrieve the number of \@catch statements in this try-catch-finally
201 unsigned getNumCatchStmts() const { return NumCatchStmts; }
203 /// \brief Retrieve a \@catch statement.
204 const ObjCAtCatchStmt *getCatchStmt(unsigned I) const {
205 assert(I < NumCatchStmts && "Out-of-bounds @catch index");
206 return cast_or_null<ObjCAtCatchStmt>(getStmts()[I + 1]);
209 /// \brief Retrieve a \@catch statement.
210 ObjCAtCatchStmt *getCatchStmt(unsigned I) {
211 assert(I < NumCatchStmts && "Out-of-bounds @catch index");
212 return cast_or_null<ObjCAtCatchStmt>(getStmts()[I + 1]);
215 /// \brief Set a particular catch statement.
216 void setCatchStmt(unsigned I, ObjCAtCatchStmt *S) {
217 assert(I < NumCatchStmts && "Out-of-bounds @catch index");
218 getStmts()[I + 1] = S;
221 /// \brief Retrieve the \@finally statement, if any.
222 const ObjCAtFinallyStmt *getFinallyStmt() const {
226 return cast_or_null<ObjCAtFinallyStmt>(getStmts()[1 + NumCatchStmts]);
228 ObjCAtFinallyStmt *getFinallyStmt() {
232 return cast_or_null<ObjCAtFinallyStmt>(getStmts()[1 + NumCatchStmts]);
234 void setFinallyStmt(Stmt *S) {
235 assert(HasFinally && "@try does not have a @finally slot!");
236 getStmts()[1 + NumCatchStmts] = S;
239 SourceRange getSourceRange() const LLVM_READONLY;
241 static bool classof(const Stmt *T) {
242 return T->getStmtClass() == ObjCAtTryStmtClass;
245 child_range children() {
246 return child_range(getStmts(),
247 getStmts() + 1 + NumCatchStmts + HasFinally);
251 /// \brief Represents Objective-C's \@synchronized statement.
255 /// @synchronized (sem) {
259 class ObjCAtSynchronizedStmt : public Stmt {
261 enum { SYNC_EXPR, SYNC_BODY, END_EXPR };
262 Stmt* SubStmts[END_EXPR];
263 SourceLocation AtSynchronizedLoc;
266 ObjCAtSynchronizedStmt(SourceLocation atSynchronizedLoc, Stmt *synchExpr,
268 : Stmt(ObjCAtSynchronizedStmtClass) {
269 SubStmts[SYNC_EXPR] = synchExpr;
270 SubStmts[SYNC_BODY] = synchBody;
271 AtSynchronizedLoc = atSynchronizedLoc;
273 explicit ObjCAtSynchronizedStmt(EmptyShell Empty) :
274 Stmt(ObjCAtSynchronizedStmtClass, Empty) { }
276 SourceLocation getAtSynchronizedLoc() const { return AtSynchronizedLoc; }
277 void setAtSynchronizedLoc(SourceLocation Loc) { AtSynchronizedLoc = Loc; }
279 const CompoundStmt *getSynchBody() const {
280 return reinterpret_cast<CompoundStmt*>(SubStmts[SYNC_BODY]);
282 CompoundStmt *getSynchBody() {
283 return reinterpret_cast<CompoundStmt*>(SubStmts[SYNC_BODY]);
285 void setSynchBody(Stmt *S) { SubStmts[SYNC_BODY] = S; }
287 const Expr *getSynchExpr() const {
288 return reinterpret_cast<Expr*>(SubStmts[SYNC_EXPR]);
290 Expr *getSynchExpr() {
291 return reinterpret_cast<Expr*>(SubStmts[SYNC_EXPR]);
293 void setSynchExpr(Stmt *S) { SubStmts[SYNC_EXPR] = S; }
295 SourceRange getSourceRange() const LLVM_READONLY {
296 return SourceRange(AtSynchronizedLoc, getSynchBody()->getLocEnd());
299 static bool classof(const Stmt *T) {
300 return T->getStmtClass() == ObjCAtSynchronizedStmtClass;
303 child_range children() {
304 return child_range(&SubStmts[0], &SubStmts[0]+END_EXPR);
308 /// \brief Represents Objective-C's \@throw statement.
309 class ObjCAtThrowStmt : public Stmt {
311 SourceLocation AtThrowLoc;
313 ObjCAtThrowStmt(SourceLocation atThrowLoc, Stmt *throwExpr)
314 : Stmt(ObjCAtThrowStmtClass), Throw(throwExpr) {
315 AtThrowLoc = atThrowLoc;
317 explicit ObjCAtThrowStmt(EmptyShell Empty) :
318 Stmt(ObjCAtThrowStmtClass, Empty) { }
320 const Expr *getThrowExpr() const { return reinterpret_cast<Expr*>(Throw); }
321 Expr *getThrowExpr() { return reinterpret_cast<Expr*>(Throw); }
322 void setThrowExpr(Stmt *S) { Throw = S; }
324 SourceLocation getThrowLoc() { return AtThrowLoc; }
325 void setThrowLoc(SourceLocation Loc) { AtThrowLoc = Loc; }
327 SourceRange getSourceRange() const LLVM_READONLY {
329 return SourceRange(AtThrowLoc, Throw->getLocEnd());
331 return SourceRange(AtThrowLoc);
334 static bool classof(const Stmt *T) {
335 return T->getStmtClass() == ObjCAtThrowStmtClass;
338 child_range children() { return child_range(&Throw, &Throw+1); }
341 /// \brief Represents Objective-C's \@autoreleasepool Statement
342 class ObjCAutoreleasePoolStmt : public Stmt {
344 SourceLocation AtLoc;
346 ObjCAutoreleasePoolStmt(SourceLocation atLoc,
348 : Stmt(ObjCAutoreleasePoolStmtClass),
349 SubStmt(subStmt), AtLoc(atLoc) {}
351 explicit ObjCAutoreleasePoolStmt(EmptyShell Empty) :
352 Stmt(ObjCAutoreleasePoolStmtClass, Empty) { }
354 const Stmt *getSubStmt() const { return SubStmt; }
355 Stmt *getSubStmt() { return SubStmt; }
356 void setSubStmt(Stmt *S) { SubStmt = S; }
358 SourceRange getSourceRange() const LLVM_READONLY {
359 return SourceRange(AtLoc, SubStmt->getLocEnd());
362 SourceLocation getAtLoc() const { return AtLoc; }
363 void setAtLoc(SourceLocation Loc) { AtLoc = Loc; }
365 static bool classof(const Stmt *T) {
366 return T->getStmtClass() == ObjCAutoreleasePoolStmtClass;
369 child_range children() { return child_range(&SubStmt, &SubStmt + 1); }
372 } // end namespace clang