From 83ccc1662d61f901308aa72f4ee31e788d5908b7 Mon Sep 17 00:00:00 2001 From: Rune Date: Sat, 7 Jun 2025 10:07:34 +0200 Subject: [PATCH] Add documentation reflection for comptime --- BeefLibs/corlib/src/Reflection/FieldInfo.bf | 3 + BeefLibs/corlib/src/Reflection/MethodInfo.bf | 3 + BeefLibs/corlib/src/Type.bf | 9 +++ IDEHelper/Compiler/CeMachine.cpp | 80 ++++++++++++++++++++ IDEHelper/Compiler/CeMachine.h | 3 + 5 files changed, 98 insertions(+) diff --git a/BeefLibs/corlib/src/Reflection/FieldInfo.bf b/BeefLibs/corlib/src/Reflection/FieldInfo.bf index 6de95362..c06f104c 100644 --- a/BeefLibs/corlib/src/Reflection/FieldInfo.bf +++ b/BeefLibs/corlib/src/Reflection/FieldInfo.bf @@ -37,6 +37,9 @@ namespace System.Reflection public int32 FieldIdx => Compiler.IsComptime ? mFieldData.mCustomAttributesIdx : -1; + public StringView Documentation => Compiler.IsComptime ? + Type.[Friend]Comptime_Field_GetDocumentation((.)mTypeInstance.TypeId, FieldIdx) : + null; public void GetSourceName(String outStr) { diff --git a/BeefLibs/corlib/src/Reflection/MethodInfo.bf b/BeefLibs/corlib/src/Reflection/MethodInfo.bf index da5a1d5a..caedaa10 100644 --- a/BeefLibs/corlib/src/Reflection/MethodInfo.bf +++ b/BeefLibs/corlib/src/Reflection/MethodInfo.bf @@ -66,6 +66,9 @@ namespace System.Reflection public StringView Name => Compiler.IsComptime ? Type.[Friend]Comptime_Method_GetName(mData.mComptimeMethodInstance) : mData.mMethodData.[Friend]mName; + public StringView Documentation => Compiler.IsComptime ? + Type.[Friend]Comptime_Method_GetDocumentation(mData.mComptimeMethodInstance) : + null; public void* Ptr => Compiler.IsComptime ? null : mData.mMethodData.[Friend]mFuncPtr; diff --git a/BeefLibs/corlib/src/Type.bf b/BeefLibs/corlib/src/Type.bf index 5bb91e66..abd51b30 100644 --- a/BeefLibs/corlib/src/Type.bf +++ b/BeefLibs/corlib/src/Type.bf @@ -784,6 +784,7 @@ namespace System static extern Type Comptime_GetTypeByName(StringView name); static extern String Comptime_Type_ToString(int32 typeId); static extern String Comptime_TypeName_ToString(int32 typeId); + static extern String Comptime_TypeDocumentation_ToString(int32 typeId); static extern String Comptime_Namespace_ToString(int32 typeId); static extern Type Comptime_GetSpecializedType(Type unspecializedType, Span typeArgs); static extern bool Comptime_Type_GetCustomAttribute(int32 typeId, int32 attributeIdx, void* dataPtr); @@ -796,10 +797,12 @@ namespace System static extern int64 Comptime_GetMethod(int32 typeId, int32 methodIdx); static extern String Comptime_Method_ToString(int64 methodHandle); static extern String Comptime_Method_GetName(int64 methodHandle); + static extern String Comptime_Method_GetDocumentation(int64 methodHandle); static extern ComptimeMethodData Comptime_Method_GetInfo(int64 methodHandle); static extern ComptimeParamInfo Comptime_Method_GetParamInfo(int64 methodHandle, int32 paramIdx); static extern Type Comptime_Method_GetGenericArg(int64 methodHandle, int32 genericArgIdx); static extern String Comptime_Field_GetName(int64 fieldHandle); + static extern String Comptime_Field_GetDocumentation(int32 typeId, int32 fieldIndex); static extern ComptimeFieldInfo Comptime_Field_GetInfo(int64 fieldHandle); static extern void* Comptime_Field_GetStatic(int32 typeId, int32 fieldIdx); @@ -876,6 +879,12 @@ namespace System GetBasicName(strBuffer); } + public virtual void GetDocumentation(String strBuffer) + { + if (Compiler.IsComptime) + strBuffer.Append(Comptime_TypeDocumentation_ToString((.)mTypeId)); + } + // Putting this in causes sTypes to be required when Object.ToString is reified /*public override void ToString(String strBuffer) { diff --git a/IDEHelper/Compiler/CeMachine.cpp b/IDEHelper/Compiler/CeMachine.cpp index 1adaadd4..826823a6 100644 --- a/IDEHelper/Compiler/CeMachine.cpp +++ b/IDEHelper/Compiler/CeMachine.cpp @@ -6446,7 +6446,27 @@ bool CeContext::Execute(CeFunction* startFunction, uint8* startStackPtr, uint8* SetAndRestoreValue prevTypeInstance(mCeMachine->mCeModule->mCurTypeInstance, mCallerTypeInstance); CeSetAddrVal(stackPtr + 0, GetString(str), ptrSize); _FixVariables(); + } + else if (checkFunction->mFunctionKind == CeFunctionKind_TypeDocumentation_ToString) + { + int32 typeId = *(int32*)((uint8*)stackPtr + ptrSize); + + BfType* type = GetBfType(typeId); + if (type == NULL) + { + _Fail("Invalid type"); + return false; } + + String str; + if (auto typeInst = type->ToTypeInstance()) + typeInst->mTypeDef->mTypeDeclaration->mDocumentation->GetDocString(str); + + SetAndRestoreValue prevMethodInstance(mCeMachine->mCeModule->mCurMethodInstance, mCallerMethodInstance); + SetAndRestoreValue prevTypeInstance(mCeMachine->mCeModule->mCurTypeInstance, mCallerTypeInstance); + CeSetAddrVal(stackPtr + 0, GetString(str), ptrSize); + _FixVariables(); + } else if (checkFunction->mFunctionKind == CeFunctionKind_Namespace_ToString) { int32 typeId = *(int32*)((uint8*)stackPtr + ptrSize); @@ -6671,6 +6691,24 @@ bool CeContext::Execute(CeFunction* startFunction, uint8* startStackPtr, uint8* CeSetAddrVal(stackPtr + 0, GetString(methodInstance->mMethodDef->GetReflectName()), ptrSize); _FixVariables(); } + else if (checkFunction->mFunctionKind == CeFunctionKind_Method_GetDocumentation) + { + int64 methodHandle = *(int64*)((uint8*)stackPtr + ptrSize); + + auto methodInstance = mCeMachine->GetMethodInstance(methodHandle); + if (methodInstance == NULL) + { + _Fail("Invalid method instance"); + return false; + } + + String docs; + if (auto decl = BfNodeDynCast(methodInstance->mMethodDef->mMethodDeclaration)) + decl->mDocumentation->GetDocString(docs); + + CeSetAddrVal(stackPtr + 0, GetString(docs), ptrSize); + _FixVariables(); + } else if (checkFunction->mFunctionKind == CeFunctionKind_Method_GetInfo) { // int32 mReturnType @@ -6768,6 +6806,36 @@ bool CeContext::Execute(CeFunction* startFunction, uint8* startStackPtr, uint8* _FixVariables(); CeSetAddrVal(stackPtr + 0, reflectType, ptrSize); } + else if (checkFunction->mFunctionKind == CeFunctionKind_Field_GetDocumentation) + { + int32 typeId = *(int32*)((uint8*)stackPtr + ptrSize); + int32 fieldIdx = *(int32*)((uint8*)stackPtr + ptrSize + 4); + + BfType* type = GetBfType(typeId); + if (type == NULL) + { + _Fail("Invalid type"); + return false; + } + String docs; + if (type != NULL) + { + if (auto typeInst = type->ToTypeInstance()) + { + if (fieldIdx < 0 || fieldIdx >= typeInst->mFieldInstances.size()) + { + _Fail("Invalid field"); + return false; + } + auto fieldInstance = typeInst->mFieldInstances[fieldIdx]; + if (auto decl = BfNodeDynCast(fieldInstance.GetFieldDef()->mFieldDeclaration)) + decl->mDocumentation->GetDocString(docs); + } + } + + CeSetAddrVal(stackPtr + 0, GetString(docs), ptrSize); + _FixVariables(); + } else if (checkFunction->mFunctionKind == CeFunctionKind_Field_GetStatic) { int32 typeId = *(int32*)((uint8*)stackPtr + ptrSize); @@ -10154,6 +10222,10 @@ void CeMachine::CheckFunctionKind(CeFunction* ceFunction) { ceFunction->mFunctionKind = CeFunctionKind_TypeName_ToString; } + else if (methodDef->mName == "Comptime_TypeDocumentation_ToString") + { + ceFunction->mFunctionKind = CeFunctionKind_TypeDocumentation_ToString; + } else if (methodDef->mName == "Comptime_Namespace_ToString") { ceFunction->mFunctionKind = CeFunctionKind_Namespace_ToString; @@ -10198,6 +10270,10 @@ void CeMachine::CheckFunctionKind(CeFunction* ceFunction) { ceFunction->mFunctionKind = CeFunctionKind_Method_GetName; } + else if (methodDef->mName == "Comptime_Method_GetDocumentation") + { + ceFunction->mFunctionKind = CeFunctionKind_Method_GetDocumentation; + } else if (methodDef->mName == "Comptime_Method_GetInfo") { ceFunction->mFunctionKind = CeFunctionKind_Method_GetInfo; @@ -10210,6 +10286,10 @@ void CeMachine::CheckFunctionKind(CeFunction* ceFunction) { ceFunction->mFunctionKind = CeFunctionKind_Method_GetGenericArg; } + else if (methodDef->mName == "Comptime_Field_GetDocumentation") + { + ceFunction->mFunctionKind = CeFunctionKind_Field_GetDocumentation; + } else if (methodDef->mName == "Comptime_Field_GetStatic") { ceFunction->mFunctionKind = CeFunctionKind_Field_GetStatic; diff --git a/IDEHelper/Compiler/CeMachine.h b/IDEHelper/Compiler/CeMachine.h index 1e6058f6..7445989a 100644 --- a/IDEHelper/Compiler/CeMachine.h +++ b/IDEHelper/Compiler/CeMachine.h @@ -442,6 +442,7 @@ enum CeFunctionKind CeFunctionKind_GetReflectSpecializedType, CeFunctionKind_Type_ToString, CeFunctionKind_TypeName_ToString, + CeFunctionKind_TypeDocumentation_ToString, CeFunctionKind_Namespace_ToString, CeFunctionKind_Type_GetCustomAttribute, CeFunctionKind_Field_GetCustomAttribute, @@ -453,9 +454,11 @@ enum CeFunctionKind CeFunctionKind_GetMethod, CeFunctionKind_Method_ToString, CeFunctionKind_Method_GetName, + CeFunctionKind_Method_GetDocumentation, CeFunctionKind_Method_GetInfo, CeFunctionKind_Method_GetParamInfo, CeFunctionKind_Method_GetGenericArg, + CeFunctionKind_Field_GetDocumentation, CeFunctionKind_Field_GetStatic, CeFunctionKind_SetReturnType,