public void StructInfixOpTest()
        {
            var swiftCode = @"
public struct IntRep {
    public init (with: Int32) {
		val = with
	}
    public var val:Int32 = 0
}

infix operator %^^% : AdditionPrecedence
public func %^^% (left:IntRep, right: IntRep) -> IntRep {
    return IntRep (with: left.val + right.val)
}
";

            var leftID     = new CSIdentifier("left");
            var leftDecl   = CSVariableDeclaration.VarLine(CSSimpleType.Var, leftID, new CSFunctionCall("IntRep", true, CSConstant.Val(3)));
            var rightID    = new CSIdentifier("right");
            var rightDecl  = CSVariableDeclaration.VarLine(CSSimpleType.Var, rightID, new CSFunctionCall("IntRep", true, CSConstant.Val(4)));
            var resultID   = new CSIdentifier("result");
            var resultDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, resultID, new CSFunctionCall("TopLevelEntities.InfixOperatorPercentHatHatPercent", false, leftID, rightID));
            var printer    = CSFunctionCall.ConsoleWriteLine(resultID.Dot(new CSIdentifier("Val")));

            var callingCode = new CodeElementCollection <ICodeElement> {
                leftDecl, rightDecl, resultDecl, printer
            };

            TestRunning.TestAndExecute(swiftCode, callingCode, "7\n");
        }
        public void ClassInlinePostfixOperator()
        {
            var swiftCode = @"
postfix operator ^*--*^

public class NumRep1 {
    public init (a: Int) {
        val = a
    }
    public var val: Int
    public static postfix func ^*--*^(val: NumRep1) -> NumRep1 {
        return NumRep1 (a: -val.val)
    }
}";

            var leftID     = new CSIdentifier("left");
            var leftDecl   = CSVariableDeclaration.VarLine(CSSimpleType.Var, leftID, new CSFunctionCall("NumRep1", true, CSConstant.Val(4)));
            var resultID   = new CSIdentifier("result");
            var resultDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, resultID, new CSFunctionCall("NumRep1.PostfixOperatorHatStarMinusMinusStarHat", false, leftID));
            var printer    = CSFunctionCall.ConsoleWriteLine(resultID.Dot(new CSIdentifier("Val")));

            var callingCode = new CodeElementCollection <ICodeElement> {
                leftDecl, resultDecl, printer
            };

            TestRunning.TestAndExecute(swiftCode, callingCode, "-4\n");
        }
        public void StructPostfixOpTest()
        {
            var swiftCode = @"
public struct IntRep2 {
    public init (with: Int32) {
		val = with
	}
    public var val:Int32 = 0
}

prefix operator %--% 
public prefix func %--% (left:IntRep2) -> IntRep2 {
    return IntRep2 (with: left.val - 1)
}
";

            var leftID     = new CSIdentifier("left");
            var leftDecl   = CSVariableDeclaration.VarLine(CSSimpleType.Var, leftID, new CSFunctionCall("IntRep2", true, CSConstant.Val(3)));
            var resultID   = new CSIdentifier("result");
            var resultDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, resultID, new CSFunctionCall("TopLevelEntities.PrefixOperatorPercentMinusMinusPercent", false, leftID));
            var printer    = CSFunctionCall.ConsoleWriteLine(resultID.Dot(new CSIdentifier("Val")));

            var callingCode = new CodeElementCollection <ICodeElement> {
                leftDecl, resultDecl, printer
            };

            TestRunning.TestAndExecute(swiftCode, callingCode, "2\n");
        }
        public void TestExistentialContainer()
        {
            var swiftCode = @"
public protocol SomeProtocol {
	func foo ()
}
";
            // var ocsty = typeof (ISomeProtocol);
            // var mt = StructMarshal.Marshaler.ExistentialMetatypeof (ocsty);
            // Type csty;
            // SwiftTypeRegistry.Registry.TryGetValue (mt, out csty);
            // Console.WriteLine (csty.Name);
            // Console.WriteLine (csty == ocsty);
            var ocsTypeID = new CSIdentifier("ocsty");
            var csTypeID  = new CSIdentifier("csty");
            var mtID      = new CSIdentifier("mt");

            var ocstyDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, ocsTypeID, new CSSimpleType("ISomeProtocol").Typeof());
            var mtDecl    = CSVariableDeclaration.VarLine(CSSimpleType.Var, mtID,
                                                          new CSFunctionCall("StructMarshal.Marshaler.ExistentialMetatypeof", false, ocsTypeID));
            var cstyDecl   = CSVariableDeclaration.VarLine(CSSimpleType.Type, csTypeID);
            var tryGetLine = CSFunctionCall.FunctionCallLine("SwiftTypeRegistry.Registry.TryGetValue", false,
                                                             mtID, new CSUnaryExpression(CSUnaryOperator.Out, csTypeID));
            var print1      = CSFunctionCall.ConsoleWriteLine(ocsTypeID.Dot(new CSIdentifier("Name")));
            var print2      = CSFunctionCall.ConsoleWriteLine(csTypeID == ocsTypeID);
            var callingCode = CSCodeBlock.Create(ocstyDecl, mtDecl, cstyDecl, tryGetLine, print1, print2);

            TestRunning.TestAndExecute(swiftCode, callingCode, "ISomeProtocol\nTrue\n", platform: PlatformName.macOS);
        }
        public void CanGetProtocolConformanceDesc()
        {
            // var nomDesc = SwiftProtocolTypeAttribute.DescriptorForType (typeof (IIteratorProtocol<>));
            // var witTable = SwiftCore.ConformsToSwiftProtocol (StructMarshal.Marshaler.Metatypeof (typeof (SwiftIteratorProtocolProxy<nint>)),
            //                                          nomDesc);
            // Console.WriteLine (confDesc != IntPtr.Zero);

            var swiftCode  = @"
public func canGetProtocolConfDesc () {
}
";
            var nomDescID  = new CSIdentifier("nomDesc");
            var witTableID = new CSIdentifier("witTable");

            var nomDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, nomDescID,
                                                        new CSFunctionCall("SwiftProtocolTypeAttribute.DescriptorForType", false, new CSSimpleType("IIteratorProtocol<>").Typeof()));

            var metaTypeCall = new CSFunctionCall("StructMarshal.Marshaler.Metatypeof", false, new CSSimpleType("SwiftIteratorProtocolProxy<nint>").Typeof());
            var confDescDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, witTableID,
                                                             new CSFunctionCall("SwiftCore.ConformsToSwiftProtocol", false, metaTypeCall, nomDescID));
            var printer = CSFunctionCall.ConsoleWriteLine(witTableID.Dot(new CSIdentifier("Handle")) != new CSIdentifier("IntPtr.Zero"));

            var callingCode = CSCodeBlock.Create(nomDecl, confDescDecl, printer);

            TestRunning.TestAndExecute(swiftCode, callingCode, "True\n");
        }
        public void TestDictionary()
        {
            var swiftCode = @"
public func dictionaryTestDoesNothing () {
}";

            // var ocsty = typeof (SwiftOptional<bool>);
            // var mt = StructMarshal.Marshaler.Metatypeof (ocsty);
            // Type csty;
            // SwiftTypeRegistry.Registry.TryGetValue (mt, out csty);
            // Console.WriteLine (csty.Name);
            // Console.WriteLine (genargs.Length);
            // Console.WriteLine (genargs[1].Name);
            // Console.WriteLine (csty == ocsty);
            var ocsTypeID = new CSIdentifier("ocsty");
            var csTypeID  = new CSIdentifier("csty");
            var mtID      = new CSIdentifier("mt");

            var ocstyDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, ocsTypeID, new CSSimpleType("SwiftDictionary", false, CSSimpleType.Bool, CSSimpleType.Int).Typeof());
            var mtDecl    = CSVariableDeclaration.VarLine(CSSimpleType.Var, mtID,
                                                          new CSFunctionCall("StructMarshal.Marshaler.Metatypeof", false, ocsTypeID));
            var cstyDecl   = CSVariableDeclaration.VarLine(CSSimpleType.Type, csTypeID);
            var tryGetLine = CSFunctionCall.FunctionCallLine("SwiftTypeRegistry.Registry.TryGetValue", false,
                                                             mtID, new CSUnaryExpression(CSUnaryOperator.Out, csTypeID));
            var print1      = CSFunctionCall.ConsoleWriteLine(ocsTypeID.Dot(new CSIdentifier("Name")));
            var genargsID   = new CSIdentifier("genargs");
            var genArgsDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, genargsID, new CSFunctionCall($"{csTypeID.Name}.GetGenericArguments", false));
            var print2      = CSFunctionCall.ConsoleWriteLine(genargsID.Dot(new CSIdentifier("Length")));
            var print3      = CSFunctionCall.ConsoleWriteLine(new CSIdentifier($"{genargsID.Name} [0].Name"));
            var print4      = CSFunctionCall.ConsoleWriteLine(csTypeID == ocsTypeID);
            var callingCode = CSCodeBlock.Create(ocstyDecl, mtDecl, cstyDecl, tryGetLine, print1, genArgsDecl, print2, print3, print4);

            TestRunning.TestAndExecute(swiftCode, callingCode, "SwiftDictionary`2\n2\nBoolean\nTrue\n");
        }
Ejemplo n.º 7
0
        public void VirtualOpenPropStruct(PlatformName platform)
        {
            string swiftCode =
                $"import Foundation\nopen class AnotherOpenVirtualClass{platform} {{\n\tpublic init () {{ }}\n\topen var OSVersion = OperatingSystemVersion (majorVersion: 1, minorVersion:2, patchVersion: 3)\n}}\n";

            // var cl = new AnotherVirtualClass ();
            // var vers = cl.OSVersion;
            // Console.WriteLine(vers);
            // vers.Major = 5;
            // cl.OSVersion = vers;
            // vers = cl.OSVersion;
            // Console.WriteLine(vers);
            var versID    = new CSIdentifier("vers");
            var clID      = new CSIdentifier("cl");
            var osverExpr = clID.Dot(new CSIdentifier("OSVersion"));
            var clDecl    = CSVariableDeclaration.VarLine(CSSimpleType.Var, clID, new CSFunctionCall($"AnotherOpenVirtualClass{platform}", true));
            var versDecl  = CSVariableDeclaration.VarLine(CSSimpleType.Var, versID, osverExpr);
            var printer   = CSFunctionCall.ConsoleWriteLine(versID);
            var setMajor  = CSAssignment.Assign(versID.Dot(new CSIdentifier("Major")), CSConstant.Val(5));
            var setOSVer  = CSAssignment.Assign(osverExpr, versID);
            var resetVer  = CSAssignment.Assign(versID, osverExpr);

            var callingCode = CSCodeBlock.Create(clDecl, versDecl, printer, setMajor, setOSVer, resetVer, printer);

            TestRunning.TestAndExecute(swiftCode, callingCode, "1.2.3\n5.2.3\n", platform: platform);
        }
Ejemplo n.º 8
0
        public void NSObjectSubclassableMethodTest3()
        {
            string swiftCode =
                "import Foundation\n" +
                "@objc\n" +
                "open class Subclassable3 : NSObject {\n" +
                "   public override init () { }\n" +
                "   open var returnsTrue:Bool {\n" +
                "       get { return true\n } " +
                "   }\n" +
                "}\n" +
                "public func callIt (a: Subclassable3) -> Bool {\n" +
                "    return a.returnsTrue\n" +
                "}\n";


            var theSub = new CSClass(CSVisibility.Public, "TheSub3");
            var ctor   = new CSMethod(CSVisibility.Public, CSMethodKind.None, null, theSub.Name,
                                      new CSParameterList(), new CSBaseExpression [0], true, new CSCodeBlock());

            theSub.Constructors.Add(ctor);
            theSub.Inheritance.Add(new CSIdentifier("Subclassable3"));

            var theBody = new CSCodeBlock();

            theBody.Add(CSReturn.ReturnLine(CSConstant.Val(false)));

            LineCodeElementCollection <ICodeElement> getCode =
                new LineCodeElementCollection <ICodeElement> (
                    new ICodeElement [] {
                CSReturn.ReturnLine(CSConstant.Val(false))
            }, false, true);
            CSProperty returnsFalse = new CSProperty(CSSimpleType.Bool, CSMethodKind.Override, new CSIdentifier("ReturnsTrue"),
                                                     CSVisibility.Public, new CSCodeBlock(getCode),
                                                     CSVisibility.Public, null);

            theSub.Properties.Add(returnsFalse);


            var callingCode = new CodeElementCollection <ICodeElement> ();
            var objID       = new CSIdentifier("subTest");
            var objDecl     = CSVariableDeclaration.VarLine(CSSimpleType.Var, objID, new CSFunctionCall("TheSub3", true));
            var call        = CSFunctionCall.ConsoleWriteLine(objID.Dot((CSIdentifier)"ReturnsTrue"));
            var call2       = CSFunctionCall.ConsoleWriteLine(CSFunctionCall.Function("TopLevelEntities.CallIt", objID));

            callingCode.Add(objDecl);
            callingCode.Add(call);
            callingCode.Add(call2);

            TestRunning.TestAndExecute(swiftCode, callingCode, "False\nFalse\n", otherClass: theSub, platform: PlatformName.macOS);
        }
Ejemplo n.º 9
0
        void TestObjectType(string type, string expectedOutput, PlatformName platform)
        {
            string source =
// no-op
                $"public func GetType{type} () {{\n" +
                "}\n";
            string testName    = $"TestObjectType{type}";
            var    mtID        = new CSIdentifier("mt");
            var    mtDecl      = CSVariableDeclaration.VarLine(CSSimpleType.Var, mtID, new CSFunctionCall("StructMarshal.Marshaler.Metatypeof", false, new CSSimpleType(type).Typeof()));
            var    printer     = CSFunctionCall.ConsoleWriteLine(mtID.Dot(new CSIdentifier("Kind")));
            var    callingCode = CSCodeBlock.Create(mtDecl, printer);

            TestRunning.TestAndExecute(source, callingCode, expectedOutput, testName: testName, platform: platform);
        }
        public void TestAllocCount(string tag)
        {
            var swiftCode = $"public func neverUsed{tag}() {{\n}}\n";
            var ptrID     = new CSIdentifier("ptr");
            var ptrDecl   = CSVariableDeclaration.VarLine(CSSimpleType.Var, ptrID,
                                                          new CSFunctionCall("System.Runtime.InteropServices.Marshal.AllocHGlobal", false, CSConstant.Val(128)));
            var clID   = new CSIdentifier("ubp");
            var clDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, clID, new CSFunctionCall(tag, true, ptrID, CSConstant.Val(128)));

            var printer     = CSFunctionCall.ConsoleWriteLine(clID.Dot(new CSIdentifier("Count")));
            var cleanUp     = CSFunctionCall.FunctionCallLine("System.Runtime.InteropServices.Marshal.FreeHGlobal", false, ptrID);
            var callingCode = CSCodeBlock.Create(ptrDecl, clDecl, printer, cleanUp);

            TestRunning.TestAndExecute(swiftCode, callingCode, "128\n", testName: "TestAllocCount" + tag);
        }
Ejemplo n.º 11
0
        public void TrivialEnumMarshalProp()
        {
            var swiftCode   = @"
import Foundation
@objc
public enum TrivEnum0 : Int {
	case None, Blind, Free
}
open class TrivClass0 {
	public init () { }
	open var X : TrivEnum0 = .Blind
}";
            var declID      = new CSIdentifier("cl");
            var decl        = CSVariableDeclaration.VarLine(CSSimpleType.Var, declID, new CSFunctionCall("TrivClass0", true));
            var printer     = CSFunctionCall.ConsoleWriteLine(declID.Dot(new CSIdentifier("X")));
            var callingCode = CSCodeBlock.Create(decl, printer);

            TestRunning.TestAndExecute(swiftCode, callingCode, "Blind\n", platform: PlatformName.macOS);
        }
        public void EnumInfixOpTest()
        {
            var swiftCode  = @"
public enum IntOrFloat {
	case IntVal(Int)
	case FloatVal(Float)
}
infix operator ^++^ : AdditionPrecedence
public func ^++^ (left: IntOrFloat, right: IntOrFloat) -> IntOrFloat {
    switch left {
    case .IntVal(let x):
        switch right {
        case .IntVal(let y):
            return .IntVal(x + y)
        case .FloatVal(let y):
            return .FloatVal(Float(x) + y)
        }
    case .FloatVal(let x):
        switch right {
        case .IntVal(let y):
            return .FloatVal(x + Float(y))
        case .FloatVal(let y):
            return .FloatVal(x + y)
        }
    }
}
";
            var leftID     = new CSIdentifier("left");
            var leftDecl   = CSVariableDeclaration.VarLine(CSSimpleType.Var, leftID, new CSFunctionCall("IntOrFloat.NewIntVal", false, CSConstant.Val(3)));
            var rightID    = new CSIdentifier("right");
            var rightDecl  = CSVariableDeclaration.VarLine(CSSimpleType.Var, rightID, new CSFunctionCall("IntOrFloat.NewIntVal", false, CSConstant.Val(4)));
            var resultID   = new CSIdentifier("result");
            var resultDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, resultID, new CSFunctionCall("TopLevelEntities.InfixOperatorHatPlusPlusHat", false, leftID, rightID));
            var printer    = CSFunctionCall.ConsoleWriteLine(resultID.Dot(new CSIdentifier("ValueIntVal")));

            var callingCode = new CodeElementCollection <ICodeElement> {
                leftDecl, rightDecl, resultDecl, printer
            };

            TestRunning.TestAndExecute(swiftCode, callingCode, "7\n");
        }
Ejemplo n.º 13
0
        public void VirtualPropIsVirtual()
        {
            string swiftCode =
                "open class ItsAVirtProp {\n" +
                "     open var x = 0\n" +
                "     public init (ix: Int) {\n" +
                "         x = ix\n" +
                "     }\n" +
                "}\n";

            // var pi = typeof (ItsAVirtProp).GetProperty ("X");
            // Console.WriteLine(pi.GetGetMethod().IsVirtual);
            var piID        = new CSIdentifier("pi");
            var getProp     = new CSFunctionCall("GetProperty", false, CSConstant.Val("X"));
            var propInfo    = CSVariableDeclaration.VarLine(CSSimpleType.Var, piID, new CSSimpleType("ItsAVirtProp").Typeof().Dot(getProp));
            var getGet      = new CSFunctionCall("GetGetMethod", false);
            var printer     = CSFunctionCall.ConsoleWriteLine(piID.Dot(getGet.Dot(new CSIdentifier("IsVirtual"))));
            var callingCode = CSCodeBlock.Create(propInfo, printer);

            TestRunning.TestAndExecute(swiftCode, callingCode, "True\n", platform: PlatformName.macOS);
        }
        public void WrapOptionalVirtualProp()
        {
            string swiftCode =
                "open class OptPropClass {\n" +
                "    open var x:Int? = 17\n" +
                "    public init () {\n" +
                "    }\n" +
                "    open func referenceFunc () -> Int? {\n" +
                "        return x\n" +
                "    }" +
                "}\n";

            var classID   = new CSIdentifier("cl");
            var classDecl = CSVariableDeclaration.VarLine(new CSSimpleType("OptPropClass"), classID,
                                                          new CSFunctionCall("OptPropClass", true));
            var printer = CSFunctionCall.ConsoleWriteLine(classID.Dot((CSIdentifier)"X").Dot((CSIdentifier)"Value"));

            var callingCode = CSCodeBlock.Create(classDecl, printer);

            TestRunning.TestAndExecute(swiftCode, callingCode, "17\n", platform: PlatformName.macOS);
        }
Ejemplo n.º 15
0
        public void TestAddCount(string swiftType, string cstype, string val1)
        {
            var variant   = swiftType;
            var swiftCode =
                $"public func makeSetTAC{variant}() -> Set<{swiftType}> {{\n" +
                "    return Set()\n" +
                "}\n";
            var callingCode = new CodeElementCollection <ICodeElement> ();

            var setID   = new CSIdentifier("theSet");
            var setDecl = CSVariableDeclaration.VarLine(new CSSimpleType("SwiftSet", false, new CSSimpleType(cstype)), setID,
                                                        new CSFunctionCall($"TopLevelEntities.MakeSetTAC{variant}", false));
            var countLine = CSFunctionCall.ConsoleWriteLine(setID.Dot(new CSIdentifier("Count")));
            var addLine   = CSFunctionCall.FunctionCallLine("theSet.Insert", false, new CSIdentifier(val1));

            callingCode.Add(setDecl);
            callingCode.Add(countLine);
            callingCode.Add(addLine);
            callingCode.Add(countLine);

            TestRunning.TestAndExecute(swiftCode, callingCode, "0\n1\n", testName: $"TestAddCount{variant}");
        }
        public void CanGetProtocolConformanceDescMarshal()
        {
            // var confDesc = StructMarshal.Marshaler.ProtocolConformanceof (typeof (IIteratorProtocol<>),
            //		typeof (SwiftIteratorProtocolProxy<nint>);
            // Console.WriteLine (confDesc != IntPtr.Zero);

            var swiftCode  = @"
public func canGetProtocolConfDescMarshal () {
}
";
            var confDescID = new CSIdentifier("confDesc");

            var concreteType = new CSSimpleType("SwiftIteratorProtocolProxy<nint>").Typeof();
            var ifaceType    = new CSSimpleType("IIteratorProtocol<>").Typeof();
            var confDescDecl = CSVariableDeclaration.VarLine(CSSimpleType.Var, confDescID,
                                                             new CSFunctionCall("StructMarshal.Marshaler.ProtocolConformanceof", false, ifaceType, concreteType));
            var printer = CSFunctionCall.ConsoleWriteLine(confDescID.Dot(new CSIdentifier("Handle")) != new CSIdentifier("IntPtr.Zero"));

            var callingCode = CSCodeBlock.Create(confDescDecl, printer);

            TestRunning.TestAndExecute(swiftCode, callingCode, "True\n");
        }
		public void CanGetAssociatedTypeFromAny (string swiftType, string csType, string initData, string nameSuffix = null)
		{
			nameSuffix = nameSuffix ?? swiftType;
			var swiftCode = $@"
private class FooAny{nameSuffix} : IteratorProtocol {{
	public init () {{ }}
	private var data : [{swiftType}] = {initData}
	private var x = -1
	public func next () -> {swiftType}? {{
		if x < data.count {{
			x = x + 1
			return data[x]
		}}
		else {{
			return nil
		}}
	}}
}}
public func blindAssocFuncAny{nameSuffix} () -> Any {{
	return FooAny{nameSuffix} ()
}}
";
			// var any = TopLevelEntities.BlindAssocFuncAny ();
			// var types = StructMarshal.Marshaler.GetAssociateTypes (any.ObjectMetadata, typeof (IIteratorProtocol<>), 1);
			// Console.WriteLine (types[0].Name);

			var anyID = new CSIdentifier ("any");
			var anyDecl = CSVariableDeclaration.VarLine (anyID, new CSFunctionCall ($"TopLevelEntities.BlindAssocFuncAny{nameSuffix}", false));
			var assocTypesID = new CSIdentifier ("assoc");
			var typesID = new CSIdentifier ("tyypes");
			var typesDecl = CSVariableDeclaration.VarLine (typesID, new CSFunctionCall ("StructMarshal.Marshaler.GetAssociatedTypes", false,
				anyID.Dot (new CSIdentifier ("ObjectMetadata")), new CSSimpleType ("IIteratorProtocol<>").Typeof (), CSConstant.Val (1)));
			var printer = CSFunctionCall.ConsoleWriteLine (new CSIndexExpression (typesID, false, CSConstant.Val (0)).Dot (new CSIdentifier ("Name")));

			var callingCode = CSCodeBlock.Create (anyDecl, typesDecl, printer);
			TestRunning.TestAndExecute (swiftCode, callingCode, $"{csType}\n", testName: $"CanGetAssociatedTypeFromAny{nameSuffix}");
		}