StmtObjC.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //===--- StmtObjC.cpp - Classes for representing ObjC statements ---------===//
  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. // This file implements the subclesses of Stmt class declared in StmtObjC.h
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/AST/StmtObjC.h"
  13. #include "clang/AST/Expr.h"
  14. #include "clang/AST/ASTContext.h"
  15. using namespace clang;
  16. ObjCForCollectionStmt::ObjCForCollectionStmt(Stmt *Elem, Expr *Collect,
  17. Stmt *Body, SourceLocation FCL,
  18. SourceLocation RPL)
  19. : Stmt(ObjCForCollectionStmtClass) {
  20. SubExprs[ELEM] = Elem;
  21. SubExprs[COLLECTION] = Collect;
  22. SubExprs[BODY] = Body;
  23. ForLoc = FCL;
  24. RParenLoc = RPL;
  25. }
  26. ObjCAtTryStmt::ObjCAtTryStmt(SourceLocation atTryLoc, Stmt *atTryStmt,
  27. Stmt **CatchStmts, unsigned NumCatchStmts,
  28. Stmt *atFinallyStmt)
  29. : Stmt(ObjCAtTryStmtClass), AtTryLoc(atTryLoc),
  30. NumCatchStmts(NumCatchStmts), HasFinally(atFinallyStmt != nullptr) {
  31. Stmt **Stmts = getStmts();
  32. Stmts[0] = atTryStmt;
  33. for (unsigned I = 0; I != NumCatchStmts; ++I)
  34. Stmts[I + 1] = CatchStmts[I];
  35. if (HasFinally)
  36. Stmts[NumCatchStmts + 1] = atFinallyStmt;
  37. }
  38. ObjCAtTryStmt *ObjCAtTryStmt::Create(const ASTContext &Context,
  39. SourceLocation atTryLoc, Stmt *atTryStmt,
  40. Stmt **CatchStmts, unsigned NumCatchStmts,
  41. Stmt *atFinallyStmt) {
  42. size_t Size =
  43. totalSizeToAlloc<Stmt *>(1 + NumCatchStmts + (atFinallyStmt != nullptr));
  44. void *Mem = Context.Allocate(Size, alignof(ObjCAtTryStmt));
  45. return new (Mem) ObjCAtTryStmt(atTryLoc, atTryStmt, CatchStmts, NumCatchStmts,
  46. atFinallyStmt);
  47. }
  48. ObjCAtTryStmt *ObjCAtTryStmt::CreateEmpty(const ASTContext &Context,
  49. unsigned NumCatchStmts,
  50. bool HasFinally) {
  51. size_t Size = totalSizeToAlloc<Stmt *>(1 + NumCatchStmts + HasFinally);
  52. void *Mem = Context.Allocate(Size, alignof(ObjCAtTryStmt));
  53. return new (Mem) ObjCAtTryStmt(EmptyShell(), NumCatchStmts, HasFinally);
  54. }
  55. SourceLocation ObjCAtTryStmt::getEndLoc() const {
  56. if (HasFinally)
  57. return getFinallyStmt()->getEndLoc();
  58. if (NumCatchStmts)
  59. return getCatchStmt(NumCatchStmts - 1)->getEndLoc();
  60. return getTryBody()->getEndLoc();
  61. }