|
| 1 | +using Microsoft.CodeAnalysis; |
| 2 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 3 | +using Microsoft.CodeAnalysis.Operations; |
| 4 | +using System.Collections; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Collections.Immutable; |
| 7 | +using System.Linq; |
| 8 | +using System.Threading; |
| 9 | + |
| 10 | +namespace Monkeymoto.NativeGenericDelegates |
| 11 | +{ |
| 12 | + internal sealed class MarshalMap : IReadOnlyDictionary<string, string?> |
| 13 | + { |
| 14 | + private readonly ImmutableDictionary<string, string?> map; |
| 15 | + |
| 16 | + public string? this[string key] => map[key]; |
| 17 | + public int Count => map.Count; |
| 18 | + public IEnumerable<string> Keys => map.Keys; |
| 19 | + public IEnumerable<string?> Values => map.Values; |
| 20 | + |
| 21 | + public static MarshalMap? Parse |
| 22 | + ( |
| 23 | + IArgumentOperation? marshalMapArgument, |
| 24 | + IList<Diagnostic> diagnostics, |
| 25 | + CancellationToken cancellationToken |
| 26 | + ) |
| 27 | + { |
| 28 | + _ = diagnostics; |
| 29 | + if (marshalMapArgument is null) |
| 30 | + { |
| 31 | + return null; |
| 32 | + } |
| 33 | + var builder = ImmutableDictionary.CreateBuilder<string, string?>(); |
| 34 | + var value = marshalMapArgument.Value; |
| 35 | + var invalidArgumentDiagnostic = Diagnostic.Create |
| 36 | + ( |
| 37 | + Diagnostics.NGD1004_InvalidMarshalMapArgument, |
| 38 | + marshalMapArgument.Syntax.GetLocation(), |
| 39 | + marshalMapArgument.Parameter!.Name |
| 40 | + ); |
| 41 | + if (value is IFieldReferenceOperation fieldReference && fieldReference.Field.IsReadOnly) |
| 42 | + { |
| 43 | + var fieldDeclaration = fieldReference.Field.DeclaringSyntaxReferences[0].GetSyntax(cancellationToken); |
| 44 | + var equalsValueClause = fieldDeclaration.ChildNodes().OfType<EqualsValueClauseSyntax>() |
| 45 | + .FirstOrDefault(); |
| 46 | + SemanticModel? semanticModel = equalsValueClause is not null ? |
| 47 | + fieldReference.SemanticModel!.Compilation.GetSemanticModel(equalsValueClause.SyntaxTree) : |
| 48 | + null; |
| 49 | + if (semanticModel?.GetOperation(equalsValueClause!, cancellationToken) is not |
| 50 | + IFieldInitializerOperation fieldInitializer) |
| 51 | + { |
| 52 | + diagnostics.Add(invalidArgumentDiagnostic); |
| 53 | + return null; |
| 54 | + } |
| 55 | + value = fieldInitializer.Value; |
| 56 | + } |
| 57 | + IObjectCreationOperation? mapCreation = value switch |
| 58 | + { |
| 59 | + IConversionOperation conversion => conversion.Operand as IObjectCreationOperation, |
| 60 | + _ => value as IObjectCreationOperation |
| 61 | + }; |
| 62 | + if ((mapCreation?.Initializer is null) || (mapCreation.Initializer.Initializers.Length == 0)) |
| 63 | + { |
| 64 | + return new(builder.ToImmutable()); |
| 65 | + } |
| 66 | + var initializers = mapCreation.Initializer.Initializers; |
| 67 | + foreach (var op in initializers) |
| 68 | + { |
| 69 | + cancellationToken.ThrowIfCancellationRequested(); |
| 70 | + ITypeOfOperation? typeOf; |
| 71 | + IOperation? marshalAs; |
| 72 | + if (op is IInvocationOperation invocation) |
| 73 | + { |
| 74 | + if (invocation.Arguments.Length != 2) |
| 75 | + { |
| 76 | + diagnostics.Add(invalidArgumentDiagnostic); |
| 77 | + return null; |
| 78 | + } |
| 79 | + typeOf = invocation.Arguments[0].Value as ITypeOfOperation; |
| 80 | + value = invocation.Arguments[1].Value; |
| 81 | + marshalAs = value switch |
| 82 | + { |
| 83 | + IConversionOperation conversion => conversion.Operand as IObjectCreationOperation, |
| 84 | + _ => value as IObjectCreationOperation |
| 85 | + }; |
| 86 | + } |
| 87 | + else |
| 88 | + { |
| 89 | + diagnostics.Add(invalidArgumentDiagnostic); |
| 90 | + return null; |
| 91 | + } |
| 92 | + if ((typeOf is null) || (marshalAs is null)) |
| 93 | + { |
| 94 | + diagnostics.Add(invalidArgumentDiagnostic); |
| 95 | + return null; |
| 96 | + } |
| 97 | + var key = typeOf.TypeOperand.ToDisplayString(); |
| 98 | + var marshalAsValue = DelegateMarshalling.Parser.GetMarshalAsFromOperation |
| 99 | + ( |
| 100 | + marshalAs, |
| 101 | + marshalMapArgument.Parameter!.Name, |
| 102 | + diagnostics, |
| 103 | + diagnosticTypeSuffix: "", |
| 104 | + cancellationToken |
| 105 | + ); |
| 106 | + builder[key] = marshalAsValue; |
| 107 | + } |
| 108 | + return new(builder.ToImmutable()); |
| 109 | + } |
| 110 | + |
| 111 | + private MarshalMap(ImmutableDictionary<string, string?> dictionary) |
| 112 | + { |
| 113 | + map = dictionary; |
| 114 | + } |
| 115 | + |
| 116 | + public bool ContainsKey(string key) => map.ContainsKey(key); |
| 117 | + public IEnumerator<KeyValuePair<string, string?>> GetEnumerator() => map.GetEnumerator(); |
| 118 | + IEnumerator IEnumerable.GetEnumerator() => map.GetEnumerator(); |
| 119 | + public bool TryGetValue(string key, out string? value) => map.TryGetValue(key, out value); |
| 120 | + } |
| 121 | +} |
0 commit comments