clang 20.0.0git
FunctionPointer.h
Go to the documentation of this file.
1//===--- FunctionPointer.h - Types for the constexpr VM ---------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_CLANG_AST_INTERP_FUNCTION_POINTER_H
10#define LLVM_CLANG_AST_INTERP_FUNCTION_POINTER_H
11
12#include "Function.h"
13#include "Primitives.h"
14#include "clang/AST/APValue.h"
15
16namespace clang {
17class ASTContext;
18namespace interp {
19
20class FunctionPointer final {
21private:
22 const Function *Func;
23 bool Valid;
24
25public:
26 FunctionPointer() = default;
27 FunctionPointer(const Function *Func) : Func(Func), Valid(true) {}
28
29 FunctionPointer(uintptr_t IntVal, const Descriptor *Desc = nullptr)
30 : Func(reinterpret_cast<const Function *>(IntVal)), Valid(false) {}
31
32 const Function *getFunction() const { return Func; }
33 bool isZero() const { return !Func; }
34 bool isValid() const { return Valid; }
35 bool isWeak() const {
36 if (!Func || !Valid)
37 return false;
38
39 return Func->getDecl()->isWeak();
40 }
41
42 APValue toAPValue(const ASTContext &) const {
43 if (!Func)
44 return APValue(static_cast<Expr *>(nullptr), CharUnits::Zero(), {},
45 /*OnePastTheEnd=*/false, /*IsNull=*/true);
46
47 if (!Valid)
48 return APValue(static_cast<Expr *>(nullptr),
50 /*OnePastTheEnd=*/false, /*IsNull=*/false);
51
52 return APValue(Func->getDecl(), CharUnits::Zero(), {},
53 /*OnePastTheEnd=*/false, /*IsNull=*/false);
54 }
55
56 void print(llvm::raw_ostream &OS) const {
57 OS << "FnPtr(";
58 if (Func && Valid)
59 OS << Func->getName();
60 else if (Func)
61 OS << reinterpret_cast<uintptr_t>(Func);
62 else
63 OS << "nullptr";
64 OS << ")";
65 }
66
67 std::string toDiagnosticString(const ASTContext &Ctx) const {
68 if (!Func)
69 return "nullptr";
70
71 return toAPValue(Ctx).getAsString(Ctx, Func->getDecl()->getType());
72 }
73
74 uint64_t getIntegerRepresentation() const {
75 return static_cast<uint64_t>(reinterpret_cast<uintptr_t>(Func));
76 }
77
79 if (Func == RHS.Func)
82 }
83};
84
85inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
86 FunctionPointer FP) {
87 FP.print(OS);
88 return OS;
89}
90
91} // namespace interp
92} // namespace clang
93
94#endif
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition: APValue.h:122
std::string getAsString(const ASTContext &Ctx, QualType Ty) const
Definition: APValue.cpp:946
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:186
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition: CharUnits.h:63
static CharUnits Zero()
Zero - Construct a CharUnits quantity of zero.
Definition: CharUnits.h:53
This represents one expression.
Definition: Expr.h:110
void print(llvm::raw_ostream &OS) const
const Function * getFunction() const
FunctionPointer(uintptr_t IntVal, const Descriptor *Desc=nullptr)
FunctionPointer(const Function *Func)
ComparisonCategoryResult compare(const FunctionPointer &RHS) const
APValue toAPValue(const ASTContext &) const
uint64_t getIntegerRepresentation() const
std::string toDiagnosticString(const ASTContext &Ctx) const
Bytecode function.
Definition: Function.h:77
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const Boolean &B)
Definition: Boolean.h:151
The JSON file list parser is used to communicate input to InstallAPI.
ComparisonCategoryResult
An enumeration representing the possible results of a three-way comparison.
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...
#define true
Definition: stdbool.h:25
#define false
Definition: stdbool.h:26
Describes a memory block created by an allocation site.
Definition: Descriptor.h:107