1 //===--- StmtObjC.h - Classes for representing ObjC 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 /// \file 11 /// \brief Defines the Objective-C statement AST node classes. 12 13 #ifndef LLVM_CLANG_AST_STMTOBJC_H 14 #define LLVM_CLANG_AST_STMTOBJC_H 15 16 #include "clang/AST/Stmt.h" 17 #include "llvm/Support/Compiler.h" 18 19 namespace clang { 20 21 /// \brief Represents Objective-C's collection statement. 22 /// 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; 29 public: 30 ObjCForCollectionStmt(Stmt *Elem, Expr *Collect, Stmt *Body, 31 SourceLocation FCL, SourceLocation RPL); 32 explicit ObjCForCollectionStmt(EmptyShell Empty) : 33 Stmt(ObjCForCollectionStmtClass, Empty) { } 34 35 Stmt *getElement() { return SubExprs[ELEM]; } 36 Expr *getCollection() { 37 return reinterpret_cast<Expr*>(SubExprs[COLLECTION]); 38 } 39 Stmt *getBody() { return SubExprs[BODY]; } 40 41 const Stmt *getElement() const { return SubExprs[ELEM]; } 42 const Expr *getCollection() const { 43 return reinterpret_cast<Expr*>(SubExprs[COLLECTION]); 44 } 45 const Stmt *getBody() const { return SubExprs[BODY]; } 46 47 void setElement(Stmt *S) { SubExprs[ELEM] = S; } 48 void setCollection(Expr *E) { 49 SubExprs[COLLECTION] = reinterpret_cast<Stmt*>(E); 50 } 51 void setBody(Stmt *S) { SubExprs[BODY] = S; } 52 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; } 57 58 SourceLocation getLocStart() const LLVM_READONLY { return ForLoc; } 59 SourceLocation getLocEnd() const LLVM_READONLY { 60 return SubExprs[BODY]->getLocEnd(); 61 } 62 63 static bool classof(const Stmt *T) { 64 return T->getStmtClass() == ObjCForCollectionStmtClass; 65 } 66 67 // Iterators 68 child_range children() { 69 return child_range(&SubExprs[0], &SubExprs[END_EXPR]); 70 } 71 }; 72 73 /// \brief Represents Objective-C's \@catch statement. 74 class ObjCAtCatchStmt : public Stmt { 75 private: 76 VarDecl *ExceptionDecl; 77 Stmt *Body; 78 SourceLocation AtCatchLoc, RParenLoc; 79 80 public: 81 ObjCAtCatchStmt(SourceLocation atCatchLoc, SourceLocation rparenloc, 82 VarDecl *catchVarDecl, 83 Stmt *atCatchStmt) 84 : Stmt(ObjCAtCatchStmtClass), ExceptionDecl(catchVarDecl), 85 Body(atCatchStmt), AtCatchLoc(atCatchLoc), RParenLoc(rparenloc) { } 86 87 explicit ObjCAtCatchStmt(EmptyShell Empty) : 88 Stmt(ObjCAtCatchStmtClass, Empty) { } 89 90 const Stmt *getCatchBody() const { return Body; } 91 Stmt *getCatchBody() { return Body; } 92 void setCatchBody(Stmt *S) { Body = S; } 93 94 const VarDecl *getCatchParamDecl() const { 95 return ExceptionDecl; 96 } 97 VarDecl *getCatchParamDecl() { 98 return ExceptionDecl; 99 } 100 void setCatchParamDecl(VarDecl *D) { ExceptionDecl = D; } 101 102 SourceLocation getAtCatchLoc() const { return AtCatchLoc; } 103 void setAtCatchLoc(SourceLocation Loc) { AtCatchLoc = Loc; } 104 SourceLocation getRParenLoc() const { return RParenLoc; } 105 void setRParenLoc(SourceLocation Loc) { RParenLoc = Loc; } 106 107 SourceLocation getLocStart() const LLVM_READONLY { return AtCatchLoc; } 108 SourceLocation getLocEnd() const LLVM_READONLY { return Body->getLocEnd(); } 109 110 bool hasEllipsis() const { return getCatchParamDecl() == 0; } 111 112 static bool classof(const Stmt *T) { 113 return T->getStmtClass() == ObjCAtCatchStmtClass; 114 } 115 116 child_range children() { return child_range(&Body, &Body + 1); } 117 }; 118 119 /// \brief Represents Objective-C's \@finally statement 120 class ObjCAtFinallyStmt : public Stmt { 121 Stmt *AtFinallyStmt; 122 SourceLocation AtFinallyLoc; 123 public: 124 ObjCAtFinallyStmt(SourceLocation atFinallyLoc, Stmt *atFinallyStmt) 125 : Stmt(ObjCAtFinallyStmtClass), 126 AtFinallyStmt(atFinallyStmt), AtFinallyLoc(atFinallyLoc) {} 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 getLocStart() const LLVM_READONLY { return AtFinallyLoc; } 136 SourceLocation getLocEnd() const LLVM_READONLY { 137 return AtFinallyStmt->getLocEnd(); 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 /// \brief Represents Objective-C's \@try ... \@catch ... \@finally statement. 153 class ObjCAtTryStmt : public Stmt { 154 private: 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 /// \brief 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 Stmt* const *getStmts() const { 171 return reinterpret_cast<const Stmt * const*> (this + 1); 172 } 173 174 ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt, 175 Stmt **CatchStmts, unsigned NumCatchStmts, 176 Stmt *atFinallyStmt); 177 178 explicit ObjCAtTryStmt(EmptyShell Empty, unsigned NumCatchStmts, 179 bool HasFinally) 180 : Stmt(ObjCAtTryStmtClass, Empty), NumCatchStmts(NumCatchStmts), 181 HasFinally(HasFinally) { } 182 183 public: 184 static ObjCAtTryStmt *Create(ASTContext &Context, SourceLocation atTryLoc, 185 Stmt *atTryStmt, 186 Stmt **CatchStmts, unsigned NumCatchStmts, 187 Stmt *atFinallyStmt); 188 static ObjCAtTryStmt *CreateEmpty(ASTContext &Context, 189 unsigned NumCatchStmts, 190 bool HasFinally); 191 192 /// \brief Retrieve the location of the @ in the \@try. 193 SourceLocation getAtTryLoc() const { return AtTryLoc; } 194 void setAtTryLoc(SourceLocation Loc) { AtTryLoc = Loc; } 195 196 /// \brief Retrieve the \@try body. 197 const Stmt *getTryBody() const { return getStmts()[0]; } 198 Stmt *getTryBody() { return getStmts()[0]; } 199 void setTryBody(Stmt *S) { getStmts()[0] = S; } 200 201 /// \brief Retrieve the number of \@catch statements in this try-catch-finally 202 /// block. 203 unsigned getNumCatchStmts() const { return NumCatchStmts; } 204 205 /// \brief Retrieve a \@catch statement. 206 const ObjCAtCatchStmt *getCatchStmt(unsigned I) const { 207 assert(I < NumCatchStmts && "Out-of-bounds @catch index"); 208 return cast_or_null<ObjCAtCatchStmt>(getStmts()[I + 1]); 209 } 210 211 /// \brief Retrieve a \@catch statement. 212 ObjCAtCatchStmt *getCatchStmt(unsigned I) { 213 assert(I < NumCatchStmts && "Out-of-bounds @catch index"); 214 return cast_or_null<ObjCAtCatchStmt>(getStmts()[I + 1]); 215 } 216 217 /// \brief Set a particular catch statement. 218 void setCatchStmt(unsigned I, ObjCAtCatchStmt *S) { 219 assert(I < NumCatchStmts && "Out-of-bounds @catch index"); 220 getStmts()[I + 1] = S; 221 } 222 223 /// \brief Retrieve the \@finally statement, if any. 224 const ObjCAtFinallyStmt *getFinallyStmt() const { 225 if (!HasFinally) 226 return 0; 227 228 return cast_or_null<ObjCAtFinallyStmt>(getStmts()[1 + NumCatchStmts]); 229 } 230 ObjCAtFinallyStmt *getFinallyStmt() { 231 if (!HasFinally) 232 return 0; 233 234 return cast_or_null<ObjCAtFinallyStmt>(getStmts()[1 + NumCatchStmts]); 235 } 236 void setFinallyStmt(Stmt *S) { 237 assert(HasFinally && "@try does not have a @finally slot!"); 238 getStmts()[1 + NumCatchStmts] = S; 239 } 240 241 SourceLocation getLocStart() const LLVM_READONLY { return AtTryLoc; } 242 SourceLocation getLocEnd() const LLVM_READONLY; 243 244 static bool classof(const Stmt *T) { 245 return T->getStmtClass() == ObjCAtTryStmtClass; 246 } 247 248 child_range children() { 249 return child_range(getStmts(), 250 getStmts() + 1 + NumCatchStmts + HasFinally); 251 } 252 }; 253 254 /// \brief Represents Objective-C's \@synchronized statement. 255 /// 256 /// Example: 257 /// \code 258 /// @synchronized (sem) { 259 /// do-something; 260 /// } 261 /// \endcode 262 class ObjCAtSynchronizedStmt : public Stmt { 263 private: 264 enum { SYNC_EXPR, SYNC_BODY, END_EXPR }; 265 Stmt* SubStmts[END_EXPR]; 266 SourceLocation AtSynchronizedLoc; 267 268 public: 269 ObjCAtSynchronizedStmt(SourceLocation atSynchronizedLoc, Stmt *synchExpr, 270 Stmt *synchBody) 271 : Stmt(ObjCAtSynchronizedStmtClass) { 272 SubStmts[SYNC_EXPR] = synchExpr; 273 SubStmts[SYNC_BODY] = synchBody; 274 AtSynchronizedLoc = atSynchronizedLoc; 275 } 276 explicit ObjCAtSynchronizedStmt(EmptyShell Empty) : 277 Stmt(ObjCAtSynchronizedStmtClass, Empty) { } 278 279 SourceLocation getAtSynchronizedLoc() const { return AtSynchronizedLoc; } 280 void setAtSynchronizedLoc(SourceLocation Loc) { AtSynchronizedLoc = Loc; } 281 282 const CompoundStmt *getSynchBody() const { 283 return reinterpret_cast<CompoundStmt*>(SubStmts[SYNC_BODY]); 284 } 285 CompoundStmt *getSynchBody() { 286 return reinterpret_cast<CompoundStmt*>(SubStmts[SYNC_BODY]); 287 } 288 void setSynchBody(Stmt *S) { SubStmts[SYNC_BODY] = S; } 289 290 const Expr *getSynchExpr() const { 291 return reinterpret_cast<Expr*>(SubStmts[SYNC_EXPR]); 292 } 293 Expr *getSynchExpr() { 294 return reinterpret_cast<Expr*>(SubStmts[SYNC_EXPR]); 295 } 296 void setSynchExpr(Stmt *S) { SubStmts[SYNC_EXPR] = S; } 297 298 SourceLocation getLocStart() const LLVM_READONLY { return AtSynchronizedLoc; } 299 SourceLocation getLocEnd() const LLVM_READONLY { 300 return getSynchBody()->getLocEnd(); 301 } 302 303 static bool classof(const Stmt *T) { 304 return T->getStmtClass() == ObjCAtSynchronizedStmtClass; 305 } 306 307 child_range children() { 308 return child_range(&SubStmts[0], &SubStmts[0]+END_EXPR); 309 } 310 }; 311 312 /// \brief Represents Objective-C's \@throw statement. 313 class ObjCAtThrowStmt : public Stmt { 314 Stmt *Throw; 315 SourceLocation AtThrowLoc; 316 public: 317 ObjCAtThrowStmt(SourceLocation atThrowLoc, Stmt *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() { return AtThrowLoc; } 329 void setThrowLoc(SourceLocation Loc) { AtThrowLoc = Loc; } 330 331 SourceLocation getLocStart() const LLVM_READONLY { return AtThrowLoc; } 332 SourceLocation getLocEnd() const LLVM_READONLY { 333 return Throw ? Throw->getLocEnd() : 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 /// \brief Represents Objective-C's \@autoreleasepool Statement 344 class ObjCAutoreleasePoolStmt : public Stmt { 345 Stmt *SubStmt; 346 SourceLocation AtLoc; 347 public: 348 ObjCAutoreleasePoolStmt(SourceLocation atLoc, 349 Stmt *subStmt) 350 : Stmt(ObjCAutoreleasePoolStmtClass), 351 SubStmt(subStmt), AtLoc(atLoc) {} 352 353 explicit ObjCAutoreleasePoolStmt(EmptyShell Empty) : 354 Stmt(ObjCAutoreleasePoolStmtClass, Empty) { } 355 356 const Stmt *getSubStmt() const { return SubStmt; } 357 Stmt *getSubStmt() { return SubStmt; } 358 void setSubStmt(Stmt *S) { SubStmt = S; } 359 360 SourceLocation getLocStart() const LLVM_READONLY { return AtLoc; } 361 SourceLocation getLocEnd() const LLVM_READONLY { return SubStmt->getLocEnd();} 362 363 SourceLocation getAtLoc() const { return AtLoc; } 364 void setAtLoc(SourceLocation Loc) { AtLoc = Loc; } 365 366 static bool classof(const Stmt *T) { 367 return T->getStmtClass() == ObjCAutoreleasePoolStmtClass; 368 } 369 370 child_range children() { return child_range(&SubStmt, &SubStmt + 1); } 371 }; 372 373 } // end namespace clang 374 375 #endif 376