2024-03-06 16:32:14 +01:00
|
|
|
//===--- FunctionPointer.h - Types for the constexpr VM ---------*- C++ -*-===//
|
2023-05-19 08:57:52 +02:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2023-01-11 12:12:52 +01:00
|
|
|
|
|
|
|
#ifndef LLVM_CLANG_AST_INTERP_FUNCTION_POINTER_H
|
|
|
|
#define LLVM_CLANG_AST_INTERP_FUNCTION_POINTER_H
|
|
|
|
|
|
|
|
#include "Function.h"
|
|
|
|
#include "Primitives.h"
|
|
|
|
#include "clang/AST/APValue.h"
|
|
|
|
|
|
|
|
namespace clang {
|
2023-04-25 15:46:20 +02:00
|
|
|
class ASTContext;
|
2023-01-11 12:12:52 +01:00
|
|
|
namespace interp {
|
|
|
|
|
|
|
|
class FunctionPointer final {
|
|
|
|
private:
|
|
|
|
const Function *Func;
|
2024-04-11 18:15:32 +02:00
|
|
|
bool Valid;
|
2023-01-11 12:12:52 +01:00
|
|
|
|
|
|
|
public:
|
2024-04-11 18:15:32 +02:00
|
|
|
FunctionPointer(const Function *Func) : Func(Func), Valid(true) {
|
|
|
|
assert(Func);
|
|
|
|
}
|
2024-04-10 12:53:54 +02:00
|
|
|
|
2024-04-11 18:15:32 +02:00
|
|
|
FunctionPointer(uintptr_t IntVal = 0, const Descriptor *Desc = nullptr)
|
|
|
|
: Func(reinterpret_cast<const Function *>(IntVal)), Valid(false) {}
|
2023-01-11 12:12:52 +01:00
|
|
|
|
|
|
|
const Function *getFunction() const { return Func; }
|
2024-02-25 13:50:38 +01:00
|
|
|
bool isZero() const { return !Func; }
|
2024-04-15 14:17:31 +02:00
|
|
|
bool isValid() const { return Valid; }
|
2024-04-12 08:52:18 +02:00
|
|
|
bool isWeak() const {
|
|
|
|
if (!Func || !Valid)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return Func->getDecl()->isWeak();
|
|
|
|
}
|
2023-01-11 12:12:52 +01:00
|
|
|
|
|
|
|
APValue toAPValue() const {
|
|
|
|
if (!Func)
|
|
|
|
return APValue(static_cast<Expr *>(nullptr), CharUnits::Zero(), {},
|
|
|
|
/*OnePastTheEnd=*/false, /*IsNull=*/true);
|
|
|
|
|
2024-04-11 18:15:32 +02:00
|
|
|
if (!Valid)
|
|
|
|
return APValue(static_cast<Expr *>(nullptr),
|
|
|
|
CharUnits::fromQuantity(getIntegerRepresentation()), {},
|
|
|
|
/*OnePastTheEnd=*/false, /*IsNull=*/false);
|
|
|
|
|
2023-01-11 12:12:52 +01:00
|
|
|
return APValue(Func->getDecl(), CharUnits::Zero(), {},
|
|
|
|
/*OnePastTheEnd=*/false, /*IsNull=*/false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void print(llvm::raw_ostream &OS) const {
|
|
|
|
OS << "FnPtr(";
|
2024-04-11 18:15:32 +02:00
|
|
|
if (Func && Valid)
|
2023-01-11 12:12:52 +01:00
|
|
|
OS << Func->getName();
|
2024-04-11 18:15:32 +02:00
|
|
|
else if (Func)
|
|
|
|
OS << reinterpret_cast<uintptr_t>(Func);
|
2023-01-11 12:12:52 +01:00
|
|
|
else
|
|
|
|
OS << "nullptr";
|
|
|
|
OS << ")";
|
|
|
|
}
|
|
|
|
|
2023-04-25 15:46:20 +02:00
|
|
|
std::string toDiagnosticString(const ASTContext &Ctx) const {
|
|
|
|
if (!Func)
|
|
|
|
return "nullptr";
|
|
|
|
|
|
|
|
return toAPValue().getAsString(Ctx, Func->getDecl()->getType());
|
|
|
|
}
|
|
|
|
|
2024-04-10 12:53:54 +02:00
|
|
|
uint64_t getIntegerRepresentation() const {
|
|
|
|
return static_cast<uint64_t>(reinterpret_cast<uintptr_t>(Func));
|
|
|
|
}
|
|
|
|
|
2023-01-11 12:12:52 +01:00
|
|
|
ComparisonCategoryResult compare(const FunctionPointer &RHS) const {
|
|
|
|
if (Func == RHS.Func)
|
|
|
|
return ComparisonCategoryResult::Equal;
|
|
|
|
return ComparisonCategoryResult::Unordered;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
|
|
|
|
FunctionPointer FP) {
|
|
|
|
FP.print(OS);
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace interp
|
|
|
|
} // namespace clang
|
|
|
|
|
|
|
|
#endif
|