clang 20.0.0git
MemberPointer.h
Go to the documentation of this file.
1//===------------------------- MemberPointer.h ------------------*- 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_MEMBER_POINTER_H
10#define LLVM_CLANG_AST_INTERP_MEMBER_POINTER_H
11
12#include "Pointer.h"
13#include <optional>
14
15namespace clang {
16class ASTContext;
17namespace interp {
18
19class Context;
20class FunctionPointer;
21
22class MemberPointer final {
23private:
25 const ValueDecl *Dcl = nullptr;
26 int32_t PtrOffset = 0;
27
28 MemberPointer(Pointer Base, const ValueDecl *Dcl, int32_t PtrOffset)
29 : Base(Base), Dcl(Dcl), PtrOffset(PtrOffset) {}
30
31public:
32 MemberPointer() = default;
33 MemberPointer(Pointer Base, const ValueDecl *Dcl) : Base(Base), Dcl(Dcl) {}
34 MemberPointer(uint32_t Address, const Descriptor *D) {
35 // We only reach this for Address == 0, when creating a null member pointer.
36 assert(Address == 0);
37 }
38
39 MemberPointer(const ValueDecl *D) : Dcl(D) {
40 assert((isa<FieldDecl, IndirectFieldDecl, CXXMethodDecl>(D)));
41 }
42
43 uint64_t getIntegerRepresentation() const {
44 assert(
45 false &&
46 "getIntegerRepresentation() shouldn't be reachable for MemberPointers");
47 return 17;
48 }
49
50 std::optional<Pointer> toPointer(const Context &Ctx) const;
51
53
54 Pointer getBase() const {
55 if (PtrOffset < 0)
56 return Base.atField(-PtrOffset);
57 return Base.atFieldSub(PtrOffset);
58 }
60 return isa_and_nonnull<CXXMethodDecl>(Dcl);
61 }
63 return dyn_cast_if_present<CXXMethodDecl>(Dcl);
64 }
65 const FieldDecl *getField() const {
66 return dyn_cast_if_present<FieldDecl>(Dcl);
67 }
68
69 bool hasDecl() const { return Dcl; }
70 const ValueDecl *getDecl() const { return Dcl; }
71
72 MemberPointer atInstanceBase(unsigned Offset) const {
73 if (Base.isZero())
74 return MemberPointer(Base, Dcl, Offset);
75 return MemberPointer(this->Base, Dcl, Offset + PtrOffset);
76 }
77
79 assert(this->Base.isZero());
80 return MemberPointer(Instance, this->Dcl, this->PtrOffset);
81 }
82
83 APValue toAPValue(const ASTContext &) const;
84
85 bool isZero() const { return Base.isZero() && !Dcl; }
86 bool hasBase() const { return !Base.isZero(); }
87 bool isWeak() const {
88 if (const auto *MF = getMemberFunction())
89 return MF->isWeak();
90 return false;
91 }
92
93 void print(llvm::raw_ostream &OS) const {
94 OS << "MemberPtr(" << Base << " " << (const void *)Dcl << " + " << PtrOffset
95 << ")";
96 }
97
98 std::string toDiagnosticString(const ASTContext &Ctx) const {
99 return toAPValue(Ctx).getAsString(Ctx, Dcl->getType());
100 }
101
103 if (this->Dcl == RHS.Dcl)
106 }
107};
108
109inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, MemberPointer FP) {
110 FP.print(OS);
111 return OS;
112}
113
114} // namespace interp
115} // namespace clang
116
117#endif
const Decl * D
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:188
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2078
Represents a member of a struct/union/class.
Definition: Decl.h:3033
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition: Decl.h:671
QualType getType() const
Definition: Decl.h:682
Holds all information required to evaluate constexpr code in a module.
Definition: Context.h:40
const ValueDecl * getDecl() const
Definition: MemberPointer.h:70
APValue toAPValue(const ASTContext &) const
uint64_t getIntegerRepresentation() const
Definition: MemberPointer.h:43
MemberPointer takeInstance(Pointer Instance) const
Definition: MemberPointer.h:78
std::string toDiagnosticString(const ASTContext &Ctx) const
Definition: MemberPointer.h:98
MemberPointer(uint32_t Address, const Descriptor *D)
Definition: MemberPointer.h:34
bool isMemberFunctionPointer() const
Definition: MemberPointer.h:59
MemberPointer(const ValueDecl *D)
Definition: MemberPointer.h:39
const FieldDecl * getField() const
Definition: MemberPointer.h:65
void print(llvm::raw_ostream &OS) const
Definition: MemberPointer.h:93
MemberPointer atInstanceBase(unsigned Offset) const
Definition: MemberPointer.h:72
ComparisonCategoryResult compare(const MemberPointer &RHS) const
FunctionPointer toFunctionPointer(const Context &Ctx) const
const CXXMethodDecl * getMemberFunction() const
Definition: MemberPointer.h:62
std::optional< Pointer > toPointer(const Context &Ctx) const
MemberPointer(Pointer Base, const ValueDecl *Dcl)
Definition: MemberPointer.h:33
A pointer to a memory block, live or dead.
Definition: Pointer.h:83
bool isZero() const
Checks if the pointer is null.
Definition: Pointer.h:261
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const Boolean &B)
Definition: Boolean.h:160
The JSON file list parser is used to communicate input to InstallAPI.
ComparisonCategoryResult
An enumeration representing the possible results of a three-way comparison.
Describes a memory block created by an allocation site.
Definition: Descriptor.h:116