@@ -256,9 +256,9 @@ public virtual void GenerateNamespaceFunctionsAndVariables(DeclarationContext co
256
256
. Any ( ) ;
257
257
258
258
using ( PushWriteBlock ( BlockKind . Functions , $ "public unsafe partial { ( isStruct ? "struct" : "class" ) } { parentName } ", NewLineKind . BeforeNextBlock ) )
259
- {
259
+ {
260
260
using ( PushWriteBlock ( BlockKind . InternalsClass , GetClassInternalHead ( new Class { Name = parentName } ) , NewLineKind . BeforeNextBlock ) )
261
- {
261
+ {
262
262
// Generate all the internal function declarations.
263
263
foreach ( var function in context . Functions )
264
264
{
@@ -301,7 +301,7 @@ template.OriginalNamespace is Class &&
301
301
template . Name ) ;
302
302
303
303
using ( PushWriteBlock ( BlockKind . Namespace , namespaceName , NewLineKind . BeforeNextBlock ) )
304
- {
304
+ {
305
305
var generated = GetGeneratedClasses ( template , specializations ) ;
306
306
307
307
foreach ( var nestedTemplate in template . Classes . Where (
@@ -1615,19 +1615,27 @@ private void GenerateVariable(Class @class, Variable variable)
1615
1615
var variableType = variable . Type . Visit ( TypePrinter ) ;
1616
1616
TypePrinter . PopMarshalKind ( ) ;
1617
1617
1618
- var signature = $ "public static { variableType } { variable . Name } " ;
1618
+ bool hasInitializer = variable . Initializer != null && ! string . IsNullOrWhiteSpace ( variable . Initializer . String ) ;
1619
1619
1620
- if ( variable . Initializer != null && ! string . IsNullOrWhiteSpace ( variable . Initializer . String ) )
1621
- GeneratePropertyGetterForVariableWithInitializer ( variable , signature ) ;
1620
+ if ( hasInitializer && variable . QualifiedType . Qualifiers . IsConst &&
1621
+ ( variable . Type . Desugar ( ) is BuiltinType || variableType . ToString ( ) == "string" ) )
1622
+ Write ( $ "public const { variableType } { variable . Name } = { variable . Initializer . String } ;") ;
1622
1623
else
1623
1624
{
1624
- using ( WriteBlock ( signature ) )
1625
+ var signature = $ "public static { variableType } { variable . Name } ";
1626
+
1627
+ if ( hasInitializer )
1628
+ GeneratePropertyGetterForVariableWithInitializer ( variable , signature ) ;
1629
+ else
1625
1630
{
1626
- GeneratePropertyGetter ( variable , @class ) ;
1631
+ using ( WriteBlock ( signature ) )
1632
+ {
1633
+ GeneratePropertyGetter ( variable , @class ) ;
1627
1634
1628
- if ( ! variable . QualifiedType . Qualifiers . IsConst &&
1629
- ! ( variable . Type . Desugar ( ) is ArrayType ) )
1630
- GeneratePropertySetter ( variable , @class ) ;
1635
+ if ( ! variable . QualifiedType . Qualifiers . IsConst &&
1636
+ ! ( variable . Type . Desugar ( ) is ArrayType ) )
1637
+ GeneratePropertySetter ( variable , @class ) ;
1638
+ }
1631
1639
}
1632
1640
}
1633
1641
@@ -1780,7 +1788,7 @@ public void GenerateVTable(Class @class)
1780
1788
return __vtables;
1781
1789
}}
1782
1790
1783
- set {{
1791
+ set {{
1784
1792
__vtables = value;
1785
1793
}}" , trimIndentation : true ) ;
1786
1794
}
@@ -2296,13 +2304,13 @@ private void GenerateDisposeMethods(Class @class)
2296
2304
// Normally, calling the native dtor should be controlled by whether or not we
2297
2305
// we own the underlying instance. (i.e. Helpers.OwnsNativeInstanceIdentifier).
2298
2306
// However, there are 2 situations when the caller needs to have direct control
2299
- //
2307
+ //
2300
2308
// 1. When we have a virtual dtor on the native side we detour the vtable entry
2301
2309
// even when we don't own the underlying native instance. I think we do this
2302
2310
// so that the managed side can null out the __Instance pointer and remove the
2303
2311
// instance from the NativeToManagedMap. Of course, this is somewhat half-hearted
2304
2312
// since we can't/don't do this when there's no virtual dtor available to detour.
2305
- // Anyway, we must be able to call the native dtor in this case even if we don't
2313
+ // Anyway, we must be able to call the native dtor in this case even if we don't
2306
2314
/// own the underlying native instance.
2307
2315
//
2308
2316
// 2. When we we pass a disposable object to a function "by value" then the callee
@@ -2313,7 +2321,7 @@ private void GenerateDisposeMethods(Class @class)
2313
2321
// ....
2314
2322
// compiler generates call to f.dtor() at the end of function
2315
2323
// }
2316
- //
2324
+ //
2317
2325
// IDisposable.Dispose() and Object.Finalize() set callNativeDtor = Helpers.OwnsNativeInstanceIdentifier
2318
2326
WriteLine ( "if (callNativeDtor)" ) ;
2319
2327
if ( @class . IsDependent || dtor . IsVirtual )
@@ -2334,7 +2342,7 @@ c is ClassTemplateSpecialization ?
2334
2342
}
2335
2343
2336
2344
// If we have any fields holding references to unmanaged memory allocated here, free the
2337
- // referenced memory. Don't rely on testing if the field's IntPtr is IntPtr.Zero since
2345
+ // referenced memory. Don't rely on testing if the field's IntPtr is IntPtr.Zero since
2338
2346
// unmanaged memory isn't always initialized and/or a reference may be owned by the
2339
2347
// native side.
2340
2348
//
@@ -2540,7 +2548,7 @@ private void Generate__CopyValue(Class @class, string @internal)
2540
2548
using ( WriteBlock ( $ "private static void* __CopyValue({ @internal } native)") )
2541
2549
{
2542
2550
var copyCtorMethod = @class . Methods . FirstOrDefault ( method => method . IsCopyConstructor ) ;
2543
-
2551
+
2544
2552
if ( @class . HasNonTrivialCopyConstructor && copyCtorMethod != null && copyCtorMethod . IsGenerated )
2545
2553
{
2546
2554
// Allocate memory for a new native object and call the ctor.
@@ -2848,7 +2856,7 @@ private void GenerateEquals(Class @class)
2848
2856
private void GenerateGetHashCode ( Class @class )
2849
2857
{
2850
2858
using ( WriteBlock ( "public override int GetHashCode()" ) )
2851
- {
2859
+ {
2852
2860
if ( ! @class . IsRefType )
2853
2861
WriteLine ( $ "return { Helpers . InstanceIdentifier } .GetHashCode();") ;
2854
2862
else
@@ -2999,7 +3007,7 @@ private void GenerateClassConstructor(Method method, Class @class)
2999
3007
// Copy any string references owned by the source to the new instance so we
3000
3008
// don't have to ref count them.
3001
3009
// If there is no property or no setter then this instance can never own the native
3002
- // memory. Worry about the case where there's only a setter (write-only) when we
3010
+ // memory. Worry about the case where there's only a setter (write-only) when we
3003
3011
// understand the use case and how it can occur.
3004
3012
foreach ( var prop in @class . GetConstCharFieldProperties ( ) )
3005
3013
{
0 commit comments