From e6b393e4208166dc04c1ccbcb549f29c7dbc8b6f Mon Sep 17 00:00:00 2001 From: Evan Husted Date: Sat, 25 Jan 2025 14:11:46 -0600 Subject: [PATCH] misc: chore: Use explicit types in Generator projects --- .../IpcServiceGenerator.cs | 20 ++++++------- .../Hipc/HipcGenerator.cs | 28 +++++++++---------- .../Hipc/HipcSyntaxReceiver.cs | 6 ++-- .../SyscallGenerator.cs | 8 +++--- .../SyscallSyntaxReceiver.cs | 2 +- .../LocaleGenerator.cs | 7 +++-- src/Spv.Generator/Instruction.cs | 4 +-- src/Spv.Generator/InstructionOperands.cs | 6 ++-- src/Spv.Generator/Module.cs | 8 +++--- 9 files changed, 45 insertions(+), 44 deletions(-) diff --git a/src/Ryujinx.HLE.Generators/IpcServiceGenerator.cs b/src/Ryujinx.HLE.Generators/IpcServiceGenerator.cs index 5cac4d13a..4de100cf8 100644 --- a/src/Ryujinx.HLE.Generators/IpcServiceGenerator.cs +++ b/src/Ryujinx.HLE.Generators/IpcServiceGenerator.cs @@ -10,8 +10,8 @@ namespace Ryujinx.HLE.Generators { public void Execute(GeneratorExecutionContext context) { - var syntaxReceiver = (ServiceSyntaxReceiver)context.SyntaxReceiver; - CodeGenerator generator = new CodeGenerator(); + ServiceSyntaxReceiver syntaxReceiver = (ServiceSyntaxReceiver)context.SyntaxReceiver; + CodeGenerator generator = new(); generator.AppendLine("#nullable enable"); generator.AppendLine("using System;"); @@ -19,14 +19,14 @@ namespace Ryujinx.HLE.Generators generator.EnterScope($"partial class IUserInterface"); generator.EnterScope($"public IpcService? GetServiceInstance(Type type, ServiceCtx context, object? parameter = null)"); - foreach (var className in syntaxReceiver.Types) + foreach (ClassDeclarationSyntax className in syntaxReceiver.Types) { if (className.Modifiers.Any(SyntaxKind.AbstractKeyword) || className.Modifiers.Any(SyntaxKind.PrivateKeyword) || !className.AttributeLists.Any(x => x.Attributes.Any(y => y.ToString().StartsWith("Service")))) continue; - var name = GetFullName(className, context).Replace("global::", string.Empty); + string name = GetFullName(className, context).Replace("global::", string.Empty); if (!name.StartsWith("Ryujinx.HLE.HOS.Services")) continue; - var constructors = className.ChildNodes().Where(x => x.IsKind(SyntaxKind.ConstructorDeclaration)).Select(y => y as ConstructorDeclarationSyntax).ToArray(); + ConstructorDeclarationSyntax[] constructors = className.ChildNodes().Where(x => x.IsKind(SyntaxKind.ConstructorDeclaration)).Select(y => y as ConstructorDeclarationSyntax).ToArray(); if (!constructors.Any(x => x.ParameterList.Parameters.Count >= 1)) continue; @@ -36,10 +36,10 @@ namespace Ryujinx.HLE.Generators generator.EnterScope($"if (type == typeof({GetFullName(className, context)}))"); if (constructors.Any(x => x.ParameterList.Parameters.Count == 2)) { - var type = constructors.Where(x => x.ParameterList.Parameters.Count == 2).FirstOrDefault().ParameterList.Parameters[1].Type; - var model = context.Compilation.GetSemanticModel(type.SyntaxTree); - var typeSymbol = model.GetSymbolInfo(type).Symbol as INamedTypeSymbol; - var fullName = typeSymbol.ToString(); + TypeSyntax type = constructors.Where(x => x.ParameterList.Parameters.Count == 2).FirstOrDefault().ParameterList.Parameters[1].Type; + SemanticModel model = context.Compilation.GetSemanticModel(type.SyntaxTree); + INamedTypeSymbol typeSymbol = model.GetSymbolInfo(type).Symbol as INamedTypeSymbol; + string fullName = typeSymbol.ToString(); generator.EnterScope("if (parameter != null)"); generator.AppendLine($"return new {GetFullName(className, context)}(context, ({fullName})parameter);"); generator.LeaveScope(); @@ -65,7 +65,7 @@ namespace Ryujinx.HLE.Generators private string GetFullName(ClassDeclarationSyntax syntaxNode, GeneratorExecutionContext context) { - var typeSymbol = context.Compilation.GetSemanticModel(syntaxNode.SyntaxTree).GetDeclaredSymbol(syntaxNode); + INamedTypeSymbol typeSymbol = context.Compilation.GetSemanticModel(syntaxNode.SyntaxTree).GetDeclaredSymbol(syntaxNode); return typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); } diff --git a/src/Ryujinx.Horizon.Generators/Hipc/HipcGenerator.cs b/src/Ryujinx.Horizon.Generators/Hipc/HipcGenerator.cs index d1be8298d..e66a3efa9 100644 --- a/src/Ryujinx.Horizon.Generators/Hipc/HipcGenerator.cs +++ b/src/Ryujinx.Horizon.Generators/Hipc/HipcGenerator.cs @@ -61,7 +61,7 @@ namespace Ryujinx.Horizon.Generators.Hipc { HipcSyntaxReceiver syntaxReceiver = (HipcSyntaxReceiver)context.SyntaxReceiver; - foreach (var commandInterface in syntaxReceiver.CommandInterfaces) + foreach (CommandInterface commandInterface in syntaxReceiver.CommandInterfaces) { if (!NeedsIServiceObjectImplementation(context.Compilation, commandInterface.ClassDeclarationSyntax)) { @@ -86,7 +86,7 @@ namespace Ryujinx.Horizon.Generators.Hipc GenerateMethodTable(generator, context.Compilation, commandInterface); - foreach (var method in commandInterface.CommandImplementations) + foreach (MethodDeclarationSyntax method in commandInterface.CommandImplementations) { generator.AppendLine(); @@ -127,9 +127,9 @@ namespace Ryujinx.Horizon.Generators.Hipc { generator.EnterScope($"return FrozenDictionary.ToFrozenDictionary(new []"); - foreach (var method in commandInterface.CommandImplementations) + foreach (MethodDeclarationSyntax method in commandInterface.CommandImplementations) { - foreach (var commandId in GetAttributeArguments(compilation, method, TypeCommandAttribute, 0)) + foreach (string commandId in GetAttributeArguments(compilation, method, TypeCommandAttribute, 0)) { string[] args = new string[method.ParameterList.Parameters.Count]; @@ -141,7 +141,7 @@ namespace Ryujinx.Horizon.Generators.Hipc { int index = 0; - foreach (var parameter in method.ParameterList.Parameters) + foreach (ParameterSyntax parameter in method.ParameterList.Parameters) { string canonicalTypeName = GetCanonicalTypeNameWithGenericArguments(compilation, parameter.Type); CommandArgType argType = GetCommandArgType(compilation, parameter); @@ -191,7 +191,7 @@ namespace Ryujinx.Horizon.Generators.Hipc { ISymbol symbol = compilation.GetSemanticModel(syntaxNode.SyntaxTree).GetDeclaredSymbol(syntaxNode); - foreach (var attribute in symbol.GetAttributes()) + foreach (AttributeData attribute in symbol.GetAttributes()) { if (attribute.AttributeClass.ToDisplayString() == attributeName && (uint)argIndex < (uint)attribute.ConstructorArguments.Length) { @@ -211,7 +211,7 @@ namespace Ryujinx.Horizon.Generators.Hipc int outObjectsCount = 0; int buffersCount = 0; - foreach (var parameter in method.ParameterList.Parameters) + foreach (ParameterSyntax parameter in method.ParameterList.Parameters) { if (IsObject(compilation, parameter)) { @@ -285,7 +285,7 @@ namespace Ryujinx.Horizon.Generators.Hipc int inMoveHandleIndex = 0; int inObjectIndex = 0; - foreach (var parameter in method.ParameterList.Parameters) + foreach (ParameterSyntax parameter in method.ParameterList.Parameters) { string name = parameter.Identifier.Text; string argName = GetPrefixedArgName(name); @@ -555,11 +555,11 @@ namespace Ryujinx.Horizon.Generators.Hipc { ISymbol symbol = compilation.GetSemanticModel(syntaxNode.SyntaxTree).GetTypeInfo(syntaxNode).Type; - foreach (var attribute in symbol.GetAttributes()) + foreach (AttributeData attribute in symbol.GetAttributes()) { if (attribute.AttributeClass.ToDisplayString() == attributeName) { - foreach (var kv in attribute.NamedArguments) + foreach (KeyValuePair kv in attribute.NamedArguments) { if (kv.Key == argName) { @@ -764,9 +764,9 @@ namespace Ryujinx.Horizon.Generators.Hipc private static bool HasAttribute(Compilation compilation, ParameterSyntax parameterSyntax, string fullAttributeName) { - foreach (var attributeList in parameterSyntax.AttributeLists) + foreach (AttributeListSyntax attributeList in parameterSyntax.AttributeLists) { - foreach (var attribute in attributeList.Attributes) + foreach (AttributeSyntax attribute in attributeList.Attributes) { if (GetCanonicalTypeName(compilation, attribute) == fullAttributeName) { @@ -781,8 +781,8 @@ namespace Ryujinx.Horizon.Generators.Hipc private static bool NeedsIServiceObjectImplementation(Compilation compilation, ClassDeclarationSyntax classDeclarationSyntax) { ITypeSymbol type = compilation.GetSemanticModel(classDeclarationSyntax.SyntaxTree).GetDeclaredSymbol(classDeclarationSyntax); - var serviceObjectInterface = type.AllInterfaces.FirstOrDefault(x => x.ToDisplayString() == TypeIServiceObject); - var interfaceMember = serviceObjectInterface?.GetMembers().FirstOrDefault(x => x.Name == "GetCommandHandlers"); + INamedTypeSymbol serviceObjectInterface = type.AllInterfaces.FirstOrDefault(x => x.ToDisplayString() == TypeIServiceObject); + ISymbol interfaceMember = serviceObjectInterface?.GetMembers().FirstOrDefault(x => x.Name == "GetCommandHandlers"); // Return true only if the class implements IServiceObject but does not actually implement the method // that the interface defines, since this is the only case we want to handle, if the method already exists diff --git a/src/Ryujinx.Horizon.Generators/Hipc/HipcSyntaxReceiver.cs b/src/Ryujinx.Horizon.Generators/Hipc/HipcSyntaxReceiver.cs index 9b3421076..5eced63ec 100644 --- a/src/Ryujinx.Horizon.Generators/Hipc/HipcSyntaxReceiver.cs +++ b/src/Ryujinx.Horizon.Generators/Hipc/HipcSyntaxReceiver.cs @@ -24,9 +24,9 @@ namespace Ryujinx.Horizon.Generators.Hipc return; } - CommandInterface commandInterface = new CommandInterface(classDeclaration); + CommandInterface commandInterface = new(classDeclaration); - foreach (var memberDeclaration in classDeclaration.Members) + foreach (MemberDeclarationSyntax memberDeclaration in classDeclaration.Members) { if (memberDeclaration is MethodDeclarationSyntax methodDeclaration) { @@ -44,7 +44,7 @@ namespace Ryujinx.Horizon.Generators.Hipc if (methodDeclaration.AttributeLists.Count != 0) { - foreach (var attributeList in methodDeclaration.AttributeLists) + foreach (AttributeListSyntax attributeList in methodDeclaration.AttributeLists) { if (attributeList.Attributes.Any(x => x.Name.ToString().Contains(attributeName))) { diff --git a/src/Ryujinx.Horizon.Kernel.Generators/SyscallGenerator.cs b/src/Ryujinx.Horizon.Kernel.Generators/SyscallGenerator.cs index 06b98e09d..91eb08c77 100644 --- a/src/Ryujinx.Horizon.Kernel.Generators/SyscallGenerator.cs +++ b/src/Ryujinx.Horizon.Kernel.Generators/SyscallGenerator.cs @@ -147,7 +147,7 @@ namespace Ryujinx.Horizon.Kernel.Generators List syscalls = new List(); - foreach (var method in syntaxReceiver.SvcImplementations) + foreach (MethodDeclarationSyntax method in syntaxReceiver.SvcImplementations) { GenerateMethod32(generator, context.Compilation, method); GenerateMethod64(generator, context.Compilation, method); @@ -206,7 +206,7 @@ namespace Ryujinx.Horizon.Kernel.Generators List logInArgs = new List(); List logOutArgs = new List(); - foreach (var methodParameter in method.ParameterList.Parameters) + foreach (ParameterSyntax methodParameter in method.ParameterList.Parameters) { string name = methodParameter.Identifier.Text; string argName = GetPrefixedArgName(name); @@ -325,7 +325,7 @@ namespace Ryujinx.Horizon.Kernel.Generators List logInArgs = new List(); List logOutArgs = new List(); - foreach (var methodParameter in method.ParameterList.Parameters) + foreach (ParameterSyntax methodParameter in method.ParameterList.Parameters) { string name = methodParameter.Identifier.Text; string argName = GetPrefixedArgName(name); @@ -468,7 +468,7 @@ namespace Ryujinx.Horizon.Kernel.Generators generator.EnterScope($"public static void Dispatch{suffix}(Syscall syscall, {TypeExecutionContext} context, int id)"); generator.EnterScope("switch (id)"); - foreach (var syscall in syscalls) + foreach (SyscallIdAndName syscall in syscalls) { generator.AppendLine($"case {syscall.Id}:"); generator.IncreaseIndentation(); diff --git a/src/Ryujinx.Horizon.Kernel.Generators/SyscallSyntaxReceiver.cs b/src/Ryujinx.Horizon.Kernel.Generators/SyscallSyntaxReceiver.cs index 1542fed6a..76200713d 100644 --- a/src/Ryujinx.Horizon.Kernel.Generators/SyscallSyntaxReceiver.cs +++ b/src/Ryujinx.Horizon.Kernel.Generators/SyscallSyntaxReceiver.cs @@ -27,7 +27,7 @@ namespace Ryujinx.Horizon.Kernel.Generators return; } - foreach (var memberDeclaration in classDeclaration.Members) + foreach (MemberDeclarationSyntax memberDeclaration in classDeclaration.Members) { if (memberDeclaration is MethodDeclarationSyntax methodDeclaration) { diff --git a/src/Ryujinx.UI.LocaleGenerator/LocaleGenerator.cs b/src/Ryujinx.UI.LocaleGenerator/LocaleGenerator.cs index 2ba0b60a3..a3d16c660 100644 --- a/src/Ryujinx.UI.LocaleGenerator/LocaleGenerator.cs +++ b/src/Ryujinx.UI.LocaleGenerator/LocaleGenerator.cs @@ -1,4 +1,5 @@ using Microsoft.CodeAnalysis; +using System.Collections.Generic; using System.Linq; using System.Text; @@ -9,19 +10,19 @@ namespace Ryujinx.UI.LocaleGenerator { public void Initialize(IncrementalGeneratorInitializationContext context) { - var localeFile = context.AdditionalTextsProvider.Where(static x => x.Path.EndsWith("locales.json")); + IncrementalValuesProvider localeFile = context.AdditionalTextsProvider.Where(static x => x.Path.EndsWith("locales.json")); IncrementalValuesProvider contents = localeFile.Select((text, cancellationToken) => text.GetText(cancellationToken)!.ToString()); context.RegisterSourceOutput(contents, (spc, content) => { - var lines = content.Split('\n').Where(x => x.Trim().StartsWith("\"ID\":")).Select(x => x.Split(':')[1].Trim().Replace("\"", string.Empty).Replace(",", string.Empty)); + IEnumerable lines = content.Split('\n').Where(x => x.Trim().StartsWith("\"ID\":")).Select(x => x.Split(':')[1].Trim().Replace("\"", string.Empty).Replace(",", string.Empty)); StringBuilder enumSourceBuilder = new(); enumSourceBuilder.AppendLine("namespace Ryujinx.Ava.Common.Locale;"); enumSourceBuilder.AppendLine("public enum LocaleKeys"); enumSourceBuilder.AppendLine("{"); - foreach (var line in lines) + foreach (string? line in lines) { enumSourceBuilder.AppendLine($" {line},"); } diff --git a/src/Spv.Generator/Instruction.cs b/src/Spv.Generator/Instruction.cs index 741b30d6d..a19677a93 100644 --- a/src/Spv.Generator/Instruction.cs +++ b/src/Spv.Generator/Instruction.cs @@ -239,8 +239,8 @@ namespace Spv.Generator public override string ToString() { - var labels = _operandLabels.TryGetValue(Opcode, out var opLabels) ? opLabels : Array.Empty(); - var result = _resultType == null ? string.Empty : $"{_resultType} "; + string[] labels = _operandLabels.TryGetValue(Opcode, out string[] opLabels) ? opLabels : Array.Empty(); + string result = _resultType == null ? string.Empty : $"{_resultType} "; return $"{result}{Opcode}{_operands.ToString(labels)}"; } } diff --git a/src/Spv.Generator/InstructionOperands.cs b/src/Spv.Generator/InstructionOperands.cs index de74f1127..06feb300f 100644 --- a/src/Spv.Generator/InstructionOperands.cs +++ b/src/Spv.Generator/InstructionOperands.cs @@ -63,9 +63,9 @@ namespace Spv.Generator public readonly string ToString(string[] labels) { - var labeledParams = AllOperands.Zip(labels, (op, label) => $"{label}: {op}"); - var unlabeledParams = AllOperands.Skip(labels.Length).Select(op => op.ToString()); - var paramsToPrint = labeledParams.Concat(unlabeledParams); + IEnumerable labeledParams = AllOperands.Zip(labels, (op, label) => $"{label}: {op}"); + IEnumerable unlabeledParams = AllOperands.Skip(labels.Length).Select(op => op.ToString()); + IEnumerable paramsToPrint = labeledParams.Concat(unlabeledParams); return $"({string.Join(", ", paramsToPrint)})"; } } diff --git a/src/Spv.Generator/Module.cs b/src/Spv.Generator/Module.cs index fdcd62752..5945d9b6d 100644 --- a/src/Spv.Generator/Module.cs +++ b/src/Spv.Generator/Module.cs @@ -85,7 +85,7 @@ namespace Spv.Generator public Instruction NewInstruction(Op opcode, uint id = Instruction.InvalidId, Instruction resultType = null) { - var result = _instPool.Allocate(); + Instruction result = _instPool.Allocate(); result.Set(opcode, id, resultType); return result; @@ -93,7 +93,7 @@ namespace Spv.Generator public Instruction AddExtInstImport(string import) { - var key = new DeterministicStringKey(import); + DeterministicStringKey key = new DeterministicStringKey(import); if (_extInstImports.TryGetValue(key, out Instruction extInstImport)) { @@ -113,7 +113,7 @@ namespace Spv.Generator private void AddTypeDeclaration(Instruction instruction, bool forceIdAllocation) { - var key = new TypeDeclarationKey(instruction); + TypeDeclarationKey key = new TypeDeclarationKey(instruction); if (!forceIdAllocation) { @@ -214,7 +214,7 @@ namespace Spv.Generator constant.Opcode == Op.OpConstantNull || constant.Opcode == Op.OpConstantComposite); - var key = new ConstantKey(constant); + ConstantKey key = new ConstantKey(constant); if (_constants.TryGetValue(key, out Instruction global)) {