private SwiftType TranslateNullable(TypeProxy nullableType, TypeProxy innerType) { // Nullable<T> gets translated into Optional<T> var innerSwiftType = TranslateType(innerType); if (innerSwiftType == null) { return(null); } var swiftType = new SwiftOptional(innerSwiftType); _swiftTypes.Add(nullableType, swiftType); return(swiftType); }
//private SwiftType TranslateClassDefinition(Type classType) //{ //} private SwiftType TranslateClass(TypeProxy classType) { //Console.Out.WriteLine("Translating class {0}", classType.FullName); var swiftType = TranslateWellKnownType(classType); if (swiftType != null) { return(swiftType); } string name; if (classType.IsGenericType && !classType.IsGenericTypeDefinition) { var definitionType = classType.GetGenericTypeDefinition(); if (!_swiftTypesToNames.TryGetValue(definitionType, out name)) { ErrorHandler.Error("Skipping undefined generic type definition '{0}'.", definitionType.FullName); return(null); } } else { if (!_swiftTypesToNames.TryGetValue(classType, out name)) { ErrorHandler.Error("Skipping undefined class '{0}'.", classType.FullName); return(null); } } var swiftClass = new SwiftClass(name) { BriefComment = _documentation.GetTypeSummary(classType), }; _swiftTypes.Add(classType, swiftClass); var baseType = (classType.BaseType != _appDomain.ObjectType) ? classType.BaseType : null; if (baseType != null) { if (!classType.IsGenericType && baseType.IsGenericType) { // The class is not generic, but its base class is. // This isn't a valid Swift construct, so we need to convert it. // // Given these C# classes : // class Base<T> {} // class Derived : Base<int> {} // // We need to generate these Swift classes : // class Base<T> {} var baseArgs = baseType.GetGenericArguments(); var templateType = baseType.GetGenericTypeDefinition(); var templateArgs = templateType.GetGenericArguments(); for (int i = 0; i < baseArgs.Count; ++i) { var baseArg = baseArgs[i]; var templateArg = templateArgs[i]; string parameterName; if (baseArg.IsGenericParameter) { parameterName = baseArg.Name; } else { parameterName = templateArg.Name; } var swiftBaseArg = TranslateType(baseArg); if (swiftBaseArg == null) { ErrorHandler.Error("Skipping generic parameter of class '{0}' because of undefined type '{1}'.", baseType.Name, baseArg.FullName); return(null); } var swiftTemplateArg = TranslateType(templateArg); if (swiftTemplateArg == null) { ErrorHandler.Error("Skipping generic parameter of class '{0}' because of undefined type '{1}'.", templateType.Name, templateArg.FullName); return(null); } swiftClass.AddTypeParameter(parameterName, swiftTemplateArg); swiftClass.AddTypeParameterContraint(parameterName, swiftBaseArg); } baseType = templateType; } //var generic = classType.IsGenericType; //var genericTypeDef = classType.IsGenericTypeDefinition; //var base_eneric = baseType.IsGenericType; //var base_genericTypeDef = baseType.IsGenericTypeDefinition; var baseSwiftType = TranslateType(baseType); if (baseSwiftType == null) { ErrorHandler.Error("Skipping class '{0}' because of undefined base class '{1}'.", classType.FullName, baseType.FullName); return(null); } swiftClass.BaseType = baseSwiftType; } if (classType.IsGenericType) { if (classType.IsGenericTypeDefinition) { foreach (var typeArg in classType.GetGenericArguments()) { if (typeArg.IsGenericParameter) { swiftClass.AddTypeParameter(new SwiftPlaceholder(typeArg.Name)); foreach (var constraintType in typeArg.GetGenericParameterConstraints()) { if (!constraintType.IsClass) { continue; } // A constraint on 'struct' is expressed as a constraint on System.ValueType. if (constraintType == _appDomain.ValueType) { continue; } var constraintSwiftClass = TranslateType(constraintType); swiftClass.AddTypeParameterContraint(typeArg.Name, constraintSwiftClass); break; } } else { throw new InvalidOperationException(); } } } else { var templateType = classType.GetGenericTypeDefinition(); var templateArgs = templateType.GetGenericArguments(); var classArgs = classType.GetGenericArguments(); for (int i = 0; i < classArgs.Count; ++i) { var classArg = classArgs[i]; var templateArg = templateArgs[i]; string parameterName; if (classArg.IsGenericParameter) { parameterName = classArg.Name; } else { parameterName = templateArg.Name; } var swiftClassArg = TranslateType(classArg); if (swiftClassArg == null) { ErrorHandler.Error("Skipping generic parameter of class '{0}' because of undefined type '{1}'.", classType.Name, classArg.FullName); continue; } swiftClass.AddTypeParameter(parameterName, swiftClassArg); } } //foreach (var typeArg in classType.GetGenericArguments()) //{ // var swiftTypeArg = TranslateType(typeArg); // if (swiftTypeArg == null) // { // ErrorHandler.Error("Skipping generic parameter of class '{0}' because of undefined class '{1}'.", classType.Name, typeArg.FullName); // continue; // } // //swiftClass.AddTypeParameter(typeArg.Name, swiftTypeArg); //} } foreach (var property in ReadProperties(classType)) { //Console.Out.WriteLine("Translating property {0} of class {1}", property.Name, classType.FullName); var swiftPropertyType = TranslateType(property.PropertyType); if (swiftPropertyType == null) { ErrorHandler.Error("Skipping property of undefined type '{0}.{1}'.", property.DeclaringType.FullName, property.Name); continue; } if (!swiftPropertyType.IsOptional) { swiftPropertyType = new SwiftOptional(swiftPropertyType, true); } var propertyName = _filter.GetPropertyName(property); var swiftProperty = new SwiftProperty(propertyName, swiftPropertyType) { BriefComment = _documentation.GetPropertySummary(property), }; swiftClass.AddProperty(swiftProperty); } return(swiftClass); }