xref: /freebsd/contrib/llvm-project/clang/lib/AST/ByteCode/FunctionPointer.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1*700637cbSDimitry Andric //===--- FunctionPointer.h - Types for the constexpr VM ---------*- C++ -*-===//
2*700637cbSDimitry Andric //
3*700637cbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*700637cbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*700637cbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*700637cbSDimitry Andric //
7*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
8*700637cbSDimitry Andric 
9*700637cbSDimitry Andric #ifndef LLVM_CLANG_AST_INTERP_FUNCTION_POINTER_H
10*700637cbSDimitry Andric #define LLVM_CLANG_AST_INTERP_FUNCTION_POINTER_H
11*700637cbSDimitry Andric 
12*700637cbSDimitry Andric #include "Function.h"
13*700637cbSDimitry Andric #include "Primitives.h"
14*700637cbSDimitry Andric 
15*700637cbSDimitry Andric namespace clang {
16*700637cbSDimitry Andric class ASTContext;
17*700637cbSDimitry Andric class APValue;
18*700637cbSDimitry Andric namespace interp {
19*700637cbSDimitry Andric 
20*700637cbSDimitry Andric class FunctionPointer final {
21*700637cbSDimitry Andric private:
22*700637cbSDimitry Andric   const Function *Func;
23*700637cbSDimitry Andric 
24*700637cbSDimitry Andric public:
25*700637cbSDimitry Andric   FunctionPointer() = default;
FunctionPointer(const Function * Func)26*700637cbSDimitry Andric   FunctionPointer(const Function *Func) : Func(Func) {}
27*700637cbSDimitry Andric 
getFunction()28*700637cbSDimitry Andric   const Function *getFunction() const { return Func; }
isZero()29*700637cbSDimitry Andric   bool isZero() const { return !Func; }
isWeak()30*700637cbSDimitry Andric   bool isWeak() const {
31*700637cbSDimitry Andric     if (!Func || !Func->getDecl())
32*700637cbSDimitry Andric       return false;
33*700637cbSDimitry Andric 
34*700637cbSDimitry Andric     return Func->getDecl()->isWeak();
35*700637cbSDimitry Andric   }
36*700637cbSDimitry Andric 
37*700637cbSDimitry Andric   APValue toAPValue(const ASTContext &) const;
38*700637cbSDimitry Andric   void print(llvm::raw_ostream &OS) const;
39*700637cbSDimitry Andric 
toDiagnosticString(const ASTContext & Ctx)40*700637cbSDimitry Andric   std::string toDiagnosticString(const ASTContext &Ctx) const {
41*700637cbSDimitry Andric     if (!Func)
42*700637cbSDimitry Andric       return "nullptr";
43*700637cbSDimitry Andric 
44*700637cbSDimitry Andric     return toAPValue(Ctx).getAsString(Ctx, Func->getDecl()->getType());
45*700637cbSDimitry Andric   }
46*700637cbSDimitry Andric 
getIntegerRepresentation()47*700637cbSDimitry Andric   uint64_t getIntegerRepresentation() const {
48*700637cbSDimitry Andric     return static_cast<uint64_t>(reinterpret_cast<uintptr_t>(Func));
49*700637cbSDimitry Andric   }
50*700637cbSDimitry Andric };
51*700637cbSDimitry Andric 
52*700637cbSDimitry Andric } // namespace interp
53*700637cbSDimitry Andric } // namespace clang
54*700637cbSDimitry Andric 
55*700637cbSDimitry Andric #endif
56