Skip to content

Commit 5201086

Browse files
committed
Improved Schema's compile-time validation.
1 parent 6345e22 commit 5201086

21 files changed

Lines changed: 252 additions & 113 deletions

Schema/src/binary/attributes/BMemberAttribute.cs

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,25 @@ public abstract class BMemberAttribute : Attribute {
2525
private ITypeInfo containerTypeInfo_;
2626
protected IMemberReference memberThisIsAttachedTo_;
2727

28-
protected abstract void InitFields();
28+
protected abstract void InitFields(
29+
IDiagnosticReporter diagnosticReporter,
30+
IMemberReference memberThisIsAttachedTo);
2931

3032
protected virtual void SetMemberFromName(string memberName) {
3133
this.memberThisIsAttachedTo_ =
3234
this.GetMemberRelativeToContainer(memberName);
3335
}
3436

35-
3637
internal void Init(
37-
IDiagnosticReporter? diagnosticReporter,
38+
IDiagnosticReporter diagnosticReporter,
3839
INamedTypeSymbol containerTypeSymbol,
3940
string memberName) {
4041
this.diagnosticReporter_ = diagnosticReporter;
4142
this.containerTypeSymbol_ = containerTypeSymbol;
4243
this.containerTypeInfo_ = BMemberAttribute.parser_.AssertParseType(
4344
containerTypeSymbol);
4445
this.SetMemberFromName(memberName);
45-
this.InitFields();
46+
this.InitFields(diagnosticReporter, this.memberThisIsAttachedTo_);
4647
}
4748

4849

@@ -208,16 +209,10 @@ public interface IMemberReference {
208209
ITypeInfo MemberTypeInfo { get; }
209210

210211
bool IsInteger { get; }
211-
IMemberReference AssertIsInteger();
212-
213212
bool IsFloat { get; }
214-
IMemberReference AssertIsFloat();
215-
216213
bool IsBool { get; }
217-
IMemberReference AssertIsBool();
218-
219214
bool IsSequence { get; }
220-
IMemberReference AssertIsSequence();
215+
bool IsString { get; }
221216
}
222217

223218
public interface IMemberReference<T> : IMemberReference { }
@@ -237,14 +232,6 @@ public class MemberReference(
237232

238233
public bool IsInteger => this.MemberTypeInfo is IIntegerTypeInfo;
239234

240-
public IMemberReference AssertIsInteger() {
241-
if (!this.IsInteger) {
242-
Asserts.Fail($"Expected {this.Name} to refer to an integer!");
243-
}
244-
245-
return this;
246-
}
247-
248235
public bool IsFloat
249236
=> this.MemberTypeInfo is INumberTypeInfo {
250237
NumberType: (SchemaNumberType.HALF
@@ -256,33 +243,11 @@ or SchemaNumberType.UN8
256243
or SchemaNumberType.UN16)
257244
};
258245

259-
public IMemberReference AssertIsFloat() {
260-
if (!this.IsFloat) {
261-
Asserts.Fail($"Expected {this.Name} to refer to a float!");
262-
}
263-
264-
return this;
265-
}
266-
267246
public bool IsBool => this.MemberTypeInfo is IBoolTypeInfo;
268247

269-
public IMemberReference AssertIsBool() {
270-
if (!this.IsBool) {
271-
Asserts.Fail($"Expected {this.Name} to refer to an bool!");
272-
}
273-
274-
return this;
275-
}
276-
277248
public bool IsSequence => this.MemberTypeInfo is ISequenceTypeInfo;
278249

279-
public IMemberReference AssertIsSequence() {
280-
if (!this.IsSequence) {
281-
Asserts.Fail($"Expected {this.Name} to refer to a sequence!");
282-
}
283-
284-
return this;
285-
}
250+
public bool IsString => this.MemberTypeInfo is IStringTypeInfo;
286251
}
287252

288253
public class MemberReference<T> : MemberReference, IMemberReference<T> {

Schema/src/binary/attributes/align/AlignStartAttribute.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22

3+
using schema.util.diagnostics;
4+
35

46
namespace schema.binary.attributes;
57

@@ -17,11 +19,14 @@ public AlignStartAttribute(string otherMemberName) {
1719
this.otherMemberName_ = otherMemberName;
1820
}
1921

20-
protected override void InitFields() {
22+
protected override void InitFields(
23+
IDiagnosticReporter diagnosticReporter,
24+
IMemberReference memberThisIsAttachedTo) {
2125
if (this.otherMemberName_ != null) {
2226
this.OtherMember =
23-
this.GetOtherMemberRelativeToContainer(this.otherMemberName_)
24-
.AssertIsInteger();
27+
this.GetOtherMemberRelativeToContainer(this.otherMemberName_);
28+
29+
// TODO: Validate type
2530
}
2631
}
2732

Schema/src/binary/attributes/if_boolean/RIfBooleanAttribute.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace schema.binary.attributes;
1+
using schema.util.diagnostics;
2+
3+
namespace schema.binary.attributes;
24

35
public class RIfBooleanAttribute : BMemberAttribute, IIfBooleanAttribute {
46
private readonly string? otherMemberName_;
@@ -8,11 +10,14 @@ public RIfBooleanAttribute(string otherMemberName) {
810
this.otherMemberName_ = otherMemberName;
911
}
1012

11-
protected override void InitFields() {
13+
protected override void InitFields(
14+
IDiagnosticReporter diagnosticReporter,
15+
IMemberReference memberThisIsAttachedTo) {
1216
if (this.otherMemberName_ != null) {
1317
this.OtherMember =
14-
this.GetReadTimeOnlySourceRelativeToContainer(this.otherMemberName_)
15-
.AssertIsBool();
18+
this.GetReadTimeOnlySourceRelativeToContainer(this.otherMemberName_);
19+
20+
// TODO: Validate types
1621
}
1722
}
1823

Schema/src/binary/attributes/memory/RPositionRelativeToStreamAttribute.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22

3+
using schema.util.diagnostics;
4+
35

46
namespace schema.binary.attributes;
57

@@ -34,5 +36,7 @@ namespace schema.binary.attributes;
3436
/// </summary>
3537
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
3638
public class RPositionRelativeToStreamAttribute : BMemberAttribute<long> {
37-
protected override void InitFields() { }
39+
protected override void InitFields(
40+
IDiagnosticReporter diagnosticReporter,
41+
IMemberReference memberThisIsAttachedTo) { }
3842
}

Schema/src/binary/attributes/memory/WPointerToAttribute.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22

3+
using schema.util.diagnostics;
4+
35

46
namespace schema.binary.attributes;
57

@@ -15,7 +17,8 @@ public WPointerToAttribute(string otherMemberName) {
1517
this.otherMemberName_ = otherMemberName;
1618
}
1719

18-
protected override void InitFields() {
20+
protected override void InitFields(IDiagnosticReporter diagnosticReporter,
21+
IMemberReference memberThisIsAttachedTo) {
1922
this.AccessChainToOtherMember =
2023
this.GetAccessChainRelativeToContainer(
2124
this.otherMemberName_,

Schema/src/binary/attributes/memory/WPointerToOrNullAttribute.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22

3+
using schema.util.diagnostics;
4+
35

46
namespace schema.binary.attributes;
57

@@ -19,7 +21,8 @@ public WPointerToOrNullAttribute(string otherMemberName,
1921
this.NullValue = nullValue;
2022
}
2123

22-
protected override void InitFields() {
24+
protected override void InitFields(IDiagnosticReporter diagnosticReporter,
25+
IMemberReference memberThisIsAttachedTo) {
2326
this.AccessChainToOtherMember =
2427
this.GetAccessChainRelativeToContainer(
2528
this.otherMemberName_,

Schema/src/binary/attributes/memory/WSizeOfMemberInBytesAttribute.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22

3+
using schema.util.diagnostics;
4+
35

46
namespace schema.binary.attributes;
57

@@ -24,11 +26,15 @@ public WSizeOfMemberInBytesAttribute(string otherMemberName) {
2426
this.otherMemberName_ = otherMemberName;
2527
}
2628

27-
protected override void InitFields() {
29+
protected override void InitFields(
30+
IDiagnosticReporter diagnosticReporter,
31+
IMemberReference memberThisIsAttachedTo) {
2832
this.AccessChainToOtherMember =
2933
this.GetAccessChainRelativeToContainer(
3034
this.otherMemberName_,
3135
false);
36+
37+
// TODO: Validate types
3238
}
3339

3440
public IChain<IAccessChainNode> AccessChainToOtherMember { get; private set; }

Schema/src/binary/attributes/numbers/FixedPointAttribute.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22

3+
using schema.util.diagnostics;
4+
35

46
namespace schema.binary.attributes;
57

@@ -12,8 +14,11 @@ public class FixedPointAttribute(
1214
public int IntegerBits { get; set; } = integerBits;
1315
public int FractionBits { get; set; } = fractionBits;
1416

15-
protected override void InitFields() {
16-
this.memberThisIsAttachedTo_.AssertIsFloat();
17+
protected override void InitFields(IDiagnosticReporter diagnosticReporter,
18+
IMemberReference memberThisIsAttachedTo) {
19+
if (!memberThisIsAttachedTo.IsFloat) {
20+
diagnosticReporter.ReportDiagnostic(Rules.FixedPointCanOnlyBeUsedOnFloats);
21+
}
1722
}
1823

1924
public SchemaIntegerType IntegerType

Schema/src/binary/attributes/sequence/RSequenceLengthSourceAttribute.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22

3+
using schema.util.diagnostics;
4+
35

46
namespace schema.binary.attributes;
57

@@ -18,11 +20,22 @@ public RSequenceLengthSourceAttribute(string otherMemberName) {
1820
this.otherMemberName_ = otherMemberName;
1921
}
2022

21-
protected override void InitFields() {
23+
protected override void InitFields(IDiagnosticReporter diagnosticReporter,
24+
IMemberReference memberThisIsAttachedTo) {
2225
if (this.otherMemberName_ != null) {
2326
this.OtherMember =
24-
this.GetReadTimeOnlySourceRelativeToContainer(this.otherMemberName_)
25-
.AssertIsInteger();
27+
this.GetReadTimeOnlySourceRelativeToContainer(this.otherMemberName_);
28+
29+
if (!memberThisIsAttachedTo.IsSequence) {
30+
diagnosticReporter.ReportDiagnostic(
31+
memberThisIsAttachedTo.MemberSymbol,
32+
Rules.SequenceLengthSourceCanOnlyBeUsedOnSequences);
33+
}
34+
if (!this.OtherMember.IsInteger) {
35+
diagnosticReporter.ReportDiagnostic(
36+
memberThisIsAttachedTo.MemberSymbol,
37+
Rules.RSequenceLengthSourceOtherFieldMustBeAnInteger);
38+
}
2639
}
2740
}
2841

Schema/src/binary/attributes/sequence/SequenceLengthSourceAttribute.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
using System;
22

3+
using schema.util.diagnostics;
4+
35

46
namespace schema.binary.attributes;
57

68
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
79
public class SequenceLengthSourceAttribute
8-
: Attribute,
10+
: BMemberAttribute,
911
ISequenceLengthSourceAttribute {
1012
/// <summary>
1113
/// Parses an integer length with the given format immediately before the array.
@@ -23,6 +25,16 @@ public SequenceLengthSourceAttribute(uint constLength) {
2325
this.ConstLength = constLength;
2426
}
2527

28+
protected override void InitFields(
29+
IDiagnosticReporter diagnosticReporter,
30+
IMemberReference memberThisIsAttachedTo) {
31+
if (!memberThisIsAttachedTo.IsSequence) {
32+
diagnosticReporter.ReportDiagnostic(
33+
memberThisIsAttachedTo.MemberSymbol,
34+
Rules.SequenceLengthSourceCanOnlyBeUsedOnSequences);
35+
}
36+
}
37+
2638
public SequenceLengthSourceType Method { get; }
2739

2840
public SchemaIntegerType LengthType { get; }

0 commit comments

Comments
 (0)