1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-19 16:40:26 +02:00

Early code generation support

This commit is contained in:
Brian Fiete 2021-01-11 09:41:43 -08:00
parent 0b48a60592
commit 71d4dd0e90
26 changed files with 2422 additions and 1576 deletions

View file

@ -274,6 +274,21 @@ namespace System
}
[AttributeUsage(.Method)]
struct OnCompileAttribute : Attribute
{
public enum Kind
{
None,
TypeInit,
TypeDone
}
public this(Kind kind)
{
}
}
[AttributeUsage(.Method)]
public struct ComptimeAttribute : Attribute
{

View file

@ -1,21 +1,6 @@
using System.Reflection;
namespace System
{
[AttributeUsage(.Method)]
struct OnCompileAttribute : Attribute
{
public enum Kind
{
None,
TypeInit,
TypeDone
}
public this(Kind kind)
{
}
}
static class Compiler
{
public struct MethodBuilder

View file

@ -7,9 +7,33 @@ namespace System.Reflection
*((*(T2**)&data)++)
}
public static bool HasCustomAttribute(void* inAttrData, Type attributeType)
{
TypeId findTypeId = attributeType.[Friend]mTypeId;
void* data = inAttrData;
data++;
uint8 count = Decode!<uint8>(data);
for (int32 attrIdx = 0; attrIdx < count; attrIdx++)
AttrBlock:
{
void* startPtr = data;
var len = Decode!<uint16>(data);
void* endPtr = (uint8*)startPtr + len;
var typeId = Decode!<TypeId>(data);
if (typeId == findTypeId)
return true;
data = endPtr;
}
return false;
}
public static Result<void> GetCustomAttribute(void* inAttrData, Type attributeType, Object targetAttr)
{
TypeId findTypeId = attributeType.[Friend]mTypeId;
void* data = inAttrData;

View file

@ -472,6 +472,9 @@ namespace System
}
static extern Type Comptime_GetTypeById(int32 typeId);
static extern Type Comptime_GetTypeByName(StringView name);
static extern Type Comptime_GetSpecializedType(Type unspecializedType, Span<Type> typeArgs);
static extern bool Comptime_Type_GetCustomAttribute(int32 typeId, int32 attributeId, void* dataPtr);
protected static Type GetType(TypeId typeId)
{
@ -487,6 +490,19 @@ namespace System
return sTypes[typeId];
}
public static Result<Type> GetTypeByName(StringView typeName)
{
if (Compiler.IsComptime)
{
var type = Comptime_GetTypeByName(typeName);
if (type == null)
return .Err;
return type;
}
return .Err;
}
void GetBasicName(String strBuffer)
{
switch (mTypeCode)
@ -557,8 +573,28 @@ namespace System
return FieldInfo.Enumerator(null, bindingFlags);
}
public bool HasCustomAttribute<T>() where T : Attribute
{
if (Compiler.IsComptime)
{
return Comptime_Type_GetCustomAttribute((int32)TypeId, typeof(T).TypeId, null);
}
if (var typeInstance = this as TypeInstance)
return typeInstance.[Friend]HasCustomAttribute<T>(typeInstance.[Friend]mCustomAttributesIdx);
return false;
}
public Result<T> GetCustomAttribute<T>() where T : Attribute
{
if (Compiler.IsComptime)
{
T val = ?;
if (Comptime_Type_GetCustomAttribute((int32)TypeId, typeof(T).TypeId, &val))
return val;
return .Err;
}
if (var typeInstance = this as TypeInstance)
return typeInstance.[Friend]GetCustomAttribute<T>(typeInstance.[Friend]mCustomAttributesIdx);
return .Err;
@ -577,6 +613,9 @@ namespace System
{
while (true)
{
if (Compiler.IsComptime)
Runtime.FatalError("Comptime type enumeration not supported");
if (mCurId >= sTypeCount)
return .Err;
let type = sTypes[mCurId++];
@ -929,6 +968,15 @@ namespace System.Reflection
return FieldInfo.Enumerator(this, bindingFlags);
}
bool HasCustomAttribute<T>(int customAttributeIdx) where T : Attribute
{
if (customAttributeIdx == -1)
return false;
void* data = mCustomAttrDataPtr[customAttributeIdx];
return AttributeInfo.HasCustomAttribute(data, typeof(T));
}
Result<T> GetCustomAttribute<T>(int customAttributeIdx) where T : Attribute
{
if (customAttributeIdx == -1)
@ -1043,6 +1091,17 @@ namespace System.Reflection
}
uint8 mGenericParamCount;
public Result<Type> GetSpecializedType(params Span<Type> typeArgs)
{
if (Compiler.IsComptime)
{
var specializedType = Type.[Friend]Comptime_GetSpecializedType(this, typeArgs);
if (specializedType != null)
return specializedType;
}
return .Err;
}
}
// Only for resolved types