Skip to content

Commit

Permalink
Cleaned up a bunch of logic and deleted the now unused TypeV2.
Browse files Browse the repository at this point in the history
  • Loading branch information
MeltyPlayer committed Apr 27, 2024
1 parent c537770 commit b713936
Show file tree
Hide file tree
Showing 19 changed files with 390 additions and 987 deletions.
18 changes: 9 additions & 9 deletions Schema Build Tests/attributes/sequence/ClassSequenceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public override bool Equals(object other) {


[BinarySchema]
public partial class StructArraySequenceWrapper : IBinaryConvertible {
public partial class ClassArraySequenceWrapper : IBinaryConvertible {
[SequenceLengthSource(SchemaIntegerType.BYTE)]
public SchemaClass[] Values { get; set; }

public override bool Equals(object other) {
if (other is StructArraySequenceWrapper otherSequenceWrapper) {
if (other is ClassArraySequenceWrapper otherSequenceWrapper) {
return this.Values.SequenceEqual(otherSequenceWrapper.Values);
}

Expand All @@ -39,7 +39,7 @@ public override bool Equals(object other) {

[Test]
public void TestWriteAndReadArrayObject() {
var expectedSw = new StructArraySequenceWrapper {
var expectedSw = new ClassArraySequenceWrapper {
Values = new[]
{
new SchemaClass { Value = 1 },
Expand All @@ -58,13 +58,13 @@ public void TestWriteAndReadArrayObject() {

var er = new SchemaBinaryReader(ms, endianness);
er.Position = 0;
var actualSws = er.ReadNew<StructArraySequenceWrapper>();
var actualSws = er.ReadNew<ClassArraySequenceWrapper>();
Assert.AreEqual(expectedSw, actualSws);
}

[Test]
public void TestWriteAndReadArrayValues() {
var expectedSw = new StructArraySequenceWrapper {
var expectedSw = new ClassArraySequenceWrapper {
Values = new[]
{
new SchemaClass { Value = 1 },
Expand All @@ -89,13 +89,13 @@ public void TestWriteAndReadArrayValues() {


[BinarySchema]
public partial class StructListSequenceWrapper : IBinaryConvertible
public partial class ClassListSequenceWrapper : IBinaryConvertible
{
[SequenceLengthSource(SchemaIntegerType.BYTE)]
public List<SchemaClass> Values { get; set; } = new();

public override bool Equals(object other) {
if (other is StructListSequenceWrapper otherSequenceWrapper) {
if (other is ClassListSequenceWrapper otherSequenceWrapper) {
return this.Values.SequenceEqual(otherSequenceWrapper.Values);
}

Expand All @@ -105,7 +105,7 @@ public override bool Equals(object other) {

[Test]
public void TestWriteAndReadListObject() {
var expectedSw = new StructListSequenceWrapper {
var expectedSw = new ClassListSequenceWrapper {
Values = new List<SchemaClass>
{
new() { Value = 1 },
Expand All @@ -124,7 +124,7 @@ public void TestWriteAndReadListObject() {

ms.Position = 0;
var er = new SchemaBinaryReader(ms, endianness);
var actualSw = er.ReadNew<StructListSequenceWrapper>();
var actualSw = er.ReadNew<ClassListSequenceWrapper>();
Assert.AreEqual(expectedSw, actualSw);
}
}
Expand Down
8 changes: 4 additions & 4 deletions Schema/src/binary/BinarySchemaAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;

using schema.util.symbols;
using schema.util.syntax;
using schema.util.types;

Expand All @@ -24,6 +25,7 @@ public override ImmutableArray<DiagnosticDescriptor>
Rules.ChildTypeCanOnlyBeContainedInParent,
Rules.ChildTypeMustBeContainedInParent,
Rules.ConstUninitialized,
Rules.ContainerMemberBinaryConvertabilityNeedsToSatisfyParent,
Rules.ContainerTypeMustBePartial,
Rules.DependentMustComeAfterSource,
Rules.EnumNeedsIntegerFormat,
Expand All @@ -38,7 +40,7 @@ public override ImmutableArray<DiagnosticDescriptor>
Rules.ReadAlreadyDefined,
Rules.SchemaTypeMustBePartial,
Rules.SourceMustBePrivate,
Rules.ContainerMemberBinaryConvertabilityNeedsToSatisfyParent,
Rules.SymbolException,
Rules.UnexpectedAttribute,
Rules.UnexpectedSequenceAttribute,
Rules.UnsupportedArrayType,
Expand Down Expand Up @@ -83,10 +85,8 @@ public void CheckType(
SyntaxNodeAnalysisContext context,
TypeDeclarationSyntax syntax,
INamedTypeSymbol symbol) {
var typeV2 = TypeV2.FromSymbol(symbol);

try {
if (!typeV2.HasAttribute<BinarySchemaAttribute>()) {
if (!symbol.HasAttribute<BinarySchemaAttribute>()) {
return;
}

Expand Down
22 changes: 12 additions & 10 deletions Schema/src/binary/BinarySchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using schema.binary.parser;
using schema.binary.text;
using schema.util.generators;
using schema.util.symbols;
using schema.util.syntax;
using schema.util.types;

Expand Down Expand Up @@ -70,20 +71,21 @@ public override void PreprocessCompilation(Compilation compilation) {

public override IEnumerable<(string fileName, string source)>
GenerateSourcesForMappedNamedType(IBinarySchemaContainer container) {
var containerTypeV2 = TypeV2.FromSymbol(container.TypeSymbol);
if (containerTypeV2.Implements<IBinaryDeserializable>() &&
container.TypeSymbol.MemberNames.All(member => member != "Read")) {
var containerSymbol = container.TypeSymbol;
if (containerSymbol.Implements<IBinaryDeserializable>() &&
containerSymbol.MemberNames.All(member => member != "Read")) {
var readerCode = this.readerImpl_.Generate(container);
yield return ($"{containerTypeV2.FullyQualifiedName}_{containerTypeV2.Arity}_reader.g",
readerCode);
yield return (
$"{containerSymbol.GetUniqueNameForGenerator()}_reader.g",
readerCode);
}

if (containerTypeV2.Implements<IBinarySerializable>() &&
container.TypeSymbol.MemberNames.All(
member => member != "Write")) {
if (containerSymbol.Implements<IBinarySerializable>() &&
containerSymbol.MemberNames.All(member => member != "Write")) {
var writerCode = this.writerImpl_.Generate(container);
yield return ($"{containerTypeV2.FullyQualifiedName}_{containerTypeV2.Arity}_writer.g",
writerCode);
yield return (
$"{containerSymbol.GetUniqueNameForGenerator()}_writer.g",
writerCode);
}
}
}
Expand Down
13 changes: 5 additions & 8 deletions Schema/src/binary/BinarySchemaStructureParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,28 +156,25 @@ public class BinarySchemaContainerParser : IBinarySchemaContainerParser {
public IBinarySchemaContainer ParseContainer(
INamedTypeSymbol containerSymbol) {
var containerBetterSymbol = BetterSymbol.FromType(containerSymbol);
var containerTypeV2 =
TypeV2.FromSymbol(containerBetterSymbol.TypedSymbol,
containerBetterSymbol);

// All of the types that contain the container need to be partial
new PartialContainerAsserter(containerBetterSymbol)
.AssertContainersArePartial(containerSymbol);

if (containerTypeV2.IsChild(out var parentTypeV2)) {
if (!parentTypeV2.ContainsMemberWithType(containerTypeV2)) {
if (containerSymbol.IsChild(out var parentSymbol)) {
if (!parentSymbol.ContainsMemberWithType(containerSymbol)) {
containerBetterSymbol.ReportDiagnostic(
Rules.ChildTypeMustBeContainedInParent);
}

if (!parentTypeV2.IsAtLeastAsBinaryConvertibleAs(containerTypeV2)) {
if (!parentSymbol.IsAtLeastAsBinaryConvertibleAs(containerSymbol)) {
containerBetterSymbol.ReportDiagnostic(
Rules.ParentBinaryConvertabilityMustSatisfyChild);
}
}

var localPositions =
containerTypeV2.HasAttribute<LocalPositionsAttribute>();
containerSymbol.HasAttribute<LocalPositionsAttribute>();
var containerEndianness =
new EndiannessParser().GetEndianness(containerBetterSymbol);

Expand Down Expand Up @@ -225,7 +222,7 @@ public IBinarySchemaContainer ParseContainer(
bool isSkipped =
memberBetterSymbol.HasAttribute<SkipAttribute>() ||
(memberSymbol.Name == nameof(IChildOf<IBinaryConvertible>.Parent) &&
parentTypeV2 != null);
parentSymbol != null);

// Skips parent field for child types

Expand Down
7 changes: 5 additions & 2 deletions Schema/src/binary/BinarySchemaSymbolUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.CodeAnalysis;

using schema.binary.attributes;
using schema.util.asserts;
using schema.util.symbols;
using schema.util.types;

Expand All @@ -22,9 +23,11 @@ public static bool IsBinarySerializable(this ISymbol symbol)
public static bool IsBinaryDeserializable(this ISymbol symbol)
=> symbol.Implements<IBinaryDeserializable>();

public static bool IsChild(this ISymbol symbol, out ISymbol parent) {
public static bool
IsChild(this ISymbol symbol, out INamedTypeSymbol parent) {
if (symbol.Implements(typeof(IChildOf<>), out var matchingType)) {
parent = matchingType.TypeArguments.First();
parent = Asserts.AsA<INamedTypeSymbol>(
matchingType.TypeArguments.First());
return true;
}

Expand Down
26 changes: 21 additions & 5 deletions Schema/src/binary/rules/Rules.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;

Expand Down Expand Up @@ -39,10 +40,11 @@ public static readonly DiagnosticDescriptor ChildTypeMustBeContainedInParent
"Child type must be contained in parent",
"Type '{0}' is defined as a child, but is not actually contained in its parent type.");

public static readonly DiagnosticDescriptor ParentBinaryConvertabilityMustSatisfyChild
= Rules.CreateDiagnosticDescriptor_(
"Parent's binary convertability must satisfy child's",
"Type '{0}' is defined as a child, but its binary convertability is not satisfied by its parent's. The parent must at least implement the same binary serializable/deserializable interface as the child.");
public static readonly DiagnosticDescriptor
ParentBinaryConvertabilityMustSatisfyChild
= Rules.CreateDiagnosticDescriptor_(
"Parent's binary convertability must satisfy child's",
"Type '{0}' is defined as a child, but its binary convertability is not satisfied by its parent's. The parent must at least implement the same binary serializable/deserializable interface as the child.");


public static readonly DiagnosticDescriptor
Expand Down Expand Up @@ -130,6 +132,11 @@ public static readonly DiagnosticDescriptor UnsupportedArrayType
"Array type '{0}' is not currently supported.");

public static DiagnosticDescriptor Exception { get; }
= Rules.CreateDiagnosticDescriptor_(
"Exception",
"Ran into an exception while generating source ({0}),{1}");

public static DiagnosticDescriptor SymbolException { get; }
= Rules.CreateDiagnosticDescriptor_(
"Exception",
"Ran into an exception while parsing ({0}),{1}");
Expand All @@ -154,11 +161,20 @@ public static Diagnostic CreateExceptionDiagnostic(
ISymbol symbol,
Exception exception)
=> Diagnostic.Create(
Rules.Exception,
Rules.SymbolException,
symbol.Locations.First(),
exception.Message,
exception.StackTrace.Replace("\r\n", "").Replace("\n", ""));

public static Diagnostic CreateExceptionDiagnostic(
Exception exception)
=> Diagnostic.Create(
Rules.Exception,
null,
exception.Message,
exception.StackTrace.Replace("\r\n", "").Replace("\n", ""));


public static void ReportExceptionDiagnostic(
SyntaxNodeAnalysisContext? context,
ISymbol symbol,
Expand Down
3 changes: 1 addition & 2 deletions Schema/src/readOnly/ReadOnlyTypeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ internal override bool FilterNamedTypesBeforeGenerating(
GenerateSourcesForNamedType(INamedTypeSymbol symbol,
SemanticModel semanticModel,
TypeDeclarationSyntax syntax) {
var typeV2 = TypeV2.FromSymbol(symbol);
yield return ($"{typeV2.FullyQualifiedName}_{typeV2.Arity}_readOnly.g",
yield return ($"{symbol.GetUniqueNameForGenerator()}_readOnly.g",
this.GenerateSourceForNamedType(
symbol,
semanticModel,
Expand Down
3 changes: 1 addition & 2 deletions Schema/src/util/AccessChainUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,8 @@ string prevMemberName
IsOrderValid = comesAfter,
});

var containerTypeV2 = TypeV2.FromSymbol(containerSymbol);
if (currentMemberName == nameof(IChildOf<IBinaryConvertible>.Parent) &&
containerTypeV2.IsChild(out _)) {
containerSymbol.IsChild(out _)) {
upDownStack.PushUpFrom(prevMemberName);
} else {
upDownStack.PushDownTo(currentMemberName);
Expand Down
2 changes: 1 addition & 1 deletion Schema/src/util/diagnostics/DiagnosticReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void ReportDiagnostic(ISymbol symbol,

public void ReportException(Exception exception)
=> this.ReportDiagnosticImpl_(
Diagnostic.Create(Rules.Exception,
Diagnostic.Create(Rules.SymbolException,
this.symbol_.Locations.First(),
exception.Message,
exception.StackTrace.Replace("\r\n", "")
Expand Down
7 changes: 7 additions & 0 deletions Schema/src/util/enumerables/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
using System.Collections.Generic;
using System.Linq;

namespace schema.util.enumerables {
public static class EnumerableExtensions {
public static IEnumerable<T> Yield<T>(this T value) {
yield return value;
}

public static IEnumerable<T> WhereNonnull<T>(
this IEnumerable<T?> enumerable)
=> enumerable.Select(v => (v != null, v))
.Where(pair => pair.Item1)
.Select(pair => pair.v!);

public static IEnumerable<T> Resized<T>(
this IEnumerable<T>? enumerable,
int length) where T : new() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ public abstract void PreprocessCompilation(
public void Initialize(IncrementalGeneratorInitializationContext context) {
context.RegisterImplementationSourceOutput(
context.CompilationProvider,
(_, compilation) => {
PreprocessCompilation(compilation);
(context, compilation) => {
try {
this.PreprocessCompilation(compilation);
} catch (Exception e) {
context.ReportDiagnostic(Rules.CreateExceptionDiagnostic(e));
}
});

var syntaxAndSymbolProvider
Expand Down
Loading

0 comments on commit b713936

Please sign in to comment.