Skip to content

Commit a01aea9

Browse files
authored
Add stateful marshaller support to the CustomMarshallerAttributeAnalyzer (#72888)
1 parent 32f3646 commit a01aea9

23 files changed

+2024
-111
lines changed

src/libraries/Common/src/Interop/Interop.Ldap.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ public LdapReferralCallback ToManaged()
263263
}
264264

265265
public void OnInvoked() => GC.KeepAlive(_managed);
266+
267+
public void Free() {}
266268
}
267269
}
268270
#else

src/libraries/System.Drawing.Common/src/System/Drawing/Graphics.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ public void OnInvoked()
141141
{
142142
GC.KeepAlive(_managed);
143143
}
144+
145+
public void Free() {}
144146
}
145147
}
146148
#endif

src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Analyzers/CustomMarshallerAttributeAnalyzer.cs

Lines changed: 402 additions & 18 deletions
Large diffs are not rendered by default.

src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Resources/Strings.resx

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,16 @@
117117
<resheader name="writer">
118118
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119119
</resheader>
120-
<data name="CallerAllocConstructorMustHaveBufferSizeDescription" xml:space="preserve">
120+
<data name="CallerAllocFromManagedMustHaveBufferSizeDescription" xml:space="preserve">
121121
<value>When the 'Managed to Unmanaged with Caller-Allocated Buffer' shape is used by providing a 'FromManaged' method that takes a 'Span&lt;T&gt;' on the marshaller type, the type must provide a static 'BufferSize' property to provide the number of elements in the caller-allocated buffer.</value>
122122
</data>
123-
<data name="CallerAllocConstructorMustHaveBufferSizeMessage" xml:space="preserve">
123+
<data name="CallerAllocFromManagedMustHaveBufferSizeMessage" xml:space="preserve">
124124
<value>The marshaller type '{0}' must have a static read-only 'int' 'BufferSize' property to specify the size of the caller-allocated buffer because it has a FromManaged method that takes a caller-allocated 'Span&lt;{1}&gt;'</value>
125125
</data>
126-
<data name="StatelessLinearCollectionCallerAllocConstructorMustHaveBufferSizeDescription" xml:space="preserve">
126+
<data name="StatelessLinearCollectionCallerAllocFromManagedMustHaveBufferSizeDescription" xml:space="preserve">
127127
<value>When the 'Managed to Unmanaged with Caller-Allocated Buffer' shape is used by providing an 'AllocateContainerForUnmanagedElements' method that takes a 'Span&lt;T&gt;' on the marshaller type, the type must provide a static 'BufferSize' property to provide the number of elements in the caller-allocated buffer.</value>
128128
</data>
129-
<data name="StatelessLinearCollectionCallerAllocConstructorMustHaveBufferSizeMessage" xml:space="preserve">
129+
<data name="StatelessLinearCollectionCallerAllocFromManagedMustHaveBufferSizeMessage" xml:space="preserve">
130130
<value>The marshaller type '{0}' must have a static read-only 'int' 'BufferSize' property to specify the size of the caller-allocated buffer because it has an 'AllocateContainerForUnmanagedElements' method that takes a caller-allocated 'Span&lt;{1}&gt;'</value>
131131
</data>
132132
<data name="CannotForwardToDllImportDescription" xml:space="preserve">
@@ -375,7 +375,7 @@
375375
<value>Overloading the 'FromUnmanaged' method is unuspported as some shapes are unable to distinguish between overloads.</value>
376376
</data>
377377
<data name="FromUnmanagedOverloadsNotSupportedMessage" xml:space="preserve">
378-
<value>Overloading the 'FromUnmanaged' method is not supported in custom marshallers</value>
378+
<value>The type '{0}' overloads the 'FromUnmanaged' method, which is not supported in custom marshallers</value>
379379
</data>
380380
<data name="ToUnmanagedFromManagedTypesMustMatchDescription" xml:space="preserve">
381381
<value>The return type of 'ConvertToUnmanaged' and the parameter type of 'ConvertToManaged' must be the same.</value>
@@ -443,6 +443,12 @@
443443
<data name="ElementTypesOfReturnTypesMustMatchMessage" xml:space="preserve">
444444
<value>The element type of the span returned by '{0}' must be the same type as the element type of the span returned by '{1}'.</value>
445445
</data>
446+
<data name="ElementMarshallerCannotBeStatefulDescription" xml:space="preserve">
447+
<value>A marshaller for an element scenario cannot be stateful.</value>
448+
</data>
449+
<data name="ElementMarshallerCannotBeStatefulMessage" xml:space="preserve">
450+
<value>The specified marshaller type '{0}' is a stateful marshaller, but stateful marshallers are not allowed in the provided marshal mode '{1}'</value>
451+
</data>
446452
<data name="RequiresAllowUnsafeBlocksDescription" xml:space="preserve">
447453
<value>LibraryImportAttribute requires unsafe code. Project must be updated with '&lt;AllowUnsafeBlocks&gt;true&lt;/AllowUnsafeBlocks&gt;'.</value>
448454
</data>
@@ -452,6 +458,36 @@
452458
<data name="RequiresAllowUnsafeBlocksTitle" xml:space="preserve">
453459
<value>LibraryImportAttribute requires unsafe code.</value>
454460
</data>
461+
<data name="StatefulMarshallerRequiresFromManagedDescription" xml:space="preserve">
462+
<value>A stateful marshaller that supports marshalling from managed to unmanaged must provide a 'FromManaged' instance method that takes the managed value as a parameter and returns 'void'.</value>
463+
</data>
464+
<data name="StatefulMarshallerRequiresFromManagedMessage" xml:space="preserve">
465+
<value>The type '{0}' specifies that it supports the '{1}' marshal mode for '{2}' but it does not provide a one-parameter instance method named 'FromManaged' that takes a '{2}' as a parameter and returns 'void'</value>
466+
</data>
467+
<data name="StatefulMarshallerRequiresToUnmanagedDescription" xml:space="preserve">
468+
<value>A stateful marshaller that supports marshalling from managed to unmanaged must provide a 'ToUnmanaged' instance method that takes no parameters and returns the 'unmanaged' type.</value>
469+
</data>
470+
<data name="StatefulMarshallerRequiresToUnmanagedMessage" xml:space="preserve">
471+
<value>The type '{0}' specifies that it supports the '{1}' marshal mode for '{2}' but it does not provide a zero-parameter instance method named 'ToUnmanaged' that returns the 'unmanaged' type for the marshaller</value>
472+
</data>
473+
<data name="StatefulMarshallerRequiresFromUnmanagedDescription" xml:space="preserve">
474+
<value>A stateful marshaller that supports marshalling from unmanaged to managed must provide a 'FromUnmanaged' instance method that takes the unmanaged value as a parameter and returns 'void'.</value>
475+
</data>
476+
<data name="StatefulMarshallerRequiresFromUnmanagedMessage" xml:space="preserve">
477+
<value>The type '{0}' specifies that it supports the '{1}' marshal mode for '{2}' but it does not provide a one-parameter instance method named 'FromUnmanaged' that takes the 'unmanaged' value as a parameter and returns 'void'</value>
478+
</data>
479+
<data name="StatefulMarshallerRequiresToManagedDescription" xml:space="preserve">
480+
<value>A stateful marshaller that supports marshalling from unmanaged to managed must provide a 'ToManaged' instance method that takes no parameters and returns the managed type.</value>
481+
</data>
482+
<data name="StatefulMarshallerRequiresToManagedMessage" xml:space="preserve">
483+
<value>The type '{0}' specifies that it supports the '{1}' marshal mode for '{2}' but it does not provide a zero-parameter instance method named 'ToManaged' that returns '{2}'</value>
484+
</data>
485+
<data name="StatefulMarshallerRequiresFreeDescription" xml:space="preserve">
486+
<value>A stateful marshaller must have a zero-parameter void-returning instance method named 'Free'.</value>
487+
</data>
488+
<data name="StatefulMarshallerRequiresFreeMessage" xml:space="preserve">
489+
<value>The type '{0}' is a stateful marshaller and does not have a zero-parameter void-returning instance method named 'Free'</value>
490+
</data>
455491
<data name="ConvertToLibraryImportAddUnsafe" xml:space="preserve">
456492
<value>Convert to 'LibraryImport' and enable unsafe code</value>
457493
</data>
@@ -461,4 +497,4 @@
461497
<data name="ConvertToLibraryImportWithSuffixAddUnsafe" xml:space="preserve">
462498
<value>Convert to 'LibraryImport' with '{0}' suffix and enable unsafe code</value>
463499
</data>
464-
</root>
500+
</root>

src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/Resources/xlf/Strings.cs.xlf

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
<target state="translated">Přidat chybějící vlastní typu zařazovacích členů</target>
88
<note />
99
</trans-unit>
10-
<trans-unit id="CallerAllocConstructorMustHaveBufferSizeDescription">
10+
<trans-unit id="CallerAllocFromManagedMustHaveBufferSizeDescription">
1111
<source>When the 'Managed to Unmanaged with Caller-Allocated Buffer' shape is used by providing a 'FromManaged' method that takes a 'Span&lt;T&gt;' on the marshaller type, the type must provide a static 'BufferSize' property to provide the number of elements in the caller-allocated buffer.</source>
1212
<target state="translated">Pokud se obrazec „Ze spravovaných na nespravované s vyrovnávací pamětí přidělenou volajícímu“ používá metodou FromManaged, která přebírá Span&lt;T&gt; u zařazovacího typu, musí typ poskytovat statickou vlastnost BufferSize, která určuje počet elementů ve vyrovnávací paměti přidělené volajícímu.</target>
1313
<note />
1414
</trans-unit>
15-
<trans-unit id="CallerAllocConstructorMustHaveBufferSizeMessage">
15+
<trans-unit id="CallerAllocFromManagedMustHaveBufferSizeMessage">
1616
<source>The marshaller type '{0}' must have a static read-only 'int' 'BufferSize' property to specify the size of the caller-allocated buffer because it has a FromManaged method that takes a caller-allocated 'Span&lt;{1}&gt;'</source>
1717
<target state="translated">Zařazovací typ {0} musí obsahovat statickou celočíselnou vlastnost BufferSize určenou jen pro čtení, která určuje velikost vyrovnávací paměti přidělené volajícímu, protože obsahuje metodu FromManaged, která přebírá Span&lt;{1}&gt; přidělený volajícímu.</target>
1818
<note />
@@ -117,6 +117,16 @@
117117
<target state="translated">Zařazovací typ nemá požadovaný tvar</target>
118118
<note />
119119
</trans-unit>
120+
<trans-unit id="ElementMarshallerCannotBeStatefulDescription">
121+
<source>A marshaller for an element scenario cannot be stateful.</source>
122+
<target state="new">A marshaller for an element scenario cannot be stateful.</target>
123+
<note />
124+
</trans-unit>
125+
<trans-unit id="ElementMarshallerCannotBeStatefulMessage">
126+
<source>The specified marshaller type '{0}' is a stateful marshaller, but stateful marshallers are not allowed in the provided marshal mode '{1}'</source>
127+
<target state="new">The specified marshaller type '{0}' is a stateful marshaller, but stateful marshallers are not allowed in the provided marshal mode '{1}'</target>
128+
<note />
129+
</trans-unit>
120130
<trans-unit id="ElementTypesOfReturnTypesMustMatchDescription">
121131
<source>The element type of the span returned by the first method must be the same type as the element type of the span returned by the second method.</source>
122132
<target state="new">The element type of the span returned by the first method must be the same type as the element type of the span returned by the second method.</target>
@@ -163,8 +173,8 @@
163173
<note />
164174
</trans-unit>
165175
<trans-unit id="FromUnmanagedOverloadsNotSupportedMessage">
166-
<source>Overloading the 'FromUnmanaged' method is not supported in custom marshallers</source>
167-
<target state="translated">Přetížení metody FromUnmanaged není u vlastního zařazování podporováno.</target>
176+
<source>The type '{0}' overloads the 'FromUnmanaged' method, which is not supported in custom marshallers</source>
177+
<target state="new">The type '{0}' overloads the 'FromUnmanaged' method, which is not supported in custom marshallers</target>
168178
<note />
169179
</trans-unit>
170180
<trans-unit id="GetPinnableReferenceReturnTypeBlittableDescription">
@@ -387,12 +397,62 @@
387397
<target state="new">The return type of '{0}' must be the same type as the return type of '{1}'</target>
388398
<note />
389399
</trans-unit>
390-
<trans-unit id="StatelessLinearCollectionCallerAllocConstructorMustHaveBufferSizeDescription">
400+
<trans-unit id="StatefulMarshallerRequiresFreeDescription">
401+
<source>A stateful marshaller must have a zero-parameter void-returning instance method named 'Free'.</source>
402+
<target state="new">A stateful marshaller must have a zero-parameter void-returning instance method named 'Free'.</target>
403+
<note />
404+
</trans-unit>
405+
<trans-unit id="StatefulMarshallerRequiresFreeMessage">
406+
<source>The type '{0}' is a stateful marshaller and does not have a zero-parameter void-returning instance method named 'Free'</source>
407+
<target state="new">The type '{0}' is a stateful marshaller and does not have a zero-parameter void-returning instance method named 'Free'</target>
408+
<note />
409+
</trans-unit>
410+
<trans-unit id="StatefulMarshallerRequiresFromManagedDescription">
411+
<source>A stateful marshaller that supports marshalling from managed to unmanaged must provide a 'FromManaged' instance method that takes the managed value as a parameter and returns 'void'.</source>
412+
<target state="new">A stateful marshaller that supports marshalling from managed to unmanaged must provide a 'FromManaged' instance method that takes the managed value as a parameter and returns 'void'.</target>
413+
<note />
414+
</trans-unit>
415+
<trans-unit id="StatefulMarshallerRequiresFromManagedMessage">
416+
<source>The type '{0}' specifies that it supports the '{1}' marshal mode for '{2}' but it does not provide a one-parameter instance method named 'FromManaged' that takes a '{2}' as a parameter and returns 'void'</source>
417+
<target state="new">The type '{0}' specifies that it supports the '{1}' marshal mode for '{2}' but it does not provide a one-parameter instance method named 'FromManaged' that takes a '{2}' as a parameter and returns 'void'</target>
418+
<note />
419+
</trans-unit>
420+
<trans-unit id="StatefulMarshallerRequiresFromUnmanagedDescription">
421+
<source>A stateful marshaller that supports marshalling from unmanaged to managed must provide a 'FromUnmanaged' instance method that takes the unmanaged value as a parameter and returns 'void'.</source>
422+
<target state="new">A stateful marshaller that supports marshalling from unmanaged to managed must provide a 'FromUnmanaged' instance method that takes the unmanaged value as a parameter and returns 'void'.</target>
423+
<note />
424+
</trans-unit>
425+
<trans-unit id="StatefulMarshallerRequiresFromUnmanagedMessage">
426+
<source>The type '{0}' specifies that it supports the '{1}' marshal mode for '{2}' but it does not provide a one-parameter instance method named 'FromUnmanaged' that takes the 'unmanaged' value as a parameter and returns 'void'</source>
427+
<target state="new">The type '{0}' specifies that it supports the '{1}' marshal mode for '{2}' but it does not provide a one-parameter instance method named 'FromUnmanaged' that takes the 'unmanaged' value as a parameter and returns 'void'</target>
428+
<note />
429+
</trans-unit>
430+
<trans-unit id="StatefulMarshallerRequiresToManagedDescription">
431+
<source>A stateful marshaller that supports marshalling from unmanaged to managed must provide a 'ToManaged' instance method that takes no parameters and returns the managed type.</source>
432+
<target state="new">A stateful marshaller that supports marshalling from unmanaged to managed must provide a 'ToManaged' instance method that takes no parameters and returns the managed type.</target>
433+
<note />
434+
</trans-unit>
435+
<trans-unit id="StatefulMarshallerRequiresToManagedMessage">
436+
<source>The type '{0}' specifies that it supports the '{1}' marshal mode for '{2}' but it does not provide a zero-parameter instance method named 'ToManaged' that returns '{2}'</source>
437+
<target state="new">The type '{0}' specifies that it supports the '{1}' marshal mode for '{2}' but it does not provide a zero-parameter instance method named 'ToManaged' that returns '{2}'</target>
438+
<note />
439+
</trans-unit>
440+
<trans-unit id="StatefulMarshallerRequiresToUnmanagedDescription">
441+
<source>A stateful marshaller that supports marshalling from managed to unmanaged must provide a 'ToUnmanaged' instance method that takes no parameters and returns the 'unmanaged' type.</source>
442+
<target state="new">A stateful marshaller that supports marshalling from managed to unmanaged must provide a 'ToUnmanaged' instance method that takes no parameters and returns the 'unmanaged' type.</target>
443+
<note />
444+
</trans-unit>
445+
<trans-unit id="StatefulMarshallerRequiresToUnmanagedMessage">
446+
<source>The type '{0}' specifies that it supports the '{1}' marshal mode for '{2}' but it does not provide a zero-parameter instance method named 'ToUnmanaged' that returns the 'unmanaged' type for the marshaller</source>
447+
<target state="new">The type '{0}' specifies that it supports the '{1}' marshal mode for '{2}' but it does not provide a zero-parameter instance method named 'ToUnmanaged' that returns the 'unmanaged' type for the marshaller</target>
448+
<note />
449+
</trans-unit>
450+
<trans-unit id="StatelessLinearCollectionCallerAllocFromManagedMustHaveBufferSizeDescription">
391451
<source>When the 'Managed to Unmanaged with Caller-Allocated Buffer' shape is used by providing an 'AllocateContainerForUnmanagedElements' method that takes a 'Span&lt;T&gt;' on the marshaller type, the type must provide a static 'BufferSize' property to provide the number of elements in the caller-allocated buffer.</source>
392452
<target state="new">When the 'Managed to Unmanaged with Caller-Allocated Buffer' shape is used by providing an 'AllocateContainerForUnmanagedElements' method that takes a 'Span&lt;T&gt;' on the marshaller type, the type must provide a static 'BufferSize' property to provide the number of elements in the caller-allocated buffer.</target>
393453
<note />
394454
</trans-unit>
395-
<trans-unit id="StatelessLinearCollectionCallerAllocConstructorMustHaveBufferSizeMessage">
455+
<trans-unit id="StatelessLinearCollectionCallerAllocFromManagedMustHaveBufferSizeMessage">
396456
<source>The marshaller type '{0}' must have a static read-only 'int' 'BufferSize' property to specify the size of the caller-allocated buffer because it has an 'AllocateContainerForUnmanagedElements' method that takes a caller-allocated 'Span&lt;{1}&gt;'</source>
397457
<target state="new">The marshaller type '{0}' must have a static read-only 'int' 'BufferSize' property to specify the size of the caller-allocated buffer because it has an 'AllocateContainerForUnmanagedElements' method that takes a caller-allocated 'Span&lt;{1}&gt;'</target>
398458
<note />

0 commit comments

Comments
 (0)