Skip to content

Commit 2d63fae

Browse files
authored
[clang][bytecode][NFC] Remove PT_FnPtr (llvm#135947)
We don't need this anymore since we don't return it from classify() anymore.
1 parent 1e61b37 commit 2d63fae

11 files changed

+20
-98
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4057,7 +4057,7 @@ template <class Emitter> bool Compiler<Emitter>::visitBool(const Expr *E) {
40574057
return true;
40584058

40594059
// Convert pointers to bool.
4060-
if (T == PT_Ptr || T == PT_FnPtr) {
4060+
if (T == PT_Ptr) {
40614061
if (!this->emitNull(*T, 0, nullptr, E))
40624062
return false;
40634063
return this->emitNE(*T, E);
@@ -4103,8 +4103,6 @@ bool Compiler<Emitter>::visitZeroInitializer(PrimType T, QualType QT,
41034103
case PT_Ptr:
41044104
return this->emitNullPtr(Ctx.getASTContext().getTargetNullPointerValue(QT),
41054105
nullptr, E);
4106-
case PT_FnPtr:
4107-
return this->emitNullFnPtr(0, nullptr, E);
41084106
case PT_MemberPtr:
41094107
return this->emitNullMemberPtr(0, nullptr, E);
41104108
case PT_Float:
@@ -4255,7 +4253,6 @@ bool Compiler<Emitter>::emitConst(T Value, PrimType Ty, const Expr *E) {
42554253
case PT_Bool:
42564254
return this->emitConstBool(Value, E);
42574255
case PT_Ptr:
4258-
case PT_FnPtr:
42594256
case PT_MemberPtr:
42604257
case PT_Float:
42614258
case PT_IntAP:
@@ -4956,7 +4953,7 @@ bool Compiler<Emitter>::VisitCallExpr(const CallExpr *E) {
49564953
// If we know the callee already, check the known parametrs for nullability.
49574954
if (FuncDecl && NonNullArgs[ArgIndex]) {
49584955
PrimType ArgT = classify(Arg).value_or(PT_Ptr);
4959-
if (ArgT == PT_Ptr || ArgT == PT_FnPtr) {
4956+
if (ArgT == PT_Ptr) {
49604957
if (!this->emitCheckNonNullArg(ArgT, Arg))
49614958
return false;
49624959
}
@@ -5997,7 +5994,7 @@ bool Compiler<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
59975994
if (!this->visit(SubExpr))
59985995
return false;
59995996

6000-
if (T == PT_Ptr || T == PT_FnPtr) {
5997+
if (T == PT_Ptr) {
60015998
if (!this->emitIncPtr(E))
60025999
return false;
60036000

@@ -6021,7 +6018,7 @@ bool Compiler<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
60216018
if (!this->visit(SubExpr))
60226019
return false;
60236020

6024-
if (T == PT_Ptr || T == PT_FnPtr) {
6021+
if (T == PT_Ptr) {
60256022
if (!this->emitDecPtr(E))
60266023
return false;
60276024

@@ -6045,7 +6042,7 @@ bool Compiler<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
60456042
if (!this->visit(SubExpr))
60466043
return false;
60476044

6048-
if (T == PT_Ptr || T == PT_FnPtr) {
6045+
if (T == PT_Ptr) {
60496046
if (!this->emitLoadPtr(E))
60506047
return false;
60516048
if (!this->emitConstUint8(1, E))
@@ -6088,7 +6085,7 @@ bool Compiler<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
60886085
if (!this->visit(SubExpr))
60896086
return false;
60906087

6091-
if (T == PT_Ptr || T == PT_FnPtr) {
6088+
if (T == PT_Ptr) {
60926089
if (!this->emitLoadPtr(E))
60936090
return false;
60946091
if (!this->emitConstUint8(1, E))

clang/lib/AST/ByteCode/Disasm.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,6 @@ static const char *primTypeToString(PrimType T) {
256256
return "Float";
257257
case PT_Ptr:
258258
return "Ptr";
259-
case PT_FnPtr:
260-
return "FnPtr";
261259
case PT_MemberPtr:
262260
return "MemberPtr";
263261
case PT_FixedPoint:

clang/lib/AST/ByteCode/EvalEmitter.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,6 @@ bool EvalEmitter::speculate(const CallExpr *E, const LabelTy &EndLabel) {
142142
if (T == PT_Ptr) {
143143
const auto &Ptr = S.Stk.pop<Pointer>();
144144
return this->emitBool(CheckBCPResult(S, Ptr), E);
145-
} else if (T == PT_FnPtr) {
146-
S.Stk.discard<FunctionPointer>();
147-
// Never accepted
148-
return this->emitBool(false, E);
149145
}
150146

151147
// Otherwise, this is fine!
@@ -210,14 +206,6 @@ template <> bool EvalEmitter::emitRet<PT_Ptr>(const SourceInfo &Info) {
210206

211207
return true;
212208
}
213-
template <> bool EvalEmitter::emitRet<PT_FnPtr>(const SourceInfo &Info) {
214-
if (!isActive())
215-
return true;
216-
217-
// Function pointers cannot be converted to rvalues.
218-
EvalResult.setFunctionPointer(S.Stk.pop<FunctionPointer>());
219-
return true;
220-
}
221209

222210
bool EvalEmitter::emitRetVoid(const SourceInfo &Info) {
223211
EvalResult.setValid();

clang/lib/AST/ByteCode/FunctionPointer.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,22 @@ APValue FunctionPointer::toAPValue(const ASTContext &) const {
1616
return APValue(static_cast<Expr *>(nullptr), CharUnits::Zero(), {},
1717
/*OnePastTheEnd=*/false, /*IsNull=*/true);
1818

19-
if (!Valid)
20-
return APValue(static_cast<Expr *>(nullptr),
21-
CharUnits::fromQuantity(getIntegerRepresentation()), {},
22-
/*OnePastTheEnd=*/false, /*IsNull=*/false);
23-
2419
if (Func->getDecl())
25-
return APValue(Func->getDecl(), CharUnits::fromQuantity(Offset), {},
20+
return APValue(Func->getDecl(), CharUnits::fromQuantity(0), {},
2621
/*OnePastTheEnd=*/false, /*IsNull=*/false);
27-
return APValue(Func->getExpr(), CharUnits::fromQuantity(Offset), {},
22+
return APValue(Func->getExpr(), CharUnits::fromQuantity(0), {},
2823
/*OnePastTheEnd=*/false, /*IsNull=*/false);
2924
}
3025

3126
void FunctionPointer::print(llvm::raw_ostream &OS) const {
3227
OS << "FnPtr(";
33-
if (Func && Valid)
28+
if (Func)
3429
OS << Func->getName();
3530
else if (Func)
3631
OS << reinterpret_cast<uintptr_t>(Func);
3732
else
3833
OS << "nullptr";
39-
OS << ") + " << Offset;
34+
OS << ")";
4035
}
4136

4237
} // namespace interp

clang/lib/AST/ByteCode/FunctionPointer.h

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,15 @@ namespace interp {
2020
class FunctionPointer final {
2121
private:
2222
const Function *Func;
23-
uint64_t Offset;
24-
bool Valid;
2523

2624
public:
2725
FunctionPointer() = default;
28-
FunctionPointer(const Function *Func, uint64_t Offset = 0)
29-
: Func(Func), Offset(Offset), Valid(true) {}
30-
31-
FunctionPointer(uintptr_t IntVal, const Descriptor *Desc = nullptr)
32-
: Func(reinterpret_cast<const Function *>(IntVal)), Offset(0),
33-
Valid(false) {}
26+
FunctionPointer(const Function *Func) : Func(Func) {}
3427

3528
const Function *getFunction() const { return Func; }
36-
uint64_t getOffset() const { return Offset; }
3729
bool isZero() const { return !Func; }
38-
bool isValid() const { return Valid; }
3930
bool isWeak() const {
40-
if (!Func || !Valid || !Func->getDecl())
31+
if (!Func || !Func->getDecl())
4132
return false;
4233

4334
return Func->getDecl()->isWeak();
@@ -56,20 +47,8 @@ class FunctionPointer final {
5647
uint64_t getIntegerRepresentation() const {
5748
return static_cast<uint64_t>(reinterpret_cast<uintptr_t>(Func));
5849
}
59-
60-
ComparisonCategoryResult compare(const FunctionPointer &RHS) const {
61-
if (Func == RHS.Func && Offset == RHS.Offset)
62-
return ComparisonCategoryResult::Equal;
63-
return ComparisonCategoryResult::Unordered;
64-
}
6550
};
6651

67-
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
68-
FunctionPointer FP) {
69-
FP.print(OS);
70-
return OS;
71-
}
72-
7352
} // namespace interp
7453
} // namespace clang
7554

clang/lib/AST/ByteCode/Interp.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include "FixedPoint.h"
2121
#include "Floating.h"
2222
#include "Function.h"
23-
#include "FunctionPointer.h"
2423
#include "InterpBuiltinBitCast.h"
2524
#include "InterpFrame.h"
2625
#include "InterpStack.h"
@@ -984,26 +983,6 @@ bool CmpHelperEQ(InterpState &S, CodePtr OpPC, CompareFn Fn) {
984983
return CmpHelper<T>(S, OpPC, Fn);
985984
}
986985

987-
template <>
988-
inline bool CmpHelperEQ<FunctionPointer>(InterpState &S, CodePtr OpPC,
989-
CompareFn Fn) {
990-
const auto &RHS = S.Stk.pop<FunctionPointer>();
991-
const auto &LHS = S.Stk.pop<FunctionPointer>();
992-
993-
// We cannot compare against weak declarations at compile time.
994-
for (const auto &FP : {LHS, RHS}) {
995-
if (FP.isWeak()) {
996-
const SourceInfo &Loc = S.Current->getSource(OpPC);
997-
S.FFDiag(Loc, diag::note_constexpr_pointer_weak_comparison)
998-
<< FP.toDiagnosticString(S.getASTContext());
999-
return false;
1000-
}
1001-
}
1002-
1003-
S.Stk.push<Boolean>(Boolean::from(Fn(LHS.compare(RHS))));
1004-
return true;
1005-
}
1006-
1007986
template <>
1008987
inline bool CmpHelper<Pointer>(InterpState &S, CodePtr OpPC, CompareFn Fn) {
1009988
using BoolT = PrimConv<PT_Bool>::T;

clang/lib/AST/ByteCode/InterpBuiltin.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ static bool retPrimValue(InterpState &S, CodePtr OpPC,
130130
return Ret<X>(S, OpPC);
131131
switch (*T) {
132132
RET_CASE(PT_Ptr);
133-
RET_CASE(PT_FnPtr);
134133
RET_CASE(PT_Float);
135134
RET_CASE(PT_Bool);
136135
RET_CASE(PT_Sint8);
@@ -766,10 +765,7 @@ static bool interp__builtin_addressof(InterpState &S, CodePtr OpPC,
766765
assert(Call->getArg(0)->isLValue());
767766
PrimType PtrT = S.getContext().classify(Call->getArg(0)).value_or(PT_Ptr);
768767

769-
if (PtrT == PT_FnPtr) {
770-
const FunctionPointer &Arg = S.Stk.peek<FunctionPointer>();
771-
S.Stk.push<FunctionPointer>(Arg);
772-
} else if (PtrT == PT_Ptr) {
768+
if (PtrT == PT_Ptr) {
773769
const Pointer &Arg = S.Stk.peek<Pointer>();
774770
S.Stk.push<Pointer>(Arg);
775771
} else {

clang/lib/AST/ByteCode/InterpStack.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,6 @@ class InterpStack final {
183183
return PT_Uint64;
184184
else if constexpr (std::is_same_v<T, Floating>)
185185
return PT_Float;
186-
else if constexpr (std::is_same_v<T, FunctionPointer>)
187-
return PT_FnPtr;
188186
else if constexpr (std::is_same_v<T, IntegralAP<true>>)
189187
return PT_IntAP;
190188
else if constexpr (std::is_same_v<T, IntegralAP<false>>)

clang/lib/AST/ByteCode/Opcodes.td

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ def IntAP : Type;
2929
def IntAPS : Type;
3030
def Float : Type;
3131
def Ptr : Type;
32-
def FnPtr : Type;
3332
def MemberPtr : Type;
3433
def FixedPoint : Type;
3534

@@ -106,9 +105,7 @@ def AluTypeClass : TypeClass {
106105
let Types = !listconcat(IntegerTypeClass.Types, [Bool], [FixedPoint]);
107106
}
108107

109-
def PtrTypeClass : TypeClass {
110-
let Types = [Ptr, FnPtr, MemberPtr];
111-
}
108+
def PtrTypeClass : TypeClass { let Types = [Ptr, MemberPtr]; }
112109

113110
def NonPtrTypeClass : TypeClass {
114111
let Types = !listconcat(IntegerTypeClass.Types, [Bool], [Float], [FixedPoint]);
@@ -119,7 +116,7 @@ def AllTypeClass : TypeClass {
119116
}
120117

121118
def ComparableTypeClass : TypeClass {
122-
let Types = !listconcat(AluTypeClass.Types, [Ptr], [Float], [FnPtr]);
119+
let Types = !listconcat(AluTypeClass.Types, [Ptr], [Float]);
123120
}
124121

125122
class SingletonTypeClass<Type Ty> : TypeClass {

clang/lib/AST/ByteCode/Pointer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,10 @@ APValue Pointer::toAPValue(const ASTContext &ASTCtx) const {
155155
if (isFunctionPointer()) {
156156
const FunctionPointer &FP = asFunctionPointer();
157157
if (const FunctionDecl *FD = FP.getFunction()->getDecl())
158-
return APValue(FD, CharUnits::fromQuantity(FP.getOffset() + Offset), {},
158+
return APValue(FD, CharUnits::fromQuantity(Offset), {},
159159
/*OnePastTheEnd=*/false, /*IsNull=*/false);
160-
return APValue(FP.getFunction()->getExpr(),
161-
CharUnits::fromQuantity(FP.getOffset() + Offset), {},
160+
return APValue(FP.getFunction()->getExpr(), CharUnits::fromQuantity(Offset),
161+
{},
162162
/*OnePastTheEnd=*/false, /*IsNull=*/false);
163163
}
164164

clang/lib/AST/ByteCode/PrimType.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,11 @@ enum PrimType : unsigned {
4646
PT_FixedPoint = 11,
4747
PT_Float = 12,
4848
PT_Ptr = 13,
49-
PT_FnPtr = 14,
50-
PT_MemberPtr = 15,
49+
PT_MemberPtr = 14,
5150
};
5251

5352
inline constexpr bool isPtrType(PrimType T) {
54-
return T == PT_Ptr || T == PT_FnPtr || T == PT_MemberPtr;
53+
return T == PT_Ptr || T == PT_MemberPtr;
5554
}
5655

5756
enum class CastKind : uint8_t {
@@ -114,9 +113,6 @@ template <> struct PrimConv<PT_Bool> {
114113
template <> struct PrimConv<PT_Ptr> {
115114
using T = Pointer;
116115
};
117-
template <> struct PrimConv<PT_FnPtr> {
118-
using T = FunctionPointer;
119-
};
120116
template <> struct PrimConv<PT_MemberPtr> {
121117
using T = MemberPointer;
122118
};
@@ -166,7 +162,6 @@ static inline bool aligned(const void *P) {
166162
TYPE_SWITCH_CASE(PT_Float, B) \
167163
TYPE_SWITCH_CASE(PT_Bool, B) \
168164
TYPE_SWITCH_CASE(PT_Ptr, B) \
169-
TYPE_SWITCH_CASE(PT_FnPtr, B) \
170165
TYPE_SWITCH_CASE(PT_MemberPtr, B) \
171166
TYPE_SWITCH_CASE(PT_FixedPoint, B) \
172167
} \

0 commit comments

Comments
 (0)