clang 24.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 "llvm/ADT/PointerIntPair.h"
14#include <optional>
15
16namespace clang {
17class ASTContext;
18class CXXRecordDecl;
19namespace interp {
20
21class Context;
22
23class MemberPointer final {
24private:
25 Pointer Base;
26 /// The member declaration, and a flag indicating
27 /// whether the member is a member of some class derived from the class type
28 /// of the member pointer.
29 llvm::PointerIntPair<const ValueDecl *, 1, bool> DeclAndIsDerivedMember;
30 /// The path of base/derived classes from the member declaration's
31 /// class (exclusive) to the class type of the member pointer (inclusive).
32 /// This a allocated by the InterpState or the Program.
33 const CXXRecordDecl **Path = nullptr;
34 int32_t PtrOffset = 0;
35 uint8_t PathLength = 0;
36
37 MemberPointer(Pointer Base, const ValueDecl *Dcl, int32_t PtrOffset,
38 uint8_t PathLength = 0, const CXXRecordDecl **Path = nullptr,
39 bool IsDerived = false)
40 : Base(Base), DeclAndIsDerivedMember(Dcl, IsDerived), Path(Path),
41 PtrOffset(PtrOffset), PathLength(PathLength) {}
42
43public:
44 MemberPointer() = default;
46 : Base(Base), DeclAndIsDerivedMember(Dcl) {}
48 // We only reach this for Address == 0, when creating a null member pointer.
49 assert(Address == 0);
50 }
51
52 MemberPointer(const ValueDecl *D) : DeclAndIsDerivedMember(D) {
54 }
55
56 uint64_t getIntegerRepresentation() const {
57 assert(
58 false &&
59 "getIntegerRepresentation() shouldn't be reachable for MemberPointers");
60 return 17;
61 }
62
63 /// Does this member pointer have a base declaration?
64 bool hasDecl() const { return DeclAndIsDerivedMember.getPointer(); }
65 bool isDerivedMember() const { return DeclAndIsDerivedMember.getInt(); }
66 /// Return the base declaration. Might be null.
67 const ValueDecl *getDecl() const {
68 return DeclAndIsDerivedMember.getPointer();
69 }
70 /// Does this member pointer have a path (i.e. path length is > 0)?
71 bool hasPath() const { return PathLength != 0; }
72 /// Return the length of the cast path.
73 unsigned getPathLength() const { return PathLength; }
74 /// Return the cast path entry at the given position.
75 const CXXRecordDecl *getPathEntry(unsigned Index) const {
76 assert(Index < PathLength);
77 return Path[Index];
78 }
79 /// Return the cast path. Might return null.
80 const CXXRecordDecl **path() const { return Path; }
81 bool isZero() const { return Base.isZero() && !hasDecl(); }
82 bool hasBase() const { return !Base.isZero(); }
83 bool isWeak() const {
84 if (const auto *MF = getMemberFunction())
85 return MF->isWeak();
86 return false;
87 }
88
89 /// Sets the path of this member pointer. After this call,
90 /// the memory pointed to by \p NewPath is assumed to be owned
91 /// by this member pointer.
92 void takePath(const CXXRecordDecl **NewPath) {
93 assert(Path != NewPath);
94 Path = NewPath;
95 }
96
97 // Pretend we always have a path.
98 bool singleWord() const { return false; }
100
101 std::optional<Pointer> toPointer(const Context &Ctx) const;
102
103 bool isBaseCastPossible() const {
104 if (!Base.isBlockPointer())
105 return false;
106 if (PtrOffset < 0)
107 return true;
108 return static_cast<uint64_t>(PtrOffset) <= Base.getByteOffset();
109 }
110
111 Pointer getBase() const {
112 if (PtrOffset < 0)
113 return Base.atField(-PtrOffset);
114 return Base.atFieldSub(PtrOffset);
115 }
116 /// Is the base declaration a member function?
118 return isa_and_nonnull<CXXMethodDecl>(DeclAndIsDerivedMember.getPointer());
119 }
120 /// Return the base declaration as a CXXMethodDecl. Might return null.
122 return dyn_cast_if_present<CXXMethodDecl>(
123 DeclAndIsDerivedMember.getPointer());
124 }
125 /// Return the base declaration as a FieldDecl. Might return null.
126 const FieldDecl *getField() const {
127 return dyn_cast_if_present<FieldDecl>(DeclAndIsDerivedMember.getPointer());
128 }
129 /// Returns the record decl this member pointer points into.
131 if (const FieldDecl *FD = getField())
132 return cast<CXXRecordDecl>(FD->getParent());
133
134 if (const CXXMethodDecl *MD = getMemberFunction())
135 return MD->getParent();
136 return nullptr;
137 }
138
139 MemberPointer atInstanceBase(unsigned Offset, uint8_t PathLength = 0,
140 const CXXRecordDecl **Path = nullptr,
141 bool NewIsDerived = false) const {
142 if (Base.isZero())
143 return MemberPointer(Base, DeclAndIsDerivedMember.getPointer(), Offset,
144 PathLength, Path, NewIsDerived);
145 return MemberPointer(this->Base, DeclAndIsDerivedMember.getPointer(),
146 Offset + PtrOffset, PathLength, Path, NewIsDerived);
147 }
148
149 MemberPointer takeInstance(Pointer Instance) const {
150 assert(this->Base.isZero());
151 return MemberPointer(Instance, DeclAndIsDerivedMember.getPointer(),
152 this->PtrOffset, PathLength, Path,
153 DeclAndIsDerivedMember.getInt());
154 }
155
156 MemberPointer withPath(uint8_t PathLength, const CXXRecordDecl **Path,
157 bool IsDerived) const {
158 return MemberPointer(this->Base, DeclAndIsDerivedMember.getPointer(),
159 PtrOffset, PathLength, Path, IsDerived);
160 }
161
162 APValue toAPValue(const ASTContext &) const;
163
164 void print(llvm::raw_ostream &OS) const {
165 OS << "MemberPtr(" << Base << " " << (const void *)getDecl() << " + "
166 << PtrOffset << ". PathLength: " << getPathLength()
167 << ". IsDerived: " << isDerivedMember() << ")";
168 }
169
170 std::string toDiagnosticString(const ASTContext &Ctx) const {
171 return toAPValue(Ctx).getAsString(Ctx, getDecl()->getType());
172 }
173};
174
175inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
176 const MemberPointer &FP) {
177 FP.print(OS);
178 return OS;
179}
180
181} // namespace interp
182} // namespace clang
183
184#endif
TokenType getType() const
Returns the token's type, e.g.
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:991
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:239
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2149
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
Represents a member of a struct/union/class.
Definition Decl.h:3295
The base class of the type hierarchy.
Definition TypeBase.h:1879
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:713
Holds all information required to evaluate constexpr code in a module.
Definition Context.h:47
const ValueDecl * getDecl() const
Return the base declaration. Might be null.
bool hasDecl() const
Does this member pointer have a base declaration?
APValue toAPValue(const ASTContext &) const
uint64_t getIntegerRepresentation() const
MemberPointer takeInstance(Pointer Instance) const
const CXXRecordDecl * getRecordDecl() const
Returns the record decl this member pointer points into.
const CXXRecordDecl * getPathEntry(unsigned Index) const
Return the cast path entry at the given position.
bool hasPath() const
Does this member pointer have a path (i.e. path length is > 0)?
std::string toDiagnosticString(const ASTContext &Ctx) const
MemberPointer(uint32_t Address, const Type *)
bool isMemberFunctionPointer() const
Is the base declaration a member function?
MemberPointer(const ValueDecl *D)
const FieldDecl * getField() const
Return the base declaration as a FieldDecl. Might return null.
MemberPointer atInstanceBase(unsigned Offset, uint8_t PathLength=0, const CXXRecordDecl **Path=nullptr, bool NewIsDerived=false) const
void print(llvm::raw_ostream &OS) const
MemberPointer withPath(uint8_t PathLength, const CXXRecordDecl **Path, bool IsDerived) const
void takePath(const CXXRecordDecl **NewPath)
Sets the path of this member pointer.
unsigned getPathLength() const
Return the length of the cast path.
ComparisonCategoryResult compare(const MemberPointer &RHS) const
const CXXRecordDecl ** path() const
Return the cast path. Might return null.
const CXXMethodDecl * getMemberFunction() const
Return the base declaration as a CXXMethodDecl. Might return null.
std::optional< Pointer > toPointer(const Context &Ctx) const
MemberPointer(Pointer Base, const ValueDecl *Dcl)
A pointer to a memory block, live or dead.
Definition Pointer.h:530
Pointer atFieldSub(unsigned Off) const
Subtract the given offset from the current Base and Offset of the pointer.
Definition Pointer.h:619
Pointer atField(unsigned Off) const
Creates a pointer to a field.
Definition Pointer.h:613
bool isZero() const
Checks if the pointer is null.
Definition Pointer.h:640
@ Address
A pointer to a ValueDecl.
Definition Primitives.h:28
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const Boolean &B)
Definition Boolean.h:147
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
Definition Address.h:330
ComparisonCategoryResult
An enumeration representing the possible results of a three-way comparison.
U cast(CodeGen::Address addr)
Definition Address.h:327
__packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 int32_t
__packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 __packed_splat2 uint8_t
__packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 __packed_splat2 __packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 uint32_t