Browse Source

Merged comptime reflection info into normal reflection data types

Brian Fiete 3 years ago
parent
commit
e7f0b21cf6

+ 3 - 3
BeefLibs/corlib/src/Attribute.bf

@@ -552,12 +552,12 @@ namespace System
 
 	interface IOnFieldInit
 	{
-		void OnFieldInit(ComptimeFieldInfo type, Self* prev) mut;
+		void OnFieldInit(FieldInfo fieldInfo, Self* prev) mut;
 	}
 
 	interface IOnMethodInit
 	{
-		void OnMethodInit(ComptimeMethodInfo type, Self* prev) mut;
+		void OnMethodInit(MethodInfo methodInfo, Self* prev) mut;
 	}
 
 	interface IComptimeTypeApply
@@ -567,6 +567,6 @@ namespace System
 
 	interface IComptimeMethodApply
 	{
-		void ApplyToMethod(ComptimeMethodInfo methodInfo);
+		void ApplyToMethod(MethodInfo methodInfo);
 	}
 }

+ 3 - 3
BeefLibs/corlib/src/Bitfield.bf

@@ -50,7 +50,7 @@ namespace System
 		}
 
 		[Comptime]
-		public void OnFieldInit(ComptimeFieldInfo fieldInfo, Self* prev) mut
+		public void OnFieldInit(FieldInfo fieldInfo, Self* prev) mut
 		{
 			if (mBFieldType == null)
 				mBFieldType = fieldInfo.FieldType;
@@ -149,7 +149,7 @@ namespace System
 				str.Append("protected ");
 			if (fieldInfo.IsStatic)
 				str.Append("static ");
-			bool wantsMut = fieldInfo.Owner.IsStruct && !fieldInfo.IsStatic;
+			bool wantsMut = fieldInfo.DeclaringType.IsStruct && !fieldInfo.IsStatic;
 
 			void TypeStr(String str, Type t)
 			{
@@ -235,7 +235,7 @@ namespace System
 
 			str.Append("}\n");
 
-			Compiler.EmitTypeBody(fieldInfo.Owner, str);
+			Compiler.EmitTypeBody(fieldInfo.DeclaringType, str);
 
 			if (!isRev)
 				mBitPos += bitCount;

+ 4 - 4
BeefLibs/corlib/src/Compiler.bf

@@ -290,15 +290,15 @@ namespace System
 		}
 
 		[Comptime(OnlyFromComptime=true)]
-		public static void EmitMethodEntry(ComptimeMethodInfo methodHandle, StringView text)
+		public static void EmitMethodEntry(MethodInfo methodHandle, StringView text)
 		{
-			Comptime_EmitMethodEntry(methodHandle.mNativeMethodInstance, text);
+			Comptime_EmitMethodEntry(methodHandle.[Friend]mData.mComptimeMethodInstance, text);
 		}
 
 		[Comptime(OnlyFromComptime=true)]
-		public static void EmitMethodExit(ComptimeMethodInfo methodHandle, StringView text)
+		public static void EmitMethodExit(MethodInfo methodHandle, StringView text)
 		{
-			Comptime_EmitMethodExit(methodHandle.mNativeMethodInstance, text);
+			Comptime_EmitMethodExit(methodHandle.[Friend]mData.mComptimeMethodInstance, text);
 		}
 
 		[Comptime(ConstEval=true)]

+ 1 - 1
BeefLibs/corlib/src/Reflection/AttributeInfo.bf

@@ -60,7 +60,7 @@ namespace System.Reflection
 			    TypeInstance attrTypeInst = attrType as TypeInstance;
 				MethodInfo methodInfo = .(attrTypeInst, attrTypeInst.[Friend]mMethodDataPtr + methodIdx);
 
-			    Object[] args = scope Object[methodInfo.[Friend]mMethodData.mParamCount];
+			    Object[] args = scope Object[methodInfo.[Friend]mData.mMethodData.mParamCount];
 
 				int argIdx = 0;
 			    while (data < endPtr)

+ 0 - 180
BeefLibs/corlib/src/Reflection/CEMethodInfo.bf

@@ -1,180 +0,0 @@
-using System.Diagnostics;
-using System.Collections;
-
-namespace System.Reflection
-{
-	struct ComptimeMethodInfo
-	{
-		[CRepr, Packed]
-		public struct Info
-		{
-			public int32 mReturnTypeId;
-			public int32 mParamCount;
-			public MethodFlags mMethodFlags;
-		}
-
-		[CRepr, Packed]
-		public struct ParamInfo
-		{
-			public int32 mParamTypeId;
-			public TypeInstance.ParamFlags mParamFlags;
-			public String mName;
-		}
-
-		public int64 mNativeMethodInstance;
-
-		public bool IsInitialized => true;
-		public StringView Name
-		{
-			get
-			{
-				if (Compiler.IsComptime)
-					return Type.[Friend]Comptime_Method_GetName(mNativeMethodInstance);
-				return "?";
-			}
-		}
-		public int ParamCount
-		{
-			get
-			{
-				if (Compiler.IsComptime)
-					return Type.[Friend]Comptime_Method_GetInfo(mNativeMethodInstance).mParamCount;
-				return 0;
-			}
-		}
-		public bool IsConstructor => Name == "__BfCtor" || Name == "__BfStaticCtor";
-		public bool IsDestructor => Name == "__BfStaticDtor" || Name == "__BfStaticDtor";
-		public Type ReturnType
-		{
-			get
-			{
-				if (Compiler.IsComptime)
-					return Type.[Friend]GetType((.)Type.[Friend]Comptime_Method_GetInfo(mNativeMethodInstance).mReturnTypeId);
-				return null;
-			}
-		}
-
-		public this(int64 nativeMethodInstance)
-		{
-			mNativeMethodInstance = nativeMethodInstance;
-		}
-
-		public Type GetParamType(int paramIdx)
-		{
-			if (Compiler.IsComptime)
-				return Type.[Friend]GetType((.)Type.[Friend]Comptime_Method_GetParamInfo(mNativeMethodInstance, (.)paramIdx).mParamTypeId);
-			return null;
-		}
-
-		public StringView GetParamName(int paramIdx)
-		{
-			if (Compiler.IsComptime)
-				return Type.[Friend]Comptime_Method_GetParamInfo(mNativeMethodInstance, (.)paramIdx).mName;
-			return default;
-		}
-
-		public override void ToString(String strBuffer)
-		{
-			if (Compiler.IsComptime)
-			{
-				String str = Type.[Friend]Comptime_Method_ToString(mNativeMethodInstance);
-				strBuffer.Append(str);
-			}
-		}
-
-		public struct Enumerator : IEnumerator<ComptimeMethodInfo>
-		{
-			BindingFlags mBindingFlags;
-			TypeInstance mTypeInstance;
-		    int32 mIdx;
-			int32 mCount;
-
-		    public this(TypeInstance typeInst, BindingFlags bindingFlags)
-		    {
-				//Debug.WriteLine($"this {typeInst}");
-
-		        mTypeInstance = typeInst;
-				mBindingFlags = bindingFlags;
-		        mIdx = -1;
-				if ((mTypeInstance == null) || (!Compiler.IsComptime))
-					mCount = 0;
-				else
-					mCount = Type.[Friend]Comptime_GetMethodCount((.)mTypeInstance.TypeId);
-		    }
-
-		    public void Reset() mut
-		    {
-		        mIdx = -1;
-		    }
-
-		    public void Dispose()
-		    {
-		    }
-
-		    public bool MoveNext() mut
-		    {
-				if (mTypeInstance == null)
-					return false;
-
-				for (;;)
-				{
-					mIdx++;
-					if (mIdx == mCount)
-						return false;
-
-					int64 nativeMethodHandle = Type.[Friend]Comptime_GetMethod((int32)mTypeInstance.TypeId, mIdx);
-					let info = Type.[Friend]Comptime_Method_GetInfo(nativeMethodHandle);
-
-					bool matches = (mBindingFlags.HasFlag(BindingFlags.Static) && (info.mMethodFlags.HasFlag(.Static)));
-					matches |= (mBindingFlags.HasFlag(BindingFlags.Instance) && (!info.mMethodFlags.HasFlag(.Static)));
-					if (matches)
-						break;
-				}
-		        return true;
-		    }
-
-		    public ComptimeMethodInfo Current
-		    {
-		        get
-		        {
-					int64 nativeMethodHandle = Type.[Friend]Comptime_GetMethod((int32)mTypeInstance.TypeId, mIdx);
-		            return ComptimeMethodInfo(nativeMethodHandle);
-		        }
-		    }
-
-			public Result<ComptimeMethodInfo> GetNext() mut
-			{
-				if (!MoveNext())
-					return .Err;
-				return Current;
-			}
-		}
-	}
-
-	[Ordered]
-	struct ComptimeFieldInfo
-	{
-		int64 mNativeFieldInstance;
-		TypeId mOwner;
-		TypeId mTypeId;
-		int32 mFieldIdx;
-		FieldFlags mFlags;
-
-		public StringView Name
-		{
-			get
-			{
-				if (Compiler.IsComptime)
-					return Type.[Friend]Comptime_Field_GetName(mNativeFieldInstance);
-				return "?";
-			}
-		}
-
-		public Type Owner => Type.[Friend]GetType_((.)mOwner);
-		public Type FieldType => Type.[Friend]GetType_((.)mTypeId);
-		public int FieldIdx => mFieldIdx;
-		public bool IsConst => mFlags.HasFlag(.Const);
-		public bool IsStatic => mFlags.HasFlag(.Static);
-		public bool IsInstanceField => !mFlags.HasFlag(.Static) && !mFlags.HasFlag(.Const);
-	}
-}

+ 3 - 0
BeefLibs/corlib/src/Reflection/FieldInfo.bf

@@ -27,6 +27,9 @@ namespace System.Reflection
 		public bool IsStatic => mFieldData.mFlags.HasFlag(.Static);
 		public bool IsInstanceField => !mFieldData.mFlags.HasFlag(.Static) && !mFieldData.mFlags.HasFlag(.Const);
 		public StringView Name => mFieldData.mName;
+		public int32 FieldIdx => Compiler.IsComptime ?
+			mFieldData.mCustomAttributesIdx :
+			-1;
 
 	    public Result<void, Error> SetValue(Object obj, Object value)
 	    {    

+ 159 - 66
BeefLibs/corlib/src/Reflection/MethodInfo.bf

@@ -9,49 +9,101 @@ namespace System.Reflection
 	[CRepr, AlwaysInclude]
 	public struct MethodInfo
 	{
+		[Union]
+		struct Data
+		{
+			public TypeInstance.MethodData* mMethodData;
+			public int64 mComptimeMethodInstance;
+		}
+
 		TypeInstance mTypeInstance;
-		TypeInstance.MethodData* mMethodData;
+		Data mData;
 
 		public this(TypeInstance typeInstance, TypeInstance.MethodData* methodData)
 		{
 		    mTypeInstance = typeInstance;
-		    mMethodData = methodData;
+		    mData.mMethodData = methodData;
+		}
+
+		public this(TypeInstance typeInstance, int64 comptimeMethodInstance)
+		{
+			mTypeInstance = typeInstance;
+			mData.mMethodData = null;
+			mData.mComptimeMethodInstance = comptimeMethodInstance;
 		}
 
 		public TypeInstance DeclaringType => mTypeInstance;
-		public bool IsInitialized => mMethodData != null;
-		public StringView Name => mMethodData.[Friend]mName;
-		public int ParamCount => mMethodData.[Friend]mParamCount;
-		public bool IsConstructor => mMethodData.mName === "__BfCtor" || mMethodData.mName === "__BfStaticCtor";
-		public bool IsDestructor => mMethodData.mName === "__BfStaticDtor" || mMethodData.mName === "__BfStaticDtor";
-		public Type ReturnType => Type.[Friend]GetType(mMethodData.mReturnType);
+
+		public bool IsInitialized => Compiler.IsComptime ?
+			(mData.mComptimeMethodInstance != 0) :
+			(mData.mMethodData != null);
+
+		public StringView Name => Compiler.IsComptime ?
+			Type.[Friend]Comptime_Method_GetName(mData.mComptimeMethodInstance) :
+			mData.mMethodData.[Friend]mName;
+
+		public int ParamCount => Compiler.IsComptime ?
+			Type.[Friend]Comptime_Method_GetInfo(mData.mComptimeMethodInstance).mParamCount :
+			mData.mMethodData.[Friend]mParamCount;
+
+		public bool IsConstructor => Compiler.IsComptime ?
+			(Name == "__BfCtor" || Name == "__BfStaticCtor") :
+			(mData.mMethodData.mName === "__BfCtor" || mData.mMethodData.mName === "__BfStaticCtor");
+
+		public bool IsDestructor => Compiler.IsComptime ?
+			(Name == "__BfDtor" || Name == "__BfStaticDtor") :
+			(mData.mMethodData.mName === "__BfDtor" || mData.mMethodData.mName === "__BfStaticDtor");
+
+		public Type ReturnType => Compiler.IsComptime ?
+			Type.[Friend]GetType((.)Type.[Friend]Comptime_Method_GetInfo(mData.mComptimeMethodInstance).mReturnTypeId) :
+			Type.[Friend]GetType(mData.mMethodData.mReturnType);
 		
 		public Type GetParamType(int paramIdx)
 		{
-			Debug.Assert((uint)paramIdx < (uint)mMethodData.mParamCount);
-			return Type.[Friend]GetType(mMethodData.mParamData[paramIdx].mType);
+			if (Compiler.IsComptime)
+			{
+				return Type.[Friend]GetType((.)Type.[Friend]Comptime_Method_GetParamInfo(mData.mComptimeMethodInstance, (.)paramIdx).mParamTypeId);
+			}
+			else
+			{
+				Debug.Assert((uint)paramIdx < (uint)mData.mMethodData.mParamCount);
+				return Type.[Friend]GetType(mData.mMethodData.mParamData[paramIdx].mType);
+			}
 		}
 
 		public StringView GetParamName(int paramIdx)
 		{
-			Debug.Assert((uint)paramIdx < (uint)mMethodData.mParamCount);
-			return mMethodData.mParamData[paramIdx].mName;
+			if (Compiler.IsComptime)
+			{
+				return Type.[Friend]Comptime_Method_GetParamInfo(mData.mComptimeMethodInstance, (.)paramIdx).mName;
+			}
+			else
+			{
+				Debug.Assert((uint)paramIdx < (uint)mData.mMethodData.mParamCount);
+				return mData.mMethodData.mParamData[paramIdx].mName;
+			}
 		}
 
 		public Result<T> GetParamCustomAttribute<T>(int paramIdx) where T : Attribute
 		{
-			Debug.Assert((uint)paramIdx < (uint)mMethodData.mParamCount);
-			return mTypeInstance.[Friend]GetCustomAttribute<T>(mMethodData.mParamData[paramIdx].mCustomAttributesIdx);
+			if (Compiler.IsComptime)
+				return .Err;
+			Debug.Assert((uint)paramIdx < (uint)mData.mMethodData.mParamCount);
+			return mTypeInstance.[Friend]GetCustomAttribute<T>(mData.mMethodData.mParamData[paramIdx].mCustomAttributesIdx);
 		}
 
 		public Result<T> GetCustomAttribute<T>() where T : Attribute
 		{
-			return mTypeInstance.[Friend]GetCustomAttribute<T>(mMethodData.mCustomAttributesIdx);
+			if (Compiler.IsComptime)
+				return .Err;
+			return mTypeInstance.[Friend]GetCustomAttribute<T>(mData.mMethodData.mCustomAttributesIdx);
 		}
 
 		public Result<T> GetReturnCustomAttribute<T>() where T : Attribute
 		{
-			return mTypeInstance.[Friend]GetCustomAttribute<T>(mMethodData.mReturnCustomAttributesIdx);
+			if (Compiler.IsComptime)
+				return .Err;
+			return mTypeInstance.[Friend]GetCustomAttribute<T>(mData.mMethodData.mReturnCustomAttributesIdx);
 		}
 
 		public enum CallError
@@ -67,13 +119,15 @@ namespace System.Reflection
 
 		public Result<Variant, CallError> Invoke(Variant target, params Span<Variant> args)
 		{
-			var retType = Type.[Friend]GetType(mMethodData.mReturnType);
+			if (Compiler.IsComptime)
+				return .Err(.InvalidTarget);
+			var retType = Type.[Friend]GetType(mData.mMethodData.mReturnType);
 
 			FFIABI abi = .Default;
 #if BF_PLATFORM_WINDOWS && BF_32_BIT
-			if (mMethodData.mFlags.HasFlag(.ThisCall))
+			if (mData.mMethodData.mFlags.HasFlag(.ThisCall))
 				abi = .ThisCall;
-			else if (!mMethodData.mFlags.HasFlag(.Static))
+			else if (!mData.mMethodData.mFlags.HasFlag(.Static))
 				abi = .StdCall;
 #endif
 
@@ -265,7 +319,7 @@ namespace System.Reflection
 
 			void* funcPtr = null;
 			int ifaceOffset = -1;
-			if (mMethodData.mFlags.HasFlag(.Static))
+			if (mData.mMethodData.mFlags.HasFlag(.Static))
 			{
 				if (target.HasValue)
 					return .Err(.TargetNotExpected);
@@ -310,7 +364,7 @@ namespace System.Reflection
 						if (interfaceData == null)
 							return .Err(.InvalidTarget);
 
-						int ifaceMethodIdx = interfaceData.mStartInterfaceTableIdx + mMethodData.mMethodIdx;
+						int ifaceMethodIdx = interfaceData.mStartInterfaceTableIdx + mData.mMethodData.mMethodIdx;
 						if (ifaceMethodIdx >= thisType.[Friend]mInterfaceMethodCount)
 							return .Err(.InvalidTarget);
 						funcPtr = *(thisType.[Friend]mInterfaceMethodTable + ifaceMethodIdx);
@@ -319,7 +373,7 @@ namespace System.Reflection
 					ifaceOffset = mTypeInstance.[Friend]mMemberDataOffset;
 				}
 
-				bool splatThis = thisType.IsSplattable && !mMethodData.mFlags.HasFlag(.Mutating);
+				bool splatThis = thisType.IsSplattable && !mData.mMethodData.mFlags.HasFlag(.Mutating);
 #if BF_PLATFORM_WINDOWS && BF_32_BIT
 				if ((mTypeInstance.IsInterface) && (splatThis))
 					abi = .MS_CDecl;
@@ -327,7 +381,7 @@ namespace System.Reflection
 				AddArg!::(-1, ref target, &target.[Friend]mData, thisType, splatThis);
 			}
 
-			if (args.Length != mMethodData.mParamCount)
+			if (args.Length != mData.mMethodData.mParamCount)
 				return .Err(.ParamCountMismatch);
 
 			var variantData = Variant.Alloc(retType, var retVal);
@@ -348,7 +402,7 @@ namespace System.Reflection
 
 			for (var arg in ref args)
 			{
-				let paramData = ref mMethodData.mParamData[@arg.Index];
+				let paramData = ref mData.mMethodData.mParamData[@arg.Index];
 				let argType = Type.[Friend]GetType(paramData.mType);
 				AddArg!::(@arg.Index, ref arg, &arg.[Friend]mData, argType, paramData.mParamFlags.HasFlag(.Splat));
 			}
@@ -367,8 +421,8 @@ namespace System.Reflection
 
 			if (funcPtr == null)
 			{
-				funcPtr = mMethodData.mFuncPtr;
-				if (mMethodData.mFlags.HasFlag(.Virtual))
+				funcPtr = mData.mMethodData.mFuncPtr;
+				if (mData.mMethodData.mFlags.HasFlag(.Virtual))
 				{
 					Object objTarget = target.Get<Object>();
 	
@@ -380,16 +434,16 @@ namespace System.Reflection
 					if (ifaceOffset >= 0)
 					{
 						void* ifaceVirtualTable = *(void**)((uint8*)classVData + ifaceOffset);
-						funcPtr = (void*)*(int*)((uint8*)ifaceVirtualTable + mMethodData.mVirtualIdx);
+						funcPtr = (void*)*(int*)((uint8*)ifaceVirtualTable + mData.mMethodData.mVirtualIdx);
 					}
-					else if (mMethodData.mVirtualIdx >= 0x100000)
+					else if (mData.mMethodData.mVirtualIdx >= 0x100000)
 					{
-						void* extAddr = (void*)*((int*)classVData + ((mMethodData.mVirtualIdx>>20) - 1));
-						funcPtr = (void*)*((int*)extAddr + (mMethodData.mVirtualIdx & 0xFFFFF));
+						void* extAddr = (void*)*((int*)classVData + ((mData.mMethodData.mVirtualIdx>>20) - 1));
+						funcPtr = (void*)*((int*)extAddr + (mData.mMethodData.mVirtualIdx & 0xFFFFF));
 					}
 					else
 					{
-						funcPtr = (void*)*(int*)((uint8*)classVData + mMethodData.mVirtualIdx);
+						funcPtr = (void*)*(int*)((uint8*)classVData + mData.mMethodData.mVirtualIdx);
 					}
 				}
 			}
@@ -407,13 +461,15 @@ namespace System.Reflection
 
 		public Result<Variant, CallError> Invoke(Object target, params Object[] args)
 		{
-			var retType = Type.[Friend]GetType(mMethodData.mReturnType);
+			if (Compiler.IsComptime)
+				return .Err(.InvalidTarget);
+			var retType = Type.[Friend]GetType(mData.mMethodData.mReturnType);
 
 			FFIABI abi = .Default;
 #if BF_PLATFORM_WINDOWS && BF_32_BIT
-			if (mMethodData.mFlags.HasFlag(.ThisCall))
+			if (mData.mMethodData.mFlags.HasFlag(.ThisCall))
 				abi = .ThisCall;
-			else if (!mMethodData.mFlags.HasFlag(.Static))
+			else if (!mData.mMethodData.mFlags.HasFlag(.Static))
 				abi = .StdCall;
 #endif
 
@@ -619,10 +675,10 @@ namespace System.Reflection
 				}
 			}
 
-			void* funcPtr = mMethodData.mFuncPtr;
+			void* funcPtr = mData.mMethodData.mFuncPtr;
 			int virtualOffset = 0;
 			int ifaceOffset = -1;
-			if (mMethodData.mFlags.HasFlag(.Static))
+			if (mData.mMethodData.mFlags.HasFlag(.Static))
 			{
 				if (target != null)
 					return .Err(.TargetNotExpected);
@@ -662,11 +718,11 @@ namespace System.Reflection
 					virtualOffset = interfaceData.mStartVirtualIdx * sizeof(int);*/
 				}
 
-				bool splatThis = thisType.IsSplattable && !mMethodData.mFlags.HasFlag(.Mutating);
+				bool splatThis = thisType.IsSplattable && !mData.mMethodData.mFlags.HasFlag(.Mutating);
 				AddArg!::(-1, target, &target, thisType, splatThis);
 			}
 
-			if (args.Count != mMethodData.mParamCount)
+			if (args.Count != mData.mMethodData.mParamCount)
 				return .Err(.ParamCountMismatch);
 
 			var variantData = Variant.Alloc(retType, var retVal);
@@ -687,7 +743,7 @@ namespace System.Reflection
 
 			for (var arg in ref args)
 			{
-				let paramData = ref mMethodData.mParamData[@arg];
+				let paramData = ref mData.mMethodData.mParamData[@arg];
 				let argType = Type.[Friend]GetType(paramData.mType);
 				AddArg!::(@arg, arg, &arg, argType, paramData.mParamFlags.HasFlag(.Splat));
 			}
@@ -704,7 +760,7 @@ namespace System.Reflection
 					return .Err(.FFIError);
 			}
 
-			if (mMethodData.mFlags.HasFlag(.Virtual))
+			if (mData.mMethodData.mFlags.HasFlag(.Virtual))
 			{
 #if BF_ENABLE_OBJECT_DEBUG_FLAGS
 				void* classVData = (void*)(target.[Friend]mClassVData & ~0xFF);
@@ -714,16 +770,16 @@ namespace System.Reflection
 				if (ifaceOffset >= 0)
 				{
 					void* ifaceVirtualTable = *(void**)((uint8*)classVData + ifaceOffset);
-					funcPtr = (void*)*(int*)((uint8*)ifaceVirtualTable + mMethodData.mVirtualIdx + virtualOffset);
+					funcPtr = (void*)*(int*)((uint8*)ifaceVirtualTable + mData.mMethodData.mVirtualIdx + virtualOffset);
 				}
-				else if (mMethodData.mVirtualIdx >= 0x100000)
+				else if (mData.mMethodData.mVirtualIdx >= 0x100000)
 				{
-					void* extAddr = (void*)*((int*)classVData + ((mMethodData.mVirtualIdx>>20) - 1));
-					funcPtr = (void*)*((int*)extAddr + (mMethodData.mVirtualIdx & 0xFFFFF) + virtualOffset);
+					void* extAddr = (void*)*((int*)classVData + ((mData.mMethodData.mVirtualIdx>>20) - 1));
+					funcPtr = (void*)*((int*)extAddr + (mData.mMethodData.mVirtualIdx & 0xFFFFF) + virtualOffset);
 				}
 				else
 				{
-					funcPtr = (void*)*(int*)((uint8*)classVData + mMethodData.mVirtualIdx + virtualOffset);
+					funcPtr = (void*)*(int*)((uint8*)classVData + mData.mMethodData.mVirtualIdx + virtualOffset);
 				}
 			}
 
@@ -740,16 +796,23 @@ namespace System.Reflection
 
 		public override void ToString(String strBuffer)
 		{
-			let retType = Type.[Friend]GetType(mMethodData.mReturnType);
+			if (Compiler.IsComptime)
+			{
+				String str = Type.[Friend]Comptime_Method_ToString(mData.mComptimeMethodInstance);
+				strBuffer.Append(str);
+				return;
+			}
+
+			let retType = Type.[Friend]GetType(mData.mMethodData.mReturnType);
 			retType.ToString(strBuffer);
 			strBuffer.Append(' ');
-			strBuffer.Append(mMethodData.mName);
+			strBuffer.Append(mData.mMethodData.mName);
 			strBuffer.Append('(');
-			for (int paramIdx < mMethodData.mParamCount)
+			for (int paramIdx < mData.mMethodData.mParamCount)
 			{
 				if (paramIdx > 0)
 					strBuffer.Append(", ");
-				let paramData = mMethodData.mParamData[paramIdx];
+				let paramData = mData.mMethodData.mParamData[paramIdx];
 				let paramType = Type.[Friend]GetType(paramData.mType);
 				paramType.ToString(strBuffer);
 				strBuffer.Append(' ');
@@ -785,24 +848,43 @@ namespace System.Reflection
 				if (mTypeInstance == null)
 					return false;
 
-				for (;;)
+				if (Compiler.IsComptime)
 				{
-					mIdx++;
-					if (mIdx == mTypeInstance.[Friend]mMethodDataCount)
+					for (;;)
 					{
-						if (mBindingFlags.HasFlag(.DeclaredOnly))
-							return false;
-						if (mTypeInstance.[Friend]mBaseType == 0)
+						mIdx++;
+						int64 nativeMethodHandle = Type.[Friend]Comptime_GetMethod((int32)mTypeInstance.TypeId, mIdx);
+						if (nativeMethodHandle == 0)
 							return false;
-						mTypeInstance = Type.[Friend]GetType(mTypeInstance.[Friend]mBaseType) as TypeInstance;
-						mIdx = -1;
-						continue;
-					}	
-					var methodData = &mTypeInstance.[Friend]mMethodDataPtr[mIdx];
-					bool matches = (mBindingFlags.HasFlag(BindingFlags.Static) && (methodData.mFlags.HasFlag(.Static)));
-					matches |= (mBindingFlags.HasFlag(BindingFlags.Instance) && (!methodData.mFlags.HasFlag(.Static)));
-					if (matches)
-						break;
+						let info = Type.[Friend]Comptime_Method_GetInfo(nativeMethodHandle);
+
+						bool matches = (mBindingFlags.HasFlag(BindingFlags.Static) && (info.mMethodFlags.HasFlag(.Static)));
+						matches |= (mBindingFlags.HasFlag(BindingFlags.Instance) && (!info.mMethodFlags.HasFlag(.Static)));
+						if (matches)
+							break;
+					}
+				}
+				else
+				{
+					for (;;)
+					{
+						mIdx++;
+						if (mIdx == mTypeInstance.[Friend]mMethodDataCount)
+						{
+							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;
+						}	
+						var methodData = &mTypeInstance.[Friend]mMethodDataPtr[mIdx];
+						bool matches = (mBindingFlags.HasFlag(BindingFlags.Static) && (methodData.mFlags.HasFlag(.Static)));
+						matches |= (mBindingFlags.HasFlag(BindingFlags.Instance) && (!methodData.mFlags.HasFlag(.Static)));
+						if (matches)
+							break;
+					}
 				}
 		        return true;
 		    }
@@ -811,8 +893,16 @@ namespace System.Reflection
 		    {
 		        get
 		        {
-					var methodData = &mTypeInstance.[Friend]mMethodDataPtr[mIdx];
-		            return MethodInfo(mTypeInstance, methodData);
+					if (Compiler.IsComptime)
+					{
+						int64 nativeMethodHandle = Type.[Friend]Comptime_GetMethod((int32)mTypeInstance.TypeId, mIdx);
+						return MethodInfo(mTypeInstance, nativeMethodHandle);
+					}
+					else
+					{
+						var methodData = &mTypeInstance.[Friend]mMethodDataPtr[mIdx];
+			            return MethodInfo(mTypeInstance, methodData);
+					}
 		        }
 		    }
 
@@ -824,4 +914,7 @@ namespace System.Reflection
 			}
 		}
 	}
+
+	[Obsolete("Use MethodInfo", false)]
+	typealias ComptimeMethodInfo = MethodInfo;
 }

+ 13 - 13
BeefLibs/corlib/src/Reflection/TypeInstance.bf

@@ -16,32 +16,32 @@ namespace System
 		    return MethodInfo.Enumerator(null, bindingFlags);
 		}
 
-		[Comptime]
+		/*[Comptime]
 		public virtual ComptimeMethodInfo.Enumerator GetMethods(BindingFlags bindingFlags = cDefaultLookup)
 		{
 		    return ComptimeMethodInfo.Enumerator(null, bindingFlags);
-		}
+		}*/
 
 		public virtual Result<MethodInfo, MethodError> GetMethod(StringView methodName, BindingFlags bindingFlags = cDefaultLookup)
 		{
 			MethodInfo matched = default;
 			for (let methodInfo in GetMethods(bindingFlags))
 			{
-				if (methodInfo.[Friend]mMethodData.[Friend]mName == methodName)
+				if (methodInfo.[Friend]mData.mMethodData.[Friend]mName == methodName)
 				{
-					if (matched.[Friend]mMethodData != null)
+					if (matched.[Friend]mData.mMethodData != null)
 						return .Err(.MultipleResults);
 					else
 					    matched = methodInfo;
 				}
 			}
 
-			if (matched.[Friend]mMethodData == null)
+			if (matched.[Friend]mData.mMethodData == null)
 				return .Err(.NoResults);
 			return .Ok(matched);
 		}
 
-		[Comptime]
+		/*[Comptime]
 		public virtual Result<ComptimeMethodInfo, MethodError> GetMethod(StringView methodName, BindingFlags bindingFlags = cDefaultLookup)
 		{
 			ComptimeMethodInfo matched = default;
@@ -59,7 +59,7 @@ namespace System
 			if (matched.mNativeMethodInstance == 0)
 				return .Err(.NoResults);
 			return .Ok(matched);
-		}
+		}*/
 
 		public virtual Result<MethodInfo, MethodError> GetMethod(int methodIdx)
 		{
@@ -67,13 +67,13 @@ namespace System
 		}
 
 		[Comptime]
-		public virtual Result<ComptimeMethodInfo, MethodError> GetMethod(int methodIdx)
+		public virtual Result<MethodInfo, MethodError> GetMethod(int methodIdx)
 		{
 			int64 nativeMethod = Comptime_GetMethod((.)TypeId, (.)methodIdx);
 			if (nativeMethod == 0)
 				return .Err(.NoResults);
 
-			return ComptimeMethodInfo(nativeMethod);
+			return MethodInfo(this as TypeInstance, nativeMethod);
 		}
 
 		public virtual Result<Object> CreateObject()
@@ -102,11 +102,11 @@ namespace System.Reflection
 		    return MethodInfo.Enumerator(this, bindingFlags);
 		}
 
-		[Comptime]
+		/*[Comptime]
 		public override ComptimeMethodInfo.Enumerator GetMethods(BindingFlags bindingFlags = cDefaultLookup)
 		{
 		    return ComptimeMethodInfo.Enumerator(this, bindingFlags);
-		}
+		}*/
 
 		public override Result<MethodInfo, MethodError> GetMethod(int methodIdx)
 		{
@@ -144,7 +144,7 @@ namespace System.Reflection
 
 				if (!methodInfo.IsInitialized)
 					return .Err;
-				if ((methodInfo.[Friend]mMethodData.mParamCount != 0) && (!calcAppendMethodInfo.IsInitialized))
+				if ((methodInfo.[Friend]mData.mMethodData.mParamCount != 0) && (!calcAppendMethodInfo.IsInitialized))
 					return .Err;
 			}
 			Object obj;
@@ -152,7 +152,7 @@ namespace System.Reflection
 			let objType = typeof(Object) as TypeInstance;
 
 			int allocSize = mInstSize;
-			bool hasAppendAlloc = (methodInfo.IsInitialized) && (methodInfo.[Friend]mMethodData.mParamCount != 0);
+			bool hasAppendAlloc = (methodInfo.IsInitialized) && (methodInfo.[Friend]mData.mMethodData.mParamCount != 0);
 
 			if (hasAppendAlloc)
 			{

+ 30 - 4
BeefLibs/corlib/src/Type.bf

@@ -498,6 +498,32 @@ namespace System
             return (int32)mTypeId;
         }
 
+		[CRepr, Packed]
+		public struct ComptimeMethodData
+		{
+			public int32 mReturnTypeId;
+			public int32 mParamCount;
+			public MethodFlags mMethodFlags;
+			public int32 mMethodIdx;
+		}
+
+		[CRepr, Packed]
+		public struct ComptimeParamInfo
+		{
+			public int32 mParamTypeId;
+			public TypeInstance.ParamFlags mParamFlags;
+			public String mName;
+		}
+
+		[CRepr, Packed]
+		public struct ComptimeFieldInfo
+		{
+			public TypeId mTypeId;
+			public int32 mFieldIdx;
+			public FieldFlags mFlags;
+			public int64 mData;
+		}
+
 		static extern Type Comptime_GetTypeById(int32 typeId);
 		static extern Type Comptime_GetTypeByName(StringView name);
 		static extern String Comptime_Type_ToString(int32 typeId);
@@ -507,9 +533,11 @@ 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 ComptimeMethodData Comptime_Method_GetInfo(int64 methodHandle);
+		static extern ComptimeParamInfo Comptime_Method_GetParamInfo(int64 methodHandle, int32 paramIdx);
+		static extern int64 Comptime_GetField(int32 typeId, int32 fieldIdx);
 		static extern String Comptime_Field_GetName(int64 fieldHandle);
-		static extern ComptimeMethodInfo.Info Comptime_Method_GetInfo(int64 methodHandle);
-		static extern ComptimeMethodInfo.ParamInfo Comptime_Method_GetParamInfo(int64 methodHandle, int32 paramIdx);
+		static extern ComptimeFieldInfo Comptime_Field_GetInfo(int64 fieldHandle);
 
         protected static Type GetType(TypeId typeId)
         {
@@ -667,8 +695,6 @@ namespace System
 		}	
     }
 
-
-
     enum TypeCode : uint8
 	{   
 	    None,

+ 2 - 2
IDE/mintest/minlib/src/System/Attribute.bf

@@ -508,12 +508,12 @@ namespace System
 
 	interface IOnFieldInit
 	{
-		void OnFieldInit(ComptimeFieldInfo type, Self* prev) mut;
+		void OnFieldInit(FieldInfo type, Self* prev) mut;
 	}
 
 	interface IOnMethodInit
 	{
-		void OnMethodInit(ComptimeMethodInfo type, Self* prev) mut;
+		void OnMethodInit(MethodInfo type, Self* prev) mut;
 	}
 
 	interface IComptimeTypeApply

+ 10 - 10
IDE/mintest/minlib/src/System/Reflection/FieldInfo.bf

@@ -328,17 +328,17 @@ namespace System.Reflection
 	    }
 	}
 
-	struct ComptimeMethodInfo
+	[CRepr, AlwaysInclude]
+	public struct MethodInfo
 	{
-		public int64 mNativeMethodInstance;
-	}
+		[Union]
+		struct Data
+		{
+			public TypeInstance.MethodData* mMethodData;
+			public int64 mComptimeMethodInstance;
+		}
 
-	struct ComptimeFieldInfo
-	{
-		int64 mNativeFieldInstance;
-		TypeId mOwner;
-		TypeId mTypeId;
-		int32 mFieldIdx;
-		FieldFlags mFlags;
+		TypeInstance mTypeInstance;
+		Data mData;
 	}
 }

+ 6 - 4
IDEHelper/Compiler/BfCompiler.cpp

@@ -429,8 +429,7 @@ BfCompiler::BfCompiler(BfSystem* bfSystem, bool isResolveOnly)
 	mIPrintableTypeDef = NULL;
 	mIHashableTypeDef = NULL;
 	mIComptimeTypeApply = NULL;
-	mIComptimeMethodApply = NULL;
-	mComptimeFieldInfoTypeDef = NULL;
+	mIComptimeMethodApply = NULL;	
 	mIOnTypeInitTypeDef = NULL;
 	mIOnTypeDoneTypeDef = NULL;
 	mIOnFieldInitTypeDef = NULL;
@@ -457,6 +456,8 @@ BfCompiler::BfCompiler(BfSystem* bfSystem, bool isResolveOnly)
 	mReflectSpecializedGenericType = NULL;
 	mReflectTypeInstanceTypeDef = NULL;
 	mReflectUnspecializedGenericType = NULL;
+	mReflectFieldInfoTypeDef = NULL;
+	mReflectMethodInfoTypeDef = NULL;
 	mSizedArrayTypeDef = NULL;
 	mStaticInitAfterAttributeTypeDef = NULL;	
 	mStaticInitPriorityAttributeTypeDef = NULL;
@@ -6832,8 +6833,7 @@ bool BfCompiler::DoCompile(const StringImpl& outputDirectory)
 	mIPrintableTypeDef = _GetRequiredType("System.IPrintable");
 	mIHashableTypeDef = _GetRequiredType("System.IHashable");
 	mIComptimeTypeApply = _GetRequiredType("System.IComptimeTypeApply");
-	mIComptimeMethodApply = _GetRequiredType("System.IComptimeMethodApply");
-	mComptimeFieldInfoTypeDef = _GetRequiredType("System.Reflection.ComptimeFieldInfo");
+	mIComptimeMethodApply = _GetRequiredType("System.IComptimeMethodApply");	
 	mIOnTypeInitTypeDef = _GetRequiredType("System.IOnTypeInit");
 	mIOnTypeDoneTypeDef = _GetRequiredType("System.IOnTypeDone");
 	mIOnFieldInitTypeDef = _GetRequiredType("System.IOnFieldInit");
@@ -6861,6 +6861,8 @@ bool BfCompiler::DoCompile(const StringImpl& outputDirectory)
 	mReflectSpecializedGenericType = _GetRequiredType("System.Reflection.SpecializedGenericType");
 	mReflectTypeInstanceTypeDef = _GetRequiredType("System.Reflection.TypeInstance");
 	mReflectUnspecializedGenericType = _GetRequiredType("System.Reflection.UnspecializedGenericType");
+	mReflectFieldInfoTypeDef = _GetRequiredType("System.Reflection.FieldInfo");
+	mReflectMethodInfoTypeDef = _GetRequiredType("System.Reflection.MethodInfo");
 	mSizedArrayTypeDef = _GetRequiredType("System.SizedArray", 2);	
 	mStaticInitAfterAttributeTypeDef = _GetRequiredType("System.StaticInitAfterAttribute");
 	mStaticInitPriorityAttributeTypeDef = _GetRequiredType("System.StaticInitPriorityAttribute");

+ 3 - 2
IDEHelper/Compiler/BfCompiler.h

@@ -389,8 +389,7 @@ public:
 	BfTypeDef* mIPrintableTypeDef;
 	BfTypeDef* mIHashableTypeDef;
 	BfTypeDef* mIComptimeTypeApply;
-	BfTypeDef* mIComptimeMethodApply;
-	BfTypeDef* mComptimeFieldInfoTypeDef;
+	BfTypeDef* mIComptimeMethodApply;	
 	BfTypeDef* mIOnTypeInitTypeDef;
 	BfTypeDef* mIOnTypeDoneTypeDef;
 	BfTypeDef* mIOnFieldInitTypeDef;
@@ -416,6 +415,8 @@ public:
 	BfTypeDef* mReflectSpecializedGenericType;
 	BfTypeDef* mReflectTypeInstanceTypeDef;
 	BfTypeDef* mReflectUnspecializedGenericType;		
+	BfTypeDef* mReflectFieldInfoTypeDef;
+	BfTypeDef* mReflectMethodInfoTypeDef;
 	
 	BfTypeDef* mSizedArrayTypeDef;
 	BfTypeDef* mAttributeTypeDef;

+ 142 - 101
IDEHelper/Compiler/BfModule.cpp

@@ -1567,24 +1567,37 @@ BfIRValue BfModule::CreateStringCharPtr(const StringImpl& str, int stringId, boo
 	return mBfIRBuilder->CreateInBoundsGEP(gv, 0, 0);
 }
 
-void BfModule::FixConstValueParams(BfTypeInstance* typeInst, SizedArrayImpl<BfIRValue>& valueParams)
+void BfModule::FixConstValueParams(BfTypeInstance* typeInst, SizedArrayImpl<BfIRValue>& valueParams, bool fillInPadding)
 {
-	if (!typeInst->mTypeDef->mIsCombinedPartial)
+	if ((!typeInst->mTypeDef->mIsCombinedPartial) && (!fillInPadding))
 		return;
 
 	int prevDataIdx = -1;
 	int usedDataIdx = 0;
+	int valueParamIdx = 0;
+
 	if (typeInst->mBaseType != NULL)
+	{
 		usedDataIdx++;
-
+		valueParamIdx++;
+		prevDataIdx++;
+	}
+		
 	int startingParamsSize = (int)valueParams.mSize;
 	for (int fieldIdx = 0; fieldIdx < (int)typeInst->mFieldInstances.size(); fieldIdx++)
 	{
 		auto fieldInstance = &typeInst->mFieldInstances[fieldIdx];
 		if (fieldInstance->mDataIdx < 0)
 			continue;
-
+		
 		BF_ASSERT(fieldInstance->mDataIdx > prevDataIdx);
+		if (fillInPadding)
+		{
+			for (int i = prevDataIdx + 1; i < fieldInstance->mDataIdx; i++)			
+				valueParams.Insert(valueParamIdx++, mBfIRBuilder->CreateConstArrayZero(0));
+		}
+
+		valueParamIdx++;
 		prevDataIdx = fieldInstance->mDataIdx;
 
 		usedDataIdx++;
@@ -5355,6 +5368,123 @@ void BfModule::EncodeAttributeData(BfTypeInstance* typeInstance, BfType* argType
 	}	
 }
 
+BfIRValue BfModule::CreateFieldData(BfFieldInstance* fieldInstance, int customAttrIdx)
+{
+	bool isComptime = mBfIRBuilder->mIgnoreWrites;
+	BfFieldDef* fieldDef = fieldInstance->GetFieldDef();
+
+	auto typeInstance = fieldInstance->mOwner;
+	
+	BfType* intType = GetPrimitiveType(BfTypeCode_Int32);
+	BfType* intPtrType = GetPrimitiveType(BfTypeCode_IntPtr);
+	BfType* shortType = GetPrimitiveType(BfTypeCode_Int16);
+	BfType* typeIdType = intType;
+
+	BfTypeInstance* reflectFieldDataType = ResolveTypeDef(mCompiler->mReflectFieldDataDef)->ToTypeInstance();
+	BfIRValue emptyValueType = mBfIRBuilder->mIgnoreWrites ? 
+		mBfIRBuilder->CreateConstAgg(mBfIRBuilder->MapTypeInst(reflectFieldDataType->mBaseType), SizedArray<BfIRValue, 1>()) :
+		mBfIRBuilder->CreateConstAgg_Value(mBfIRBuilder->MapTypeInst(reflectFieldDataType->mBaseType), SizedArray<BfIRValue, 1>());
+	BfIRValue fieldNameConst = GetStringObjectValue(fieldDef->mName, !mIsComptimeModule);
+	bool is32Bit = mCompiler->mSystem->mPtrSize == 4;
+
+	int typeId = 0;
+	auto fieldType = fieldInstance->GetResolvedType();
+	if (fieldType->IsGenericParam())
+	{
+		//TODO: 
+	}
+	else
+		typeId = fieldType->mTypeId;
+
+	BfFieldFlags fieldFlags = (BfFieldFlags)0;
+
+	if (fieldDef->mProtection == BfProtection_Protected)
+		fieldFlags = (BfFieldFlags)(fieldFlags | BfFieldFlags_Protected);
+	if (fieldDef->mProtection == BfProtection_Public)
+		fieldFlags = (BfFieldFlags)(fieldFlags | BfFieldFlags_Public);
+	if (fieldDef->mIsStatic)
+		fieldFlags = (BfFieldFlags)(fieldFlags | BfFieldFlags_Static);
+	if (fieldDef->mIsConst)
+		fieldFlags = (BfFieldFlags)(fieldFlags | BfFieldFlags_Const);
+	if (fieldDef->IsEnumCaseEntry())
+		fieldFlags = (BfFieldFlags)(fieldFlags | BfFieldFlags_EnumCase);
+	
+	BfIRValue constValue;
+	BfIRValue constValue2;
+	if (fieldInstance->GetFieldDef()->mIsConst)
+	{
+		if (fieldInstance->mConstIdx != -1)
+		{
+			auto constant = typeInstance->mConstHolder->GetConstantById(fieldInstance->mConstIdx);
+			constValue = mBfIRBuilder->CreateConst(BfTypeCode_IntPtr, constant->mUInt64);
+			if (is32Bit)
+				constValue2 = mBfIRBuilder->CreateConst(BfTypeCode_IntPtr, constant->mUInt64 >> 32);
+		}
+	}
+	else if (fieldInstance->GetFieldDef()->mIsStatic)
+	{
+		BfTypedValue refVal;
+		if (!mIsComptimeModule) // This can create circular reference issues for a `Self` static
+			refVal = ReferenceStaticField(fieldInstance);
+		if (refVal.mValue.IsConst())
+		{
+			auto constant = mBfIRBuilder->GetConstant(refVal.mValue);
+			if (constant->mConstType == BfConstType_GlobalVar)
+			{
+				auto globalVar = (BfGlobalVar*)constant;
+				if (globalVar->mName[0] == '#')
+					refVal = BfTypedValue();
+			}
+		}
+
+		if ((refVal.IsAddr()) && (!isComptime))
+			constValue = mBfIRBuilder->CreatePtrToInt(refVal.mValue, BfTypeCode_IntPtr);
+	}
+
+	if (!constValue)
+		constValue = mBfIRBuilder->CreateConst(BfTypeCode_IntPtr, fieldInstance->mDataOffset);
+
+	BfIRValue result;
+	if (is32Bit)
+	{
+		if (!constValue2)
+			constValue2 = mBfIRBuilder->CreateConst(BfTypeCode_IntPtr, 0);
+
+		SizedArray<BfIRValue, 8> fieldVals =
+		{
+			emptyValueType,
+			fieldNameConst, // mName			
+			GetConstValue(typeId, typeIdType), // mFieldTypeId			
+			constValue, // mData
+			constValue2, // mDataHi
+			GetConstValue(fieldFlags, shortType), // mFlags
+			GetConstValue(customAttrIdx, intType), // mCustomAttributesIdx
+		};		
+		FixConstValueParams(reflectFieldDataType, fieldVals, isComptime);
+		result = isComptime ?
+			mBfIRBuilder->CreateConstAgg(mBfIRBuilder->MapTypeInst(reflectFieldDataType, BfIRPopulateType_Full), fieldVals) :
+			mBfIRBuilder->CreateConstAgg_Value(mBfIRBuilder->MapTypeInst(reflectFieldDataType, BfIRPopulateType_Full), fieldVals);		
+	}
+	else
+	{
+		SizedArray<BfIRValue, 8> fieldVals =
+		{
+			emptyValueType,
+			fieldNameConst, // mName			
+			GetConstValue(typeId, typeIdType), // mFieldTypeId			
+			constValue, // mData
+			GetConstValue(fieldFlags, shortType), // mFlags
+			GetConstValue(customAttrIdx, intType), // mCustomAttributesIdx
+		};		
+		FixConstValueParams(reflectFieldDataType, fieldVals, isComptime);
+		result = isComptime ?
+			mBfIRBuilder->CreateConstAgg(mBfIRBuilder->MapTypeInst(reflectFieldDataType, BfIRPopulateType_Full), fieldVals) :
+			mBfIRBuilder->CreateConstAgg_Value(mBfIRBuilder->MapTypeInst(reflectFieldDataType, BfIRPopulateType_Full), fieldVals);
+	}
+
+	return result;
+}
+
 BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStringIdMap, bool forceReflectFields, bool needsTypeData, bool needsTypeNames, bool needsVData)
 {
 	if ((IsHotCompile()) && (!type->mDirty))
@@ -6562,102 +6692,7 @@ BfIRValue BfModule::CreateTypeData(BfType* type, Dictionary<int, int>& usedStrin
 			break;
 
 		BfFieldInstance* fieldInstance = &typeInstance->mFieldInstances[fieldIdx];
-		BfFieldDef* fieldDef = fieldInstance->GetFieldDef();
-
-		BfIRValue fieldNameConst = GetStringObjectValue(fieldDef->mName, !mIsComptimeModule);		
-
-		int typeId = 0;
-		auto fieldType = fieldInstance->GetResolvedType();
-		if (fieldType->IsGenericParam())
-		{
-			//TODO: 
-		}
-		else
-			typeId = fieldType->mTypeId;		
-
-		BfFieldFlags fieldFlags = (BfFieldFlags)0;
-
-		if (fieldDef->mProtection == BfProtection_Protected)
-			fieldFlags = (BfFieldFlags)(fieldFlags | BfFieldFlags_Protected);
-		if (fieldDef->mProtection == BfProtection_Public)
-			fieldFlags = (BfFieldFlags)(fieldFlags | BfFieldFlags_Public);
-		if (fieldDef->mIsStatic)
-			fieldFlags = (BfFieldFlags)(fieldFlags | BfFieldFlags_Static);
-		if (fieldDef->mIsConst)
-			fieldFlags = (BfFieldFlags)(fieldFlags | BfFieldFlags_Const);
-		if (fieldDef->IsEnumCaseEntry())
-			fieldFlags = (BfFieldFlags)(fieldFlags | BfFieldFlags_EnumCase);
-
-		int customAttrIdx = _HandleCustomAttrs(fieldInstance->mCustomAttributes);
-		BfIRValue constValue;
-		BfIRValue constValue2;
-		if (fieldInstance->GetFieldDef()->mIsConst)
-		{			
-			if (fieldInstance->mConstIdx != -1)
-			{
-				auto constant = typeInstance->mConstHolder->GetConstantById(fieldInstance->mConstIdx);
-				constValue = mBfIRBuilder->CreateConst(BfTypeCode_IntPtr, constant->mUInt64);
-				if (is32Bit)
-					constValue2 = mBfIRBuilder->CreateConst(BfTypeCode_IntPtr, constant->mUInt64 >> 32);
-			}
-		}
-		else if (fieldInstance->GetFieldDef()->mIsStatic)
-		{			
-			BfTypedValue refVal;
-			if (!mIsComptimeModule) // This can create circular reference issues for a `Self` static
-				refVal = ReferenceStaticField(fieldInstance);
-			if (refVal.mValue.IsConst())
-			{
-				auto constant = mBfIRBuilder->GetConstant(refVal.mValue);
-				if (constant->mConstType == BfConstType_GlobalVar)
-				{
-					auto globalVar = (BfGlobalVar*)constant;
-					if (globalVar->mName[0] == '#')
-						refVal = BfTypedValue();
-				}
-			}
-
-			if (refVal.IsAddr())
-			{
-				constValue = mBfIRBuilder->CreatePtrToInt(refVal.mValue, BfTypeCode_IntPtr);
-			}
-		}
-		
-		if (!constValue)
-			constValue = mBfIRBuilder->CreateConst(BfTypeCode_IntPtr, fieldInstance->mDataOffset);
-		
-		if (is32Bit)
-		{
-			if (!constValue2)
-				constValue2 = mBfIRBuilder->CreateConst(BfTypeCode_IntPtr, 0);
-
-			SizedArray<BfIRValue, 8> fieldVals =
-			{
-				emptyValueType,
-				fieldNameConst, // mName			
-				GetConstValue(typeId, typeIdType), // mFieldTypeId			
-				constValue, // mData
-				constValue2, // mDataHi
-				GetConstValue(fieldFlags, shortType), // mFlags
-				GetConstValue(customAttrIdx, intType), // mCustomAttributesIdx
-			};
-			auto fieldData = mBfIRBuilder->CreateConstAgg_Value(mBfIRBuilder->MapTypeInst(reflectFieldDataType->ToTypeInstance(), BfIRPopulateType_Full), fieldVals);
-			fieldTypes.push_back(fieldData);
-		}
-		else
-		{
-			SizedArray<BfIRValue, 8> fieldVals =
-			{
-				emptyValueType,
-				fieldNameConst, // mName			
-				GetConstValue(typeId, typeIdType), // mFieldTypeId			
-				constValue, // mData
-				GetConstValue(fieldFlags, shortType), // mFlags
-				GetConstValue(customAttrIdx, intType), // mCustomAttributesIdx
-			};
-			auto fieldData = mBfIRBuilder->CreateConstAgg_Value(mBfIRBuilder->MapTypeInst(reflectFieldDataType->ToTypeInstance(), BfIRPopulateType_Full), fieldVals);
-			fieldTypes.push_back(fieldData);
-		}			
+		fieldTypes.push_back(CreateFieldData(fieldInstance, _HandleCustomAttrs(fieldInstance->mCustomAttributes)));
 	}	
 
 	auto reflectFieldDataIRType = mBfIRBuilder->MapType(reflectFieldDataType);
@@ -20844,7 +20879,13 @@ void BfModule::ProcessMethod(BfMethodInstance* methodInstance, bool isInlineDup,
 				if (bodyBlock->mCloseBrace != NULL)
 				{
 					BfAstNode* target = bodyBlock->mCloseBrace;
-					Fail("Method must return value", target);
+					if (!mCompiler->mHasRequiredTypes)
+					{
+						AddFailType(mCurTypeInstance);
+						mHadBuildError = true;
+					}
+					else
+						Fail("Method must return value", target);
 				}
 				else
 				{

+ 2 - 1
IDEHelper/Compiler/BfModule.h

@@ -1562,7 +1562,7 @@ public:
 	BfIRValue GetDefaultValue(BfType* type);	
 	BfTypedValue GetFakeTypedValue(BfType* type);
 	BfTypedValue GetDefaultTypedValue(BfType* type, bool allowRef = false, BfDefaultValueKind defaultValueKind = BfDefaultValueKind_Const);			
-	void FixConstValueParams(BfTypeInstance* typeInst, SizedArrayImpl<BfIRValue>& valueParams);
+	void FixConstValueParams(BfTypeInstance* typeInst, SizedArrayImpl<BfIRValue>& valueParams, bool fillInPadding = false);
 	BfIRValue CreateStringObjectValue(const StringImpl& str, int stringId, bool define);
 	BfIRValue CreateStringCharPtr(const StringImpl& str, int stringId, bool define);
 	int GetStringPoolIdx(BfIRValue constantStr, BfIRConstHolder* constHolder = NULL);
@@ -1978,6 +1978,7 @@ public:
 	BfIRValue CreateClassVDataExtGlobal(BfTypeInstance* declTypeInst, BfTypeInstance* implTypeInst, int startVirtIdx);
 	BfIRValue CreateTypeDataRef(BfType* type);
 	void EncodeAttributeData(BfTypeInstance* typeInstance, BfType* argType, BfIRValue arg, SizedArrayImpl<uint8>& data, Dictionary<int, int>& usedStringIdMap);
+	BfIRValue CreateFieldData(BfFieldInstance* fieldInstance, int customAttrIdx);
 	BfIRValue CreateTypeData(BfType* type, Dictionary<int, int>& usedStringIdMap, bool forceReflectFields, bool needsTypeData, bool needsTypeNames, bool needsVData);
 	BfIRValue FixClassVData(BfIRValue value);
 

+ 20 - 30
IDEHelper/Compiler/BfModuleTypeUtils.cpp

@@ -2197,30 +2197,16 @@ void BfModule::HandleCEAttributes(CeEmitContext* ceEmitContext, BfTypeInstance*
 			args.Add(attrVal);
 		if (isFieldApply)
 		{
-			auto fieldDef = fieldInstance->GetFieldDef();
-			BfFieldFlags fieldFlags = (BfFieldFlags)0;
-
-			if (fieldDef->mProtection == BfProtection_Protected)
-				fieldFlags = (BfFieldFlags)(fieldFlags | BfFieldFlags_Protected);
-			if (fieldDef->mProtection == BfProtection_Public)
-				fieldFlags = (BfFieldFlags)(fieldFlags | BfFieldFlags_Public);
-			if (fieldDef->mIsStatic)
-				fieldFlags = (BfFieldFlags)(fieldFlags | BfFieldFlags_Static);
-			if (fieldDef->mIsConst)
-				fieldFlags = (BfFieldFlags)(fieldFlags | BfFieldFlags_Const);
-
-			auto fieldInfoType = ResolveTypeDef(mCompiler->mComptimeFieldInfoTypeDef);
+			auto fieldInfoType = ResolveTypeDef(mCompiler->mReflectFieldInfoTypeDef);
 			if (fieldInfoType != NULL)
 			{
+				SetAndRestoreValue<bool> prevIgnoreWrites(mBfIRBuilder->mIgnoreWrites, true);
 				SizedArray<BfIRValue, 9> fieldData =
 				{			
-					mBfIRBuilder->CreateConstAggZero(mBfIRBuilder->MapType(fieldInfoType, BfIRPopulateType_Identity)),
-					GetConstValue((uint64)(intptr)fieldInstance, GetPrimitiveType(BfTypeCode_Int64)), // mNativeFieldInstance
-					GetConstValue(typeInstance->mTypeId, GetPrimitiveType(BfTypeCode_Int32)), // mOwner
-					GetConstValue((fieldInstance->mResolvedType != NULL) ? fieldInstance->mResolvedType->mTypeId : 0, GetPrimitiveType(BfTypeCode_Int32)), // mTypeId
-					GetConstValue(fieldDef->mIdx, GetPrimitiveType(BfTypeCode_Int32)), // mFieldIdx
-					GetConstValue((int)fieldFlags, GetPrimitiveType(BfTypeCode_Int16)), // mFieldFlags
-				};				
+					mBfIRBuilder->CreateConstAggZero(mBfIRBuilder->MapType(fieldInfoType->ToTypeInstance()->mBaseType, BfIRPopulateType_Identity)),
+					mBfIRBuilder->CreateTypeOf(mCurTypeInstance), // mTypeInstance
+					CreateFieldData(fieldInstance, fieldInstance->GetFieldDef()->mIdx)
+				};
 				FixConstValueParams(fieldInfoType->ToTypeInstance(), fieldData);
 				auto fieldDataAgg = mBfIRBuilder->CreateConstAgg(mBfIRBuilder->MapType(fieldInfoType, BfIRPopulateType_Identity), fieldData);
 				args.Add(fieldDataAgg);
@@ -2612,7 +2598,19 @@ void BfModule::DoCEEmit(BfMethodInstance* methodInstance)
 		SizedArray<BfIRValue, 1> args;
 		if (!attrType->IsValuelessType())
 			args.Add(attrVal);
-		args.Add(mBfIRBuilder->CreateConst(BfTypeCode_UInt64, (uint64)(intptr)methodInstance));
+				
+		auto methodInfoType = ResolveTypeDef(mCompiler->mReflectMethodInfoTypeDef);
+		SizedArray<BfIRValue, 9> methodData =
+		{
+			mBfIRBuilder->CreateConstAggZero(mBfIRBuilder->MapType(methodInfoType->ToTypeInstance()->mBaseType, BfIRPopulateType_Identity)),
+			mBfIRBuilder->CreateTypeOf(mCurTypeInstance), // mTypeInstance
+			GetConstValue((int64)methodInstance, GetPrimitiveType(BfTypeCode_Int64)), // mNativeMethodInstance
+
+		};
+		FixConstValueParams(methodInfoType->ToTypeInstance(), methodData);
+		auto fieldDataAgg = mBfIRBuilder->CreateConstAgg(mBfIRBuilder->MapType(methodInfoType, BfIRPopulateType_Identity), methodData);
+		args.Add(fieldDataAgg);
+
 		if (applyMethodInstance->GetParamCount() > 1)
 		{
 			if (irValue)
@@ -2628,16 +2626,8 @@ void BfModule::DoCEEmit(BfMethodInstance* methodInstance)
 		}
 
 		mCompiler->mCEMachine->mMethodInstanceSet.Add(methodInstance);
-
-		//TESTING
-// 			mCompiler->mCEMachine->ReleaseContext(ceContext);			
-// 			ceContext = mCompiler->mCEMachine->AllocContext();
-// 			ceContext->mMemory.mSize = ceContext->mMemory.mAllocSize;
-			
-
 		auto activeTypeDef = typeInstance->mTypeDef;			
-		
-		//auto result = ceContext->Call(customAttribute.mRef, this, applyMethodInstance, args, CeEvalFlags_None, NULL);
+				
 		BfTypedValue result;
 		///
 		{

+ 28 - 25
IDEHelper/Compiler/CeMachine.cpp

@@ -3517,6 +3517,18 @@ bool CeContext::WriteConstant(BfModule* module, addr_ce addr, BfConstant* consta
 
 	if (constant->mConstType == BfConstType_Agg)
 	{
+		if (type->IsPointer())
+		{
+			auto elementType = type->GetUnderlyingType();
+			auto toPtr = CeMalloc(elementType->mSize);
+			addr_ce toAddr = (addr_ce)(toPtr - mMemory.mVals);
+			if (mCeMachine->mCeModule->mSystem->mPtrSize == 4)
+				CE_GETC(int32) = (int32)toAddr;
+			else
+				CE_GETC(int64) = (int64)toAddr;
+			return WriteConstant(module, toAddr, constant, elementType, isParams);
+		}
+
 		auto aggConstant = (BfConstantAgg*)constant;
 		if (type->IsSizedArray())
 		{
@@ -3671,6 +3683,14 @@ bool CeContext::WriteConstant(BfModule* module, addr_ce addr, BfConstant* consta
 		return WriteConstant(module, addr, constTarget, type);
 	}
 
+	if (constant->mConstType == BfConstType_PtrToInt)
+	{
+		auto ptrToIntConst = (BfConstantPtrToInt*)constant;		
+
+		auto constTarget = module->mBfIRBuilder->GetConstantById(ptrToIntConst->mTarget);
+		return WriteConstant(module, addr, constTarget, type);
+	}
+
 	if (constant->mConstType == BfConstType_BitCastNull)
 	{
 		BF_ASSERT(type->IsPointer() || type->IsObjectOrInterface());
@@ -3764,7 +3784,7 @@ bool CeContext::WriteConstant(BfModule* module, addr_ce addr, BfConstant* consta
 			if (checkConstant->mConstType == BfConstType_AggCE)
 				return WriteConstant(module, addr, checkConstant, type, isParams);
 		}				
-	}
+	}	
 
 	return false;
 }
@@ -5048,11 +5068,10 @@ bool CeContext::Execute(CeFunction* startFunction, uint8* startStackPtr, uint8*
 				}
 				if ((methodIdx < 0) || (methodIdx >= typeInfo->mMethodInstances.mSize))
 				{
-					_Fail("Method out of bounds");
-					return false;
+					*(int64*)(stackPtr + 0) = 0;
 				}
-
-				*(int64*)(stackPtr + 0) = (int64)(intptr)typeInfo->mMethodInstances[methodIdx];
+				else
+					*(int64*)(stackPtr + 0) = (int64)(intptr)typeInfo->mMethodInstances[methodIdx];
 			}
 			else if (checkFunction->mFunctionKind == CeFunctionKind_Method_ToString)
 			{				
@@ -5087,8 +5106,9 @@ bool CeContext::Execute(CeFunction* startFunction, uint8* startStackPtr, uint8*
 				// int32 mReturnType
 				// int32 mParamCount
 				// int16 mFlags
+				// int32 mMethodIdx
 
-				int64 methodHandle = *(int64*)((uint8*)stackPtr + 4+4+2);
+				int64 methodHandle = *(int64*)((uint8*)stackPtr + 4+4+2+4);
 				
 				auto methodInstance = mCeMachine->GetMethodInstance(methodHandle);
 				if (methodInstance == NULL)
@@ -5099,7 +5119,8 @@ bool CeContext::Execute(CeFunction* startFunction, uint8* startStackPtr, uint8*
 				
 				*(int32*)(stackPtr + 0) = methodInstance->mReturnType->mTypeId;
 				*(int32*)(stackPtr + 4) = methodInstance->GetParamCount();
-				*(int16*)(stackPtr + 4+4) = methodInstance->GetMethodFlags();								
+				*(int16*)(stackPtr + 4+4) = methodInstance->GetMethodFlags();
+				*(int32*)(stackPtr + 4+4+2) = methodInstance->mMethodDef->mIdx;
 			}
 			else if (checkFunction->mFunctionKind == CeFunctionKind_Method_GetParamInfo)			
 			{	
@@ -5129,20 +5150,6 @@ bool CeContext::Execute(CeFunction* startFunction, uint8* startStackPtr, uint8*
 				*(int16*)(stackPtr + 4) = 0; // Flags
 				CeSetAddrVal(stackPtr + 4+2, stringAddr, ptrSize);								
 			}
-			else if (checkFunction->mFunctionKind == CeFunctionKind_Field_GetName)
-			{				
-				int64 fieldHandle = *(int64*)((uint8*)stackPtr + ptrSize);
-				
-				auto fieldInstance = mCeMachine->GetFieldInstance(fieldHandle);
-				if (fieldInstance == NULL)
-				{
-					_Fail("Invalid field instance");
-					return false;
-				}
-				
-				CeSetAddrVal(stackPtr + 0, GetString(fieldInstance->GetFieldDef()->mName), ptrSize);
-				_FixVariables();
-			}
 			else if (checkFunction->mFunctionKind == CeFunctionKind_EmitTypeBody)
 			{
 				int32 typeId = *(int32*)((uint8*)stackPtr);
@@ -7997,10 +8004,6 @@ void CeMachine::CheckFunctionKind(CeFunction* ceFunction)
 				{
 					ceFunction->mFunctionKind = CeFunctionKind_Method_GetParamInfo;
 				}
-				else if (methodDef->mName == "Comptime_Field_GetName")
-				{
-					ceFunction->mFunctionKind = CeFunctionKind_Field_GetName;
-				}
 			}
 			else if (owner->IsInstanceOf(mCeModule->mCompiler->mCompilerTypeDef))
 			{

+ 0 - 1
IDEHelper/Compiler/CeMachine.h

@@ -332,7 +332,6 @@ enum CeFunctionKind
 	CeFunctionKind_Method_GetName,
 	CeFunctionKind_Method_GetInfo,
 	CeFunctionKind_Method_GetParamInfo,
-	CeFunctionKind_Field_GetName,
 	
 	CeFunctionKind_EmitTypeBody,
 	CeFunctionKind_EmitAddInterface,

+ 1 - 1
IDEHelper/Tests/src/Comptime.bf

@@ -43,7 +43,7 @@ namespace Tests
 			public static String gLog = new .() ~ delete _;
 
 			[Comptime] 
-			public void ApplyToMethod(ComptimeMethodInfo method)
+			public void ApplyToMethod(MethodInfo method)
 			{
 				String emit = scope $"LogAttribute.gLog.AppendF($\"Called {method}";
 				for (var fieldIdx < method.ParamCount)

+ 1 - 1
IDEHelper/Tests/src/Interfaces.bf

@@ -362,7 +362,7 @@ namespace Tests
 			public static String gLog = new .() ~ delete _;
 
 			[Comptime] 
-			public void ApplyToMethod(ComptimeMethodInfo method)
+			public void ApplyToMethod(MethodInfo method)
 			{
 				Compiler.EmitMethodEntry(method, "int b = 2;");
 			}