clang 22.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 bool isBaseCastPossible() const {
55 if (PtrOffset < 0)
56 return true;
57 return static_cast<uint64_t>(PtrOffset) <= Base.getByteOffset();
58 }
59
60 Pointer getBase() const {
61 if (PtrOffset < 0)
62 return Base.atField(-PtrOffset);
63 return Base.atFieldSub(PtrOffset);
64 }
66 return isa_and_nonnull<CXXMethodDecl>(Dcl);
67 }
69 return dyn_cast_if_present<CXXMethodDecl>(Dcl);
70 }
71 const FieldDecl *getField() const {
72 return dyn_cast_if_present<FieldDecl>(Dcl);
73 }
74
75 bool hasDecl() const { return Dcl; }
76 const ValueDecl *getDecl() const { return Dcl; }
77
78 MemberPointer atInstanceBase(unsigned Offset) const {
79 if (Base.isZero())
80 return MemberPointer(Base, Dcl, Offset);
81 return MemberPointer(this->Base, Dcl, Offset + PtrOffset);
82 }
83
85 assert(this->Base.isZero());
86 return MemberPointer(Instance, this->Dcl, this->PtrOffset);
87 }
88
89 APValue toAPValue(const ASTContext &) const;
90
91 bool isZero() const { return Base.isZero() && !Dcl; }
92 bool hasBase() const { return !Base.isZero(); }
93 bool isWeak() const {
94 if (const auto *MF = getMemberFunction())
95 return MF->isWeak();
96 return false;
97 }
98
99 void print(llvm::raw_ostream &OS) const {
100 OS << "MemberPtr(" << Base << " " << (const void *)Dcl << " + " << PtrOffset
101 << ")";
102 }
103
104 std::string toDiagnosticString(const ASTContext &Ctx) const {
105 return toAPValue(Ctx).getAsString(Ctx, Dcl->getType());
106 }
107
109 if (this->Dcl == RHS.Dcl)
112 }
113};
114
115inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, MemberPointer FP) {
116 FP.print(OS);
117 return OS;
118}
119
120} // namespace interp
121} // namespace clang
122
123#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:956
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:2129
Represents a member of a struct/union/class.
Definition: Decl.h:3157
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition: Decl.h:711
QualType getType() const
Definition: Decl.h:722
Holds all information required to evaluate constexpr code in a module.
Definition: Context.h:41
const ValueDecl * getDecl() const
Definition: MemberPointer.h:76
APValue toAPValue(const ASTContext &) const
uint64_t getIntegerRepresentation() const
Definition: MemberPointer.h:43
MemberPointer takeInstance(Pointer Instance) const
Definition: MemberPointer.h:84
std::string toDiagnosticString(const ASTContext &Ctx) const
MemberPointer(uint32_t Address, const Descriptor *D)
Definition: MemberPointer.h:34
bool isMemberFunctionPointer() const
Definition: MemberPointer.h:65
MemberPointer(const ValueDecl *D)
Definition: MemberPointer.h:39
const FieldDecl * getField() const
Definition: MemberPointer.h:71
void print(llvm::raw_ostream &OS) const
Definition: MemberPointer.h:99
MemberPointer atInstanceBase(unsigned Offset) const
Definition: MemberPointer.h:78
ComparisonCategoryResult compare(const MemberPointer &RHS) const
FunctionPointer toFunctionPointer(const Context &Ctx) const
const CXXMethodDecl * getMemberFunction() const
Definition: MemberPointer.h:68
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:90
bool isZero() const
Checks if the pointer is null.
Definition: Pointer.h:253
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const Boolean &B)
Definition: Boolean.h:154
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:122