浏览代码

Add documentation reflection for comptime

Rune 1 月之前
父节点
当前提交
83ccc1662d

+ 3 - 0
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)
 		{

+ 3 - 0
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;

+ 9 - 0
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<Type> 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)
         {

+ 80 - 0
IDEHelper/Compiler/CeMachine.cpp

@@ -6446,7 +6446,27 @@ bool CeContext::Execute(CeFunction* startFunction, uint8* startStackPtr, uint8*
 				SetAndRestoreValue<BfTypeInstance*> 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<BfMethodInstance*> prevMethodInstance(mCeMachine->mCeModule->mCurMethodInstance, mCallerMethodInstance);
+				SetAndRestoreValue<BfTypeInstance*> 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<BfMethodDeclaration>(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<BfFieldDeclaration>(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;

+ 3 - 0
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,