Skip to content

Commit

Permalink
Cleaned up a bunch of logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
MeltyPlayer committed Aug 6, 2023
1 parent 1591288 commit 3956cf7
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 62 deletions.
3 changes: 2 additions & 1 deletion Schema/src/binary/BinarySchemaAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.CodeAnalysis.Diagnostics;

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


namespace schema.binary {
Expand Down Expand Up @@ -88,7 +89,7 @@ public void CheckType(
return;
}

if (!SymbolTypeUtil.IsPartial(syntax)) {
if (!syntax.IsPartial()) {
Rules.ReportDiagnostic(
context,
symbol,
Expand Down
3 changes: 2 additions & 1 deletion Schema/src/binary/BinarySchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using schema.binary.text;
using schema.util;
using schema.util.symbols;
using schema.util.syntax;

namespace schema.binary {
[Generator(LanguageNames.CSharp)]
Expand Down Expand Up @@ -89,7 +90,7 @@ public void CheckType(
return;
}

if (!SymbolTypeUtil.IsPartial(syntax)) {
if (!syntax.IsPartial()) {
return;
}

Expand Down
8 changes: 3 additions & 5 deletions Schema/src/binary/parser/asserts/PartialContainerAsserter.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System.Collections.Generic;

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;

using schema.util.diagnostics;
using schema.util.symbols;
using schema.util.syntax;


namespace schema.binary.parser.asserts {
Expand All @@ -25,7 +23,7 @@ public void AssertContainersArePartial(INamedTypeSymbol containerSymbol) {
containingType.DeclaringSyntaxReferences[0].GetSyntax() as
TypeDeclarationSyntax;

if (!SymbolTypeUtil.IsPartial(typeDeclarationSyntax!)) {
if (!typeDeclarationSyntax!.IsPartial()) {
this.diagnosticReporter_.ReportDiagnostic(
containingType,
Rules.ContainerTypeMustBePartial);
Expand Down
56 changes: 1 addition & 55 deletions Schema/src/util/symbols/SymbolTypeUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
using System.Text;

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

using schema.binary;
using schema.binary.attributes;
Expand All @@ -19,38 +17,6 @@

namespace schema.util.symbols {
internal static class SymbolTypeUtil {
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ISymbol GetSymbolFromType(SemanticModel semanticModel,
Type type)
=> SymbolTypeUtil.GetSymbolFromIdentifier(semanticModel, type.FullName);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ISymbol GetSymbolFromIdentifier(
SemanticModel semanticModel,
string identifier) {
var symbol = semanticModel.LookupSymbols(0, null, identifier);
return symbol.First();
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool CanBeStoredAs<TType>(this ITypeSymbol symbol)
=> symbol.CanBeStoredAs(typeof(TType));

public static bool CanBeStoredAs(this ITypeSymbol symbol, Type type) {
if (symbol.IsExactlyType(type) || symbol.Implements(type) ||
symbol.ImplementsGeneric(type)) {
return true;
}

if (symbol is INamedTypeSymbol namedSymbol &&
namedSymbol.MatchesGeneric(type)) {
return true;
}

return false;
}


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool ImplementsGeneric(this ITypeSymbol symbol, Type type)
=> symbol.ImplementsGeneric(type, out _);
Expand Down Expand Up @@ -87,11 +53,6 @@ public static bool Implements(this ITypeSymbol symbol, Type type)
=> symbol.AllInterfaces.Any(i => i.IsExactlyType(type));


// Namespace
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static bool IsInSameNamespaceAs(this ISymbol symbol, ISymbol other)
=> symbol.ContainingNamespace.Equals(other.ContainingNamespace);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static bool IsInSameNamespaceAs(this ISymbol symbol, Type other)
=> symbol.IsInNamespace(other.Namespace);
Expand Down Expand Up @@ -163,10 +124,7 @@ private static IEnumerable<string> GetContainingNamespacesReversed_(
namespaceSymbol = namespaceSymbol.ContainingNamespace;
}
}

public static bool IsPartial(this TypeDeclarationSyntax syntax)
=> syntax.Modifiers.Any(m => m.IsKind(SyntaxKind.PartialKeyword));


public static bool MatchesGeneric(this INamedTypeSymbol symbol,
Type expectedGenericType) {
var indexOfBacktick = expectedGenericType.Name.IndexOf('`');
Expand All @@ -185,10 +143,6 @@ public static bool MatchesGeneric(this INamedTypeSymbol symbol,
}


[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsSameAs(this ISymbol symbol, ISymbol other)
=> SymbolEqualityComparer.Default.Equals(symbol, other);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsExactlyType(this ISymbol symbol, Type expectedType) {
var expectedName = expectedType.Name;
Expand Down Expand Up @@ -236,14 +190,6 @@ internal static bool HasAttribute<TAttribute>(this ISymbol symbol)
where TAttribute : Attribute
=> symbol.GetAttributeData<TAttribute>().Any();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static TAttribute? GetAttribute<TAttribute>(
this ISymbol symbol,
IDiagnosticReporter? diagnosticReporter = null)
where TAttribute : Attribute
=> symbol.GetAttributes<TAttribute>(diagnosticReporter)
.SingleOrDefault();


[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static TAttribute? GetAttribute<TAttribute>(
Expand Down
14 changes: 14 additions & 0 deletions Schema/src/util/syntax/SyntaxExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Linq;

using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

using CSharpExtensions = Microsoft.CodeAnalysis.CSharpExtensions;

namespace schema.util.syntax {
internal static class SyntaxExtensions {
public static bool IsPartial(this TypeDeclarationSyntax syntax)
=> syntax.Modifiers.Any(
m => CSharpExtensions.IsKind(m, SyntaxKind.PartialKeyword));
}
}

0 comments on commit 3956cf7

Please sign in to comment.