Path: blob/main/contrib/llvm-project/clang/lib/AST/Interp/FunctionPointer.h
35291 views
//===--- FunctionPointer.h - Types for the constexpr VM ---------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLVM_CLANG_AST_INTERP_FUNCTION_POINTER_H9#define LLVM_CLANG_AST_INTERP_FUNCTION_POINTER_H1011#include "Function.h"12#include "Primitives.h"13#include "clang/AST/APValue.h"1415namespace clang {16class ASTContext;17namespace interp {1819class FunctionPointer final {20private:21const Function *Func;22bool Valid;2324public:25FunctionPointer(const Function *Func) : Func(Func), Valid(true) {26assert(Func);27}2829FunctionPointer(uintptr_t IntVal = 0, const Descriptor *Desc = nullptr)30: Func(reinterpret_cast<const Function *>(IntVal)), Valid(false) {}3132const Function *getFunction() const { return Func; }33bool isZero() const { return !Func; }34bool isValid() const { return Valid; }35bool isWeak() const {36if (!Func || !Valid)37return false;3839return Func->getDecl()->isWeak();40}4142APValue toAPValue(const ASTContext &) const {43if (!Func)44return APValue(static_cast<Expr *>(nullptr), CharUnits::Zero(), {},45/*OnePastTheEnd=*/false, /*IsNull=*/true);4647if (!Valid)48return APValue(static_cast<Expr *>(nullptr),49CharUnits::fromQuantity(getIntegerRepresentation()), {},50/*OnePastTheEnd=*/false, /*IsNull=*/false);5152return APValue(Func->getDecl(), CharUnits::Zero(), {},53/*OnePastTheEnd=*/false, /*IsNull=*/false);54}5556void print(llvm::raw_ostream &OS) const {57OS << "FnPtr(";58if (Func && Valid)59OS << Func->getName();60else if (Func)61OS << reinterpret_cast<uintptr_t>(Func);62else63OS << "nullptr";64OS << ")";65}6667std::string toDiagnosticString(const ASTContext &Ctx) const {68if (!Func)69return "nullptr";7071return toAPValue(Ctx).getAsString(Ctx, Func->getDecl()->getType());72}7374uint64_t getIntegerRepresentation() const {75return static_cast<uint64_t>(reinterpret_cast<uintptr_t>(Func));76}7778ComparisonCategoryResult compare(const FunctionPointer &RHS) const {79if (Func == RHS.Func)80return ComparisonCategoryResult::Equal;81return ComparisonCategoryResult::Unordered;82}83};8485inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,86FunctionPointer FP) {87FP.print(OS);88return OS;89}9091} // namespace interp92} // namespace clang9394#endif959697