AnyCall.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //=== AnyCall.h - Abstraction over different callables --------*- C++ -*--//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // A utility class for performing generic operations over different callables.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. //
  18. #ifndef LLVM_CLANG_ANALYSIS_ANYCALL_H
  19. #define LLVM_CLANG_ANALYSIS_ANYCALL_H
  20. #include "clang/AST/Decl.h"
  21. #include "clang/AST/ExprCXX.h"
  22. #include "clang/AST/ExprObjC.h"
  23. namespace clang {
  24. /// An instance of this class corresponds to a call.
  25. /// It might be a syntactically-concrete call, done as a part of evaluating an
  26. /// expression, or it may be an abstract callee with no associated expression.
  27. class AnyCall {
  28. public:
  29. enum Kind {
  30. /// A function, function pointer, or a C++ method call
  31. Function,
  32. /// A call to an Objective-C method
  33. ObjCMethod,
  34. /// A call to an Objective-C block
  35. Block,
  36. /// An implicit C++ destructor call (called implicitly
  37. /// or by operator 'delete')
  38. Destructor,
  39. /// An implicit or explicit C++ constructor call
  40. Constructor,
  41. /// A C++ inherited constructor produced by a "using T::T" directive
  42. InheritedConstructor,
  43. /// A C++ allocation function call (operator `new`), via C++ new-expression
  44. Allocator,
  45. /// A C++ deallocation function call (operator `delete`), via C++
  46. /// delete-expression
  47. Deallocator
  48. };
  49. private:
  50. /// Either expression or declaration (but not both at the same time)
  51. /// can be null.
  52. /// Call expression, is null when is not known (then declaration is non-null),
  53. /// or for implicit destructor calls (when no expression exists.)
  54. const Expr *E = nullptr;
  55. /// Corresponds to a statically known declaration of the called function,
  56. /// or null if it is not known (e.g. for a function pointer).
  57. const Decl *D = nullptr;
  58. Kind K;
  59. public:
  60. AnyCall(const CallExpr *CE) : E(CE) {
  61. D = CE->getCalleeDecl();
  62. K = (CE->getCallee()->getType()->getAs<BlockPointerType>()) ? Block
  63. : Function;
  64. if (D && ((K == Function && !isa<FunctionDecl>(D)) ||
  65. (K == Block && !isa<BlockDecl>(D))))
  66. D = nullptr;
  67. }
  68. AnyCall(const ObjCMessageExpr *ME)
  69. : E(ME), D(ME->getMethodDecl()), K(ObjCMethod) {}
  70. AnyCall(const CXXNewExpr *NE)
  71. : E(NE), D(NE->getOperatorNew()), K(Allocator) {}
  72. AnyCall(const CXXDeleteExpr *NE)
  73. : E(NE), D(NE->getOperatorDelete()), K(Deallocator) {}
  74. AnyCall(const CXXConstructExpr *NE)
  75. : E(NE), D(NE->getConstructor()), K(Constructor) {}
  76. AnyCall(const CXXInheritedCtorInitExpr *CIE)
  77. : E(CIE), D(CIE->getConstructor()), K(InheritedConstructor) {}
  78. AnyCall(const CXXDestructorDecl *D) : E(nullptr), D(D), K(Destructor) {}
  79. AnyCall(const CXXConstructorDecl *D) : E(nullptr), D(D), K(Constructor) {}
  80. AnyCall(const ObjCMethodDecl *D) : E(nullptr), D(D), K(ObjCMethod) {}
  81. AnyCall(const FunctionDecl *D) : E(nullptr), D(D) {
  82. if (isa<CXXConstructorDecl>(D)) {
  83. K = Constructor;
  84. } else if (isa <CXXDestructorDecl>(D)) {
  85. K = Destructor;
  86. } else {
  87. K = Function;
  88. }
  89. }
  90. /// If @c E is a generic call (to ObjC method /function/block/etc),
  91. /// return a constructed @c AnyCall object. Return None otherwise.
  92. static Optional<AnyCall> forExpr(const Expr *E) {
  93. if (const auto *ME = dyn_cast<ObjCMessageExpr>(E)) {
  94. return AnyCall(ME);
  95. } else if (const auto *CE = dyn_cast<CallExpr>(E)) {
  96. return AnyCall(CE);
  97. } else if (const auto *CXNE = dyn_cast<CXXNewExpr>(E)) {
  98. return AnyCall(CXNE);
  99. } else if (const auto *CXDE = dyn_cast<CXXDeleteExpr>(E)) {
  100. return AnyCall(CXDE);
  101. } else if (const auto *CXCE = dyn_cast<CXXConstructExpr>(E)) {
  102. return AnyCall(CXCE);
  103. } else if (const auto *CXCIE = dyn_cast<CXXInheritedCtorInitExpr>(E)) {
  104. return AnyCall(CXCIE);
  105. } else {
  106. return None;
  107. }
  108. }
  109. /// If @c D is a callable (Objective-C method or a function), return
  110. /// a constructed @c AnyCall object. Return None otherwise.
  111. // FIXME: block support.
  112. static Optional<AnyCall> forDecl(const Decl *D) {
  113. if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
  114. return AnyCall(FD);
  115. } else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
  116. return AnyCall(MD);
  117. }
  118. return None;
  119. }
  120. /// \returns formal parameters for direct calls (including virtual calls)
  121. ArrayRef<ParmVarDecl *> parameters() const {
  122. if (!D)
  123. return None;
  124. if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
  125. return FD->parameters();
  126. } else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
  127. return MD->parameters();
  128. } else if (const auto *BD = dyn_cast<BlockDecl>(D)) {
  129. return BD->parameters();
  130. } else {
  131. return None;
  132. }
  133. }
  134. using param_const_iterator = ArrayRef<ParmVarDecl *>::const_iterator;
  135. param_const_iterator param_begin() const { return parameters().begin(); }
  136. param_const_iterator param_end() const { return parameters().end(); }
  137. size_t param_size() const { return parameters().size(); }
  138. bool param_empty() const { return parameters().empty(); }
  139. QualType getReturnType(ASTContext &Ctx) const {
  140. switch (K) {
  141. case Function:
  142. if (E)
  143. return cast<CallExpr>(E)->getCallReturnType(Ctx);
  144. return cast<FunctionDecl>(D)->getReturnType();
  145. case ObjCMethod:
  146. if (E)
  147. return cast<ObjCMessageExpr>(E)->getCallReturnType(Ctx);
  148. return cast<ObjCMethodDecl>(D)->getReturnType();
  149. case Block:
  150. // FIXME: BlockDecl does not know its return type,
  151. // hence the asymmetry with the function and method cases above.
  152. return cast<CallExpr>(E)->getCallReturnType(Ctx);
  153. case Destructor:
  154. case Constructor:
  155. case InheritedConstructor:
  156. case Allocator:
  157. case Deallocator:
  158. return cast<FunctionDecl>(D)->getReturnType();
  159. }
  160. llvm_unreachable("Unknown AnyCall::Kind");
  161. }
  162. /// \returns Function identifier if it is a named declaration,
  163. /// @c nullptr otherwise.
  164. const IdentifierInfo *getIdentifier() const {
  165. if (const auto *ND = dyn_cast_or_null<NamedDecl>(D))
  166. return ND->getIdentifier();
  167. return nullptr;
  168. }
  169. const Decl *getDecl() const {
  170. return D;
  171. }
  172. const Expr *getExpr() const {
  173. return E;
  174. }
  175. Kind getKind() const {
  176. return K;
  177. }
  178. void dump() const {
  179. if (E)
  180. E->dump();
  181. if (D)
  182. D->dump();
  183. }
  184. };
  185. }
  186. #endif // LLVM_CLANG_ANALYSIS_ANYCALL_H
  187. #ifdef __GNUC__
  188. #pragma GCC diagnostic pop
  189. #endif