Index: clang/include/clang/Analysis/AnyCall.h =================================================================== --- /dev/null +++ clang/include/clang/Analysis/AnyCall.h @@ -0,0 +1,180 @@ +//=== AnyCall.h - Abstraction over different callables --------*- C++ -*--// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// A utility class for performing generic operations over different callables. +// +//===----------------------------------------------------------------------===// +// +#ifndef LLVM_CLANG_ANALYSIS_ANY_CALL_H +#define LLVM_CLANG_ANALYSIS_ANY_CALL_H + +#include "clang/AST/Decl.h" +#include "clang/AST/ExprCXX.h" +#include "clang/AST/ExprObjC.h" + +namespace clang { + +/// An instance of this class corresponds to a 'callable' call. +class AnyCall { +public: + enum Kind { + /// A function, function pointer, or a C++ method call + Function, + + /// A call to an Objective-C method + ObjCMethod, + + /// A call to an Objective-C block + Block, + + /// An implicit C++ destructor call (called implicitly + /// or by operator 'delete') + Destructor, + + /// An implicit or explicit C++ constructor call + Constructor, + + /// A C++ allocation function call (operator `new`), via C++ new-expression + Allocator, + + /// A C++ deallocation function call (operator `delete`), via C++ + /// delete-expression + Deallocator + }; + +private: + /// Call expression, remains null iff the call is an implicit destructor call. + const Expr *E = nullptr; + + /// Corresponds to a statically known declaration of the called function, + /// or null if it is not known (e.g. for a function pointer). + const Decl *D = nullptr; + Kind K; + + /// Construct a generic call. + /// \param E Call expression, if exists (e.g. does not exist for destructors) + /// \param InputDecl Declaration of the called function: can be left empty + /// iff {@code E} is not empty. + AnyCall(const Expr *E, + const Decl *InputDecl = nullptr) : E(E) { + if (!E) { + assert(InputDecl); + D = InputDecl; + K = Destructor; + } else if (const auto *ME = dyn_cast(E)) { + D = ME->getMethodDecl(); + K = ObjCMethod; + } else if (const auto *CE = dyn_cast(E)) { + const Decl *CD = CE->getCalleeDecl(); + if (CD && isa(CD)) { + D = CD; + K = Function; + + } else if ((CD && isa(CD)) || + (CE->getCallee()->getType()->getAs())) { + D = CD; + K = Block; + } else { + D = nullptr; + K = Function; + } + } else if (const auto *CXNE = dyn_cast(E)) { + D = CXNE->getOperatorNew(); + K = Allocator; + } else if (const auto *CXDE = dyn_cast(E)) { + D = CXDE->getOperatorDelete(); + K = Deallocator; + } else if (const auto *CXCE = dyn_cast(E)) { + D = CXCE->getConstructor(); + K = Constructor; + } else { + llvm_unreachable("Unexpected type"); + } + } + +public: + AnyCall(const Expr *E) : AnyCall(E, nullptr) { + assert(E && "Use a different constructor if expression is null"); + } + + static AnyCall forDestructorCall(const Decl *D) { + return AnyCall(nullptr, D); + } + + /// \returns formal parameters for direct calls (including virtual calls) + ArrayRef parameters() const { + if (!D) + return None; + + if (const auto *FD = dyn_cast(D)) { + return FD->parameters(); + } else if (const auto *MD = dyn_cast(D)) { + return MD->parameters(); + } else if (const auto *CD = dyn_cast(D)) { + return CD->parameters(); + } else if (const auto *BD = dyn_cast(D)) { + return BD->parameters(); + } else { + return None; + } + } + + using param_const_iterator = ArrayRef::const_iterator; + param_const_iterator param_begin() const { return parameters().begin(); } + param_const_iterator param_end() const { return parameters().end(); } + size_t param_size() const { return parameters().size(); } + bool param_empty() const { return parameters().empty(); } + + QualType getReturnType(ASTContext &Ctx) const { + if (const auto *CE = dyn_cast(E)) + return CE->getCallReturnType(Ctx); + + if (!D) + return E->getType(); + + if (const auto *MD = dyn_cast(D)) { + return MD->getReturnType(); + } else if (const auto *FD = dyn_cast(D)) { + return FD->getReturnType(); + } else { + llvm_unreachable("Unexpected decl"); + } + } + + /// \returns Function identifier if it is a named declaration, + /// {@code nullptr} otherwise. + const IdentifierInfo *getIdentifier() const { + if (D) + if (const auto *ND = dyn_cast(D)) + return ND->getIdentifier(); + return nullptr; + } + + const Decl *getDecl() const { + return D; + } + + const Expr *getExpr() const { + return E; + } + + Kind getKind() const { + return K; + } + + void dump() const { + if (E) + E->dump(); + if (D) + D->dump(); + } +}; + +} + +#endif // LLVM_CLANG_ANALYSIS_ANY_CALL_H