-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeSymbolExtensions.cs
More file actions
41 lines (35 loc) · 1 KB
/
TypeSymbolExtensions.cs
File metadata and controls
41 lines (35 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System.Diagnostics;
using Surab.Analysis;
namespace Surab.Compilation;
public enum RuntimeTypeKind
{
Invalid,
ZeroSized,
Numeric,
Char,
Bool,
Struct,
Ptr,
}
public static class TypeSymbolExtensions
{
public static bool IsScalar(this HirType type)
{
return type.GetRuntimeKind() switch
{
RuntimeTypeKind.Numeric or RuntimeTypeKind.Char or RuntimeTypeKind.Bool or RuntimeTypeKind.Ptr => true,
_ => false,
};
}
public static RuntimeTypeKind GetRuntimeKind(this HirType type)
{
var tag = type.KnownTypeTag;
if (tag is KnownTypeTag.@void) return RuntimeTypeKind.ZeroSized;
if (tag is KnownTypeTag.@char) return RuntimeTypeKind.Char;
if (tag is KnownTypeTag.@string || type.Kind is HirTypeKind.Ptr or HirTypeKind.Fn) return RuntimeTypeKind.Ptr;
if (type.KnownTypeTag.IsNumeric()) return RuntimeTypeKind.Numeric;
if (tag is KnownTypeTag.@bool) return RuntimeTypeKind.Bool;
if (type.Kind is HirTypeKind.Struct) return RuntimeTypeKind.Struct;
throw new UnreachableException();
}
}