Skip to content

Commit

Permalink
code cleanup and refactoring to simplify LiteralExpression / Attribut…
Browse files Browse the repository at this point in the history
…es handling easier
  • Loading branch information
adrianoc committed Mar 8, 2024
1 parent 0da71d3 commit 26bdd89
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 19 deletions.
15 changes: 3 additions & 12 deletions Cecilifier.Core/AST/EnumDeclarationVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,9 @@ public override int VisitEnumMemberDeclaration(EnumMemberDeclarationSyntax node)

public override int VisitLiteralExpression(LiteralExpressionSyntax node)
{
if (node.Kind() != SyntaxKind.NumericLiteralExpression)
{
throw new InvalidOperationException($"Invalid literal type: {node}");
}

var value = node.ToString();
if (value.StartsWith("0x"))
{
return Convert.ToInt32(value.Substring(2), 16);
}

return int.Parse(value);
return node.TryGetLiteralValueFor<int>(out var value)
? value
: throw new InvalidOperationException($"Invalid literal type: {node}");
}

public override int VisitBinaryExpression(BinaryExpressionSyntax node)
Expand Down
4 changes: 2 additions & 2 deletions Cecilifier.Core/AST/ExpressionVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using Cecilifier.Core.CodeGeneration;
using Cecilifier.Core.Extensions;
using Cecilifier.Core.Misc;
Expand Down Expand Up @@ -1388,8 +1389,7 @@ private void ProcessArgumentsTakingDefaultParametersIntoAccount(ISymbol? method,

private string ArgumentValueToUseForDefaultParameter(IParameterSymbol arg, ImmutableArray<IParameterSymbol> parameters, SeparatedSyntaxList<ArgumentSyntax> arguments)
{
var callerArgumentExpressionAttribute = arg.GetAttributes().SingleOrDefault(attr => attr.AttributeClass!.MetadataToken == Context.RoslynTypeSystem.CallerArgumentExpressionAttribute.MetadataToken);
if (callerArgumentExpressionAttribute != null)
if (arg.TryGetAttribute<CallerArgumentExpressionAttribute>(out var callerArgumentExpressionAttribute))
{
var expressionParameter = parameters.SingleOrDefault(p => p.Name == (string) callerArgumentExpressionAttribute.ConstructorArguments[0].Value);
if (expressionParameter != null)
Expand Down
7 changes: 3 additions & 4 deletions Cecilifier.Core/AST/SyntaxWalkerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,7 @@ private static void AppendStructLayoutTo(ITypeSymbol typeSymbol, StringBuilder t
if (typeSymbol.TypeKind != TypeKind.Struct)
return;

var structLayoutAttribute = typeSymbol.GetAttributes().FirstOrDefault(attrData => attrData.AttributeClass.FullyQualifiedName().Equals("System.Runtime.InteropServices.StructLayoutAttribute"));
if (structLayoutAttribute == null)
if (!typeSymbol.TryGetAttribute<StructLayoutAttribute>(out var structLayoutAttribute))
{
typeAttributes.AppendModifier("TypeAttributes.SequentialLayout");
}
Expand Down Expand Up @@ -858,8 +857,8 @@ private static IEnumerable<string> ProcessStructLayoutAttribute(AttributeSyntax
static int AssignedValue(AttributeSyntax attribute, string parameterName)
{
// whenever Size/Pack are omitted the corresponding property should be set to 0. See Ecma-335 II 22.8.
var parameterAssignmentExpression = attribute.ArgumentList?.Arguments.FirstOrDefault(a => a.NameEquals?.Name.Identifier.Text == parameterName)?.Expression;
return parameterAssignmentExpression != null ? Int32.Parse(((LiteralExpressionSyntax) parameterAssignmentExpression).Token.Text) : 0;
var parameterAssignmentExpression = (LiteralExpressionSyntax) attribute.ArgumentList?.Arguments.FirstOrDefault(a => a.NameEquals?.Name.Identifier.Text == parameterName)?.Expression;
return parameterAssignmentExpression?.TryGetLiteralValueFor<int>(out var ret) == true ? ret : 0;
}
}

Expand Down
7 changes: 7 additions & 0 deletions Cecilifier.Core/Extensions/ISymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,5 +240,12 @@ public static IMethodSymbol Ctor(this ITypeSymbol self, params ITypeSymbol[] par
SymbolKind.Parameter => VariableMemberKind.Parameter,
_ => throw new ArgumentException($"Invalid symbol type for '{self}' ({self.Kind})")
};

public static bool TryGetAttribute<T>(this ISymbol symbol, [NotNullWhen(true)] out AttributeData attributeData) where T : Attribute
{
var typeOfT = typeof(T);
attributeData = symbol.GetAttributes().SingleOrDefault(attr => attr.AttributeClass?.Name == typeOfT.Name);
return attributeData != null;
}
}
}
2 changes: 1 addition & 1 deletion Cecilifier.Core/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ internal static string[] CloneMethodReferenceOverriding(this string methodRef, I
return exps.ToArray();
}

public static int CountNewLines(this string value) => value.Count(ch => ch == '\n');
public static int CountNewLines(this string value) => value.AsSpan().Count('\n');

private static List<string> methodReferencePropertiesToClone = new()
{
Expand Down
10 changes: 10 additions & 0 deletions Cecilifier.Core/Extensions/SyntaxNodeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,15 @@ internal static bool IsArgumentPassedToReferenceTypeParameter(this SyntaxNode to
}

internal static bool IsMemberAccessOnElementAccess(this SyntaxNode self) => self.IsKind(SyntaxKind.ElementAccessExpression) && self.Parent is MemberAccessExpressionSyntax mae && mae.Expression == self;

internal static bool TryGetLiteralValueFor<T>(this ExpressionSyntax expressionSyntax, out T value)
{
value = default;
if (expressionSyntax is not LiteralExpressionSyntax literalExpression)
return false;

value = (T) literalExpression.Token.Value;
return true;
}
}
}

0 comments on commit 26bdd89

Please sign in to comment.