Skip to content
This repository was archived by the owner on Dec 12, 2024. It is now read-only.

Commit 5e9ce30

Browse files
Merge pull request #437 from xamarin/redo-ISwiftStruct-Enum-implementations
Changed code to not implement ISwiftStruct/ISwiftEnum manually
2 parents a2653dc + 032c1fe commit 5e9ce30

File tree

1 file changed

+22
-83
lines changed

1 file changed

+22
-83
lines changed

SwiftReflector/NewClassCompiler.cs

Lines changed: 22 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,8 @@ CSClass CompileEnumClass (EnumDeclaration enumDecl, string enumCaseName, CSEnum
835835
pinvokes.Add (enumPI);
836836
var usedPinvokeNames = new List<string> ();
837837

838+
use.AddIfNotPresent (typeof (SwiftNativeValueType));
839+
enumClass.Inheritance.Add (typeof (SwiftNativeValueType));
838840
use.AddIfNotPresent (typeof (ISwiftEnum));
839841
enumClass.Inheritance.Add (typeof (ISwiftEnum));
840842
var enumContents = moduleInventory.FindClass (enumDecl.ToFullyQualifiedName (true));
@@ -843,7 +845,6 @@ CSClass CompileEnumClass (EnumDeclaration enumDecl, string enumCaseName, CSEnum
843845
}
844846
AddGenerics (enumClass, enumDecl, enumContents, use);
845847

846-
ReserveFieldSpace (enumClass);
847848
string witName = enumContents.WitnessTable.ValueWitnessTable != null ? enumContents.WitnessTable.ValueWitnessTable.MangledName.Substring (1) : "";
848849
string nomSym = enumContents.TypeDescriptor.MangledName.Substring (1);
849850
string metaDataSym = enumContents.DirectMetadata != null ? enumContents.DirectMetadata.MangledName.Substring (1) : "";
@@ -852,7 +853,7 @@ CSClass CompileEnumClass (EnumDeclaration enumDecl, string enumCaseName, CSEnum
852853

853854
string libPath = PInvokeName (wrapper.ModuleLibPath, swiftLibPath);
854855

855-
ImplementNominalIDisposable (enumClass, use);
856+
ImplementValueTypeIDisposable (enumClass, use);
856857
AddInheritedProtocols (enumDecl, enumClass, enumContents, PInvokeName (swiftLibPath), use, errors);
857858

858859
ImplementMethods (enumClass, enumPI, usedPinvokeNames, swiftEnumName, classContents, enumDecl, use, wrapper, tlf => true, swiftLibPath, errors);
@@ -944,7 +945,7 @@ CSClass CompileEnumClass (EnumDeclaration enumDecl, string enumCaseName, CSEnum
944945

945946
CompileInnerNominalsInto (enumDecl, enumClass, moduleInventory, use, wrapper, swiftLibPath, pinvokes, errors);
946947

947-
TypeNameAttribute (enumDecl).AttachBefore (enumClass);
948+
TypeNameAttribute (enumDecl, use).AttachBefore (enumClass);
948949
return enumClass;
949950
}
950951

@@ -1100,17 +1101,6 @@ void CompileEnumPayload(CSClass cl, EnumDeclaration enumDecl, EnumElement elemen
11001101
cl.Properties.Add (payloadProperty);
11011102
}
11021103

1103-
1104-
1105-
static void ReserveFieldSpace (CSClass enumStruct)
1106-
{
1107-
// public byte[] SwiftData { get; set; }
1108-
1109-
enumStruct.Properties.Add (new CSProperty (new CSSimpleType ("byte", true),
1110-
CSMethodKind.None, new CSIdentifier ("SwiftData"), CSVisibility.Public, new CSCodeBlock (), CSVisibility.Public, new CSCodeBlock ()));
1111-
1112-
}
1113-
11141104
static TLFunction FindEnumCaseFinderWrapper (EnumDeclaration enumDecl, WrappingResult wrapper)
11151105
{
11161106
string caseFinderName = MethodWrapping.EnumCaseFinderWrapperName (enumDecl);
@@ -1401,7 +1391,8 @@ CSEnum CompileTrivialEnum (EnumDeclaration enumDecl, ModuleInventory module, CSU
14011391
currentRawValue++;
14021392
csEnum.Values.Add (enumBinding);
14031393
}
1404-
TypeNameAttribute (enumDecl).AttachBefore (csEnum);
1394+
1395+
TypeNameAttribute (enumDecl, use).AttachBefore (csEnum);
14051396
return csEnum;
14061397
}
14071398

@@ -1491,12 +1482,13 @@ CSClass CompileFinalStruct (StructDeclaration structDecl, ModuleInventory modInv
14911482
var picl = new CSClass (CSVisibility.Internal, PIClassName (swiftClassName));
14921483
var usedPinvokeNames = new List<string> ();
14931484

1485+
use.AddIfNotPresent (typeof (SwiftNativeValueType));
1486+
st.Inheritance.Add (typeof (SwiftNativeValueType));
14941487
use.AddIfNotPresent (typeof (ISwiftStruct));
14951488
st.Inheritance.Add (typeof (ISwiftStruct));
14961489
pinvokes.Add (picl);
14971490
AddGenerics (st, structDecl, classContents, use);
1498-
ReserveFieldSpace (st);
1499-
ImplementNominalIDisposable (st, use);
1491+
ImplementValueTypeIDisposable (st, use);
15001492

15011493
CompileInnerNominalsInto (structDecl, st, modInventory, use, wrapper, swiftLibraryPath, pinvokes, errors);
15021494

@@ -1509,7 +1501,7 @@ CSClass CompileFinalStruct (StructDeclaration structDecl, ModuleInventory modInv
15091501
classContents.WitnessTable.ValueWitnessTable.MangledName.Substring (1) :
15101502
"";
15111503
MakeSwiftStructTypeAttribute (PInvokeName (swiftLibraryPath),
1512-
nomSym, metaDataSym, witSym).AttachBefore (st);
1504+
nomSym, metaDataSym, witSym, use).AttachBefore (st);
15131505

15141506
var ctors = MakeStructConstructors (st, picl, usedPinvokeNames, structDecl, classContents,
15151507
use, st.ToCSType (), wrapper, swiftLibraryPath, errors);
@@ -1526,7 +1518,7 @@ CSClass CompileFinalStruct (StructDeclaration structDecl, ModuleInventory modInv
15261518
ImplementProperties (st, picl, usedPinvokeNames, structDecl, classContents, null, use, wrapper, true, false, tlf => true, swiftLibraryPath, errors);
15271519
ImplementSubscripts (st, picl, usedPinvokeNames, structDecl.AllSubscripts (), classContents, null, use, wrapper, true, tlf => true, swiftLibraryPath, errors);
15281520

1529-
TypeNameAttribute (structDecl).AttachBefore (st);
1521+
TypeNameAttribute (structDecl, use).AttachBefore (st);
15301522
return st;
15311523
}
15321524

@@ -1846,7 +1838,7 @@ CSInterface CompileInterfaceAndProxy (ProtocolDeclaration protocolDecl, ModuleIn
18461838
ImplementProxyConstructorAndFields (proxyClass, use, hasVtable, iface, false);
18471839
}
18481840

1849-
TypeNameAttribute (protocolDecl).AttachBefore (iface);
1841+
TypeNameAttribute (protocolDecl, use).AttachBefore (iface);
18501842
return iface;
18511843
}
18521844

@@ -1987,7 +1979,7 @@ CSClass CompileVirtualClass (ClassDeclaration classDecl, ModuleInventory modInve
19871979
if (wrapUse != null)
19881980
use.Remove (wrapUse);
19891981

1990-
TypeNameAttribute (classDecl).AttachBefore (cl);
1982+
TypeNameAttribute (classDecl, use).AttachBefore (cl);
19911983
return cl;
19921984
}
19931985

@@ -3099,7 +3091,7 @@ CSClass CompileFinalClass (ClassDeclaration classDecl, ModuleInventory modInvent
30993091
ImplementProperties (cl, picl, usedPinvokeNames, classDecl, classContents, null, use, wrapper, false, false, tlf => true, swiftLibraryPath, errors);
31003092
ImplementSubscripts (cl, picl, usedPinvokeNames, classDecl.AllSubscripts (), classContents, null, use, wrapper, true, tlf => true, swiftLibraryPath, errors);
31013093

3102-
TypeNameAttribute (classDecl).AttachBefore (cl);
3094+
TypeNameAttribute (classDecl, use).AttachBefore (cl);
31033095
return cl;
31043096
}
31053097

@@ -3511,62 +3503,8 @@ void ImplementObjCClassField (CSClass cl)
35113503
cl.Properties.Add (prop);
35123504
}
35133505

3514-
void ImplementNominalIDisposable (CSClass cl, CSUsingPackages use)
3506+
void ImplementValueTypeIDisposable (CSClass cl, CSUsingPackages use)
35153507
{
3516-
//public void Dispose()
3517-
//{
3518-
// Dispose(true);
3519-
// GC.SuppressFinalize(this);
3520-
//}
3521-
var disposeID = new CSIdentifier ("Dispose");
3522-
var disp1Body = new CSCodeBlock ();
3523-
var disp1 = new CSMethod (CSVisibility.Public, CSMethodKind.None, CSSimpleType.Void,
3524-
disposeID, new CSParameterList (), disp1Body);
3525-
disp1Body.Add (CSFunctionCall.FunctionCallLine (disposeID, false, CSConstant.Val (true)));
3526-
use.AddIfNotPresent (typeof (GC));
3527-
disp1Body.Add (CSFunctionCall.FunctionCallLine ("GC.SuppressFinalize", false, CSIdentifier.This));
3528-
cl.Methods.Add (disp1);
3529-
3530-
//private void Dispose(bool disposing)
3531-
//{
3532-
// if (SwiftData != null)
3533-
// {
3534-
// unsafe
3535-
// {
3536-
// fixed (byte* p = SwiftData)
3537-
// {
3538-
// StructMarshal.Marshaler.NominalDestroy(typeof(this), p);
3539-
// }
3540-
// SwiftData = null;
3541-
// }
3542-
// }
3543-
//}
3544-
var swiftDataID = new CSIdentifier ("SwiftData");
3545-
var nullID = new CSIdentifier ("null");
3546-
var disp2body = new CSCodeBlock ();
3547-
var disposingID = new CSIdentifier ("disposing");
3548-
var disp2 = new CSMethod (CSVisibility.None, CSMethodKind.None, CSSimpleType.Void,
3549-
disposeID, new CSParameterList (new CSParameter (CSSimpleType.Bool, disposingID)),
3550-
disp2body);
3551-
var bytestarID = new CSIdentifier ("p");
3552-
var fixedBody = new CSCodeBlock ();
3553-
var fixedBlock = new CSFixedCodeBlock (CSSimpleType.ByteStar, bytestarID, swiftDataID, fixedBody);
3554-
use.AddIfNotPresent (typeof (StructMarshal));
3555-
fixedBlock.Add (CSFunctionCall.FunctionCallLine ("StructMarshal.Marshaler.NominalDestroy", false,
3556-
new CSFunctionCall ("typeof", false, new CSIdentifier (cl.ToCSType ().ToString ())),
3557-
bytestarID));
3558-
3559-
var unsafeBlock = new CSUnsafeCodeBlock (null);
3560-
unsafeBlock.Add (fixedBlock);
3561-
unsafeBlock.Add (CSAssignment.Assign (swiftDataID, CSAssignmentOperator.Assign, nullID));
3562-
3563-
var ifBlock = new CSCodeBlock ();
3564-
var ifelse = new CSIfElse (swiftDataID != nullID, ifBlock);
3565-
ifBlock.Add (unsafeBlock);
3566-
disp2body.Add (ifelse);
3567-
3568-
cl.Methods.Add (disp2);
3569-
35703508
ImplementFinalizer (cl);
35713509
}
35723510

@@ -3846,10 +3784,10 @@ IEnumerable<CSMethod> MakeStructConstructors (CSClass st, CSClass picl, List<str
38463784
foreach (CSMethod m in StructConstructorToMethod (structDecl, funcDecl, st, picl, usedPinvokeNames, csStructType, classContents, tlf, use, wrapper, swiftLibraryPath, errors))
38473785
yield return m;
38483786
}
3849-
yield return NominalDefaultConstructor (csStructType, classContents, use);
3787+
yield return ValueTypeDefaultConstructor (csStructType, classContents, use);
38503788
}
38513789

3852-
CSMethod NominalDefaultConstructor (CSType structType, ClassContents classContents, CSUsingPackages use)
3790+
CSMethod ValueTypeDefaultConstructor (CSType structType, ClassContents classContents, CSUsingPackages use)
38533791
{
38543792
var parms = new CSParameterList ();
38553793
use.AddIfNotPresent (typeof (SwiftValueTypeCtorArgument));
@@ -3858,8 +3796,7 @@ CSMethod NominalDefaultConstructor (CSType structType, ClassContents classConten
38583796

38593797
string consName = StubbedClassName (classContents.Name);
38603798

3861-
var ctor = new CSMethod (CSVisibility.Internal, CSMethodKind.None, null, new CSIdentifier (consName), parms, body);
3862-
body.Add (CSFunctionCall.FunctionCallLine ("StructMarshal.Marshaler.PrepareValueType", false, CSIdentifier.This));
3799+
var ctor = new CSMethod (CSVisibility.Internal, CSMethodKind.None, null, new CSIdentifier (consName), parms, new CSBaseExpression [0], true, body);
38633800

38643801
return ctor;
38653802
}
@@ -6066,8 +6003,9 @@ static CSAttribute MakeNominalTypeAttribute (string library, string nominalSym,
60666003
return CSAttribute.FromAttr (typeof (SwiftValueTypeAttribute), al, true);
60676004
}
60686005

6069-
static CSAttribute MakeSwiftStructTypeAttribute (string library, string nominalSym, string metaSym, string witnessSym)
6006+
static CSAttribute MakeSwiftStructTypeAttribute (string library, string nominalSym, string metaSym, string witnessSym, CSUsingPackages use)
60706007
{
6008+
use.AddIfNotPresent (typeof (SwiftStructAttribute));
60716009
var al = new CSArgumentList ();
60726010
al.Add (CSConstant.Val (library));
60736011
al.Add (CSConstant.Val (nominalSym));
@@ -6297,8 +6235,9 @@ static bool JustTheTypeNamesMatch (SwiftType st, TypeSpec sp)
62976235
return ct.ClassName.ToFullyQualifiedName (false) == named.NameWithoutModule;
62986236
}
62996237

6300-
static CSAttribute TypeNameAttribute (BaseDeclaration decl)
6238+
static CSAttribute TypeNameAttribute (BaseDeclaration decl, CSUsingPackages use)
63016239
{
6240+
use.AddIfNotPresent (typeof (SwiftTypeNameAttribute));
63026241
var argList = new CSArgumentList ();
63036242
argList.Add (new CSArgument (CSConstant.Val (decl.ToFullyQualifiedName ())));
63046243
return CSAttribute.FromAttr (typeof (SwiftTypeNameAttribute), argList);

0 commit comments

Comments
 (0)