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 Decl *Dcl = nullptr;
26 int32_t PtrOffset = 0;
27
28 MemberPointer(Pointer Base, const Decl *Dcl, int32_t PtrOffset)
29 : Base(Base), Dcl(Dcl), PtrOffset(PtrOffset) {}
30
31public:
32 MemberPointer() = default;
33 MemberPointer(Pointer Base, const Decl *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 Decl *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 Decl *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
88 void print(llvm::raw_ostream &OS) const {
89 OS << "MemberPtr(" << Base << " " << (const void *)Dcl << " + " << PtrOffset
90 << ")";
91 }
92
93 std::string toDiagnosticString(const ASTContext &Ctx) const {
94 return "FIXME";
95 }
96
98 if (this->Dcl == RHS.Dcl)
101 }
102};
103
104inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, MemberPointer FP) {
105 FP.print(OS);
106 return OS;
107}
108
109} // namespace interp
110} // namespace clang
111
112#endif
const Decl * D
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition: APValue.h:122
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:187
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:2064
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
Represents a member of a struct/union/class.
Definition: Decl.h:3030
Holds all information required to evaluate constexpr code in a module.
Definition: Context.h:40
APValue toAPValue(const ASTContext &) const
uint64_t getIntegerRepresentation() const
Definition: MemberPointer.h:43
MemberPointer takeInstance(Pointer Instance) const
Definition: MemberPointer.h:78
MemberPointer(Pointer Base, const Decl *Dcl)
Definition: MemberPointer.h:33
std::string toDiagnosticString(const ASTContext &Ctx) const
Definition: MemberPointer.h:93
const Decl * getDecl() const
Definition: MemberPointer.h:70
MemberPointer(uint32_t Address, const Descriptor *D)
Definition: MemberPointer.h:34
bool isMemberFunctionPointer() const
Definition: MemberPointer.h:59
const FieldDecl * getField() const
Definition: MemberPointer.h:65
void print(llvm::raw_ostream &OS) const
Definition: MemberPointer.h:88
MemberPointer atInstanceBase(unsigned Offset) const
Definition: MemberPointer.h:72
ComparisonCategoryResult compare(const MemberPointer &RHS) const
Definition: MemberPointer.h:97
FunctionPointer toFunctionPointer(const Context &Ctx) const
const CXXMethodDecl * getMemberFunction() const
Definition: MemberPointer.h:62
std::optional< Pointer > toPointer(const Context &Ctx) const
A pointer to a memory block, live or dead.
Definition: Pointer.h:82
bool isZero() const
Checks if the pointer is null.
Definition: Pointer.h:260
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.
Describes a memory block created by an allocation site.
Definition: Descriptor.h:111