|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Collections.Immutable; |
| 5 | +using System.Diagnostics.CodeAnalysis; |
| 6 | +using System.IO; |
| 7 | +using System.Linq; |
| 8 | +using Microsoft.CodeAnalysis; |
| 9 | +using Microsoft.Macios.Generator.Attributes; |
| 10 | +using Microsoft.Macios.Generator.Context; |
| 11 | +using Microsoft.Macios.Generator.DataModel; |
| 12 | +using Microsoft.Macios.Generator.IO; |
| 13 | +using ObjCBindings; |
| 14 | +using static Microsoft.Macios.Generator.Emitters.BindingSyntaxFactory; |
| 15 | +using Property = Microsoft.Macios.Generator.DataModel.Property; |
| 16 | +using Method = Microsoft.Macios.Generator.DataModel.Method; |
| 17 | + |
| 18 | +namespace Microsoft.Macios.Generator.Emitters; |
| 19 | + |
| 20 | +/// <summary> |
| 21 | +/// Emitter responsible for generating protocol interfaces. |
| 22 | +/// Generates C# interfaces that represent Objective-C protocols with proper protocol member attributes. |
| 23 | +/// </summary> |
| 24 | +class ProtocolEmitter : ICodeEmitter { |
| 25 | + /// <inheritdoc /> |
| 26 | + public string GetSymbolName (in Binding binding) => binding.Name; |
| 27 | + |
| 28 | + /// <inheritdoc /> |
| 29 | + public IEnumerable<string> UsingStatements => []; |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Emits the default constructors and dynamic dependency attributes for the protocol interface. |
| 33 | + /// This includes DynamicDependencyAttribute for each property and a static constructor with GC.KeepAlive. |
| 34 | + /// </summary> |
| 35 | + /// <param name="bindingContext">The binding context containing protocol information.</param> |
| 36 | + /// <param name="interfaceBlock">The writer for the interface block.</param> |
| 37 | + void EmitDefaultConstructors (in BindingContext bindingContext, TabbedWriter<StringWriter> interfaceBlock) |
| 38 | + { |
| 39 | + // emit the DynamicDependencyAttribute per property |
| 40 | + foreach (var property in bindingContext.Changes.Properties.OrderBy (p => p.Name)) { |
| 41 | + interfaceBlock.AppendDynamicDependencyAttribute (property.Name); |
| 42 | + } |
| 43 | + interfaceBlock.AppendGeneratedCodeAttribute (); |
| 44 | + interfaceBlock.WriteRaw ( |
| 45 | +$@"static {bindingContext.Changes.Name} () |
| 46 | +{{ |
| 47 | + {GC}.KeepAlive (null); |
| 48 | +}} |
| 49 | +"); |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Gets the properties from the binding context and their corresponding extension methods. |
| 54 | + /// Returns a collection of tuples containing the property and its optional getter/setter methods. |
| 55 | + /// </summary> |
| 56 | + /// <param name="bindingContext">The binding context containing the properties.</param> |
| 57 | + /// <returns>An immutable array of tuples containing properties and their extension methods.</returns> |
| 58 | + static ImmutableArray<(Property Property, Method? Getter, Method? Setter)> GetProperties (in BindingContext bindingContext) |
| 59 | + { |
| 60 | + // collect all properties and generate the extension methods, this will be used to generate the protocol |
| 61 | + // member data and later the extension methods. |
| 62 | + var propertiesBucket = ImmutableArray.CreateBuilder<(Property Property, Method? Getter, Method? Setter)> (bindingContext.Changes.Properties.Length); |
| 63 | + foreach (var property in bindingContext.Changes.Properties.OrderBy (p => p.Name)) { |
| 64 | + var (getter, setter) = property.ToExtensionMethods (new (bindingContext.Changes.Name, SpecialType.None)); |
| 65 | + propertiesBucket.Add ((property, getter, setter)); |
| 66 | + } |
| 67 | + var properties = propertiesBucket.ToImmutable (); |
| 68 | + return properties; |
| 69 | + } |
| 70 | + |
| 71 | + /// <inheritdoc /> |
| 72 | + public bool TryEmit (in BindingContext bindingContext, [NotNullWhen (false)] out ImmutableArray<Diagnostic>? diagnostics) |
| 73 | + { |
| 74 | + diagnostics = null; |
| 75 | + if (bindingContext.Changes.BindingType != BindingType.Protocol) { |
| 76 | + diagnostics = [Diagnostic.Create ( |
| 77 | + Diagnostics |
| 78 | + .RBI0000, // An unexpected error occurred while processing '{0}'. Please fill a bug report at https://github.com/dotnet/macios/issues/new. |
| 79 | + null, |
| 80 | + bindingContext.Changes.FullyQualifiedSymbol)]; |
| 81 | + return false; |
| 82 | + } |
| 83 | + |
| 84 | + var bindingData = (BindingTypeData<Protocol>) bindingContext.Changes.BindingInfo; |
| 85 | + // namespace declaration |
| 86 | + this.EmitNamespace (bindingContext); |
| 87 | + |
| 88 | + using (var _ = this.EmitOuterClasses (bindingContext, out var builder)) { |
| 89 | + // append the class availability, this will add the necessary attributes to the class |
| 90 | + builder.AppendMemberAvailability (bindingContext.Changes.SymbolAvailability); |
| 91 | + |
| 92 | + // Protocol registration |
| 93 | + var protocolName = bindingData.Name ?? bindingContext.Changes.Name [1..]; |
| 94 | + builder.AppendProtocolAttribute (protocolName, Nomenclator.GetProtocolWrapperName (protocolName)); |
| 95 | + |
| 96 | + // we need to collect the properties extension methods, we do that with a helper method |
| 97 | + // that will return the properties and their getters/setters. |
| 98 | + var properties = GetProperties (bindingContext); |
| 99 | + |
| 100 | + // append the properties to the protocol member data |
| 101 | + foreach (var (property, getter, setter) in properties) { |
| 102 | + var protocolMember = new ProtocolMemberData (property, getter, setter); |
| 103 | + builder.AppendProtocolMemberData (protocolMember); |
| 104 | + } |
| 105 | + |
| 106 | + var modifiers = $"{string.Join (' ', bindingContext.Changes.Modifiers)} "; |
| 107 | + // class declaration, the analyzer should ensure that the class is static, otherwise it will fail to compile with an error. |
| 108 | + using (var interfaceBlock = builder.CreateBlock ( |
| 109 | + $"{(string.IsNullOrWhiteSpace (modifiers) ? string.Empty : modifiers)}interface {bindingContext.Changes.Name} : INativeObject, IDisposable", |
| 110 | + true)) { |
| 111 | + // space for readability |
| 112 | + interfaceBlock.WriteLine (); |
| 113 | + |
| 114 | + // emit static constructor |
| 115 | + EmitDefaultConstructors (in bindingContext, interfaceBlock); |
| 116 | + } |
| 117 | + } |
| 118 | + return true; |
| 119 | + } |
| 120 | +} |
0 commit comments