forked from MochiLibraries/Biohazrd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCSharpBuiltinType.cs
147 lines (126 loc) · 5.71 KB
/
CSharpBuiltinType.cs
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using Biohazrd.Transformation;
using System;
using System.Collections.Immutable;
namespace Biohazrd.CSharp
{
public sealed class CSharpBuiltinType
{
public int SizeOf { get; }
public string CSharpKeyword { get; }
public string FullyQualifiedDotNetName { get; }
public bool IsValidUnderlyingEnumType { get; init; }
public bool IsIntegral { get; init; }
public bool IsSigned => IsIntegral && MinValue < 0;
private readonly ulong _MaxValue;
private readonly long _MinValue;
public ulong MaxValue
{
get => IsIntegral ? _MaxValue : throw new NotSupportedException("You can only get the maximum value of an integral type.");
init => _MaxValue = value;
}
public long MinValue
{
get => IsIntegral ? _MinValue : throw new NotSupportedException("You can only get the minimum value of an integral type.");
init => _MinValue = value;
}
public ulong FullBitMask => SizeOf == 8 ? ulong.MaxValue : (1UL << (8 * SizeOf)) - 1UL;
internal CSharpBuiltinTypeReference Reference { get; }
private CSharpBuiltinType(int sizeOf, string cSharpKeyword, string fullyQualifiedDotNetName)
{
SizeOf = sizeOf;
CSharpKeyword = cSharpKeyword;
Reference = new CSharpBuiltinTypeReference(this);
FullyQualifiedDotNetName = fullyQualifiedDotNetName;
}
public override string ToString()
// We use the fully qualified .NET name to make it easier to tell the difference between this and Clang types in the debugger.
=> FullyQualifiedDotNetName;
public static implicit operator CSharpBuiltinTypeReference(CSharpBuiltinType type)
=> type.Reference;
public static implicit operator TypeReference(CSharpBuiltinType type)
=> type.Reference;
//=========================================================================================================================================================================
// Type definitions
//=========================================================================================================================================================================
public static readonly CSharpBuiltinType Byte = new(sizeof(byte), "byte", "System.Byte")
{
IsValidUnderlyingEnumType = true,
IsIntegral = true,
MinValue = byte.MinValue,
MaxValue = byte.MaxValue
};
public static readonly CSharpBuiltinType SByte = new(sizeof(sbyte), "sbyte", "System.SByte")
{
IsValidUnderlyingEnumType = true,
IsIntegral = true,
MinValue = sbyte.MinValue,
MaxValue = (long)sbyte.MaxValue
};
public static readonly CSharpBuiltinType Short = new(sizeof(short), "short", "System.Int16")
{
IsValidUnderlyingEnumType = true,
IsIntegral = true,
MinValue = short.MinValue,
MaxValue = (long)short.MaxValue
};
public static readonly CSharpBuiltinType UShort = new(sizeof(ushort), "ushort", "System.UInt16")
{
IsValidUnderlyingEnumType = true,
IsIntegral = true,
MinValue = ushort.MinValue,
MaxValue = ushort.MaxValue
};
public static readonly CSharpBuiltinType Int = new(sizeof(int), "int", "System.Int32")
{
IsValidUnderlyingEnumType = true,
IsIntegral = true,
MinValue = int.MinValue,
MaxValue = int.MaxValue
};
public static readonly CSharpBuiltinType UInt = new(sizeof(uint), "uint", "System.UInt32")
{
IsValidUnderlyingEnumType = true,
IsIntegral = true,
MinValue = uint.MinValue,
MaxValue = uint.MaxValue
};
public static readonly CSharpBuiltinType Long = new(sizeof(long), "long", "System.Int64")
{
IsValidUnderlyingEnumType = true,
IsIntegral = true,
MinValue = long.MinValue,
MaxValue = long.MaxValue
};
public static readonly CSharpBuiltinType ULong = new(sizeof(ulong), "ulong", "System.UInt64")
{
IsValidUnderlyingEnumType = true,
IsIntegral = true,
MinValue = (long)ulong.MinValue,
MaxValue = ulong.MaxValue
};
public static readonly CSharpBuiltinType Bool = new(sizeof(bool), "bool", "System.Boolean");
public static readonly CSharpBuiltinType Char = new(sizeof(char), "char", "System.Char");
public static readonly CSharpBuiltinType Float = new(sizeof(float), "float", "System.Single");
public static readonly CSharpBuiltinType Double = new(sizeof(double), "double", "System.Double");
public static readonly ImmutableArray<CSharpBuiltinType> AllTypes = ImmutableArray.Create
(
Byte,
SByte,
Short,
UShort,
Int,
UInt,
Long,
ULong,
Bool,
Char,
Float,
Double
);
// The following are placeholders and should not be added to AllTypes or made public
// They don't fit into the constraints CSharpBuiltinType. If we're going to expose these I'd rather make something specific to them.
internal static readonly TypeReference String = new ExternallyDefinedTypeReference("string");
internal static readonly TypeReference NativeInt = new ExternallyDefinedTypeReference("nint");
internal static readonly TypeReference NativeUnsignedInt = new ExternallyDefinedTypeReference("nuint");
}
}