| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758 |
- using Microsoft.CodeAnalysis;
- using Microsoft.CodeAnalysis.CSharp;
- using Microsoft.CodeAnalysis.CSharp.Syntax;
- using System.Collections.Generic;
- using System.Linq;
- namespace Ryujinx.Horizon.Generators.Hipc
- {
- [Generator]
- class HipcGenerator : ISourceGenerator
- {
- private const string ArgVariablePrefix = "arg";
- private const string ResultVariableName = "result";
- private const string IsBufferMapAliasVariableName = "isBufferMapAlias";
- private const string InObjectsVariableName = "inObjects";
- private const string OutObjectsVariableName = "outObjects";
- private const string ResponseVariableName = "response";
- private const string OutRawDataVariableName = "outRawData";
- private const string TypeSystemReadOnlySpan = "System.ReadOnlySpan";
- private const string TypeSystemSpan = "System.Span";
- private const string TypeStructLayoutAttribute = "System.Runtime.InteropServices.StructLayoutAttribute";
- public const string CommandAttributeName = "CmifCommandAttribute";
- private const string TypeResult = "Ryujinx.Horizon.Common.Result";
- private const string TypeBufferAttribute = "Ryujinx.Horizon.Sdk.Sf.BufferAttribute";
- private const string TypeCopyHandleAttribute = "Ryujinx.Horizon.Sdk.Sf.CopyHandleAttribute";
- private const string TypeMoveHandleAttribute = "Ryujinx.Horizon.Sdk.Sf.MoveHandleAttribute";
- private const string TypeClientProcessIdAttribute = "Ryujinx.Horizon.Sdk.Sf.ClientProcessIdAttribute";
- private const string TypeCommandAttribute = "Ryujinx.Horizon.Sdk.Sf." + CommandAttributeName;
- private const string TypeIServiceObject = "Ryujinx.Horizon.Sdk.Sf.IServiceObject";
- private enum Modifier
- {
- None,
- Ref,
- Out,
- In
- }
- private struct OutParameter
- {
- public readonly string Name;
- public readonly string TypeName;
- public readonly int Index;
- public readonly CommandArgType Type;
- public OutParameter(string name, string typeName, int index, CommandArgType type)
- {
- Name = name;
- TypeName = typeName;
- Index = index;
- Type = type;
- }
- }
- public void Execute(GeneratorExecutionContext context)
- {
- HipcSyntaxReceiver syntaxReceiver = (HipcSyntaxReceiver)context.SyntaxReceiver;
- foreach (var commandInterface in syntaxReceiver.CommandInterfaces)
- {
- if (!NeedsIServiceObjectImplementation(context.Compilation, commandInterface.ClassDeclarationSyntax))
- {
- continue;
- }
- CodeGenerator generator = new CodeGenerator();
- string className = commandInterface.ClassDeclarationSyntax.Identifier.ToString();
- generator.AppendLine("using Ryujinx.Horizon.Common;");
- generator.AppendLine("using Ryujinx.Horizon.Sdk.Sf;");
- generator.AppendLine("using Ryujinx.Horizon.Sdk.Sf.Cmif;");
- generator.AppendLine("using Ryujinx.Horizon.Sdk.Sf.Hipc;");
- generator.AppendLine("using System;");
- generator.AppendLine("using System.Collections.Generic;");
- generator.AppendLine("using System.Runtime.CompilerServices;");
- generator.AppendLine("using System.Runtime.InteropServices;");
- generator.AppendLine();
- generator.EnterScope($"namespace {GetNamespaceName(commandInterface.ClassDeclarationSyntax)}");
- generator.EnterScope($"partial class {className}");
- GenerateMethodTable(generator, context.Compilation, commandInterface);
- foreach (var method in commandInterface.CommandImplementations)
- {
- generator.AppendLine();
- GenerateMethod(generator, context.Compilation, method);
- }
- generator.LeaveScope();
- generator.LeaveScope();
- context.AddSource($"{className}.g.cs", generator.ToString());
- }
- }
- private static string GetNamespaceName(SyntaxNode syntaxNode)
- {
- while (syntaxNode != null && !(syntaxNode is NamespaceDeclarationSyntax))
- {
- syntaxNode = syntaxNode.Parent;
- }
- if (syntaxNode == null)
- {
- return string.Empty;
- }
- return ((NamespaceDeclarationSyntax)syntaxNode).Name.ToString();
- }
- private static void GenerateMethodTable(CodeGenerator generator, Compilation compilation, CommandInterface commandInterface)
- {
- generator.EnterScope($"public IReadOnlyDictionary<int, CommandHandler> GetCommandHandlers()");
- generator.EnterScope($"return new Dictionary<int, CommandHandler>()");
- foreach (var method in commandInterface.CommandImplementations)
- {
- foreach (var commandId in GetAttributeAguments(compilation, method, TypeCommandAttribute, 0))
- {
- string[] args = new string[method.ParameterList.Parameters.Count];
- if (args.Length == 0)
- {
- generator.AppendLine($"{{ {commandId}, new CommandHandler({method.Identifier.Text}, Array.Empty<CommandArg>()) }},");
- }
- else
- {
- int index = 0;
- foreach (var parameter in method.ParameterList.Parameters)
- {
- string canonicalTypeName = GetCanonicalTypeNameWithGenericArguments(compilation, parameter.Type);
- CommandArgType argType = GetCommandArgType(compilation, parameter);
- string arg;
- if (argType == CommandArgType.Buffer)
- {
- string bufferFlags = GetFirstAttributeAgument(compilation, parameter, TypeBufferAttribute, 0);
- string bufferFixedSize = GetFirstAttributeAgument(compilation, parameter, TypeBufferAttribute, 1);
- if (bufferFixedSize != null)
- {
- arg = $"new CommandArg({bufferFlags}, {bufferFixedSize})";
- }
- else
- {
- arg = $"new CommandArg({bufferFlags})";
- }
- }
- else if (argType == CommandArgType.InArgument || argType == CommandArgType.OutArgument)
- {
- string alignment = GetTypeAlignmentExpression(compilation, parameter.Type);
- arg = $"new CommandArg(CommandArgType.{argType}, Unsafe.SizeOf<{canonicalTypeName}>(), {alignment})";
- }
- else
- {
- arg = $"new CommandArg(CommandArgType.{argType})";
- }
- args[index++] = arg;
- }
- generator.AppendLine($"{{ {commandId}, new CommandHandler({method.Identifier.Text}, {string.Join(", ", args)}) }},");
- }
- }
- }
- generator.LeaveScope(";");
- generator.LeaveScope();
- }
- private static IEnumerable<string> GetAttributeAguments(Compilation compilation, SyntaxNode syntaxNode, string attributeName, int argIndex)
- {
- ISymbol symbol = compilation.GetSemanticModel(syntaxNode.SyntaxTree).GetDeclaredSymbol(syntaxNode);
- foreach (var attribute in symbol.GetAttributes())
- {
- if (attribute.AttributeClass.ToDisplayString() == attributeName && (uint)argIndex < (uint)attribute.ConstructorArguments.Length)
- {
- yield return attribute.ConstructorArguments[argIndex].ToCSharpString();
- }
- }
- }
- private static string GetFirstAttributeAgument(Compilation compilation, SyntaxNode syntaxNode, string attributeName, int argIndex)
- {
- return GetAttributeAguments(compilation, syntaxNode, attributeName, argIndex).FirstOrDefault();
- }
- private static void GenerateMethod(CodeGenerator generator, Compilation compilation, MethodDeclarationSyntax method)
- {
- int inObjectsCount = 0;
- int outObjectsCount = 0;
- int buffersCount = 0;
- foreach (var parameter in method.ParameterList.Parameters)
- {
- if (IsObject(compilation, parameter))
- {
- if (IsIn(parameter))
- {
- inObjectsCount++;
- }
- else
- {
- outObjectsCount++;
- }
- }
- else if (IsBuffer(compilation, parameter))
- {
- buffersCount++;
- }
- }
- generator.EnterScope($"private Result {method.Identifier.Text}(" +
- "ref ServiceDispatchContext context, " +
- "HipcCommandProcessor processor, " +
- "ServerMessageRuntimeMetadata runtimeMetadata, " +
- "ReadOnlySpan<byte> inRawData, " +
- "ref Span<CmifOutHeader> outHeader)");
- bool returnsResult = method.ReturnType != null && GetCanonicalTypeName(compilation, method.ReturnType) == TypeResult;
- if (returnsResult || buffersCount != 0 || inObjectsCount != 0)
- {
- generator.AppendLine($"Result {ResultVariableName};");
- if (buffersCount != 0)
- {
- generator.AppendLine($"bool[] {IsBufferMapAliasVariableName} = new bool[{method.ParameterList.Parameters.Count}];");
- generator.AppendLine();
- generator.AppendLine($"{ResultVariableName} = processor.ProcessBuffers(ref context, {IsBufferMapAliasVariableName}, runtimeMetadata);");
- generator.EnterScope($"if ({ResultVariableName}.IsFailure)");
- generator.AppendLine($"return {ResultVariableName};");
- generator.LeaveScope();
- }
- generator.AppendLine();
- }
- List<OutParameter> outParameters = new List<OutParameter>();
- string[] args = new string[method.ParameterList.Parameters.Count];
- if (inObjectsCount != 0)
- {
- generator.AppendLine($"var {InObjectsVariableName} = new IServiceObject[{inObjectsCount}];");
- generator.AppendLine();
- generator.AppendLine($"{ResultVariableName} = processor.GetInObjects(context.Processor, {InObjectsVariableName});");
- generator.EnterScope($"if ({ResultVariableName}.IsFailure)");
- generator.AppendLine($"return {ResultVariableName};");
- generator.LeaveScope();
- generator.AppendLine();
- }
- if (outObjectsCount != 0)
- {
- generator.AppendLine($"var {OutObjectsVariableName} = new IServiceObject[{outObjectsCount}];");
- }
- int index = 0;
- int inArgIndex = 0;
- int outArgIndex = 0;
- int inCopyHandleIndex = 0;
- int inMoveHandleIndex = 0;
- int inObjectIndex = 0;
- foreach (var parameter in method.ParameterList.Parameters)
- {
- string name = parameter.Identifier.Text;
- string argName = GetPrefixedArgName(name);
- string canonicalTypeName = GetCanonicalTypeNameWithGenericArguments(compilation, parameter.Type);
- CommandArgType argType = GetCommandArgType(compilation, parameter);
- Modifier modifier = GetModifier(parameter);
- bool isNonSpanBuffer = false;
- if (modifier == Modifier.Out)
- {
- if (IsNonSpanOutBuffer(compilation, parameter))
- {
- generator.AppendLine($"using var {argName} = CommandSerialization.GetWritableRegion(processor.GetBufferRange({outArgIndex++}));");
- argName = $"out {GenerateSpanCastElement0(canonicalTypeName, $"{argName}.Memory.Span")}";
- }
- else
- {
- outParameters.Add(new OutParameter(argName, canonicalTypeName, index, argType));
- argName = $"out {canonicalTypeName} {argName}";
- }
- }
- else
- {
- string value = $"default({canonicalTypeName})";
- switch (argType)
- {
- case CommandArgType.InArgument:
- value = $"CommandSerialization.DeserializeArg<{canonicalTypeName}>(inRawData, processor.GetInArgOffset({inArgIndex++}))";
- break;
- case CommandArgType.InCopyHandle:
- value = $"CommandSerialization.DeserializeCopyHandle(ref context, {inCopyHandleIndex++})";
- break;
- case CommandArgType.InMoveHandle:
- value = $"CommandSerialization.DeserializeMoveHandle(ref context, {inMoveHandleIndex++})";
- break;
- case CommandArgType.ProcessId:
- value = "CommandSerialization.DeserializeClientProcessId(ref context)";
- break;
- case CommandArgType.InObject:
- value = $"{InObjectsVariableName}[{inObjectIndex++}]";
- break;
- case CommandArgType.Buffer:
- if (IsReadOnlySpan(compilation, parameter))
- {
- string spanGenericTypeName = GetCanonicalTypeNameOfGenericArgument(compilation, parameter.Type, 0);
- value = GenerateSpanCast(spanGenericTypeName, $"CommandSerialization.GetReadOnlySpan(processor.GetBufferRange({index}))");
- }
- else if (IsSpan(compilation, parameter))
- {
- value = $"CommandSerialization.GetWritableRegion(processor.GetBufferRange({index}))";
- }
- else
- {
- value = $"CommandSerialization.GetRef<{canonicalTypeName}>(processor.GetBufferRange({index}))";
- isNonSpanBuffer = true;
- }
- break;
- }
- if (IsSpan(compilation, parameter))
- {
- generator.AppendLine($"using var {argName} = {value};");
- string spanGenericTypeName = GetCanonicalTypeNameOfGenericArgument(compilation, parameter.Type, 0);
- argName = GenerateSpanCast(spanGenericTypeName, $"{argName}.Memory.Span"); ;
- }
- else if (isNonSpanBuffer)
- {
- generator.AppendLine($"ref var {argName} = ref {value};");
- }
- else if (argType == CommandArgType.InObject)
- {
- generator.EnterScope($"if (!({value} is {canonicalTypeName} {argName}))");
- generator.AppendLine("return SfResult.InvalidInObject;");
- generator.LeaveScope();
- }
- else
- {
- generator.AppendLine($"var {argName} = {value};");
- }
- }
- if (modifier == Modifier.Ref)
- {
- argName = $"ref {argName}";
- }
- else if (modifier == Modifier.In)
- {
- argName = $"in {argName}";
- }
- args[index++] = argName;
- }
- if (args.Length - outParameters.Count > 0)
- {
- generator.AppendLine();
- }
- if (returnsResult)
- {
- generator.AppendLine($"{ResultVariableName} = {method.Identifier.Text}({string.Join(", ", args)});");
- generator.AppendLine();
- generator.AppendLine($"Span<byte> {OutRawDataVariableName};");
- generator.AppendLine();
- generator.EnterScope($"if ({ResultVariableName}.IsFailure)");
- generator.AppendLine($"context.Processor.PrepareForErrorReply(ref context, out {OutRawDataVariableName}, runtimeMetadata);");
- generator.AppendLine($"CommandHandler.GetCmifOutHeaderPointer(ref outHeader, ref {OutRawDataVariableName});");
- generator.AppendLine($"return {ResultVariableName};");
- generator.LeaveScope();
- }
- else
- {
- generator.AppendLine($"{method.Identifier.Text}({string.Join(", ", args)});");
- generator.AppendLine();
- generator.AppendLine($"Span<byte> {OutRawDataVariableName};");
- }
- generator.AppendLine();
- generator.AppendLine($"var {ResponseVariableName} = context.Processor.PrepareForReply(ref context, out {OutRawDataVariableName}, runtimeMetadata);");
- generator.AppendLine($"CommandHandler.GetCmifOutHeaderPointer(ref outHeader, ref {OutRawDataVariableName});");
- generator.AppendLine();
- generator.EnterScope($"if ({OutRawDataVariableName}.Length < processor.OutRawDataSize)");
- generator.AppendLine("return SfResult.InvalidOutRawSize;");
- generator.LeaveScope();
- if (outParameters.Count != 0)
- {
- generator.AppendLine();
- int outCopyHandleIndex = 0;
- int outMoveHandleIndex = outObjectsCount;
- int outObjectIndex = 0;
- for (int outIndex = 0; outIndex < outParameters.Count; outIndex++)
- {
- OutParameter outParameter = outParameters[outIndex];
- switch (outParameter.Type)
- {
- case CommandArgType.OutArgument:
- generator.AppendLine($"CommandSerialization.SerializeArg<{outParameter.TypeName}>({OutRawDataVariableName}, processor.GetOutArgOffset({outParameter.Index}), {outParameter.Name});");
- break;
- case CommandArgType.OutCopyHandle:
- generator.AppendLine($"CommandSerialization.SerializeCopyHandle({ResponseVariableName}, {outCopyHandleIndex++}, {outParameter.Name});");
- break;
- case CommandArgType.OutMoveHandle:
- generator.AppendLine($"CommandSerialization.SerializeMoveHandle({ResponseVariableName}, {outMoveHandleIndex++}, {outParameter.Name});");
- break;
- case CommandArgType.OutObject:
- generator.AppendLine($"{OutObjectsVariableName}[{outObjectIndex++}] = {outParameter.Name};");
- break;
- }
- }
- }
- generator.AppendLine();
- if (outObjectsCount != 0 || buffersCount != 0)
- {
- if (outObjectsCount != 0)
- {
- generator.AppendLine($"processor.SetOutObjects(ref context, {ResponseVariableName}, {OutObjectsVariableName});");
- }
- if (buffersCount != 0)
- {
- generator.AppendLine($"processor.SetOutBuffers({ResponseVariableName}, {IsBufferMapAliasVariableName});");
- }
- generator.AppendLine();
- }
- generator.AppendLine("return Result.Success;");
- generator.LeaveScope();
- }
- private static string GetPrefixedArgName(string name)
- {
- return ArgVariablePrefix + name[0].ToString().ToUpperInvariant() + name.Substring(1);
- }
- private static string GetCanonicalTypeNameOfGenericArgument(Compilation compilation, SyntaxNode syntaxNode, int argIndex)
- {
- if (syntaxNode is GenericNameSyntax genericNameSyntax)
- {
- if ((uint)argIndex < (uint)genericNameSyntax.TypeArgumentList.Arguments.Count)
- {
- return GetCanonicalTypeNameWithGenericArguments(compilation, genericNameSyntax.TypeArgumentList.Arguments[argIndex]);
- }
- }
- return GetCanonicalTypeName(compilation, syntaxNode);
- }
- private static string GetCanonicalTypeNameWithGenericArguments(Compilation compilation, SyntaxNode syntaxNode)
- {
- TypeInfo typeInfo = compilation.GetSemanticModel(syntaxNode.SyntaxTree).GetTypeInfo(syntaxNode);
- return typeInfo.Type.ToDisplayString();
- }
- private static string GetCanonicalTypeName(Compilation compilation, SyntaxNode syntaxNode)
- {
- TypeInfo typeInfo = compilation.GetSemanticModel(syntaxNode.SyntaxTree).GetTypeInfo(syntaxNode);
- string typeName = typeInfo.Type.ToDisplayString();
- int genericArgsStartIndex = typeName.IndexOf('<');
- if (genericArgsStartIndex >= 0)
- {
- return typeName.Substring(0, genericArgsStartIndex);
- }
- return typeName;
- }
- private static SpecialType GetSpecialTypeName(Compilation compilation, SyntaxNode syntaxNode)
- {
- TypeInfo typeInfo = compilation.GetSemanticModel(syntaxNode.SyntaxTree).GetTypeInfo(syntaxNode);
- return typeInfo.Type.SpecialType;
- }
- private static string GetTypeAlignmentExpression(Compilation compilation, SyntaxNode syntaxNode)
- {
- TypeInfo typeInfo = compilation.GetSemanticModel(syntaxNode.SyntaxTree).GetTypeInfo(syntaxNode);
- // Since there's no way to get the alignment for a arbitrary type here, let's assume that all
- // "special" types are primitive types aligned to their own length.
- // Otherwise, assume that the type is a custom struct, that either defines an explicit alignment
- // or has an alignment of 1 which is the lowest possible value.
- if (typeInfo.Type.SpecialType == SpecialType.None)
- {
- string pack = GetTypeFirstNamedAttributeAgument(compilation, syntaxNode, TypeStructLayoutAttribute, "Pack");
- return pack ?? "1";
- }
- else
- {
- return $"Unsafe.SizeOf<{typeInfo.Type.ToDisplayString()}>()";
- }
- }
- private static string GetTypeFirstNamedAttributeAgument(Compilation compilation, SyntaxNode syntaxNode, string attributeName, string argName)
- {
- ISymbol symbol = compilation.GetSemanticModel(syntaxNode.SyntaxTree).GetTypeInfo(syntaxNode).Type;
- foreach (var attribute in symbol.GetAttributes())
- {
- if (attribute.AttributeClass.ToDisplayString() == attributeName)
- {
- foreach (var kv in attribute.NamedArguments)
- {
- if (kv.Key == argName)
- {
- return kv.Value.ToCSharpString();
- }
- }
- }
- }
- return null;
- }
- private static CommandArgType GetCommandArgType(Compilation compilation, ParameterSyntax parameter)
- {
- CommandArgType type = CommandArgType.Invalid;
- if (IsIn(parameter))
- {
- if (IsArgument(compilation, parameter))
- {
- type = CommandArgType.InArgument;
- }
- else if (IsBuffer(compilation, parameter))
- {
- type = CommandArgType.Buffer;
- }
- else if (IsCopyHandle(compilation, parameter))
- {
- type = CommandArgType.InCopyHandle;
- }
- else if (IsMoveHandle(compilation, parameter))
- {
- type = CommandArgType.InMoveHandle;
- }
- else if (IsObject(compilation, parameter))
- {
- type = CommandArgType.InObject;
- }
- else if (IsProcessId(compilation, parameter))
- {
- type = CommandArgType.ProcessId;
- }
- }
- else if (IsOut(parameter))
- {
- if (IsArgument(compilation, parameter))
- {
- type = CommandArgType.OutArgument;
- }
- else if (IsNonSpanOutBuffer(compilation, parameter))
- {
- type = CommandArgType.Buffer;
- }
- else if (IsCopyHandle(compilation, parameter))
- {
- type = CommandArgType.OutCopyHandle;
- }
- else if (IsMoveHandle(compilation, parameter))
- {
- type = CommandArgType.OutMoveHandle;
- }
- else if (IsObject(compilation, parameter))
- {
- type = CommandArgType.OutObject;
- }
- }
- return type;
- }
- private static bool IsArgument(Compilation compilation,ParameterSyntax parameter)
- {
- return !IsBuffer(compilation, parameter) &&
- !IsHandle(compilation, parameter) &&
- !IsObject(compilation, parameter) &&
- !IsProcessId(compilation, parameter) &&
- IsUnmanagedType(compilation, parameter.Type);
- }
- private static bool IsBuffer(Compilation compilation, ParameterSyntax parameter)
- {
- return HasAttribute(compilation, parameter, TypeBufferAttribute) &&
- IsValidTypeForBuffer(compilation, parameter);
- }
- private static bool IsNonSpanOutBuffer(Compilation compilation, ParameterSyntax parameter)
- {
- return HasAttribute(compilation, parameter, TypeBufferAttribute) &&
- IsUnmanagedType(compilation, parameter.Type);
- }
- private static bool IsValidTypeForBuffer(Compilation compilation, ParameterSyntax parameter)
- {
- return IsReadOnlySpan(compilation, parameter) ||
- IsSpan(compilation, parameter) ||
- IsUnmanagedType(compilation, parameter.Type);
- }
- private static bool IsUnmanagedType(Compilation compilation, SyntaxNode syntaxNode)
- {
- TypeInfo typeInfo = compilation.GetSemanticModel(syntaxNode.SyntaxTree).GetTypeInfo(syntaxNode);
- return typeInfo.Type.IsUnmanagedType;
- }
- private static bool IsReadOnlySpan(Compilation compilation, ParameterSyntax parameter)
- {
- return GetCanonicalTypeName(compilation, parameter.Type) == TypeSystemReadOnlySpan;
- }
- private static bool IsSpan(Compilation compilation, ParameterSyntax parameter)
- {
- return GetCanonicalTypeName(compilation, parameter.Type) == TypeSystemSpan;
- }
- private static bool IsHandle(Compilation compilation, ParameterSyntax parameter)
- {
- return IsCopyHandle(compilation, parameter) || IsMoveHandle(compilation, parameter);
- }
- private static bool IsCopyHandle(Compilation compilation, ParameterSyntax parameter)
- {
- return HasAttribute(compilation, parameter, TypeCopyHandleAttribute) &&
- GetSpecialTypeName(compilation, parameter.Type) == SpecialType.System_Int32;
- }
- private static bool IsMoveHandle(Compilation compilation, ParameterSyntax parameter)
- {
- return HasAttribute(compilation, parameter, TypeMoveHandleAttribute) &&
- GetSpecialTypeName(compilation, parameter.Type) == SpecialType.System_Int32;
- }
- private static bool IsObject(Compilation compilation, ParameterSyntax parameter)
- {
- SyntaxNode syntaxNode = parameter.Type;
- TypeInfo typeInfo = compilation.GetSemanticModel(syntaxNode.SyntaxTree).GetTypeInfo(syntaxNode);
- return typeInfo.Type.ToDisplayString() == TypeIServiceObject ||
- typeInfo.Type.AllInterfaces.Any(x => x.ToDisplayString() == TypeIServiceObject);
- }
- private static bool IsProcessId(Compilation compilation, ParameterSyntax parameter)
- {
- return HasAttribute(compilation, parameter, TypeClientProcessIdAttribute) &&
- GetSpecialTypeName(compilation, parameter.Type) == SpecialType.System_UInt64;
- }
- private static bool IsIn(ParameterSyntax parameter)
- {
- return !IsOut(parameter);
- }
- private static bool IsOut(ParameterSyntax parameter)
- {
- return parameter.Modifiers.Any(SyntaxKind.OutKeyword);
- }
- private static Modifier GetModifier(ParameterSyntax parameter)
- {
- foreach (SyntaxToken syntaxToken in parameter.Modifiers)
- {
- if (syntaxToken.IsKind(SyntaxKind.RefKeyword))
- {
- return Modifier.Ref;
- }
- else if (syntaxToken.IsKind(SyntaxKind.OutKeyword))
- {
- return Modifier.Out;
- }
- else if (syntaxToken.IsKind(SyntaxKind.InKeyword))
- {
- return Modifier.In;
- }
- }
- return Modifier.None;
- }
- private static string GenerateSpanCastElement0(string targetType, string input)
- {
- return $"{GenerateSpanCast(targetType, input)}[0]";
- }
- private static string GenerateSpanCast(string targetType, string input)
- {
- return $"MemoryMarshal.Cast<byte, {targetType}>({input})";
- }
- private static bool HasAttribute(Compilation compilation, ParameterSyntax parameterSyntax, string fullAttributeName)
- {
- foreach (var attributeList in parameterSyntax.AttributeLists)
- {
- foreach (var attribute in attributeList.Attributes)
- {
- if (GetCanonicalTypeName(compilation, attribute) == fullAttributeName)
- {
- return true;
- }
- }
- }
- return false;
- }
- 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");
- // 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
- // we have nothing to do.
- return serviceObjectInterface != null && type.FindImplementationForInterfaceMember(interfaceMember) == null;
- }
- public void Initialize(GeneratorInitializationContext context)
- {
- context.RegisterForSyntaxNotifications(() => new HipcSyntaxReceiver());
- }
- }
- }
|