Skip to content

Commit

Permalink
Cleaned up the new symbol logic a bit.
Browse files Browse the repository at this point in the history
  • Loading branch information
MeltyPlayer committed Aug 2, 2023
1 parent dbf70cf commit 7a90a5b
Show file tree
Hide file tree
Showing 27 changed files with 356 additions and 181 deletions.
3 changes: 3 additions & 0 deletions Schema/src/binary/BinarySchemaAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;

using schema.util.symbols;


namespace schema.binary {
[DiagnosticAnalyzer(LanguageNames.CSharp)]
Expand Down Expand Up @@ -47,6 +49,7 @@ public override void Initialize(AnalysisContext context) {
/*if (!Debugger.IsAttached) {
Debugger.Launch();
}*/

context.RegisterSyntaxNodeAction(
syntaxNodeContext => {
var syntax = syntaxNodeContext.Node as ClassDeclarationSyntax;
Expand Down
1 change: 1 addition & 0 deletions Schema/src/binary/BinarySchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using schema.binary.attributes;
using schema.binary.text;
using schema.util;
using schema.util.symbols;

namespace schema.binary {
[Generator(LanguageNames.CSharp)]
Expand Down
111 changes: 45 additions & 66 deletions Schema/src/binary/BinarySchemaStructureParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using schema.binary.attributes;
using schema.binary.parser;
using schema.binary.parser.asserts;
using schema.util.diagnostics;
using schema.util.symbols;


namespace schema.binary {
Expand Down Expand Up @@ -140,14 +140,13 @@ public class BinarySchemaStructureParser : IBinarySchemaStructureParser {
public IBinarySchemaStructure ParseStructure(
INamedTypeSymbol structureSymbol,
SyntaxNodeAnalysisContext? context = null) {
var structureDiagnosticReporter =
new DiagnosticReporter(structureSymbol, context);
var betterStructureSymbol = BetterSymbol.FromType(structureSymbol);

// All of the types that contain the structure need to be partial
new PartialContainerAsserter(structureDiagnosticReporter).AssertContainersArePartial(
new PartialContainerAsserter(betterStructureSymbol).AssertContainersArePartial(
structureSymbol);

var iChildOfParser = new ChildOfParser(structureDiagnosticReporter);
var iChildOfParser = new ChildOfParser(betterStructureSymbol);
var parentTypeSymbol =
iChildOfParser.GetParentTypeSymbolOf(structureSymbol);
if (parentTypeSymbol != null) {
Expand All @@ -159,11 +158,9 @@ public IBinarySchemaStructure ParseStructure(
structureSymbol);
}

var localPositions =
structureSymbol.HasAttribute<LocalPositionsAttribute>();
var localPositions = betterStructureSymbol.HasAttribute<LocalPositionsAttribute>();
var structureEndianness =
new EndiannessParser().GetEndianness(structureDiagnosticReporter,
structureSymbol);
new EndiannessParser().GetEndianness(betterStructureSymbol);

var typeInfoParser = new TypeInfoParser();
var parsedMembers =
Expand All @@ -177,8 +174,7 @@ public IBinarySchemaStructure ParseStructure(
continue;
}

var memberDiagnosticReporter =
structureDiagnosticReporter.GetSubReporter(memberSymbol);
var betterMemberSymbol = betterStructureSymbol.GetChild(memberSymbol);

{
var methodSymbol = memberSymbol as IMethodSymbol;
Expand All @@ -194,10 +190,10 @@ public IBinarySchemaStructure ParseStructure(
members.Add(
new SchemaMethodMember { Name = methodSymbol.Name });
} else {
memberDiagnosticReporter.ReportDiagnostic(Rules.NotSupported);
betterMemberSymbol.ReportDiagnostic(Rules.NotSupported);
}
} else {
memberDiagnosticReporter.ReportDiagnostic(Rules.NotSupported);
betterMemberSymbol.ReportDiagnostic(Rules.NotSupported);
}
}

Expand All @@ -216,8 +212,8 @@ public IBinarySchemaStructure ParseStructure(
var field =
!isIgnored
? this.ParseNonIgnoredField_(
memberDiagnosticReporter,
structureSymbol,
betterStructureSymbol,
betterMemberSymbol,
parsedMember)
: this.ParseIgnoredField_(parsedMember);

Expand All @@ -227,7 +223,7 @@ public IBinarySchemaStructure ParseStructure(
}

var schemaStructure = new BinarySchemaStructure {
Diagnostics = structureDiagnosticReporter.Diagnostics,
Diagnostics = betterStructureSymbol.Diagnostics,
TypeSymbol = structureSymbol,
Members = members,
LocalPositions = localPositions,
Expand Down Expand Up @@ -263,14 +259,15 @@ public IBinarySchemaStructure ParseStructure(
}

private ISchemaValueMember? ParseNonIgnoredField_(
IDiagnosticReporter memberDiagnosticReporter,
INamedTypeSymbol structureSymbol,
IBetterSymbol<INamedTypeSymbol> betterStructureSymbol,
IBetterSymbol betterMemberSymbol,
(TypeInfoParser.ParseStatus, ISymbol, ITypeInfo) parsedMember
) {
var structureSymbol = betterStructureSymbol.TypedSymbol;
var (parseStatus, memberSymbol, memberTypeInfo) = parsedMember;

if (parseStatus == TypeInfoParser.ParseStatus.NOT_IMPLEMENTED) {
memberDiagnosticReporter.ReportDiagnostic(Rules.NotSupported);
betterMemberSymbol.ReportDiagnostic(Rules.NotSupported);
return null;
}

Expand All @@ -287,15 +284,15 @@ public IBinarySchemaStructure ParseStructure(

if ((isDeserializable && !isMemberDeserializable) ||
(isSerializable && !isMemberSerializable)) {
memberDiagnosticReporter.ReportDiagnostic(
betterMemberSymbol.ReportDiagnostic(
Rules.StructureMemberBinaryConvertabilityNeedsToSatisfyParent);
return null;
}
}
}

var memberEndianness =
new EndiannessParser().GetEndianness(memberDiagnosticReporter, memberSymbol);
new EndiannessParser().GetEndianness(betterMemberSymbol);

// Gets the type of the current member
var memberType =
Expand All @@ -310,66 +307,58 @@ public IBinarySchemaStructure ParseStructure(
};

foreach (var attributeParser in attributeParsers) {
attributeParser.ParseIntoMemberType(memberDiagnosticReporter,
memberSymbol,
attributeParser.ParseIntoMemberType(betterMemberSymbol,
memberTypeInfo,
memberType);
}

// Get attributes
var align =
memberSymbol.GetAttribute<AlignAttribute>(memberDiagnosticReporter);
var align = betterMemberSymbol.GetAttribute<AlignAttribute>();

{
var sizeOfStreamAttribute =
SymbolTypeUtil.GetAttribute<WSizeOfStreamInBytesAttribute>(
memberDiagnosticReporter,
memberSymbol);
betterMemberSymbol.GetAttribute<WSizeOfStreamInBytesAttribute>();
if (sizeOfStreamAttribute != null) {
if (memberTypeInfo is IIntegerTypeInfo &&
memberType is PrimitiveMemberType primitiveMemberType) {
primitiveMemberType.SizeOfStream = true;
} else {
memberDiagnosticReporter.ReportDiagnostic(Rules.NotSupported);
betterMemberSymbol.ReportDiagnostic(Rules.NotSupported);
}
}
}

var isPosition = false;
{
var positionAttribute =
memberSymbol.GetAttribute<RPositionRelativeToStreamAttribute>(
memberDiagnosticReporter);
betterMemberSymbol.GetAttribute<RPositionRelativeToStreamAttribute>();
if (positionAttribute != null) {
isPosition = true;
if (memberTypeInfo is not IIntegerTypeInfo {
IntegerType: SchemaIntegerType.INT64
}) {
memberDiagnosticReporter.ReportDiagnostic(Rules.NotSupported);
betterMemberSymbol.ReportDiagnostic(Rules.NotSupported);
}
}
}

var ifBooleanAttribute =
(IIfBooleanAttribute?)
memberSymbol.GetAttribute<IfBooleanAttribute>(
memberDiagnosticReporter) ??
memberSymbol.GetAttribute<RIfBooleanAttribute>(
memberDiagnosticReporter);
betterMemberSymbol.GetAttribute<IfBooleanAttribute>() ??
betterMemberSymbol.GetAttribute<RIfBooleanAttribute>();
if (ifBooleanAttribute != null && !memberTypeInfo.IsNullable) {
memberDiagnosticReporter.ReportDiagnostic(Rules.IfBooleanNeedsNullable);
betterMemberSymbol.ReportDiagnostic(Rules.IfBooleanNeedsNullable);
}

IOffset? offset = null;
{
var offsetAttribute =
memberSymbol.GetAttribute<RAtPositionAttribute>(
memberDiagnosticReporter);
betterMemberSymbol.GetAttribute<RAtPositionAttribute>();

if (offsetAttribute != null) {
var offsetName = offsetAttribute.OffsetName;
SymbolTypeUtil.GetMemberRelativeToAnother(
memberDiagnosticReporter,
betterMemberSymbol,
structureSymbol,
offsetName,
memberSymbol.Name,
Expand All @@ -389,7 +378,7 @@ public IBinarySchemaStructure ParseStructure(
}

new SupportedElementTypeAsserter()
.AssertElementTypesAreSupported(memberDiagnosticReporter, memberType);
.AssertElementTypesAreSupported(betterMemberSymbol, memberType);

{
IMemberType? targetMemberType;
Expand Down Expand Up @@ -420,8 +409,7 @@ public IBinarySchemaStructure ParseStructure(

{
var numberFormatAttribute =
memberSymbol
.GetAttribute<NumberFormatAttribute>(memberDiagnosticReporter);
betterMemberSymbol.GetAttribute<NumberFormatAttribute>();
if (numberFormatAttribute != null) {
formatNumberType = numberFormatAttribute.NumberType;

Expand All @@ -430,16 +418,15 @@ public IBinarySchemaStructure ParseStructure(
targetPrimitiveType);
if (!(targetMemberType is PrimitiveMemberType &&
canPrimitiveTypeBeReadAsNumber)) {
memberDiagnosticReporter.ReportDiagnostic(
betterMemberSymbol.ReportDiagnostic(
Rules.UnexpectedAttribute);
}
}
}

{
var integerFormatAttribute =
memberSymbol
.GetAttribute<IntegerFormatAttribute>(memberDiagnosticReporter);
betterMemberSymbol.GetAttribute<IntegerFormatAttribute>();
if (integerFormatAttribute != null) {
formatIntegerType = integerFormatAttribute.IntegerType;

Expand All @@ -448,8 +435,7 @@ public IBinarySchemaStructure ParseStructure(
targetPrimitiveType);
if (!(targetMemberType is PrimitiveMemberType &&
canPrimitiveTypeBeReadAsInteger)) {
memberDiagnosticReporter.ReportDiagnostic(
Rules.UnexpectedAttribute);
betterMemberSymbol.ReportDiagnostic(Rules.UnexpectedAttribute);
}
}
}
Expand All @@ -464,26 +450,24 @@ public IBinarySchemaStructure ParseStructure(
primitiveMemberType.UseAltFormat = true;
primitiveMemberType.AltFormat = formatNumberType;
} else if (targetPrimitiveType == SchemaPrimitiveType.ENUM) {
memberDiagnosticReporter.ReportDiagnostic(
Rules.EnumNeedsIntegerFormat);
betterMemberSymbol.ReportDiagnostic(Rules.EnumNeedsIntegerFormat);
} else if (targetPrimitiveType == SchemaPrimitiveType.BOOLEAN) {
memberDiagnosticReporter.ReportDiagnostic(
Rules.BooleanNeedsIntegerFormat);
betterMemberSymbol.ReportDiagnostic(Rules.BooleanNeedsIntegerFormat);
}
}

// Checks if the member is a child of the current type
{
if (targetMemberType is StructureMemberType structureMemberType) {
var expectedParentTypeSymbol =
new ChildOfParser(memberDiagnosticReporter)
new ChildOfParser(betterMemberSymbol)
.GetParentTypeSymbolOf(
structureMemberType.StructureTypeInfo.NamedTypeSymbol);
if (expectedParentTypeSymbol != null) {
if (expectedParentTypeSymbol.Equals(structureSymbol)) {
structureMemberType.IsChild = true;
} else {
memberDiagnosticReporter.ReportDiagnostic(
betterMemberSymbol.ReportDiagnostic(
Rules.ChildTypeCanOnlyBeContainedInParent);
}
}
Expand All @@ -494,27 +478,23 @@ public IBinarySchemaStructure ParseStructure(
{
// TODO: Implement this, support strings in arrays?
var stringLengthSourceAttribute =
(IStringLengthSourceAttribute?) memberSymbol
.GetAttribute<StringLengthSourceAttribute>(
memberDiagnosticReporter) ?? memberSymbol
.GetAttribute<RStringLengthSourceAttribute>(
memberDiagnosticReporter);
(IStringLengthSourceAttribute?)
betterMemberSymbol.GetAttribute<StringLengthSourceAttribute>() ??
betterMemberSymbol.GetAttribute<RStringLengthSourceAttribute>();
var isNullTerminatedString = memberSymbol
.HasAttribute<NullTerminatedStringAttribute>();
var hasStringLengthAttribute =
stringLengthSourceAttribute != null || isNullTerminatedString;


var encodingTypeAttribute =
memberSymbol.GetAttribute<StringEncodingAttribute>(
memberDiagnosticReporter);
betterMemberSymbol.GetAttribute<StringEncodingAttribute>();


if (hasStringLengthAttribute || encodingTypeAttribute != null) {
if (memberType is StringType stringType) {
if (memberTypeInfo.IsReadOnly && hasStringLengthAttribute) {
memberDiagnosticReporter.ReportDiagnostic(
Rules.UnexpectedAttribute);
betterMemberSymbol.ReportDiagnostic(Rules.UnexpectedAttribute);
}

stringType.EncodingType = encodingTypeAttribute?.EncodingType ??
Expand Down Expand Up @@ -543,13 +523,12 @@ public IBinarySchemaStructure ParseStructure(
break;
}
default: {
memberDiagnosticReporter.ReportDiagnostic(Rules.NotSupported);
betterMemberSymbol.ReportDiagnostic(Rules.NotSupported);
break;
}
}
} else {
memberDiagnosticReporter.ReportDiagnostic(
Rules.UnexpectedAttribute);
betterMemberSymbol.ReportDiagnostic(Rules.UnexpectedAttribute);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions Schema/src/binary/attributes/BMemberAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using schema.binary.parser;
using schema.util;
using schema.util.diagnostics;
using schema.util.symbols;

namespace schema.binary.attributes {
public abstract class BMemberAttribute<T> : BMemberAttribute {
Expand Down
9 changes: 3 additions & 6 deletions Schema/src/binary/attributes/IAttributeParser.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using Microsoft.CodeAnalysis;

using schema.binary.parser;
using schema.util.diagnostics;
using schema.binary.parser;
using schema.util.symbols;

namespace schema.binary.attributes {
internal interface IAttributeParser {
void ParseIntoMemberType(IDiagnosticReporter diagnosticReporter,
ISymbol memberSymbol,
void ParseIntoMemberType(IBetterSymbol memberSymbol,
ITypeInfo memberTypeInfo,
IMemberType memberType);
}
Expand Down
Loading

0 comments on commit 7a90a5b

Please sign in to comment.