//
        // ObjCRepresentationIsPrimitive
        //
        // Returns true if ObjC repesentation of facet is a primitive.
        // Returns false if ObjC repesentation of facet is an object.
        //
        public bool ObjCRepresentationIsPrimitive(CodeFacet facet)
        {
            // if a type association exits then query it
            ObjCTypeAssociation objCTypeAssociate = ObjCTypeAssociate(facet);

            if (objCTypeAssociate != null)
            {
                if (objCTypeAssociate.IsNSObject)
                {
                    return(false);
                }
                else
                {
                    return(true);
                }
            }

            // Order is important here
            if (facet.IsGenericType)
            {
                return(false);
            }
            if (facet.IsStruct)
            {
                return(false);
            }

            // pointers are not value types!
            if (facet.IsValueType || facet.IsPointer)
            {
                return(true);
            }

            return(false);
        }
        //
        // ObjCValueToManaged
        //
        // Return an ObjC expression that converts an ObjC object to its corresponding managed representation
        //
        public string ObjCValueToManaged(string objCVarName, string objCTypeDecl, CodeFacet managedFacet)
        {
            string exp = null;

            // extract type info in a format suitable for association
            string managedType = ManagedTypeForAssociation(managedFacet);

            // if type is an enum then use its underlying type
            if (managedFacet.IsEnum)
            {
                managedType = managedFacet.UnderlyingType;
            }

            // retrieve an ObjCTypeAssociation for the given managedType
            string key = ObjCTypeAssociation.UniqueTypeName(objCTypeDecl, managedType);

            if (ManagedTypeAssociations.ContainsKey(key))
            {
                ManagedTypeAssociation managedTypeAssoc = ManagedTypeAssociations[key];
                ObjCTypeAssociation    objCTypeAssoc    = ObjCTypeAssociations[managedType];

                // use the value object format specifier if available
                string setterFormat = objCTypeAssoc.SetterFormat;
                if (setterFormat != null)
                {
                    if (managedFacet.IsPointer)
                    {
                        setterFormat = "&{0}";
                    }
                    exp = string.Format(setterFormat, objCVarName);
                }

                // use custom method
                else if (objCTypeAssoc.SetterMethod != null)
                {
                    string     methodName = objCTypeAssoc.SetterMethod;
                    Type       type       = GetType();
                    MethodInfo method     = type.GetMethod(methodName);
                    if (method != null)
                    {
                        exp = (string)method.Invoke(this, new object[] { objCVarName, objCTypeDecl });
                    }
                }
            }

            // no ObjC expression defined.
            // generate default object representation expression.
            if (exp == null)
            {
                if (ObjCRepresentationIsPrimitive(managedFacet))
                {
                    exp = string.Format("&{0}", objCVarName);
                }
                else
                {
                    exp = string.Format("[{0} monoObject]", objCVarName);
                }
            }
            return(exp);
        }
        //
        // ObjCTypeAssociate()
        //
        public ObjCTypeAssociation ObjCTypeAssociate(string managedType)
        {
            ObjCTypeAssociation typeAssoc = null;

            // look for literal association
            if (managedType != null && ObjCTypeAssociations.ContainsKey(managedType))
            {
                typeAssoc = ObjCTypeAssociations[managedType];
            }

            return(typeAssoc);
        }
Example #4
0
        //
        // AssociateTypes()
        //
        void AssociateTypes(ManagedTypeAssociation managedTA, ObjCTypeAssociation objcTA)
        {
            objcTA.ManagedTypeAssociate = managedTA;

            // 1:1 association from managed type to objc type
            // the managed type name key is undecorated
            ObjCTypeAssociations.Add(managedTA.ManagedType, objcTA);

            // 1:N association from objc declaration to managed type.
            // hence we qualify the objc declaration with the managed type name.
            string objCKey = objcTA.UniqueTypeNameForManagedType(managedTA.ManagedType);

            ManagedTypeAssociations.Add(objCKey, managedTA);
        }
Example #5
0
        private void ProcessParameters(Net2ObjC n2c, EventFacet facet, Dictionary <string, object> options = null)
        {
            int idx    = 0;
            int idxMax = facet.Parameters.Count > 0 ? facet.Parameters.Count - 1 : 0;

            foreach (ParameterFacet parameter in facet.Parameters)
            {
                // Known issues:
                //
                // 1. Rendering of T[]& fails as in System.Array:Resize<T>(ref T[], int)
                // <Parameter Name="array" Type="T[]&" ElementType="T[]" IsByRef="True" ContainsGenericParameters="True"/>
                // Issue : T[]& renders as T**
                // Workaround: exclude in config.objc.xml like so <string>System.Array:Resize</string>
                // Suggested fix: provide a separate Element xml element to detail the ElementType more fully.

                // if the mono parameters is passed by ref then strip
                // the suffix to enable type association
                string monoParameterType = parameter.Type;
                if (parameter.IsByRef || parameter.IsPointer)
                {
                    monoParameterType = parameter.ElementType;
                }

                //
                // build the ObjC method interleaved parameter representation
                // eg: name1:(int32_t)p1 name2:(int64_t)p2 name3:(NSString *)p3
                //
                string objCParamTypeDecl     = null;
                bool   objCParameterIsObject = true;

                //
                // Get the ObjC type associated with the parameter.
                //
                ObjCTypeAssociation    objCTypeAssociate    = n2c.ObjCTypeAssociate(parameter);
                ManagedTypeAssociation managedTypeAssociate = null;
                if (objCTypeAssociate != null)
                {
                    //
                    // If the parameter is an array, say Int64[], then its Obj-C rep will be System.Array
                    // The mono type association however must reflect the represented type, Int64.
                    //
                    if (parameter.IsArray)
                    {
                        ObjCTypeAssociation objCRepresentedTypeAssociate = n2c.ObjCTypeAssociate(parameter.ElementType);
                        if (objCRepresentedTypeAssociate != null)
                        {
                            managedTypeAssociate = objCRepresentedTypeAssociate.ManagedTypeAssociate;
                        }
                    }

                    if (managedTypeAssociate == null)
                    {
                        managedTypeAssociate = objCTypeAssociate.ManagedTypeAssociate;
                    }

                    objCParamTypeDecl     = objCTypeAssociate.ObjCTypeDecl;
                    objCParameterIsObject = objCTypeAssociate.IsNSObject;
                }
                else
                {
                    //
                    // Generate default objC representations
                    //
                    objCParamTypeDecl     = n2c.ObjCTypeDeclFromManagedFacet(parameter);
                    objCParameterIsObject = n2c.ObjCRepresentationIsObject(parameter);
                }

                ObjCEventBlockParameterGetters[idx] = objCTypeAssociate?.GetterFormat ?? string.Empty;

                // if parameter is an interface then use adoption conforming type ie: id <typename>
                if (parameter.IsInterface)
                {
                    objCParamTypeDecl = n2c.ObjCConformingTypeFromObjCTypeDecl(objCParamTypeDecl, false);
                }
                if (parameter.IsByRef || parameter.IsPointer)
                {
                    objCParamTypeDecl += "*";                       // add additional indirection
                }

                //
                // Build the mono method argument invocation signature
                //
                if (idx > 0)
                {
                    MonoSigBuilder.Append(",");
                }
                string monoParameterTypeInvoke = null;

                // if type is a GenericParameter defined by the class, as opposed to via a method like so Method<T>(T).
                // in this case we want to identify the parameter by its position as this makes it simple
                // to build the required signature at run time
                if (parameter.IsGenericParameter && !parameter.DeclaredByMethod)
                {
                    // generic parameters must have an associate
                    if (managedTypeAssociate == null)
                    {
                        throw new Exception("Missing managed type association for generic parameter.");
                    }

                    monoParameterTypeInvoke = managedTypeAssociate.ManagedTypeInvoke;
                    if (parameter.IsArray)
                    {
                        monoParameterTypeInvoke += "[]";
                    }

                    // in order for the C api to substitute the correct type at run time
                    // the generic parameter position needs to be indicated.
                    monoParameterTypeInvoke = string.Format(monoParameterTypeInvoke, parameter.GenericParameterPosition);
                }

                // if parameter is declared by the method like so Method<T>(T) then we want to preserve the type name
                // as this constitutes part of the method signature this is used to lookup the generic method for inflation
                else if (parameter.IsGenericParameter && parameter.DeclaredByMethod)
                {
                    // we expect to be operating on a generic method definition
                    if (!facet.IsGenericMethodDefinition)
                    {
                        throw new Exception("Generic method definition expected.");
                    }

                    // the type sig will be something like Declaring.Type+T but the embedded API sig
                    // uses just the type parameter name T
                    int symbolIndex = monoParameterType.IndexOf('+');
                    if (symbolIndex == -1)
                    {
                        throw new Exception("Missing nested type symbol for generic parameter.");
                    }
                    monoParameterTypeInvoke = monoParameterType.Substring(symbolIndex + 1);
                    if (parameter.IsArray)
                    {
                        monoParameterTypeInvoke += "[]";
                    }
                }
                else
                {
                    monoParameterTypeInvoke = n2c.ManagedTypeInvokeFromManagedType(monoParameterType);
                }

                // Note that we use a separate variable to hold the actual type sig used in the in mono_method_desc call
                // as the signature may need to be specfically modified for the mono_method_desc API.
                string monoParameterTypeInvoke_ = monoParameterTypeInvoke;

                // The mono_method_desc * APIs prefix nested classes with a '/' rather than a '+' to conform with IL/CTS conventions
                // The approach used here is trivial and is likely fragile.
                // We probably need a separate mono param type builder like that found in debug-helpers.c append_class_name().
                // Note that Delegates will present as nested classes.
                // Also note that although we have an IsNested property we shouldn't use it as a conditional test for this operation
                // as generic types with nested type paramaters such as System.Collections.Generic.IEnumerable`1<A.B+C>
                // won't identify as nested.
                monoParameterTypeInvoke_ = monoParameterTypeInvoke_.Replace("+", "/");

                // add type signature and access modifier
                MonoSigBuilder.Append(monoParameterTypeInvoke_);
                if (parameter.IsPointer)
                {
                    MonoSigBuilder.Append("*");
                }
                if (parameter.IsByRef)
                {
                    MonoSigBuilder.Append("&"); // the signature needs to express by ref
                }

                // Build ObjC parameter name.
                // In order to represent overloaded methods effectively the
                // ObjC paramter name is constructed as follows:
                // Managed parameter name + Managed parameter type + Ref
                string objCParamName = ObjCIdentifierFromManagedIdentifier(parameter.Name);

                // If the method is overloaded by parameter then make the ObjC method
                // name unique by including type info in the name.
                //
                // Managed methods are overloaded by name only.
                // The Obj-C metjods representation uses interleaved parameters which may
                // be sufficient to produce a unique method signature.
                //
                // If however a managed method overload differs only in the type of its parameters
                // (the managed method name, parameter count and parameter names all being equal)
                // then the Obj-C interleaved parameters will include type info.
                string objCParamOverloadSuffix = "";
                if (facet.IsOverloadedParameterMethod)
                {
                    // We adopt a minimal as opposed to a full type repesentation here in order
                    // to minimize the parameter length.
                    // Time will tell how it flies.
                    objCParamOverloadSuffix = n2c.ObjCMinimalIdentifierFromManagedIdentifier(monoParameterTypeInvoke);

                    if (parameter.IsArray)
                    {
                        objCParamOverloadSuffix += "Array";
                    }

                    if (parameter.IsPointer)
                    {
                        objCParamOverloadSuffix += "Ptr";
                    }
                }
                if (parameter.IsByRef)
                {
                    objCParamOverloadSuffix += "Ref";
                }

                if (objCParamOverloadSuffix.Length > 0)
                {
                    objCParamName += objCParamOverloadSuffix.FirstCharacterToUpper();
                }

                // append the complete interleaved parameter expression
                if (idx == 0)
                {
                    if (n2c.AppendFirstArgSignatureToMethodName)
                    {
                        // the leading underscore helps identify the preceding characters as the managed method name
                        ObjCMethodName += "_with";
                        ObjCParameterBuilder.AppendFormat("{0}", objCParamName.FirstCharacterToUpper());
                    }
                }
                else
                {
                    ObjCParameterBuilder.AppendFormat(" {0}", objCParamName.FirstCharacterToLower());
                }
                ObjCParameterBuilder.AppendFormat(":({0})p{1}", objCParamTypeDecl, idx + 1);

                // build C parameter list
                string cParamDelim = idxMax > 0 && idx < idxMax ? ", " : "";
                CParameterBuilder.AppendFormat($"{objCParamTypeDecl} {objCParamName.FirstCharacterToLower()}{cParamDelim}");

                idx++;
            }
        }
Example #6
0
        //
        // AssociateTypes()
        //
        void AssociateTypes(ManagedTypeAssociation managedTA, ObjCTypeAssociation objcTA)
        {
            objcTA.ManagedTypeAssociate = managedTA;

            // 1:1 association from managed type to objc type
            // the managed type name key is undecorated
            ObjCTypeAssociations.Add(managedTA.ManagedType, objcTA);

            // 1:N association from objc declaration to managed type.
            // hence we qualify the objc declaration with the managed type name.
            string objCKey = objcTA.UniqueTypeNameForManagedType(managedTA.ManagedType);
            ManagedTypeAssociations.Add(objCKey, managedTA);
        }
Example #7
0
        //
        // BuildTypeAssociations()
        //
        // Provide associations between ObjC and managed types.
        //
        // The managed built in types require that their aliases be used when
        // constructing method signatures.
        // Built in type list: http://msdn.microsoft.com/en-us/library/ya5y69ds.aspx
        // 
        // See source mono/metadata/debug-helpers.c find_system_class (const char *name)
        // https://github.com/mono/mono/blob/master/mono/metadata/debug-helpers.c#L90
        //
        void BuildTypeAssociations()
        {
            ObjCTypeAssociations = new Dictionary<string, ObjCTypeAssociation>();
            ManagedTypeAssociations = new Dictionary<string, ManagedTypeAssociation>();

            ObjCTypeAssociation objcTA = null;
            ManagedTypeAssociation manTA = null;

            //===============================================================================================
            // reference types
            //===============================================================================================

            // generic parameter
            manTA = new ManagedTypeAssociation { ManagedType = GenericTypePlaceholder };
            objcTA = new ObjCTypeAssociation { ObjCType = "System_Object", GetterFormat = "[System_Object subclassObjectWithMonoObject:{0}]" };
            AssociateTypes(manTA, objcTA);

            // System.Object
            manTA = new ManagedTypeAssociation { ManagedType = "System.Object", ManagedTypeAlias = "object" };
            objcTA = new ObjCTypeAssociation { ObjCType = "System_Object", GetterFormat = "[System_Object objectWithMonoObject:{0}]" };
            AssociateTypes(manTA, objcTA);

            // System.String
            manTA = new ManagedTypeAssociation { ManagedType = "System.String", ManagedTypeAlias = "string" };
            objcTA = new ObjCTypeAssociation { ObjCType = "NSString", GetterFormat = "[NSString stringWithMonoString:DB_STRING({0})]" };
            AssociateTypes(manTA, objcTA);

            // System.Array
            manTA = new ManagedTypeAssociation { ManagedType = "System.Array"};
            objcTA = new ObjCTypeAssociation { ObjCType = "DBSystem_Array", GetterFormat = "[DBSystem_Array arrayWithMonoArray:DB_ARRAY({0})]" };
            AssociateTypes(manTA, objcTA);

            // System.Collections.ArrayList
            manTA = new ManagedTypeAssociation { ManagedType = "System.Collections.ArrayList" };
            objcTA = new ObjCTypeAssociation { ObjCType = "DBSystem_Collections_ArrayList", GetterFormat = "[DBSystem_Collections_ArrayList listWithMonoObject:{0}]" };
            AssociateTypes(manTA, objcTA);

            // System.Collections.Generic.List
            manTA = new ManagedTypeAssociation { ManagedType = "System.Collections.Generic.List`1" };
            objcTA = new ObjCTypeAssociation { ObjCType = "DBSystem_Collections_Generic_ListA1" };
            AssociateTypes(manTA, objcTA);

            // System.Collections.Generic.Dictionary
            manTA = new ManagedTypeAssociation { ManagedType = "System.Collections.Generic.Dictionary`2" };
            objcTA = new ObjCTypeAssociation { ObjCType = "DBSystem_Collections_Generic_DictionaryA2" };
            AssociateTypes(manTA, objcTA);

            // System.Byte[]
            manTA = new ManagedTypeAssociation { ManagedType = "System.Byte[]" };
            objcTA = new ObjCTypeAssociation { ObjCType = "NSData", GetterFormat = "[NSData dataWithMonoArray:DB_ARRAY({0})]" };
            AssociateTypes(manTA, objcTA);

            // ObjectSet
            manTA = new ManagedTypeAssociation { ManagedType = "System.Data.Entity.Core.Objects.ObjectSet`1"};
            objcTA = new ObjCTypeAssociation { ObjCType = "DBSystem_Data_Entity_Core_Objects_ObjectSetA1", GetterFormat = "[DBSystem_Data_Entity_Core_Objects_ObjectSetA1 objectSetWithMonoObject:{0}]" };
            AssociateTypes(manTA, objcTA);

            // ObjectContext
            manTA = new ManagedTypeAssociation { ManagedType = "System.Data.Entity.Core.Objects.ObjectContext" };
            objcTA = new ObjCTypeAssociation { ObjCType = "DBSystem_Data_Entity_Core_Objects_ObjectContext" };
            AssociateTypes(manTA, objcTA);

            //===============================================================================================
            // value types
            //===============================================================================================

            // System.ValueType - struct
            manTA = new ManagedTypeAssociation { ManagedType = "System.ValueType" };
            objcTA = new ObjCTypeAssociation { ObjCType = "DBManagedObject", GetterFormat = "[DBManagedObject objectWithMonoObject:{0}]" };
            AssociateTypes(manTA, objcTA);

            // System.Void
            manTA = new ManagedTypeAssociation { ManagedType = "System.Void", ManagedTypeAlias = "void"};
            objcTA = new ObjCTypeAssociation { ObjCType = "void", GetterFormat = "" };
            AssociateTypes(manTA, objcTA);

            // System.Int64
            manTA = new ManagedTypeAssociation { ManagedType = "System.Int64", ManagedTypeAlias = "long"};
            objcTA = new ObjCTypeAssociation { ObjCType = "int64_t", GetterFormat = "DB_UNBOX_INT64({0})" };
            AssociateTypes(manTA, objcTA);

            // System.Int32
            manTA = new ManagedTypeAssociation { ManagedType = "System.Int32", ManagedTypeAlias = "int"};
            objcTA = new ObjCTypeAssociation { ObjCType = "int32_t", GetterFormat = "DB_UNBOX_INT32({0})" };
            AssociateTypes(manTA, objcTA);

            // System.Int16
            manTA = new ManagedTypeAssociation { ManagedType = "System.Int16", ManagedTypeAlias = "short", ManagedTypeInvoke = "int16"};
            objcTA = new ObjCTypeAssociation { ObjCType = "int16_t", GetterFormat = "DB_UNBOX_INT16({0})" };
            AssociateTypes(manTA, objcTA);

            // System.SByte
            manTA = new ManagedTypeAssociation { ManagedType = "System.SByte", ManagedTypeAlias = "sbyte"};
            objcTA = new ObjCTypeAssociation { ObjCType = "int8_t", GetterFormat = "DB_UNBOX_INT8({0})" };
            AssociateTypes(manTA, objcTA);

            // System.IntPtr
            manTA = new ManagedTypeAssociation { ManagedType = "System.IntPtr", ManagedTypeInvoke = "intptr" };
            objcTA = new ObjCTypeAssociation { ObjCType = "void *", GetterFormat = "DB_UNBOX_PTR({0})" };
            AssociateTypes(manTA, objcTA);

            // System.UInt64
            manTA = new ManagedTypeAssociation { ManagedType = "System.UInt64", ManagedTypeAlias = "ulong"};
            objcTA = new ObjCTypeAssociation { ObjCType = "uint64_t", GetterFormat = "DB_UNBOX_UINT64({0})" };
            AssociateTypes(manTA, objcTA);
            
            // System.UInt32
            manTA = new ManagedTypeAssociation { ManagedType = "System.UInt32", ManagedTypeAlias = "uint"};
            objcTA = new ObjCTypeAssociation { ObjCType = "uint32_t", GetterFormat = "DB_UNBOX_UINT32({0})" };
            AssociateTypes(manTA, objcTA);

            // System.UInt16
            manTA = new ManagedTypeAssociation { ManagedType = "System.UInt16", ManagedTypeAlias = "ushort", ManagedTypeInvoke = "uint16" };
            objcTA = new ObjCTypeAssociation { ObjCType = "uint16_t", GetterFormat = "DB_UNBOX_UINT16({0})" };
            AssociateTypes(manTA, objcTA);

            // System.Byte
            manTA = new ManagedTypeAssociation { ManagedType = "System.Byte", ManagedTypeAlias = "byte" };
            objcTA = new ObjCTypeAssociation { ObjCType = "uint8_t", GetterFormat = "DB_UNBOX_UINT8({0})" };
            AssociateTypes(manTA, objcTA);

            // System.UIntPtr
            manTA = new ManagedTypeAssociation { ManagedType = "System.UIntPtr", ManagedTypeInvoke = "uintptr" };
            objcTA = new ObjCTypeAssociation { ObjCType = "void *", GetterFormat = "DB_UNBOX_UPTR({0})" };
            AssociateTypes(manTA, objcTA);

            // System.Char
            manTA = new ManagedTypeAssociation { ManagedType = "System.Char", ManagedTypeAlias = "char" };
            objcTA = new ObjCTypeAssociation { ObjCType = "uint16_t", GetterFormat = "DB_UNBOX_UINT16({0})" };
            AssociateTypes(manTA, objcTA);

            // System.Double
            manTA = new ManagedTypeAssociation { ManagedType = "System.Double", ManagedTypeAlias = "double" };
            objcTA = new ObjCTypeAssociation { ObjCType = "double", GetterFormat = "DB_UNBOX_DOUBLE({0})" };
            AssociateTypes(manTA, objcTA);

            // System.Single
            manTA = new ManagedTypeAssociation { ManagedType = "System.Single", ManagedTypeAlias = "float", ManagedTypeInvoke = "single" };
            objcTA = new ObjCTypeAssociation { ObjCType = "float", GetterFormat = "DB_UNBOX_FLOAT({0})" };
            AssociateTypes(manTA, objcTA);

            // System.Boolean
            manTA = new ManagedTypeAssociation { ManagedType = "System.Boolean", ManagedTypeAlias = "bool" };
            objcTA = new ObjCTypeAssociation { ObjCType = "BOOL", GetterFormat = "DB_UNBOX_BOOLEAN({0})" };
            AssociateTypes(manTA, objcTA);

            // System.DateTime
            manTA = new ManagedTypeAssociation { ManagedType = "System.DateTime" };
            objcTA = new ObjCTypeAssociation { ObjCType = "NSDate", GetterFormat = "[NSDate dateWithMonoDateTime:{0}]" };
            AssociateTypes(manTA, objcTA);

            // System.Decimal
            manTA = new ManagedTypeAssociation { ManagedType = "System.Decimal", ManagedTypeAlias = "decimal" };
            objcTA = new ObjCTypeAssociation { ObjCType = "NSDecimalNumber", GetterFormat = "[NSDecimalNumber decimalNumberWithMonoDecimal:{0}]" };
            AssociateTypes(manTA, objcTA);

            // System.Enum
            manTA = new ManagedTypeAssociation { ManagedType = "System.Enum" };
            objcTA = new ObjCTypeAssociation { ObjCType = "DBSystem_Enum", GetterFormat = "[DBSystem_Enum objectWithMonoObject:{0}]" };
            AssociateTypes(manTA, objcTA);
        }
Example #8
0
            public Accessor(Net2ObjC n2c, CodeFacet facet, Dictionary <string, object> options = null)
            {
                Name        = facet.Name;
                Description = facet is PropertyFacet ? "property" : "field";

                // define getters and setters
                GetterName     = Name.FirstCharacterToLower();
                SetterName     = "set" + Name.FirstCharacterToUpper();
                ObjCMethodType = null;
                if (facet.IsStatic)
                {
                    ObjCMethodType = "+";

                    // decorate class accessor method names known to be unsafe
                    if (n2c.UnsafeObjCClassMethodNames().Contains(GetterName))
                    {
                        GetterName += "_";
                        SetterName += "_";
                    }
                }
                else
                {
                    ObjCMethodType = "-";
                }

                string accessorType = facet.Type;

                ObjCTypeDecl     = n2c.ObjCTypeDeclFromManagedFacet(facet);
                IsObjectProperty = n2c.ObjCRepresentationIsObject(facet);
                MonoObjectPtr    = "MonoObject *";

                // some NSObject properties need a bit of TLC
                BaseProperties = new List <string> {
                    "description"
                };

                // property storage and evaluation
                PropertyAttributes = "";
                PropertyStorage    = "_" + GetterName;
                if (facet.IsStatic)
                {
                    PropertyStorage = "m" + PropertyStorage;
                    if (IsObjectProperty)
                    {
                        n2c.StaticObjectPropertyStorageNames.Add(PropertyStorage);
                    }
                }
                DoPropertyEqualityTest = "";
                if (IsObjectProperty)
                {
                    // test if mono object pointer and property storage reference the same managed object
                    DoPropertyEqualityTest = string.Format("if ([self object:{0} isEqualToMonoObject:{1}]) return {0};", PropertyStorage, ManagedVariableName);
                }

                // instance property.
                if (!facet.IsStatic)
                {
                    string attributes = "nonatomic";

                    // object property attributes
                    if (n2c.ObjCRepresentationIsObject(facet))
                    {
                        attributes += ", strong";
                    }
                    if (!facet.IsWritable)
                    {
                        attributes += ", readonly";
                    }
                    PropertyAttributes = String.Format("({0}) ", attributes);
                }

                // create Obj-C representation of managed object
                ManagedValueToObjC = n2c.ManagedValueToObjc(ManagedVariableName, facet);
                ObjCValueToMono    = n2c.ObjCValueToManaged(ObjCVariableName, ObjCTypeDecl, facet);
                ObjCTypeAssociation objCTypeAssociate = n2c.ObjCTypeAssociate(facet);

                // form mono method invocation name.
                // a prefix may be required, for instance when calling explicit interface properties.
                string monoMethodPrefix = "";

                if (options != null)
                {
                    if (options.ContainsKey("cAPIMethodPrefix"))
                    {
                        monoMethodPrefix = (string)options["cAPIMethodPrefix"];
                    }
                }
                MonoInvocationName = monoMethodPrefix + Name;

                IsValid = true;
            }
Example #9
0
 public string UniqueTypeNameForManagedType(string managedType)
 {
     return(ObjCTypeAssociation.UniqueTypeName(this.ObjCTypeDecl, managedType));
 }
Example #10
0
        //
        // BuildTypeAssociations()
        //
        // Provide associations between ObjC and managed types.
        //
        // The managed built in types require that their aliases be used when
        // constructing method signatures.
        // Built in type list: http://msdn.microsoft.com/en-us/library/ya5y69ds.aspx
        //
        // See source mono/metadata/debug-helpers.c find_system_class (const char *name)
        // https://github.com/mono/mono/blob/master/mono/metadata/debug-helpers.c#L90
        //
        void BuildTypeAssociations()
        {
            ObjCTypeAssociations    = new Dictionary <string, ObjCTypeAssociation>();
            ManagedTypeAssociations = new Dictionary <string, ManagedTypeAssociation>();

            ObjCTypeAssociation    objcTA = null;
            ManagedTypeAssociation manTA  = null;

            //===============================================================================================
            // reference types
            //===============================================================================================

            // generic parameter
            manTA = new ManagedTypeAssociation {
                ManagedType = GenericTypePlaceholder
            };
            objcTA = new ObjCTypeAssociation {
                ObjCType = "System_Object", GetterFormat = "[System_Object bestObjectWithMonoObject:{0}]"
            };
            AssociateTypes(manTA, objcTA);

            // System.Object
            manTA = new ManagedTypeAssociation {
                ManagedType = "System.Object", ManagedTypeAlias = "object"
            };
            objcTA = new ObjCTypeAssociation {
                ObjCType = "System_Object", GetterFormat = "[System_Object bestObjectWithMonoObject:{0}]"
            };
            AssociateTypes(manTA, objcTA);

            // System.String
            manTA = new ManagedTypeAssociation {
                ManagedType = "System.String", ManagedTypeAlias = "string"
            };
            objcTA = new ObjCTypeAssociation {
                ObjCType = "NSString", GetterFormat = "[NSString stringWithMonoString:DB_STRING({0})]"
            };
            AssociateTypes(manTA, objcTA);

            // System.Array
            manTA = new ManagedTypeAssociation {
                ManagedType = "System.Array"
            };
            objcTA = new ObjCTypeAssociation {
                ObjCType = "System_Array", GetterFormat = "[System_Array arrayWithMonoArray:DB_ARRAY({0})]"
            };
            AssociateTypes(manTA, objcTA);

            // System.Collections.ArrayList
            manTA = new ManagedTypeAssociation {
                ManagedType = "System.Collections.ArrayList"
            };
            objcTA = new ObjCTypeAssociation {
                ObjCType = "System_Collections_ArrayList", GetterFormat = "[System_Collections_ArrayList listWithMonoObject:{0}]"
            };
            AssociateTypes(manTA, objcTA);

            // System.Collections.Generic.List
            manTA = new ManagedTypeAssociation {
                ManagedType = "System.Collections.Generic.List`1"
            };
            objcTA = new ObjCTypeAssociation {
                ObjCType = "System_Collections_Generic_ListA1"
            };
            AssociateTypes(manTA, objcTA);

            // System.Collections.Generic.Dictionary
            manTA = new ManagedTypeAssociation {
                ManagedType = "System.Collections.Generic.Dictionary`2"
            };
            objcTA = new ObjCTypeAssociation {
                ObjCType = "System_Collections_Generic_DictionaryA2"
            };
            AssociateTypes(manTA, objcTA);

            // System.Byte[]
            manTA = new ManagedTypeAssociation {
                ManagedType = "System.Byte[]"
            };
            objcTA = new ObjCTypeAssociation {
                ObjCType = "NSData", GetterFormat = "[NSData dataWithMonoArray:DB_ARRAY({0})]"
            };
            AssociateTypes(manTA, objcTA);

            // ObjectSet
            manTA = new ManagedTypeAssociation {
                ManagedType = "System.Data.Entity.Core.Objects.ObjectSet`1"
            };
            objcTA = new ObjCTypeAssociation {
                ObjCType = "System_Data_Entity_Core_Objects_ObjectSetA1", GetterFormat = "[System_Data_Entity_Core_Objects_ObjectSetA1 objectSetWithMonoObject:{0}]"
            };
            AssociateTypes(manTA, objcTA);

            // ObjectContext
            manTA = new ManagedTypeAssociation {
                ManagedType = "System.Data.Entity.Core.Objects.ObjectContext"
            };
            objcTA = new ObjCTypeAssociation {
                ObjCType = "System_Data_Entity_Core_Objects_ObjectContext"
            };
            AssociateTypes(manTA, objcTA);

            //===============================================================================================
            // value types
            //===============================================================================================

            // System.ValueType - struct
            //manTA = new ManagedTypeAssociation { ManagedType = "System.ValueType" };
            //objcTA = new ObjCTypeAssociation { ObjCType = "DBManagedObject", GetterFormat = "[DBManagedObject objectWithMonoObject:{0}]" };
            //AssociateTypes(manTA, objcTA);

            // System.Void
            manTA = new ManagedTypeAssociation {
                ManagedType = "System.Void", ManagedTypeAlias = "void"
            };
            objcTA = new ObjCTypeAssociation {
                ObjCType = "void", GetterFormat = ""
            };
            AssociateTypes(manTA, objcTA);

            // System.Int64
            manTA = new ManagedTypeAssociation {
                ManagedType = "System.Int64", ManagedTypeAlias = "long"
            };
            objcTA = new ObjCTypeAssociation {
                ObjCType = "int64_t", GetterFormat = "DB_UNBOX_INT64({0})"
            };
            AssociateTypes(manTA, objcTA);

            // System.Int32
            manTA = new ManagedTypeAssociation {
                ManagedType = "System.Int32", ManagedTypeAlias = "int"
            };
            objcTA = new ObjCTypeAssociation {
                ObjCType = "int32_t", GetterFormat = "DB_UNBOX_INT32({0})"
            };
            AssociateTypes(manTA, objcTA);

            // System.Int16
            manTA = new ManagedTypeAssociation {
                ManagedType = "System.Int16", ManagedTypeAlias = "short", ManagedTypeInvoke = "int16"
            };
            objcTA = new ObjCTypeAssociation {
                ObjCType = "int16_t", GetterFormat = "DB_UNBOX_INT16({0})"
            };
            AssociateTypes(manTA, objcTA);

            // System.SByte
            manTA = new ManagedTypeAssociation {
                ManagedType = "System.SByte", ManagedTypeAlias = "sbyte"
            };
            objcTA = new ObjCTypeAssociation {
                ObjCType = "int8_t", GetterFormat = "DB_UNBOX_INT8({0})"
            };
            AssociateTypes(manTA, objcTA);

            // System.IntPtr
            manTA = new ManagedTypeAssociation {
                ManagedType = "System.IntPtr", ManagedTypeInvoke = "intptr"
            };
            objcTA = new ObjCTypeAssociation {
                ObjCType = "void *", GetterFormat = "DB_UNBOX_PTR({0})"
            };
            AssociateTypes(manTA, objcTA);

            // System.UInt64
            manTA = new ManagedTypeAssociation {
                ManagedType = "System.UInt64", ManagedTypeAlias = "ulong"
            };
            objcTA = new ObjCTypeAssociation {
                ObjCType = "uint64_t", GetterFormat = "DB_UNBOX_UINT64({0})"
            };
            AssociateTypes(manTA, objcTA);

            // System.UInt32
            manTA = new ManagedTypeAssociation {
                ManagedType = "System.UInt32", ManagedTypeAlias = "uint"
            };
            objcTA = new ObjCTypeAssociation {
                ObjCType = "uint32_t", GetterFormat = "DB_UNBOX_UINT32({0})"
            };
            AssociateTypes(manTA, objcTA);

            // System.UInt16
            manTA = new ManagedTypeAssociation {
                ManagedType = "System.UInt16", ManagedTypeAlias = "ushort", ManagedTypeInvoke = "uint16"
            };
            objcTA = new ObjCTypeAssociation {
                ObjCType = "uint16_t", GetterFormat = "DB_UNBOX_UINT16({0})"
            };
            AssociateTypes(manTA, objcTA);

            // System.Byte
            manTA = new ManagedTypeAssociation {
                ManagedType = "System.Byte", ManagedTypeAlias = "byte"
            };
            objcTA = new ObjCTypeAssociation {
                ObjCType = "uint8_t", GetterFormat = "DB_UNBOX_UINT8({0})"
            };
            AssociateTypes(manTA, objcTA);

            // System.UIntPtr
            manTA = new ManagedTypeAssociation {
                ManagedType = "System.UIntPtr", ManagedTypeInvoke = "uintptr"
            };
            objcTA = new ObjCTypeAssociation {
                ObjCType = "void *", GetterFormat = "DB_UNBOX_UPTR({0})"
            };
            AssociateTypes(manTA, objcTA);

            // System.Char
            manTA = new ManagedTypeAssociation {
                ManagedType = "System.Char", ManagedTypeAlias = "char"
            };
            objcTA = new ObjCTypeAssociation {
                ObjCType = "uint16_t", GetterFormat = "DB_UNBOX_UINT16({0})"
            };
            AssociateTypes(manTA, objcTA);

            // System.Double
            manTA = new ManagedTypeAssociation {
                ManagedType = "System.Double", ManagedTypeAlias = "double"
            };
            objcTA = new ObjCTypeAssociation {
                ObjCType = "double", GetterFormat = "DB_UNBOX_DOUBLE({0})"
            };
            AssociateTypes(manTA, objcTA);

            // System.Single
            manTA = new ManagedTypeAssociation {
                ManagedType = "System.Single", ManagedTypeAlias = "float", ManagedTypeInvoke = "single"
            };
            objcTA = new ObjCTypeAssociation {
                ObjCType = "float", GetterFormat = "DB_UNBOX_FLOAT({0})"
            };
            AssociateTypes(manTA, objcTA);

            // System.Boolean
            manTA = new ManagedTypeAssociation {
                ManagedType = "System.Boolean", ManagedTypeAlias = "bool"
            };
            objcTA = new ObjCTypeAssociation {
                ObjCType = "BOOL", GetterFormat = "DB_UNBOX_BOOLEAN({0})"
            };
            AssociateTypes(manTA, objcTA);

            // System.DateTime
            manTA = new ManagedTypeAssociation {
                ManagedType = "System.DateTime"
            };
            objcTA = new ObjCTypeAssociation {
                ObjCType = "NSDate", GetterFormat = "[NSDate dateWithMonoDateTime:{0}]"
            };
            AssociateTypes(manTA, objcTA);

            // System.Decimal
            manTA = new ManagedTypeAssociation {
                ManagedType = "System.Decimal", ManagedTypeAlias = "decimal", ManagedTypeInvoke = "System.Decimal"
            };
            objcTA = new ObjCTypeAssociation {
                ObjCType = "NSDecimalNumber", GetterFormat = "[NSDecimalNumber decimalNumberWithMonoDecimal:{0}]"
            };
            AssociateTypes(manTA, objcTA);

            // System.Enum
            manTA = new ManagedTypeAssociation {
                ManagedType = "System.Enum"
            };
            objcTA = new ObjCTypeAssociation {
                ObjCType = "System_Enum", GetterFormat = "[System_Enum objectWithMonoObject:{0}]"
            };
            AssociateTypes(manTA, objcTA);
        }
        //
        // ManagedValueToObjc()
        //
        // Return an ObjC expression that converts a Managed object to its corresponding ObjC representation
        //
        public string ManagedValueToObjc(string managedVarName, CodeFacet managedFacet, IList <string> args = null)
        {
            string managedType = ManagedTypeForAssociation(managedFacet);
            string exp         = null;
            string objCType    = null;

            // if type is an enum then use its underlying type
            if (managedFacet.IsEnum)
            {
                managedType = managedFacet.UnderlyingType;
            }

            // use type association if available
            ObjCTypeAssociation typeAssoc = ObjCTypeAssociate(managedType);

            if (typeAssoc != null)
            {
                // Use the getter format specifier if available.
                // This takes up to two arguments:
                // 1. a MonoObject * pointing to the underlying MonoObject.
                // 2. a Obj-C Class indicating the class to be used for types occurring in collections.
                string GetterFormat = typeAssoc.GetterFormat;
                if (GetterFormat != null)
                {
                    List <string> getterArgs = new List <string>();
                    getterArgs.Add(managedVarName);

                    if (managedFacet.IsPointer)
                    {
                        GetterFormat = "DB_UNBOX_PTR({0})";
                    }
                    else
                    {
                        // add any child type arguments representing generic types
                        if (managedFacet.ObjCFacet.GenericArgumentTypes != null && managedFacet.ObjCFacet.GenericArgumentTypes.Count() > 0)
                        {
                            getterArgs.AddRange(managedFacet.ObjCFacet.GenericArgumentTypes);
                        }

                        // TODO: provide class representation for arrays.
                        // Just as we provide a class rep for a generic the same will be required for an array.
                        if (managedFacet.IsArray)
                        {
                            getterArgs.Add("System_Object");
                        }

                        // We may require at least two arguments.
                        if (getterArgs.Count < 2)
                        {
                            getterArgs.Add("System_Object");
                        }

                        // add additional arguments
                        if (args != null)
                        {
                            getterArgs.AddRange(args);
                        }
                    }
                    exp = string.Format(GetterFormat, getterArgs.ToArray <string>());
                }

                // use custom method formatter
                else if (typeAssoc.GetterMethod != null)
                {
                    string     methodName = typeAssoc.GetterMethod;
                    Type       type       = GetType();
                    MethodInfo method     = type.GetMethod(methodName);
                    if (method != null)
                    {
                        exp = (string)method.Invoke(this, new object[] { managedVarName, managedType });
                    }
                }

                // use default object type
                else if (typeAssoc.IsNSObject)
                {
                    objCType = typeAssoc.ObjCType;
                }
            }

            if (exp == null)
            {
                // default to canonical type representation
                if (objCType == null)
                {
                    objCType = ObjCIdentifierFromManagedIdentifier(managedType);
                }

                // create DBManagedObject subclass
                exp = string.Format("[{0} bestObjectWithMonoObject:{1}]", objCType, managedVarName);
            }

            return(exp);
        }