Beispiel #1
0
        protected override void Write(Type type, bool nameOnly)
        {
            var underlyingType = FickleNullable.GetUnderlyingType(type);

            if (underlyingType != null && underlyingType.BaseType == typeof(Enum))
            {
                this.WriteLine(type.Name);

                return;
            }
            else if (type == typeof(object))
            {
                this.Write("var");

                return;
            }
            else if (type is FickleListType)
            {
                this.Write("Array");

                return;
            }

            base.Write(type, nameOnly);
        }
Beispiel #2
0
        protected override void Write(Type type, bool nameOnly)
        {
            var underlyingType = FickleNullable.GetUnderlyingType(type);

            if (underlyingType != null && underlyingType.BaseType == typeof(Enum))
            {
                this.WriteLine("enum");

                return;
            }
            else if (type == typeof(object))
            {
                this.Write("Object");

                return;
            }
            else if (type == typeof(void))
            {
                this.Write("void");

                return;
            }
            else if (underlyingType != null && underlyingType.IsNumericType())
            {
                this.Write("int");

                return;
            }
            else if (type.IsClass && type.Name == "Class")
            {
                this.Write("class");

                return;
            }
            else if (type.IsInterface)
            {
                this.Write("interface ", type.Name);

                return;
            }
            else if (type is FickleListType)
            {
                var listType = type.GetFickleListElementType();

                if (type.GetFickleListElementType().GetUnderlyingType() != null)
                {
                    listType = type.GetFickleListElementType().GetUnderlyingType();
                }

                this.Write("ArrayList <");
                this.Write(listType, true);
                this.Write(">");

                return;
            }
            else if (type is FickleDelegateType)
            {
                var delegateType = (FickleDelegateType)type;

                this.Write(delegateType.ReturnType, false);
                for (var i = 0; i < delegateType.Parameters.Length; i++)
                {
                    this.Write(delegateType.Parameters[i].ParameterType, false);
                    this.Write(' ');
                    this.Write(delegateType.Parameters[i].Name);

                    if (i != delegateType.Parameters.Length - 1)
                    {
                        this.Write(", ");
                    }
                }

                return;
            }

            base.Write(type, nameOnly);
        }
        protected override void Write(Type type, bool nameOnly)
        {
            var nullableType = type as FickleNullable;

            if (nullableType != null)
            {
                var underlyingType = FickleNullable.GetUnderlyingType(type);
                this.Write(underlyingType, true);
                this.Write('?');

                return;
            }

            if (type.IsClass && type.Name == "Class")
            {
                this.Write("class");

                return;
            }

            if (type.IsInterface)
            {
                this.Write("interface ", type.Name);

                return;
            }

            var fickleListType = type as FickleListType;

            if (fickleListType != null)
            {
                this.Write("List");

                if (fickleListType.ListElementType != null)
                {
                    this.Write("<");
                    this.Write(fickleListType.ListElementType, true);
                    this.Write(">");
                }

                return;
            }

            var fickleTaskType = type as CSharpAwaitedTaskType;

            if (fickleTaskType != null)
            {
                this.Write("Task");

                if (fickleTaskType.TaskElementType != null)
                {
                    this.Write("<");
                    this.Write(fickleTaskType.TaskElementType, true);
                    this.Write(">");
                }

                return;
            }

            if (type == typeof(InterpolatedString))
            {
                this.Write("string");

                return;
            }

            var delegateType = type as FickleDelegateType;

            if (delegateType != null)
            {
                this.Write(delegateType.ReturnType, false);

                for (var i = 0; i < delegateType.Parameters.Length; i++)
                {
                    this.Write(delegateType.Parameters[i].ParameterType, false);
                    this.Write(' ');
                    this.Write(delegateType.Parameters[i].Name);

                    if (i != delegateType.Parameters.Length - 1)
                    {
                        this.Write(", ");
                    }
                }

                return;
            }

            if (type.IsGenericType)
            {
                if (type.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    this.Write(Nullable.GetUnderlyingType(type), true);
                    this.Write('?');

                    return;
                }

                this.Write(GetNameWithoutGenericArity(type) + "<");

                var genericTypeArguments = type.GenericTypeArguments;

                for (var i = 0; i < genericTypeArguments.Length; i++)
                {
                    this.Write(genericTypeArguments[i], true);

                    if (i + 1 < genericTypeArguments.Length)
                    {
                        this.Write(", ");
                    }
                }

                this.Write(">");

                return;
            }

            Type mappedType;

            if (this.mappedTypes.TryGetValue(type.Name, out mappedType))
            {
                this.Write(mappedType.FullName);
            }
            else
            {
                base.Write(type, nameOnly);
            }
        }
        protected override void Write(Type type, bool nameOnly)
        {
            var underlyingType = FickleNullable.GetUnderlyingType(type);

            if (underlyingType != null && underlyingType.BaseType == typeof(Enum))
            {
                if (nameOnly)
                {
                    this.Write("NSNumber");
                }
                else
                {
                    this.Write("NSNumber*");
                }

                return;
            }
            else if (type == typeof(object))
            {
                if (nameOnly)
                {
                    this.Write("NSObject");
                }
                else
                {
                    this.Write("NSObject*");
                }

                return;
            }
            else if (type == typeof(void))
            {
                this.Write("void");

                return;
            }
            else if (underlyingType != null && underlyingType.IsNumericType())
            {
                if (nameOnly)
                {
                    this.Write("NSNumber");
                }
                else
                {
                    this.Write("NSNumber*");
                }

                return;
            }
            else if (type.IsClass && type.Name == "id")
            {
                this.Write("id");

                return;
            }
            else if (type.IsClass && type.Name == "Class")
            {
                this.Write("Class");

                return;
            }
            else if (type.IsInterface)
            {
                this.Write("id<{0}>", type.Name);

                return;
            }
            else if (type.GetFickleListElementType() != null)
            {
                if (type.GetFickleListElementType() == typeof(byte))
                {
                    this.Write(nameOnly ? "NSData" : "NSData*");

                    return;
                }

                this.Write(nameOnly ? "NSMutableArray" : "NSMutableArray*");

                return;
            }
            else if (type is FickleDelegateType)
            {
                var delegateType = (FickleDelegateType)type;

                this.Write(delegateType.ReturnType, false);
                this.Write("(^)");
                this.Write("(");
                for (var i = 0; i < delegateType.Parameters.Length; i++)
                {
                    this.Write(delegateType.Parameters[i].ParameterType, false);
                    this.Write(' ');
                    this.Write(delegateType.Parameters[i].Name);

                    if (i != delegateType.Parameters.Length - 1)
                    {
                        this.Write(", ");
                    }
                }
                this.Write(")");

                return;
            }

            var fickleType = type as FickleType;

            if (fickleType != null)
            {
                base.Write(type, nameOnly);

                if (!nameOnly)
                {
                    if (fickleType.IsEnum && fickleType.IsByRef)
                    {
                        this.Write("*");
                    }
                    else if (!(fickleType.IsPrimitive || fickleType.IsEnum))
                    {
                        this.Write("*");
                    }
                }
            }
            else
            {
                base.Write(type, nameOnly);

                if (!nameOnly && this.IsReferenceType(type))
                {
                    base.Write("*");
                }
            }
        }
        protected override Expression VisitUnary(UnaryExpression node)
        {
            if (node.NodeType == ExpressionType.Not)
            {
                this.Write('!');
                this.Write('(');
                this.Visit(node.Operand);
                this.Write(')');
            }
            else if (node.NodeType == ExpressionType.Convert)
            {
                if ((node.Type == typeof(Guid) || node.Type == typeof(Guid?)) &&
                    node.Operand.Type != FickleType.Define("id"))
                {
                    if (node.Operand.Type.GetUnwrappedNullableType() == typeof(Guid))
                    {
                        this.Visit(node.Operand);

                        return(node);
                    }

                    this.Write("[");
                    this.Write(typeof(Guid), true);
                    this.Write(" uuidFromString:");
                    if (node.Operand.Type != typeof(string))
                    {
                        this.Write("(NSString*)");
                    }
                    this.Visit(node.Operand);
                    this.Write("]");
                }
                else if ((node.Type.GetUnwrappedNullableType().IsNumericType() ||
                          node.Type.GetUnwrappedNullableType() == typeof(bool)) &&
                         (node.Operand.Type == typeof(object) ||
                          FickleNullable.GetUnderlyingType(node.Operand.Type) != null ||
                          node.Operand.Type.Name == "NSNumber" ||
                          node.Operand.Type.Name == "id"))
                {
                    var type = node.Type;

                    if (type == typeof(bool))
                    {
                        this.Write("(BOOL)((NSNumber*)");
                        this.Visit(node.Operand);
                        this.Write(").boolValue");
                    }
                    else if (type == typeof(bool?))
                    {
                        this.Write("((NSNumber*)");
                        this.Visit(node.Operand);
                        this.Write(")");
                    }
                    else if (type == typeof(byte))
                    {
                        this.Write("(uint8_t)((NSNumber*)");
                        this.Visit(node.Operand);
                        this.Write(").intValue");
                    }
                    else if (type == typeof(byte?))
                    {
                        this.Write("((NSNumber*)");
                        this.Visit(node.Operand);
                        this.Write(")");
                    }
                    else if (type == typeof(short))
                    {
                        this.Write("((NSNumber*)");
                        this.Visit(node.Operand);
                        this.Write(").shortValue");
                    }
                    else if (type == typeof(short?))
                    {
                        this.Write("((NSNumber*)");
                        this.Visit(node.Operand);
                        this.Write(")");
                    }
                    else if (type == typeof(ushort))
                    {
                        this.Write("((NSNumber*)");
                        this.Visit(node.Operand);
                        this.Write(").unsignedShortValue");
                    }
                    else if (type == typeof(ushort?))
                    {
                        this.Write("((NSNumber*)");
                        this.Visit(node.Operand);
                        this.Write(")");
                    }
                    else if (type == typeof(int))
                    {
                        this.Write("((NSNumber*)");
                        this.Visit(node.Operand);
                        this.Write(").intValue");
                    }
                    else if (type == typeof(int?))
                    {
                        this.Write("((NSNumber*)");
                        this.Visit(node.Operand);
                        this.Write(")");
                    }
                    else if (type == typeof(uint))
                    {
                        this.Write("((NSNumber*)");
                        this.Visit(node.Operand);
                        this.Write(").unsignedIntValue");
                    }
                    else if (type == typeof(uint?))
                    {
                        this.Write("((NSNumber*)");
                        this.Visit(node.Operand);
                        this.Write(")");
                    }
                    else if (type == typeof(long))
                    {
                        this.Write("(int64_t)((NSNumber*)");
                        this.Visit(node.Operand);
                        this.Write(").longLongValue");
                    }
                    else if (type == typeof(long?))
                    {
                        this.Write("((NSNumber*)");
                        this.Visit(node.Operand);
                        this.Write(")");
                    }
                    else if (type == typeof(ulong))
                    {
                        this.Write("(int64_t)((NSNumber*)");
                        this.Visit(node.Operand);
                        this.Write(").unsignedLongLongValue");
                    }
                    else if (type == typeof(ulong?))
                    {
                        this.Write("((NSNumber*)");
                        this.Visit(node.Operand);
                        this.Write(")");
                    }
                    else if (type == typeof(float))
                    {
                        this.Write("(float)((NSNumber*)");
                        this.Visit(node.Operand);
                        this.Write(").floatValue");
                    }
                    else if (type == typeof(float?))
                    {
                        this.Write("((NSNumber*)");
                        this.Visit(node.Operand);
                        this.Write(")");
                    }
                    else if (type == typeof(double))
                    {
                        this.Write("(double)((NSNumber*)");
                        this.Visit(node.Operand);
                        this.Write(").doubleValue");
                    }
                    else if (type == typeof(double?))
                    {
                        this.Write("((NSNumber*)");
                        this.Visit(node.Operand);
                        this.Write(")");
                    }
                    else if (type == typeof(decimal))
                    {
                        this.Write("([NSDecimalNumber decimalNumberWithString:((NSString*)");
                        this.Visit(node.Operand);
                        this.Write(") ?: @\"0\"])");
                    }
                    else if (type == typeof(decimal?))
                    {
                        this.Write("(");
                        this.Visit(node.Operand);
                        this.Write(" == nil ? nil : [NSDecimalNumber decimalNumberWithString:((NSString*)");
                        this.Visit(node.Operand);
                        this.Write(")])");
                    }
                    else
                    {
                        throw new Exception("Unexpected type: " + node.Type.Name);
                    }
                }
                else if (node.Type == typeof(DateTime?) || node.Type == typeof(DateTime))
                {
                    this.Write("[jsDateFormatter dateFromString:(NSString*)currentValueFromDictionary] ?: [isoDateFormatter dateFromString:(NSString*)currentValueFromDictionary]");
                }
                else if ((node.Type == typeof(TimeSpan?) || node.Type == typeof(TimeSpan)) && node.Operand.Type != FickleType.Define("id"))
                {
                    this.Write("[");
                    this.Write(typeof(TimeSpan), true);
                    this.Write(" fromIsoString:");

                    if (node.Operand.Type != typeof(string))
                    {
                        this.Write("(NSString*)");
                    }

                    this.Visit(node.Operand);
                    this.Write("]");
                }
                else if ((node.Type.IsNullable() &&
                          node.Type.GetUnwrappedNullableType().IsEnum) &&
                         node.Operand.Type.IsEnum)
                {
                    this.Write("@(");
                    this.Visit(node.Operand);
                    this.Write(")");
                }
                else if (node.Type == typeof(object))
                {
                    if (!this.IsReferenceType(node.Operand.Type))
                    {
                        this.Write("@(");
                        this.Visit(node.Operand);
                        this.Write(")");
                    }
                    else
                    {
                        this.Visit(node.Operand);
                    }
                }
                else
                {
                    this.Write("((");

                    if (node.Operand.Type.IsPrimitive && node.Operand.Type.Name.StartsWith("CF"))
                    {
                        this.Write("__bridge ");
                    }

                    this.Write(node.Type);
                    this.Write(')');
                    this.Write('(');
                    this.Visit(node.Operand);
                    this.Write(')');
                    this.Write(')');
                }
            }
            else if (node.NodeType == ExpressionType.Quote)
            {
                this.Visit(node.Operand);
                this.WriteLine(';');
            }
            else if (node.NodeType == ExpressionType.IsTrue)
            {
                this.Visit(node.Operand);
            }
            else if (node.NodeType == ExpressionType.IsFalse)
            {
                this.Write('!');
                this.Visit(node.Operand);
            }

            return(node);
        }