Skip to content

Commit

Permalink
Remove pretty much all type to string conversions from log generator
Browse files Browse the repository at this point in the history
  • Loading branch information
ThadHouse committed Feb 23, 2024
1 parent a433259 commit 29ac533
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 55 deletions.
49 changes: 24 additions & 25 deletions codehelp/CodeHelpers/LogGenerator/LoggableMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,24 +221,29 @@ public static DeclarationKind GetInnerType(this ITypeSymbol typeSymbol, out ITyp
return DeclarationKind.NullableValueType;
}

switch (typeSymbol.GetFullyQualifiedTypeNameWithoutGenerics())
if (typeSymbol.IsReadOnlySpan())
{
case "global::System.ReadOnlySpan":
namedTypeSymbol = (INamedTypeSymbol)typeSymbol;
innerType = namedTypeSymbol.TypeArguments[0];
return DeclarationKind.ReadOnlySpan;
case "global::System.Span":
namedTypeSymbol = (INamedTypeSymbol)typeSymbol;
innerType = namedTypeSymbol.TypeArguments[0];
return DeclarationKind.Span;
case "global::System.ReadOnlyMemory":
namedTypeSymbol = (INamedTypeSymbol)typeSymbol;
innerType = namedTypeSymbol.TypeArguments[0];
return DeclarationKind.ReadOnlyMemory;
case "global::System.Memory":
namedTypeSymbol = (INamedTypeSymbol)typeSymbol;
innerType = namedTypeSymbol.TypeArguments[0];
return DeclarationKind.Memory;
namedTypeSymbol = (INamedTypeSymbol)typeSymbol;
innerType = namedTypeSymbol.TypeArguments[0];
return DeclarationKind.ReadOnlySpan;
}
else if (typeSymbol.IsSpan())
{
namedTypeSymbol = (INamedTypeSymbol)typeSymbol;
innerType = namedTypeSymbol.TypeArguments[0];
return DeclarationKind.Span;
}
else if (typeSymbol.IsReadOnlyMemory())
{
namedTypeSymbol = (INamedTypeSymbol)typeSymbol;
innerType = namedTypeSymbol.TypeArguments[0];
return DeclarationKind.ReadOnlyMemory;
}
else if (typeSymbol.IsMemory())
{
namedTypeSymbol = (INamedTypeSymbol)typeSymbol;
innerType = namedTypeSymbol.TypeArguments[0];
return DeclarationKind.Memory;
}

innerType = typeSymbol;
Expand Down Expand Up @@ -307,17 +312,11 @@ public static DeclarationKind GetInnerType(this ITypeSymbol typeSymbol, out ITyp
}

token.ThrowIfCancellationRequested();
var fullTypeName = typeSymbol.GetFullyQualifiedTypeName();
token.ThrowIfCancellationRequested();
var structName = $"{Strings.IStructSerializableName}<{fullTypeName}>";
var protobufName = $"{Strings.IProtobufSerializableString}<{fullTypeName}>";

foreach (var inf in typeSymbol.AllInterfaces)
{
token.ThrowIfCancellationRequested();
var interfaceName = inf.GetFullyQualifiedTypeName();
token.ThrowIfCancellationRequested();
if (interfaceName == structName)
if (inf.IsStructSerializable())
{
if (!attributeInfo.UseProtobuf)
{
Expand All @@ -332,7 +331,7 @@ public static DeclarationKind GetInnerType(this ITypeSymbol typeSymbol, out ITyp
return new(DeclarationType.Struct, SpecialType.None, nestedKind);
}
}
else if (interfaceName == protobufName)
else if (inf.IsProtobufSerializable())
{
if (attributeInfo.UseProtobuf)
{
Expand Down
6 changes: 4 additions & 2 deletions codehelp/CodeHelpers/LogGenerator/Strings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public static class Strings
public const string LoggerInterfaceFullyQualified = "global::Stereologue.ILogged";
public const string UpdateStereologueName = "UpdateStereologue";
public const string FullMethodDeclaration = $"public void {UpdateStereologueName}(string path, global::{LogNamespace}.{LoggerTypeName} logger)";
public const string IStructSerializableName = "global::WPIUtil.Serialization.Struct.IStructSerializable";
public const string IProtobufSerializableString = "global::WPIUtil.Serialization.Protobuf.IProtobufSerializable";
public const string IStructSerializableName = "IStructSerializable";

// WPIUtil.Serialization.Struct
public const string IProtobufSerializableString = "IProtobufSerializable";
}
46 changes: 46 additions & 0 deletions codehelp/CodeHelpers/LogGenerator/SymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,52 @@ namespace WPILib.CodeHelpers.LogGenerator;

public static class SymbolExtensions
{
public static bool IsReadOnlySpan(this ITypeSymbol symbol)
{
if (symbol.Name != "ReadOnlySpan")
{
return false;
}
return symbol.ContainingNamespace?.Name == "System";
}

public static bool IsSpan(this ITypeSymbol symbol)
{
if (symbol.Name != "Span")
{
return false;
}
return symbol.ContainingNamespace?.Name == "System";
}

public static bool IsReadOnlyMemory(this ITypeSymbol symbol)
{
if (symbol.Name != "ReadOnlyMemory")
{
return false;
}
return symbol.ContainingNamespace?.Name == "System";
}

public static bool IsMemory(this ITypeSymbol symbol)
{
if (symbol.Name != "Memory")
{
return false;
}
return symbol.ContainingNamespace?.Name == "System";
}

public static bool IsStructSerializable(this INamedTypeSymbol symbol)
{
return symbol.Name == Strings.IStructSerializableName;
}

public static bool IsProtobufSerializable(this INamedTypeSymbol symbol)
{
return symbol.Name == Strings.IProtobufSerializableString;
}

public static bool IsLogAttributeClass(this INamedTypeSymbol symbol)
{
if (symbol.Name != Strings.LogAttributeTypeName)
Expand Down
15 changes: 0 additions & 15 deletions codehelp/CodeHelpers/SymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,9 @@ public static bool RequiresUnsafe(this ITypeSymbol symbol)
return symbol.TypeKind == TypeKind.Pointer || symbol.TypeKind == TypeKind.FunctionPointer;
}

public static string GetFullyQualifiedTypeName(this ITypeSymbol symbol)
{
// TODO stop using fully qualified
return symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
}

public static string? GetNamespace(this ITypeSymbol symbol)
{
// TODO Stop using ToDisplayString
return symbol.ContainingNamespace is { IsGlobalNamespace: false } ns ? ns.ToDisplayString() : null;
}

public static string GetFullyQualifiedTypeNameWithoutGenerics(this ITypeSymbol symbol)
{
// TODO stop using fully qualified
var fmt = new SymbolDisplayFormat(
typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces,
globalNamespaceStyle: SymbolDisplayGlobalNamespaceStyle.Included);
return symbol.ToDisplayString(fmt);
}
}
13 changes: 0 additions & 13 deletions codehelp/CodeHelpers/TypeModel.cs

This file was deleted.

6 changes: 6 additions & 0 deletions codehelp/CodeHelpers/TypeParameterModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ public void WriteConstraint(IndentedStringBuilder builder)

public static class TypeParameterExtensions
{
private static string GetFullyQualifiedTypeName(this ITypeSymbol symbol)
{
// TODO stop using fully qualified
return symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
}

public static TypeParameterModel GetTypeParameterModel(this ITypeParameterSymbol symbol, bool grabConstraints)
{
TypeParameterConstraintFlags flags = TypeParameterConstraintFlags.None;
Expand Down

0 comments on commit 29ac533

Please sign in to comment.