Example #1
0
 protected override void GenerateCodeProperty(MemberPropertyDefinition p)
 {
     if (p.IsVirtual)
     {
         base.GenerateCodeProperty(p);
     }
 }
Example #2
0
        protected MemberPropertyDefinition EnhanceProperty(MemberPropertyDefinition property)
        {
            MemberPropertyDefinition prop = property.Clone();

            if (_classDefinition.BaseClass != null)
            {
                if (!prop.CanWrite)
                {
                    // There's a chance the class overrides only the get function. If the
                    // property is not declared virtual then it will be read-only, so if
                    // it's not virtual check if a base class contains the set property
                    // and if it does, include it.
                    // If it's virtual and its base function will be declared virtual, this
                    // function will be declared virtual too, so it's not necessary to add
                    // the set function.

                    if (!DeclareAsVirtual(prop.GetterFunction))
                    {
                        MemberPropertyDefinition bp = _classDefinition.BaseClass.GetProperty(prop.Name, true);
                        if (bp != null && bp.CanWrite)
                        {
                            prop.SetterFunction = bp.SetterFunction;
                        }
                    }
                }

                if (!prop.CanRead)
                {
                    // There's a chance the class overrides only the set function. If the
                    // property is not declared virtual then it will be write-only, so if
                    // it's not virtual check if a base class contains the set property
                    // and if it does, include it.
                    // If it's virtual and its base function will be declared virtual, this
                    // function will be declared virtual too, so it's not necessary to add
                    // the get function.

                    if (!DeclareAsVirtual(prop.SetterFunction))
                    {
                        MemberPropertyDefinition bp = _classDefinition.BaseClass.GetProperty(prop.Name, true);
                        if (bp != null && bp.CanRead)
                        {
                            prop.GetterFunction = bp.GetterFunction;
                        }
                    }
                }
            }

            return(prop);
        }
Example #3
0
        /// <summary>
        /// Checks whether the specified property can be added to the generated source code.
        /// </summary>
        protected virtual bool IsPropertyAllowed(MemberPropertyDefinition p)
        {
            // If the property is ignored or the property is unhandled
            if (p.IsIgnored() || !p.IsTypeHandled())
            {
                return(false);
            }

            if (p.ContainingClass.IsSingleton && (p.Name == "Singleton" || p.Name == "SingletonPtr"))
            {
                return(false);
            }

            return(true);
        }
Example #4
0
 protected override bool IsPropertyAllowed(MemberPropertyDefinition p)
 {
     if (base.IsPropertyAllowed(p))
     {
         if (p.Name == "Singleton" || p.Name == "SingletonPtr")
         {
             return(false);
         }
         else
         {
             return(true);
         }
     }
     else
     {
         return(false);
     }
 }
Example #5
0
        public virtual MemberPropertyDefinition[] GetProperties()
        {
            if (_properties != null)
            {
                return(_properties);
            }

            _properties = MemberPropertyDefinition.GetPropertiesFromMethods(this.Methods);

            if (GetInterfaces().Length > 0)
            {
                foreach (MemberPropertyDefinition prop in _properties)
                {
                    if (!prop.CanWrite)
                    {
                        foreach (ClassDefinition iface in GetInterfaces())
                        {
                            MemberPropertyDefinition ip = iface.GetProperty(prop.Name, true);
                            if (ip != null && ip.CanWrite)
                            {
                                prop.SetterFunction = ip.SetterFunction;
                                break;
                            }
                        }
                    }

                    if (!prop.CanRead)
                    {
                        foreach (ClassDefinition iface in GetInterfaces())
                        {
                            MemberPropertyDefinition ip = iface.GetProperty(prop.Name, true);
                            if (ip != null && ip.CanRead)
                            {
                                prop.GetterFunction = ip.GetterFunction;
                                break;
                            }
                        }
                    }
                }
            }

            return(_properties);
        }
Example #6
0
        public virtual MemberPropertyDefinition GetProperty(string name, bool inherit)
        {
            MemberPropertyDefinition prop = null;

            foreach (MemberPropertyDefinition p in this.GetProperties())
            {
                if (p.Name == name)
                {
                    prop = p;
                    break;
                }
            }

            if (prop == null && inherit && BaseClass != null)
            {
                return(BaseClass.GetProperty(name, inherit));
            }
            else
            {
                return(prop);
            }
        }
        /// <summary>
        /// Converts the specified methods into CLR properties.
        /// </summary>
        /// <param name="methods">The methods to convert. Methods that are no accesors (<c>IsProperty == false</c>) will be ignored.</param>
        public static MemberPropertyDefinition[] GetPropertiesFromMethods(IEnumerable <MemberMethodDefinition> methods)
        {
            // NOTE: Use a sorted "list" here so that the order (and thereby the position) of thy properties in
            //   the generated source code doesn't vary each time the sources are generated.
            SortedDictionary <string, MemberPropertyDefinition> props = new SortedDictionary <string, MemberPropertyDefinition>();

            foreach (MemberMethodDefinition f in methods)
            {
                if (!f.IsProperty || !f.IsDeclarableFunction)
                {
                    continue;
                }

                MemberPropertyDefinition p    = null;
                string propName               = GetPropertyName(f);
                CodeStyleDefinition codeStyle = f.MetaDef.CodeStyleDef;

                if (props.ContainsKey(propName))
                {
                    p = props[propName];
                }
                else
                {
                    //
                    // Merge properties with "is" or "has" prefix as this prefix can only be determined
                    // from the get accessor but not from the set accessor.
                    //
                    if (f.IsPropertySetAccessor)
                    {
                        // Set accessor - check for existing properties prefixed with "is" or "has".
                        if (codeStyle.AllowIsInPropertyName && props.ContainsKey(codeStyle.CLRPropertyIsPrefix + propName))
                        {
                            propName = codeStyle.CLRPropertyIsPrefix + propName;
                            p        = props[propName];
                        }
                        else if (props.ContainsKey(codeStyle.CLRPropertyHasPrefix + propName))
                        {
                            propName = codeStyle.CLRPropertyHasPrefix + propName;
                            p        = props[propName];
                        }
                    }
                    else
                    {
                        // Get accessor
                        string oldPropName = null;
                        if (codeStyle.AllowIsInPropertyName &&
                            propName.StartsWith(codeStyle.CLRPropertyIsPrefix) &&
                            props.ContainsKey(propName.Substring(codeStyle.CLRPropertyIsPrefix.Length)))
                        {
                            // is prefix
                            oldPropName = propName.Substring(codeStyle.CLRPropertyIsPrefix.Length);
                        }
                        else if (propName.StartsWith(codeStyle.CLRPropertyHasPrefix) &&
                                 props.ContainsKey(propName.Substring(codeStyle.CLRPropertyHasPrefix.Length)))
                        {
                            // has prefix
                            oldPropName = propName.Substring(codeStyle.CLRPropertyHasPrefix.Length);
                        }

                        if (oldPropName != null)
                        {
                            MemberPropertyDefinition oldProp = props[oldPropName];
                            // We need to check here whether the set accessor is actually
                            if (oldProp.MemberTypeName == f.MemberTypeName)
                            {
                                props.Remove(oldPropName);

                                p = new MemberPropertyDefinition(propName);
                                p.MemberTypeName = oldProp.MemberTypeName;
                                p.PassedByType   = oldProp.PassedByType;
                                p.SetterFunction = oldProp.SetterFunction;

                                props.Add(p.Name, p);
                            }
                        }
                    }

                    if (p == null)
                    {
                        // New property found
                        p = new MemberPropertyDefinition(propName);
                        if (f.IsPropertyGetAccessor)
                        {
                            p.MemberTypeName = f.MemberTypeName;
                            p.PassedByType   = f.PassedByType;
                        }
                        else
                        {
                            p.MemberTypeName = f.Parameters[0].TypeName;
                            p.PassedByType   = f.Parameters[0].PassedByType;
                        }

                        props.Add(p.Name, p);
                    }
                }

                if (f.IsPropertyGetAccessor)
                {
                    p.GetterFunction = f;
                }
                else if (f.IsPropertySetAccessor)
                {
                    p.SetterFunction = f;
                }
            }

            return(props.Values.ToArray());
        }
Example #8
0
        protected override void GenerateCodeProperty(MemberPropertyDefinition p)
        {
            string ptype = GetCLRTypeName(p);
            string pname = GetClassName() + "::" + p.Name;

            if (p.CanRead)
            {
                if (!(p.GetterFunction.IsAbstract && AllowSubclassing))
                {
                    if (AllowProtectedMembers || p.GetterFunction.ProtectionLevel != ProtectionLevel.Protected)
                    {
                        string managedType = GetMethodNativeCall(p.GetterFunction, 0);

                        _codeBuilder.AppendLine(ptype + " " + pname + "::get()");
                        _codeBuilder.AppendLine("{");
                        if (_cachedMembers.Contains(p.GetterFunction))
                        {
                            string priv = NameToPrivate(p.Name);
                            _codeBuilder.AppendLine("\treturn ( CLR_NULL == " + priv + " ) ? (" + priv + " = " + managedType + ") : " + priv + ";");
                        }
                        else
                        {
                            _codeBuilder.AppendLine("\treturn " + managedType + ";");
                        }
                        _codeBuilder.AppendLine("}");
                    }
                }
            }

            if (p.CanWrite)
            {
                if (!(p.SetterFunction.IsAbstract && AllowSubclassing))
                {
                    if (AllowProtectedMembers || p.SetterFunction.ProtectionLevel != ProtectionLevel.Protected)
                    {
                        _codeBuilder.AppendLine("void " + pname + "::set( " + ptype + " " + p.SetterFunction.Parameters[0].Name + " )");
                        _codeBuilder.AppendLine("{");
                        _codeBuilder.IncreaseIndent();

                        string preCall    = GetMethodPreNativeCall(p.SetterFunction, 1);
                        string nativeCall = GetMethodNativeCall(p.SetterFunction, 1);
                        string postCall   = GetMethodPostNativeCall(p.SetterFunction, 1);

                        if (!String.IsNullOrEmpty(preCall))
                        {
                            _codeBuilder.AppendLine(preCall);
                        }

                        _codeBuilder.AppendLine(nativeCall + ";");

                        if (!String.IsNullOrEmpty(postCall))
                        {
                            _codeBuilder.AppendLine(postCall);
                        }

                        _codeBuilder.DecreaseIndent();
                        _codeBuilder.AppendLine("}");
                    }
                }
            }
        }
Example #9
0
        protected override void GenerateCodeProperty(MemberPropertyDefinition p)
        {
            //TODO comments for properties
            //AddComments(p);
            string ptype = GetCLRTypeName(p);

            _codeBuilder.AppendFormatIndent("property {0} {1}\n{{\n", ptype, p.Name);
            if (p.CanRead)
            {
                MemberMethodDefinition f = p.GetterFunction;
                bool methodIsVirtual     = DeclareAsVirtual(f);

                if (p.GetterFunction.ProtectionLevel == ProtectionLevel.Public || (AllowProtectedMembers && p.GetterFunction.ProtectionLevel == ProtectionLevel.Protected))
                {
                    _codeBuilder.AppendLine(p.GetterFunction.ProtectionLevel.GetCLRProtectionName() + ":");

                    if (AllowMethodIndexAttributes && f.IsVirtual && !f.IsAbstract)
                    {
                        _codeBuilder.Append("\t");
                        AddMethodIndexAttribute(f);
                    }

                    _codeBuilder.AppendIndent("\t");
                    if (p.GetterFunction.IsStatic)
                    {
                        _codeBuilder.Append("static ");
                    }
                    if (methodIsVirtual)
                    {
                        _codeBuilder.Append("virtual ");
                    }
                    _codeBuilder.Append(ptype + " get()");
                    if (DeclareAsOverride(p.GetterFunction))
                    {
                        _codeBuilder.Append(" override");
                    }
                    else if (f.IsAbstract && AllowSubclassing)
                    {
                        _codeBuilder.Append(" abstract");
                    }
                    _codeBuilder.Append(";\n");
                }
            }
            if (p.CanWrite)
            {
                MemberMethodDefinition f = p.SetterFunction;
                bool methodIsVirtual     = DeclareAsVirtual(f);

                if (p.SetterFunction.ProtectionLevel == ProtectionLevel.Public || (AllowProtectedMembers && p.SetterFunction.ProtectionLevel == ProtectionLevel.Protected))
                {
                    _codeBuilder.AppendLine(p.SetterFunction.ProtectionLevel.GetCLRProtectionName() + ":");

                    if (AllowMethodIndexAttributes && f.IsVirtual && !f.IsAbstract)
                    {
                        _codeBuilder.Append("\t");
                        AddMethodIndexAttribute(f);
                    }

                    _codeBuilder.AppendIndent("\t");
                    if (p.SetterFunction.IsStatic)
                    {
                        _codeBuilder.Append("static ");
                    }
                    if (methodIsVirtual)
                    {
                        _codeBuilder.Append("virtual ");
                    }
                    _codeBuilder.Append("void set(" + ptype + " " + p.SetterFunction.Parameters[0].Name + ")");
                    if (DeclareAsOverride(p.SetterFunction))
                    {
                        _codeBuilder.Append(" override");
                    }
                    else if (f.IsAbstract && AllowSubclassing)
                    {
                        _codeBuilder.Append(" abstract");
                    }
                    _codeBuilder.Append(";\n");
                }
            }
            _codeBuilder.AppendLine("}");
        }
Example #10
0
 protected virtual void GenerateCodeInterfaceProperty(MemberPropertyDefinition prop)
 {
     GenerateCodeProperty(prop);
 }
Example #11
0
        protected virtual void Init()
        {
            if (_initCalled)
            {
                return;
            }

            _initCalled = true;

            foreach (MemberMethodDefinition f in _classDefinition.PublicMethods)
            {
                if (f.IsListenerAdder && !f.IsIgnored)
                {
                    _listeners.Add((ClassDefinition)f.Parameters[0].Type);
                }
            }

            foreach (ClassDefinition iface in _classDefinition.GetInterfaces())
            {
                // Add attributes of interface methods from the interface classes
                foreach (MemberMethodDefinition f in iface.Methods)
                {
                    MemberMethodDefinition tf = _classDefinition.GetMethodWithSignature(f.Signature);
                    if (tf != null)
                    {
                        tf.AddAttributes(f.Attributes);
                    }
                }

                //Store properties of interface classes. They have precedence over type's properties.
                foreach (MemberPropertyDefinition ip in iface.GetProperties())
                {
                    if (IsPropertyAllowed(ip) &&
                        (ip.ProtectionLevel == ProtectionLevel.Public ||
                         (AllowProtectedMembers && ip.ProtectionLevel == ProtectionLevel.Protected)))
                    {
                        _interfaceProperties.Add(ip);
                    }
                }
            }

            foreach (MemberFieldDefinition field in _classDefinition.Fields)
            {
                if (!field.IsIgnored && field.MemberType.IsSTLContainer)
                {
                    if (field.ProtectionLevel == ProtectionLevel.Public ||
                        ((AllowSubclassing || AllowProtectedMembers) && field.ProtectionLevel == ProtectionLevel.Protected))
                    {
                        MarkCachedMember(field);
                    }
                }
            }

            foreach (ClassDefinition iface in _interfaces)
            {
                if (iface == _classDefinition)
                {
                    continue;
                }

                foreach (MemberFieldDefinition field in iface.Fields)
                {
                    if (!field.IsIgnored && field.MemberType.IsSTLContainer &&
                        !field.IsStatic)
                    {
                        if (field.ProtectionLevel == ProtectionLevel.Public ||
                            ((AllowSubclassing || AllowProtectedMembers) && field.ProtectionLevel == ProtectionLevel.Protected))
                        {
                            MarkCachedMember(field);
                        }
                    }
                }
            }

            foreach (MemberMethodDefinition func in _classDefinition.AbstractFunctions)
            {
                if (func.ProtectionLevel == ProtectionLevel.Public ||
                    (AllowProtectedMembers && func.ProtectionLevel == ProtectionLevel.Protected))
                {
                    if ((func.ContainingClass.AllowSubClassing || (func.ContainingClass == _classDefinition && AllowSubclassing)) && !func.IsProperty)
                    {
                        _isAbstractClass = true;
                        _abstractFunctions.Add(func);
                    }
                }
            }

            foreach (MemberPropertyDefinition prop in _classDefinition.AbstractProperties)
            {
                if (IsPropertyAllowed(prop) && (prop.ContainingClass.AllowSubClassing ||
                                                (prop.ContainingClass == _classDefinition && AllowSubclassing)))
                {
                    if (prop.ProtectionLevel == ProtectionLevel.Public ||
                        (AllowProtectedMembers && prop.ProtectionLevel == ProtectionLevel.Protected))
                    {
                        _isAbstractClass = true;
                        _abstractProperties.Add(prop);
                    }
                }
            }

            SearchOverridableFunctions(_classDefinition);
            //SearchProtectedFields(_t);

            foreach (ClassDefinition iface in _interfaces)
            {
                SearchOverridableFunctions(iface);
                //SearchProtectedFields(iface);
            }

            _overridableProperties = MemberPropertyDefinition.GetPropertiesFromMethods(_overridableFunctions);

            //Find cached members

            foreach (MemberDefinitionBase m in _classDefinition.Members)
            {
                if (m.HasAttribute <CachedGetAccessorAttribute>())
                {
                    MarkCachedMember(m);
                }
            }

            foreach (ClassDefinition iface in _interfaces)
            {
                if (iface == _classDefinition)
                {
                    continue;
                }

                foreach (MemberDefinitionBase m in iface.Members)
                {
                    if (m.HasAttribute <CachedGetAccessorAttribute>())
                    {
                        MarkCachedMember(m);
                    }
                }
            }
        }
Example #12
0
 protected virtual void GenerateCodeProperty(MemberPropertyDefinition prop)
 {
 }
Example #13
0
        public static void AddNativeProxyMethodBody(MemberMethodDefinition f, string managedTarget, SourceCodeStringBuilder sb)
        {
            string managedCall;
            string fullPostConv = null;

            if (f.IsPropertyGetAccessor)
            {
                sb.AppendLine(f.MemberTypeCLRName + " mp_return = " + managedTarget + "->" + MemberPropertyDefinition.GetPropertyName(f) + ";");
                managedCall = "mp_return";
            }
            else if (f.IsPropertySetAccessor)
            {
                ParamDefinition param = f.Parameters[0];
                managedCall = managedTarget + "->" + MemberPropertyDefinition.GetPropertyName(f) + " = " + param.Type.ProduceNativeCallConversionCode(param.Name, param);
            }
            else
            {
                string pre, post, conv;

                foreach (ParamDefinition param in f.Parameters)
                {
                    param.Type.ProduceNativeParamConversionCode(param, out pre, out conv, out post);
                    if (!String.IsNullOrEmpty(pre))
                    {
                        sb.AppendLine(pre);
                    }

                    if (!String.IsNullOrEmpty(post))
                    {
                        fullPostConv += post + "\n";
                    }
                }

                bool explicitCast = f.HasAttribute <ExplicitCastingForParamsAttribute>();

                if (!f.HasReturnValue)
                {
                    sb.AppendIndent(f.MemberTypeCLRName + " mp_return = " + managedTarget + "->" + f.CLRName + "(");
                    for (int i = 0; i < f.Parameters.Count; i++)
                    {
                        ParamDefinition param = f.Parameters[i];
                        param.Type.ProduceNativeParamConversionCode(param, out pre, out conv, out post);
                        sb.Append(" ");
                        if (explicitCast)
                        {
                            sb.Append("(" + param.MemberTypeCLRName + ")");
                        }
                        sb.Append(conv);
                        if (i < f.Parameters.Count - 1)
                        {
                            sb.Append(",");
                        }
                    }
                    sb.Append(" );\n");
                    managedCall = "mp_return";

                    if (!String.IsNullOrEmpty(fullPostConv))
                    {
                        sb.AppendLine(fullPostConv);
                    }
                }
                else
                {
                    managedCall = managedTarget + "->" + f.CLRName + "(";
                    for (int i = 0; i < f.Parameters.Count; i++)
                    {
                        ParamDefinition param = f.Parameters[i];
                        param.Type.ProduceNativeParamConversionCode(param, out pre, out conv, out post);
                        managedCall += " ";
                        if (explicitCast)
                        {
                            managedCall += "(" + param.MemberTypeCLRName + ")";
                        }
                        managedCall += conv;
                        if (i < f.Parameters.Count - 1)
                        {
                            managedCall += ",";
                        }
                    }
                    managedCall += " )";
                }
            }

            if (!f.HasReturnValue)
            {
                if (f.MemberType is IDefString)
                {
                    sb.AppendLine("SET_NATIVE_STRING( Mogre::Implementation::cachedReturnString, " + managedCall + " )");
                    sb.AppendLine("return Mogre::Implementation::cachedReturnString;");
                }
                else
                {
                    string          returnExpr;
                    string          newname, expr, postcall;
                    ParamDefinition param = new ParamDefinition(f.MetaDef, f, managedCall);
                    expr     = f.MemberType.ProducePreCallParamConversionCode(param, out newname);
                    postcall = f.MemberType.ProducePostCallParamConversionCleanupCode(param);
                    if (!String.IsNullOrEmpty(expr))
                    {
                        sb.AppendLine(expr);
                        if (String.IsNullOrEmpty(postcall))
                        {
                            returnExpr = newname;
                        }
                        else
                        {
                            throw new Exception("Unexpected");
                        }
                    }
                    else
                    {
                        returnExpr = newname;
                    }

                    if (IsCachedFunction(f))
                    {
                        sb.AppendLine("STATIC_ASSERT( sizeof(" + f.MemberType.FullyQualifiedNativeName + ") <= CACHED_RETURN_SIZE )");
                        sb.AppendLine("memcpy( Mogre::Implementation::cachedReturn, &" + returnExpr + ", sizeof(" + f.MemberType.FullyQualifiedNativeName + ") );");
                        sb.AppendLine("return *reinterpret_cast<" + f.MemberType.FullyQualifiedNativeName + "*>(Mogre::Implementation::cachedReturn);");
                    }
                    else
                    {
                        sb.AppendLine("return " + returnExpr + ";");
                    }
                }
            }
            else
            {
                sb.AppendLine(managedCall + ";");

                if (!String.IsNullOrEmpty(fullPostConv))
                {
                    sb.AppendLine(fullPostConv);
                }
            }
        }