1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-10 04:22:20 +02:00

Hide internal generated methods from reflection, fix ctor/dtor names

This commit is contained in:
Brian Fiete 2023-10-10 13:20:35 -07:00
parent 70ab03529e
commit 078727c4a7
10 changed files with 73 additions and 15 deletions

View file

@ -105,7 +105,7 @@ namespace System
{ {
String emitStr = scope .(); String emitStr = scope .();
for (var methodInfo in typeof(T).GetMethods(.Public)) for (var methodInfo in typeof(T).GetMethods(.Public | .DeclaredOnly))
{ {
if (methodInfo.IsStatic) if (methodInfo.IsStatic)
continue; continue;
@ -200,7 +200,7 @@ namespace System
{ {
String emitStr = scope .(); String emitStr = scope .();
for (var methodInfo in typeof(T).GetMethods(.Public)) for (var methodInfo in typeof(T).GetMethods(.Public | .DeclaredOnly))
{ {
if (methodInfo.IsStatic) if (methodInfo.IsStatic)
continue; continue;

View file

@ -53,6 +53,9 @@ namespace System.Reflection
public bool IsStatic => Compiler.IsComptime ? public bool IsStatic => Compiler.IsComptime ?
Type.[Friend]Comptime_Method_GetInfo(mData.mComptimeMethodInstance).mMethodFlags.HasFlag(.Static) : Type.[Friend]Comptime_Method_GetInfo(mData.mComptimeMethodInstance).mMethodFlags.HasFlag(.Static) :
mData.mMethodData.[Friend]mFlags.HasFlag(.Static); mData.mMethodData.[Friend]mFlags.HasFlag(.Static);
public bool CanReflect => Compiler.IsComptime ?
Type.[Friend]Comptime_Method_GetInfo(mData.mComptimeMethodInstance).mComptimeMethodFlags.HasFlag(.NoReflect) :
mData.mMethodData.[Friend]mFlags.HasFlag(.SpecialName);
public StringView Name => Compiler.IsComptime ? public StringView Name => Compiler.IsComptime ?
Type.[Friend]Comptime_Method_GetName(mData.mComptimeMethodInstance) : Type.[Friend]Comptime_Method_GetName(mData.mComptimeMethodInstance) :
@ -66,12 +69,12 @@ namespace System.Reflection
0; 0;
public bool IsConstructor => Compiler.IsComptime ? public bool IsConstructor => Compiler.IsComptime ?
(Name == "__BfCtor" || Name == "__BfStaticCtor") : Name == "this" :
(mData.mMethodData.mName === "__BfCtor" || mData.mMethodData.mName === "__BfStaticCtor"); mData.mMethodData.mName === "this";
public bool IsDestructor => Compiler.IsComptime ? public bool IsDestructor => Compiler.IsComptime ?
(Name == "__BfDtor" || Name == "__BfStaticDtor") : Name == "~this" :
(mData.mMethodData.mName === "__BfDtor" || mData.mMethodData.mName === "__BfStaticDtor"); mData.mMethodData.mName === "~this";
public Type ReturnType => Compiler.IsComptime ? public Type ReturnType => Compiler.IsComptime ?
Type.[Friend]GetType((.)Type.[Friend]Comptime_Method_GetInfo(mData.mComptimeMethodInstance).mReturnTypeId) : Type.[Friend]GetType((.)Type.[Friend]Comptime_Method_GetInfo(mData.mComptimeMethodInstance).mReturnTypeId) :
@ -1071,9 +1074,19 @@ namespace System.Reflection
mIdx++; mIdx++;
int64 nativeMethodHandle = Type.[Friend]Comptime_GetMethod((int32)mTypeInstance.TypeId, mIdx); int64 nativeMethodHandle = Type.[Friend]Comptime_GetMethod((int32)mTypeInstance.TypeId, mIdx);
if (nativeMethodHandle == 0) if (nativeMethodHandle == 0)
return false; {
let info = Type.[Friend]Comptime_Method_GetInfo(nativeMethodHandle); if (mBindingFlags.HasFlag(.DeclaredOnly))
return false;
if (mTypeInstance.[Friend]mBaseType == 0)
return false;
mTypeInstance = Type.[Friend]GetType(mTypeInstance.[Friend]mBaseType) as TypeInstance;
mIdx = -1;
continue;
}
let info = Type.[Friend]Comptime_Method_GetInfo(nativeMethodHandle);
if (info.mComptimeMethodFlags.HasFlag(.NoReflect))
continue;
bool matches = (mBindingFlags.HasFlag(BindingFlags.Static) && (info.mMethodFlags.HasFlag(.Static))); bool matches = (mBindingFlags.HasFlag(BindingFlags.Static) && (info.mMethodFlags.HasFlag(.Static)));
matches |= (mBindingFlags.HasFlag(BindingFlags.Instance) && (!info.mMethodFlags.HasFlag(.Static))); matches |= (mBindingFlags.HasFlag(BindingFlags.Instance) && (!info.mMethodFlags.HasFlag(.Static)));
matches |= (mBindingFlags.HasFlag(BindingFlags.Public) && (info.mMethodFlags.HasFlag(.Public))); matches |= (mBindingFlags.HasFlag(BindingFlags.Public) && (info.mMethodFlags.HasFlag(.Public)));

View file

@ -514,6 +514,12 @@ namespace System
return (int32)mTypeId; return (int32)mTypeId;
} }
public enum ComptimeMethodFlags : int8
{
None,
NoReflect
}
[CRepr, Packed] [CRepr, Packed]
public struct ComptimeMethodData public struct ComptimeMethodData
{ {
@ -521,6 +527,7 @@ namespace System
public int32 mParamCount; public int32 mParamCount;
public int32 mGenericArgCount; public int32 mGenericArgCount;
public MethodFlags mMethodFlags; public MethodFlags mMethodFlags;
public ComptimeMethodFlags mComptimeMethodFlags;
public int32 mMethodIdx; public int32 mMethodIdx;
} }

View file

@ -7292,6 +7292,8 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
continue; continue;
if (defaultMethod->mMethodDef->mMethodType == BfMethodType_CtorNoBody) if (defaultMethod->mMethodDef->mMethodType == BfMethodType_CtorNoBody)
continue; continue;
if (!defaultMethod->mMethodDef->CanReflect())
continue;
auto methodReflectKind = (BfReflectKind)(reflectKind & ~BfReflectKind_User); auto methodReflectKind = (BfReflectKind)(reflectKind & ~BfReflectKind_User);
@ -7394,7 +7396,7 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
funcVal = mBfIRBuilder->CreateBitCast(moduleMethodInstance.mFunc, voidPtrIRType); funcVal = mBfIRBuilder->CreateBitCast(moduleMethodInstance.mFunc, voidPtrIRType);
} }
BfIRValue methodNameConst = GetStringObjectValue(methodDef->mName, !mIsComptimeModule); BfIRValue methodNameConst = GetStringObjectValue(methodDef->GetReflectName(), !mIsComptimeModule);
BfMethodFlags methodFlags = defaultMethod->GetMethodFlags(); BfMethodFlags methodFlags = defaultMethod->GetMethodFlags();

View file

@ -842,6 +842,14 @@ BfMethodFlags BfMethodInstance::GetMethodFlags()
return methodFlags; return methodFlags;
} }
BfComptimeMethodFlags BfMethodInstance::GetComptimeMethodFlags()
{
BfComptimeMethodFlags methodFlags = (BfComptimeMethodFlags)0;
if (!mMethodDef->CanReflect())
methodFlags = (BfComptimeMethodFlags)(methodFlags | BfComptimeMethodFlags_NoReflect);
return methodFlags;
}
void BfMethodInstance::UndoDeclaration(bool keepIRFunction) void BfMethodInstance::UndoDeclaration(bool keepIRFunction)
{ {
if (mMethodInfoEx != NULL) if (mMethodInfoEx != NULL)

View file

@ -973,6 +973,7 @@ public:
BfImportKind GetImportKind(); BfImportKind GetImportKind();
BfMethodFlags GetMethodFlags(); BfMethodFlags GetMethodFlags();
BfComptimeMethodFlags GetComptimeMethodFlags();
void UndoDeclaration(bool keepIRFunction = false); void UndoDeclaration(bool keepIRFunction = false);
BfTypeInstance* GetOwner(); BfTypeInstance* GetOwner();
BfModule* GetModule(); BfModule* GetModule();

View file

@ -648,6 +648,15 @@ String BfMethodDef::ToString()
return methodText; return methodText;
} }
String BfMethodDef::GetReflectName()
{
if (mMethodType == BfMethodType_Ctor)
return "this";
if (mMethodType == BfMethodType_Dtor)
return "~this";
return mName;
}
int BfMethodDef::GetExplicitParamCount() int BfMethodDef::GetExplicitParamCount()
{ {
for (int i = 0; i < (int)mParams.size(); i++) for (int i = 0; i < (int)mParams.size(); i++)
@ -675,6 +684,15 @@ void BfMethodDef::BuildParamNameMap()
(*mParamNameMap)[mParams[i]->mName] = i - startIdx; (*mParamNameMap)[mParams[i]->mName] = i - startIdx;
} }
bool BfMethodDef::CanReflect()
{
if (mMethodDeclaration != NULL)
return true;
if (mMethodType == BfMethodType_Ctor)
return true;
return false;
}
/// ///
void BfTypeDef::Reset() void BfTypeDef::Reset()

View file

@ -240,6 +240,12 @@ enum BfMethodFlags
BfMethodFlags_Constructor = 0x8000 BfMethodFlags_Constructor = 0x8000
}; };
enum BfComptimeMethodFlags
{
BfComptimeMethodFlags_None = 0,
BfComptimeMethodFlags_NoReflect = 1
};
enum BfObjectFlags : uint8 enum BfObjectFlags : uint8
{ {
BfObjectFlag_None = 0, BfObjectFlag_None = 0,
@ -970,8 +976,10 @@ public:
bool IsDefaultCtor(); bool IsDefaultCtor();
bool IsCtorOrInit(); bool IsCtorOrInit();
String ToString(); String ToString();
String GetReflectName();
int GetExplicitParamCount(); int GetExplicitParamCount();
void BuildParamNameMap(); void BuildParamNameMap();
bool CanReflect();
}; };
class BfOperatorDef : public BfMethodDef class BfOperatorDef : public BfMethodDef

View file

@ -6220,7 +6220,7 @@ bool CeContext::Execute(CeFunction* startFunction, uint8* startStackPtr, uint8*
return false; return false;
} }
CeSetAddrVal(stackPtr + 0, GetString(methodInstance->mMethodDef->mName), ptrSize); CeSetAddrVal(stackPtr + 0, GetString(methodInstance->mMethodDef->GetReflectName()), ptrSize);
_FixVariables(); _FixVariables();
} }
else if (checkFunction->mFunctionKind == CeFunctionKind_Method_GetInfo) else if (checkFunction->mFunctionKind == CeFunctionKind_Method_GetInfo)
@ -6231,7 +6231,7 @@ bool CeContext::Execute(CeFunction* startFunction, uint8* startStackPtr, uint8*
// int16 mFlags // int16 mFlags
// int32 mMethodIdx // int32 mMethodIdx
int64 methodHandle = *(int64*)((uint8*)stackPtr + 4+4+4+2+4); int64 methodHandle = *(int64*)((uint8*)stackPtr + 4+4+4+2+1+4);
auto methodInstance = mCeMachine->GetMethodInstance(methodHandle); auto methodInstance = mCeMachine->GetMethodInstance(methodHandle);
if (methodInstance == NULL) if (methodInstance == NULL)
@ -6248,7 +6248,8 @@ bool CeContext::Execute(CeFunction* startFunction, uint8* startStackPtr, uint8*
*(int32*)(stackPtr + 4) = methodInstance->GetParamCount(); *(int32*)(stackPtr + 4) = methodInstance->GetParamCount();
*(int32*)(stackPtr + 4+4) = genericArgCount; *(int32*)(stackPtr + 4+4) = genericArgCount;
*(int16*)(stackPtr + 4+4+4) = methodInstance->GetMethodFlags(); *(int16*)(stackPtr + 4+4+4) = methodInstance->GetMethodFlags();
*(int32*)(stackPtr + 4+4+4+2) = methodInstance->mMethodDef->mIdx; *(int32*)(stackPtr + 4+4+4+2) = methodInstance->GetComptimeMethodFlags();
*(int32*)(stackPtr + 4+4+4+2+1) = methodInstance->mMethodDef->mIdx;
} }
else if (checkFunction->mFunctionKind == CeFunctionKind_Method_GetParamInfo) else if (checkFunction->mFunctionKind == CeFunctionKind_Method_GetParamInfo)
{ {

View file

@ -278,10 +278,10 @@ namespace Tests
switch (methodIdx) switch (methodIdx)
{ {
case 0: case 0:
Test.Assert(methodInfo.Name == "__BfCtor"); Test.Assert(methodInfo.Name == "this");
Test.Assert(methodInfo.IsConstructor); Test.Assert(methodInfo.IsConstructor);
case 1: case 1:
Test.Assert(methodInfo.Name == "__BfStaticCtor"); Test.Assert(methodInfo.Name == "this");
Test.Assert(methodInfo.IsConstructor); Test.Assert(methodInfo.IsConstructor);
case 2: case 2:
Test.Assert(methodInfo.Name == "GetA"); Test.Assert(methodInfo.Name == "GetA");
@ -509,7 +509,7 @@ namespace Tests
switch (methodIdx) switch (methodIdx)
{ {
case 0: case 0:
Test.Assert(methodInfo.Name == "__BfCtor"); Test.Assert(methodInfo.Name == "this");
case 1: case 1:
Test.Assert(methodInfo.Name == "GetA"); Test.Assert(methodInfo.Name == "GetA");