StmtCXX.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //===--- StmtCXX.cpp - Classes for representing C++ 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 StmtCXX.h
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/AST/StmtCXX.h"
  13. #include "clang/AST/ASTContext.h"
  14. using namespace clang;
  15. QualType CXXCatchStmt::getCaughtType() const {
  16. if (ExceptionDecl)
  17. return ExceptionDecl->getType();
  18. return QualType();
  19. }
  20. CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, SourceLocation tryLoc,
  21. Stmt *tryBlock, ArrayRef<Stmt *> handlers) {
  22. const size_t Size = totalSizeToAlloc<Stmt *>(handlers.size() + 1);
  23. void *Mem = C.Allocate(Size, alignof(CXXTryStmt));
  24. return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers);
  25. }
  26. CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, EmptyShell Empty,
  27. unsigned numHandlers) {
  28. const size_t Size = totalSizeToAlloc<Stmt *>(numHandlers + 1);
  29. void *Mem = C.Allocate(Size, alignof(CXXTryStmt));
  30. return new (Mem) CXXTryStmt(Empty, numHandlers);
  31. }
  32. CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock,
  33. ArrayRef<Stmt *> handlers)
  34. : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(handlers.size()) {
  35. Stmt **Stmts = getStmts();
  36. Stmts[0] = tryBlock;
  37. std::copy(handlers.begin(), handlers.end(), Stmts + 1);
  38. }
  39. CXXForRangeStmt::CXXForRangeStmt(Stmt *Init, DeclStmt *Range,
  40. DeclStmt *BeginStmt, DeclStmt *EndStmt,
  41. Expr *Cond, Expr *Inc, DeclStmt *LoopVar,
  42. Stmt *Body, SourceLocation FL,
  43. SourceLocation CAL, SourceLocation CL,
  44. SourceLocation RPL)
  45. : Stmt(CXXForRangeStmtClass), ForLoc(FL), CoawaitLoc(CAL), ColonLoc(CL),
  46. RParenLoc(RPL) {
  47. SubExprs[INIT] = Init;
  48. SubExprs[RANGE] = Range;
  49. SubExprs[BEGINSTMT] = BeginStmt;
  50. SubExprs[ENDSTMT] = EndStmt;
  51. SubExprs[COND] = Cond;
  52. SubExprs[INC] = Inc;
  53. SubExprs[LOOPVAR] = LoopVar;
  54. SubExprs[BODY] = Body;
  55. }
  56. Expr *CXXForRangeStmt::getRangeInit() {
  57. DeclStmt *RangeStmt = getRangeStmt();
  58. VarDecl *RangeDecl = dyn_cast_or_null<VarDecl>(RangeStmt->getSingleDecl());
  59. assert(RangeDecl && "for-range should have a single var decl");
  60. return RangeDecl->getInit();
  61. }
  62. const Expr *CXXForRangeStmt::getRangeInit() const {
  63. return const_cast<CXXForRangeStmt *>(this)->getRangeInit();
  64. }
  65. VarDecl *CXXForRangeStmt::getLoopVariable() {
  66. Decl *LV = cast<DeclStmt>(getLoopVarStmt())->getSingleDecl();
  67. assert(LV && "No loop variable in CXXForRangeStmt");
  68. return cast<VarDecl>(LV);
  69. }
  70. const VarDecl *CXXForRangeStmt::getLoopVariable() const {
  71. return const_cast<CXXForRangeStmt *>(this)->getLoopVariable();
  72. }
  73. CoroutineBodyStmt *CoroutineBodyStmt::Create(
  74. const ASTContext &C, CoroutineBodyStmt::CtorArgs const &Args) {
  75. std::size_t Size = totalSizeToAlloc<Stmt *>(
  76. CoroutineBodyStmt::FirstParamMove + Args.ParamMoves.size());
  77. void *Mem = C.Allocate(Size, alignof(CoroutineBodyStmt));
  78. return new (Mem) CoroutineBodyStmt(Args);
  79. }
  80. CoroutineBodyStmt *CoroutineBodyStmt::Create(const ASTContext &C, EmptyShell,
  81. unsigned NumParams) {
  82. std::size_t Size = totalSizeToAlloc<Stmt *>(
  83. CoroutineBodyStmt::FirstParamMove + NumParams);
  84. void *Mem = C.Allocate(Size, alignof(CoroutineBodyStmt));
  85. auto *Result = new (Mem) CoroutineBodyStmt(CtorArgs());
  86. Result->NumParams = NumParams;
  87. auto *ParamBegin = Result->getStoredStmts() + SubStmt::FirstParamMove;
  88. std::uninitialized_fill(ParamBegin, ParamBegin + NumParams,
  89. static_cast<Stmt *>(nullptr));
  90. return Result;
  91. }
  92. CoroutineBodyStmt::CoroutineBodyStmt(CoroutineBodyStmt::CtorArgs const &Args)
  93. : Stmt(CoroutineBodyStmtClass), NumParams(Args.ParamMoves.size()) {
  94. Stmt **SubStmts = getStoredStmts();
  95. SubStmts[CoroutineBodyStmt::Body] = Args.Body;
  96. SubStmts[CoroutineBodyStmt::Promise] = Args.Promise;
  97. SubStmts[CoroutineBodyStmt::InitSuspend] = Args.InitialSuspend;
  98. SubStmts[CoroutineBodyStmt::FinalSuspend] = Args.FinalSuspend;
  99. SubStmts[CoroutineBodyStmt::OnException] = Args.OnException;
  100. SubStmts[CoroutineBodyStmt::OnFallthrough] = Args.OnFallthrough;
  101. SubStmts[CoroutineBodyStmt::Allocate] = Args.Allocate;
  102. SubStmts[CoroutineBodyStmt::Deallocate] = Args.Deallocate;
  103. SubStmts[CoroutineBodyStmt::ReturnValue] = Args.ReturnValue;
  104. SubStmts[CoroutineBodyStmt::ReturnStmt] = Args.ReturnStmt;
  105. SubStmts[CoroutineBodyStmt::ReturnStmtOnAllocFailure] =
  106. Args.ReturnStmtOnAllocFailure;
  107. std::copy(Args.ParamMoves.begin(), Args.ParamMoves.end(),
  108. const_cast<Stmt **>(getParamMoves().data()));
  109. }