misc: chore: Use explicit types in Generator projects

This commit is contained in:
Evan Husted 2025-01-25 14:11:46 -06:00
parent fe661dc750
commit e6b393e420
9 changed files with 45 additions and 44 deletions

View File

@ -10,8 +10,8 @@ namespace Ryujinx.HLE.Generators
{ {
public void Execute(GeneratorExecutionContext context) public void Execute(GeneratorExecutionContext context)
{ {
var syntaxReceiver = (ServiceSyntaxReceiver)context.SyntaxReceiver; ServiceSyntaxReceiver syntaxReceiver = (ServiceSyntaxReceiver)context.SyntaxReceiver;
CodeGenerator generator = new CodeGenerator(); CodeGenerator generator = new();
generator.AppendLine("#nullable enable"); generator.AppendLine("#nullable enable");
generator.AppendLine("using System;"); generator.AppendLine("using System;");
@ -19,14 +19,14 @@ namespace Ryujinx.HLE.Generators
generator.EnterScope($"partial class IUserInterface"); generator.EnterScope($"partial class IUserInterface");
generator.EnterScope($"public IpcService? GetServiceInstance(Type type, ServiceCtx context, object? parameter = null)"); 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")))) if (className.Modifiers.Any(SyntaxKind.AbstractKeyword) || className.Modifiers.Any(SyntaxKind.PrivateKeyword) || !className.AttributeLists.Any(x => x.Attributes.Any(y => y.ToString().StartsWith("Service"))))
continue; 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")) if (!name.StartsWith("Ryujinx.HLE.HOS.Services"))
continue; 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)) if (!constructors.Any(x => x.ParameterList.Parameters.Count >= 1))
continue; continue;
@ -36,10 +36,10 @@ namespace Ryujinx.HLE.Generators
generator.EnterScope($"if (type == typeof({GetFullName(className, context)}))"); generator.EnterScope($"if (type == typeof({GetFullName(className, context)}))");
if (constructors.Any(x => x.ParameterList.Parameters.Count == 2)) if (constructors.Any(x => x.ParameterList.Parameters.Count == 2))
{ {
var type = constructors.Where(x => x.ParameterList.Parameters.Count == 2).FirstOrDefault().ParameterList.Parameters[1].Type; TypeSyntax type = constructors.Where(x => x.ParameterList.Parameters.Count == 2).FirstOrDefault().ParameterList.Parameters[1].Type;
var model = context.Compilation.GetSemanticModel(type.SyntaxTree); SemanticModel model = context.Compilation.GetSemanticModel(type.SyntaxTree);
var typeSymbol = model.GetSymbolInfo(type).Symbol as INamedTypeSymbol; INamedTypeSymbol typeSymbol = model.GetSymbolInfo(type).Symbol as INamedTypeSymbol;
var fullName = typeSymbol.ToString(); string fullName = typeSymbol.ToString();
generator.EnterScope("if (parameter != null)"); generator.EnterScope("if (parameter != null)");
generator.AppendLine($"return new {GetFullName(className, context)}(context, ({fullName})parameter);"); generator.AppendLine($"return new {GetFullName(className, context)}(context, ({fullName})parameter);");
generator.LeaveScope(); generator.LeaveScope();
@ -65,7 +65,7 @@ namespace Ryujinx.HLE.Generators
private string GetFullName(ClassDeclarationSyntax syntaxNode, GeneratorExecutionContext context) 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); return typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
} }

View File

@ -61,7 +61,7 @@ namespace Ryujinx.Horizon.Generators.Hipc
{ {
HipcSyntaxReceiver syntaxReceiver = (HipcSyntaxReceiver)context.SyntaxReceiver; HipcSyntaxReceiver syntaxReceiver = (HipcSyntaxReceiver)context.SyntaxReceiver;
foreach (var commandInterface in syntaxReceiver.CommandInterfaces) foreach (CommandInterface commandInterface in syntaxReceiver.CommandInterfaces)
{ {
if (!NeedsIServiceObjectImplementation(context.Compilation, commandInterface.ClassDeclarationSyntax)) if (!NeedsIServiceObjectImplementation(context.Compilation, commandInterface.ClassDeclarationSyntax))
{ {
@ -86,7 +86,7 @@ namespace Ryujinx.Horizon.Generators.Hipc
GenerateMethodTable(generator, context.Compilation, commandInterface); GenerateMethodTable(generator, context.Compilation, commandInterface);
foreach (var method in commandInterface.CommandImplementations) foreach (MethodDeclarationSyntax method in commandInterface.CommandImplementations)
{ {
generator.AppendLine(); generator.AppendLine();
@ -127,9 +127,9 @@ namespace Ryujinx.Horizon.Generators.Hipc
{ {
generator.EnterScope($"return FrozenDictionary.ToFrozenDictionary(new []"); 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]; string[] args = new string[method.ParameterList.Parameters.Count];
@ -141,7 +141,7 @@ namespace Ryujinx.Horizon.Generators.Hipc
{ {
int index = 0; int index = 0;
foreach (var parameter in method.ParameterList.Parameters) foreach (ParameterSyntax parameter in method.ParameterList.Parameters)
{ {
string canonicalTypeName = GetCanonicalTypeNameWithGenericArguments(compilation, parameter.Type); string canonicalTypeName = GetCanonicalTypeNameWithGenericArguments(compilation, parameter.Type);
CommandArgType argType = GetCommandArgType(compilation, parameter); CommandArgType argType = GetCommandArgType(compilation, parameter);
@ -191,7 +191,7 @@ namespace Ryujinx.Horizon.Generators.Hipc
{ {
ISymbol symbol = compilation.GetSemanticModel(syntaxNode.SyntaxTree).GetDeclaredSymbol(syntaxNode); 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) if (attribute.AttributeClass.ToDisplayString() == attributeName && (uint)argIndex < (uint)attribute.ConstructorArguments.Length)
{ {
@ -211,7 +211,7 @@ namespace Ryujinx.Horizon.Generators.Hipc
int outObjectsCount = 0; int outObjectsCount = 0;
int buffersCount = 0; int buffersCount = 0;
foreach (var parameter in method.ParameterList.Parameters) foreach (ParameterSyntax parameter in method.ParameterList.Parameters)
{ {
if (IsObject(compilation, parameter)) if (IsObject(compilation, parameter))
{ {
@ -285,7 +285,7 @@ namespace Ryujinx.Horizon.Generators.Hipc
int inMoveHandleIndex = 0; int inMoveHandleIndex = 0;
int inObjectIndex = 0; int inObjectIndex = 0;
foreach (var parameter in method.ParameterList.Parameters) foreach (ParameterSyntax parameter in method.ParameterList.Parameters)
{ {
string name = parameter.Identifier.Text; string name = parameter.Identifier.Text;
string argName = GetPrefixedArgName(name); string argName = GetPrefixedArgName(name);
@ -555,11 +555,11 @@ namespace Ryujinx.Horizon.Generators.Hipc
{ {
ISymbol symbol = compilation.GetSemanticModel(syntaxNode.SyntaxTree).GetTypeInfo(syntaxNode).Type; 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) if (attribute.AttributeClass.ToDisplayString() == attributeName)
{ {
foreach (var kv in attribute.NamedArguments) foreach (KeyValuePair<string, TypedConstant> kv in attribute.NamedArguments)
{ {
if (kv.Key == argName) if (kv.Key == argName)
{ {
@ -764,9 +764,9 @@ namespace Ryujinx.Horizon.Generators.Hipc
private static bool HasAttribute(Compilation compilation, ParameterSyntax parameterSyntax, string fullAttributeName) 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) if (GetCanonicalTypeName(compilation, attribute) == fullAttributeName)
{ {
@ -781,8 +781,8 @@ namespace Ryujinx.Horizon.Generators.Hipc
private static bool NeedsIServiceObjectImplementation(Compilation compilation, ClassDeclarationSyntax classDeclarationSyntax) private static bool NeedsIServiceObjectImplementation(Compilation compilation, ClassDeclarationSyntax classDeclarationSyntax)
{ {
ITypeSymbol type = compilation.GetSemanticModel(classDeclarationSyntax.SyntaxTree).GetDeclaredSymbol(classDeclarationSyntax); ITypeSymbol type = compilation.GetSemanticModel(classDeclarationSyntax.SyntaxTree).GetDeclaredSymbol(classDeclarationSyntax);
var serviceObjectInterface = type.AllInterfaces.FirstOrDefault(x => x.ToDisplayString() == TypeIServiceObject); INamedTypeSymbol serviceObjectInterface = type.AllInterfaces.FirstOrDefault(x => x.ToDisplayString() == TypeIServiceObject);
var interfaceMember = serviceObjectInterface?.GetMembers().FirstOrDefault(x => x.Name == "GetCommandHandlers"); ISymbol interfaceMember = serviceObjectInterface?.GetMembers().FirstOrDefault(x => x.Name == "GetCommandHandlers");
// Return true only if the class implements IServiceObject but does not actually implement the method // 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 // that the interface defines, since this is the only case we want to handle, if the method already exists

View File

@ -24,9 +24,9 @@ namespace Ryujinx.Horizon.Generators.Hipc
return; 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) if (memberDeclaration is MethodDeclarationSyntax methodDeclaration)
{ {
@ -44,7 +44,7 @@ namespace Ryujinx.Horizon.Generators.Hipc
if (methodDeclaration.AttributeLists.Count != 0) 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))) if (attributeList.Attributes.Any(x => x.Name.ToString().Contains(attributeName)))
{ {

View File

@ -147,7 +147,7 @@ namespace Ryujinx.Horizon.Kernel.Generators
List<SyscallIdAndName> syscalls = new List<SyscallIdAndName>(); List<SyscallIdAndName> syscalls = new List<SyscallIdAndName>();
foreach (var method in syntaxReceiver.SvcImplementations) foreach (MethodDeclarationSyntax method in syntaxReceiver.SvcImplementations)
{ {
GenerateMethod32(generator, context.Compilation, method); GenerateMethod32(generator, context.Compilation, method);
GenerateMethod64(generator, context.Compilation, method); GenerateMethod64(generator, context.Compilation, method);
@ -206,7 +206,7 @@ namespace Ryujinx.Horizon.Kernel.Generators
List<string> logInArgs = new List<string>(); List<string> logInArgs = new List<string>();
List<string> logOutArgs = new List<string>(); List<string> logOutArgs = new List<string>();
foreach (var methodParameter in method.ParameterList.Parameters) foreach (ParameterSyntax methodParameter in method.ParameterList.Parameters)
{ {
string name = methodParameter.Identifier.Text; string name = methodParameter.Identifier.Text;
string argName = GetPrefixedArgName(name); string argName = GetPrefixedArgName(name);
@ -325,7 +325,7 @@ namespace Ryujinx.Horizon.Kernel.Generators
List<string> logInArgs = new List<string>(); List<string> logInArgs = new List<string>();
List<string> logOutArgs = new List<string>(); List<string> logOutArgs = new List<string>();
foreach (var methodParameter in method.ParameterList.Parameters) foreach (ParameterSyntax methodParameter in method.ParameterList.Parameters)
{ {
string name = methodParameter.Identifier.Text; string name = methodParameter.Identifier.Text;
string argName = GetPrefixedArgName(name); 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($"public static void Dispatch{suffix}(Syscall syscall, {TypeExecutionContext} context, int id)");
generator.EnterScope("switch (id)"); generator.EnterScope("switch (id)");
foreach (var syscall in syscalls) foreach (SyscallIdAndName syscall in syscalls)
{ {
generator.AppendLine($"case {syscall.Id}:"); generator.AppendLine($"case {syscall.Id}:");
generator.IncreaseIndentation(); generator.IncreaseIndentation();

View File

@ -27,7 +27,7 @@ namespace Ryujinx.Horizon.Kernel.Generators
return; return;
} }
foreach (var memberDeclaration in classDeclaration.Members) foreach (MemberDeclarationSyntax memberDeclaration in classDeclaration.Members)
{ {
if (memberDeclaration is MethodDeclarationSyntax methodDeclaration) if (memberDeclaration is MethodDeclarationSyntax methodDeclaration)
{ {

View File

@ -1,4 +1,5 @@
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -9,19 +10,19 @@ namespace Ryujinx.UI.LocaleGenerator
{ {
public void Initialize(IncrementalGeneratorInitializationContext context) public void Initialize(IncrementalGeneratorInitializationContext context)
{ {
var localeFile = context.AdditionalTextsProvider.Where(static x => x.Path.EndsWith("locales.json")); IncrementalValuesProvider<AdditionalText> localeFile = context.AdditionalTextsProvider.Where(static x => x.Path.EndsWith("locales.json"));
IncrementalValuesProvider<string> contents = localeFile.Select((text, cancellationToken) => text.GetText(cancellationToken)!.ToString()); IncrementalValuesProvider<string> contents = localeFile.Select((text, cancellationToken) => text.GetText(cancellationToken)!.ToString());
context.RegisterSourceOutput(contents, (spc, content) => 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<string> 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(); StringBuilder enumSourceBuilder = new();
enumSourceBuilder.AppendLine("namespace Ryujinx.Ava.Common.Locale;"); enumSourceBuilder.AppendLine("namespace Ryujinx.Ava.Common.Locale;");
enumSourceBuilder.AppendLine("public enum LocaleKeys"); enumSourceBuilder.AppendLine("public enum LocaleKeys");
enumSourceBuilder.AppendLine("{"); enumSourceBuilder.AppendLine("{");
foreach (var line in lines) foreach (string? line in lines)
{ {
enumSourceBuilder.AppendLine($" {line},"); enumSourceBuilder.AppendLine($" {line},");
} }

View File

@ -239,8 +239,8 @@ namespace Spv.Generator
public override string ToString() public override string ToString()
{ {
var labels = _operandLabels.TryGetValue(Opcode, out var opLabels) ? opLabels : Array.Empty<string>(); string[] labels = _operandLabels.TryGetValue(Opcode, out string[] opLabels) ? opLabels : Array.Empty<string>();
var result = _resultType == null ? string.Empty : $"{_resultType} "; string result = _resultType == null ? string.Empty : $"{_resultType} ";
return $"{result}{Opcode}{_operands.ToString(labels)}"; return $"{result}{Opcode}{_operands.ToString(labels)}";
} }
} }

View File

@ -63,9 +63,9 @@ namespace Spv.Generator
public readonly string ToString(string[] labels) public readonly string ToString(string[] labels)
{ {
var labeledParams = AllOperands.Zip(labels, (op, label) => $"{label}: {op}"); IEnumerable<string> labeledParams = AllOperands.Zip(labels, (op, label) => $"{label}: {op}");
var unlabeledParams = AllOperands.Skip(labels.Length).Select(op => op.ToString()); IEnumerable<string> unlabeledParams = AllOperands.Skip(labels.Length).Select(op => op.ToString());
var paramsToPrint = labeledParams.Concat(unlabeledParams); IEnumerable<string> paramsToPrint = labeledParams.Concat(unlabeledParams);
return $"({string.Join(", ", paramsToPrint)})"; return $"({string.Join(", ", paramsToPrint)})";
} }
} }

View File

@ -85,7 +85,7 @@ namespace Spv.Generator
public Instruction NewInstruction(Op opcode, uint id = Instruction.InvalidId, Instruction resultType = null) 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); result.Set(opcode, id, resultType);
return result; return result;
@ -93,7 +93,7 @@ namespace Spv.Generator
public Instruction AddExtInstImport(string import) public Instruction AddExtInstImport(string import)
{ {
var key = new DeterministicStringKey(import); DeterministicStringKey key = new DeterministicStringKey(import);
if (_extInstImports.TryGetValue(key, out Instruction extInstImport)) if (_extInstImports.TryGetValue(key, out Instruction extInstImport))
{ {
@ -113,7 +113,7 @@ namespace Spv.Generator
private void AddTypeDeclaration(Instruction instruction, bool forceIdAllocation) private void AddTypeDeclaration(Instruction instruction, bool forceIdAllocation)
{ {
var key = new TypeDeclarationKey(instruction); TypeDeclarationKey key = new TypeDeclarationKey(instruction);
if (!forceIdAllocation) if (!forceIdAllocation)
{ {
@ -214,7 +214,7 @@ namespace Spv.Generator
constant.Opcode == Op.OpConstantNull || constant.Opcode == Op.OpConstantNull ||
constant.Opcode == Op.OpConstantComposite); constant.Opcode == Op.OpConstantComposite);
var key = new ConstantKey(constant); ConstantKey key = new ConstantKey(constant);
if (_constants.TryGetValue(key, out Instruction global)) if (_constants.TryGetValue(key, out Instruction global))
{ {