Skip to content

Commit

Permalink
Expanded TypeKind API
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaMorphic committed Jul 19, 2024
1 parent 5d7ff21 commit dab6511
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 1 deletion.
1 change: 1 addition & 0 deletions Src/ILGPU/IR/Types/ArrayType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace ILGPU.IR.Types
/// <summary>
/// Represents the type of a generic array that lives in the local address space.
/// </summary>
[TypeKind(TypeKind.Array)]
public sealed class ArrayType : TypeNode
{
#region Instance
Expand Down
1 change: 1 addition & 0 deletions Src/ILGPU/IR/Types/HandleType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace ILGPU.IR.Types
/// <summary>
/// Represents a .Net runtime-specific handle type.
/// </summary>
[TypeKind(TypeKind.Handle)]
public sealed class HandleType : TypeNode
{
#region Instance
Expand Down
1 change: 1 addition & 0 deletions Src/ILGPU/IR/Types/PaddingType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace ILGPU.IR.Types
/// <summary>
/// Represents a padding type.
/// </summary>
[TypeKind(TypeKind.Padding)]
public sealed class PaddingType : TypeNode
{
#region Instance
Expand Down
2 changes: 2 additions & 0 deletions Src/ILGPU/IR/Types/PointerTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ public override string ToString() =>
/// <summary>
/// Represents the type of a generic pointer.
/// </summary>
[TypeKind(TypeKind.Pointer)]
public sealed class PointerType : AddressSpaceType
{
#region Instance
Expand Down Expand Up @@ -245,6 +246,7 @@ public override bool Equals(object? obj) =>
/// <summary>
/// Represents the type of a generic view.
/// </summary>
[TypeKind(TypeKind.View)]
public sealed class ViewType : AddressSpaceType
{
#region Instance
Expand Down
2 changes: 2 additions & 0 deletions Src/ILGPU/IR/Types/PrimitiveTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace ILGPU.IR.Types
/// <summary>
/// Represents a primitive type.
/// </summary>
[TypeKind(TypeKind.Primitive)]
public sealed class PrimitiveType : TypeNode
{
#region Static
Expand Down Expand Up @@ -152,6 +153,7 @@ obj is PrimitiveType primitiveType &&
/// <summary>
/// Represents a string type.
/// </summary>
[TypeKind(TypeKind.String)]
public sealed class StringType : TypeNode
{
#region Instance
Expand Down
1 change: 1 addition & 0 deletions Src/ILGPU/IR/Types/StructureType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace ILGPU.IR.Types
/// <summary>
/// Represents a structure type.
/// </summary>
[TypeKind(TypeKind.Structure)]
public sealed class StructureType : ObjectType, IEnumerable<(TypeNode, FieldAccess)>
{
#region Nested Types
Expand Down
59 changes: 58 additions & 1 deletion Src/ILGPU/IR/Types/TypeKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
// Copyright (c) 2024 ILGPU Project
// www.ilgpu.net
//
// File: TypeNodeKind.cs
// File: TypeKind.cs
//
// This file is part of ILGPU and is distributed under the University of Illinois Open
// Source License. See LICENSE.txt for details.
// ---------------------------------------------------------------------------------------

using ILGPU.Util;
using System;
using System.Reflection;

namespace ILGPU.IR.Types
{
/// <summary>
Expand Down Expand Up @@ -65,5 +69,58 @@ public enum TypeKind
/// See <see cref="HandleType"/>
/// </summary>
Handle,

/// <summary>
/// Represents the number of distinct type kinds described by this enumeration
/// </summary>
MaxValue,
}

/// <summary>
/// Marks value classes with specific type kinds.
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
internal sealed class TypeKindAttribute : Attribute
{
/// <summary>
/// Constructs a new type kind attribute.
/// </summary>
/// <param name="kind">The type kind.</param>
public TypeKindAttribute(TypeKind kind)
{
Kind = kind;
}

/// <summary>
/// Returns the type kind.
/// </summary>
public TypeKind Kind { get; }
}

/// <summary>
/// Utility methods for <see cref="TypeKind"/> enumeration values.
/// </summary>
public static class TypeKinds
{
/// <summary>
/// The number of different value kinds.
/// </summary>
public const int NumTypeKinds = (int)TypeKind.MaxValue;

/// <summary>
/// Gets the value kind of the value type specified.
/// </summary>
/// <typeparam name="TType">The compile-time type.</typeparam>
/// <returns>The determined type kind.</returns>
public static TypeKind GetTypeKind<TType>()
where TType : TypeNode =>
typeof(TType).GetCustomAttribute<TypeKindAttribute>().AsNotNull().Kind;

/// <summary>
/// Gets the type kind of the type specified.
/// </summary>
/// <returns>The determined type kind.</returns>
public static TypeKind? GetTypeKind(Type type) =>
type.GetCustomAttribute<TypeKindAttribute>()?.Kind;
}
}
1 change: 1 addition & 0 deletions Src/ILGPU/IR/Types/VoidType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace ILGPU.IR.Types
/// <summary>
/// Represents a void type.
/// </summary>
[TypeKind(TypeKind.Void)]
public sealed class VoidType : TypeNode
{
#region Instance
Expand Down

0 comments on commit dab6511

Please sign in to comment.