protected virtual void AddPublicConstructor(MemberMethodDefinition function)
        {
            if (function == null)
            {
                AddPublicConstructorOverload(function, 0);
            }
            else
            {
                int defcount = 0;

                if (!function.HasAttribute <NoDefaultParamOverloadsAttribute>())
                {
                    foreach (ParamDefinition param in function.Parameters)
                    {
                        if (param.DefaultValue != null)
                        {
                            defcount++;
                        }
                    }
                }

                bool hideParams = function.HasAttribute <HideParamsWithDefaultValuesAttribute>();

                // The overloads (because of default values)
                for (int dc = 0; dc <= defcount; dc++)
                {
                    if (dc < defcount && function.HasAttribute <HideParamsWithDefaultValuesAttribute>())
                    {
                        continue;
                    }
                    AddPublicConstructorOverload(function, function.Parameters.Count - dc);
                }
            }
        }
        protected virtual void AddCreator(MemberMethodDefinition f)
        {
            if (f == null)
            {
                AddCreatorOverload(f, 0);
            }
            else
            {
                int defcount = 0;

                if (!f.HasAttribute <NoDefaultParamOverloadsAttribute>())
                {
                    foreach (ParamDefinition param in f.Parameters)
                    {
                        if (param.DefaultValue != null)
                        {
                            defcount++;
                        }
                    }
                }

                // The overloads (because of default values)
                for (int dc = 0; dc <= defcount; dc++)
                {
                    if (dc < defcount && f.HasAttribute <HideParamsWithDefaultValuesAttribute>())
                    {
                        continue;
                    }

                    AddCreatorOverload(f, f.Parameters.Count - dc);
                }
            }
        }
        protected virtual void AddCreator(MemberMethodDefinition f)
        {
            if (f == null)
            {
                _codeBuilder.AppendLine("static " + _classDefinition.CLRName + " Create();");
            }
            else
            {
                int defcount = 0;

                if (!f.HasAttribute <NoDefaultParamOverloadsAttribute>())
                {
                    foreach (ParamDefinition param in f.Parameters)
                    {
                        if (param.DefaultValue != null)
                        {
                            defcount++;
                        }
                    }
                }

                // The overloads (because of default values)
                for (int dc = 0; dc <= defcount; dc++)
                {
                    if (dc < defcount && f.HasAttribute <HideParamsWithDefaultValuesAttribute>())
                    {
                        continue;
                    }

                    _codeBuilder.AppendIndent("static " + _classDefinition.CLRName + " Create");
                    AddMethodParameters(f, f.Parameters.Count - dc);
                    _codeBuilder.Append(";\n");
                }
            }
        }
        protected virtual void AddPublicConstructor(MemberMethodDefinition function)
        {
            string className = (_classDefinition.IsInterface) ? _classDefinition.Name : _classDefinition.CLRName;

            if (function == null)
            {
                _codeBuilder.AppendLine(className + "();");
            }
            else
            {
                int defcount = 0;

                if (!function.HasAttribute <NoDefaultParamOverloadsAttribute>())
                {
                    foreach (ParamDefinition param in function.Parameters)
                    {
                        if (param.DefaultValue != null)
                        {
                            defcount++;
                        }
                    }
                }

                bool hideParams = function.HasAttribute <HideParamsWithDefaultValuesAttribute>();
                // The overloads (because of default values)
                for (int dc = 0; dc <= defcount; dc++)
                {
                    if (dc < defcount && hideParams)
                    {
                        continue;
                    }


                    _codeBuilder.AppendIndent(className);
                    AddMethodParameters(function, function.Parameters.Count - dc);
                    _codeBuilder.Append(";\n");
                }
            }
        }
        public override bool HasAttribute <T>()
        {
            if (base.HasAttribute <T>())
            {
                return(true);
            }

            if (BaseMethod != null)
            {
                return(BaseMethod.HasAttribute <T>());
            }

            return(false);
        }
        protected override void GenerateCodeMethod(MemberMethodDefinition f)
        {
            string def = f.Definition.Replace(f.ContainingClass.FullyQualifiedNativeName, GetClassName()) + "(";

            if (def.StartsWith("virtual "))
            {
                def = def.Substring("virtual ".Length);
            }
            _codeBuilder.AppendIndent(def);
            for (int i = 0; i < f.Parameters.Count; i++)
            {
                ParamDefinition param = f.Parameters[i];
                _codeBuilder.Append(" ");
                AddNativeMethodParam(param);
                if (i < f.Parameters.Count - 1)
                {
                    _codeBuilder.Append(",");
                }
            }
            _codeBuilder.Append(" )\n");
            _codeBuilder.AppendLine("{");
            _codeBuilder.IncreaseIndent();

            _codeBuilder.AppendLine("if (doCallFor" + f.CLRName + ")");
            _codeBuilder.AppendLine("{");
            _codeBuilder.IncreaseIndent();

            NativeProxyClassCppProducer.AddNativeProxyMethodBody(f, "_receiver", _codeBuilder);

            _codeBuilder.DecreaseIndent();
            _codeBuilder.AppendLine("}");
            if (!f.HasReturnValue)
            {
                if (!f.HasAttribute <DefaultReturnValueAttribute>())
                {
                    throw new Exception("Default return value not set.");
                }

                _codeBuilder.AppendLine("else");
                _codeBuilder.AppendLine("\treturn " + f.GetAttribute <DefaultReturnValueAttribute>().Name + ";");
            }

            _codeBuilder.DecreaseIndent();
            _codeBuilder.AppendLine("}");
        }
        /// <summary>
        /// Checks whether this method is a set accessor for a CLR property.
        /// </summary>
        /// <seealso cref="IsPropertyGetAccessor"/>
        public static bool CheckForSetAccessor(MemberMethodDefinition methodDef)
        {
            //
            // IMPORTANT: Don't use any of the "IsProperty..." properties of MemberMethodDefinition
            //   in this method as those properties use this method to determine their values.
            //
            if (methodDef.IsOverriding)
            {
                // Check this before checking possible attributes
                return(methodDef.BaseMethod.IsPropertySetAccessor);
            }

            if (methodDef.IsConstructor || methodDef.MemberTypeName != "void" || methodDef.Parameters.Count != 1)
            {
                // Check this before checking possible attributes
                return(false);
            }

            if (methodDef.HasAttribute <PropertyAttribute>())
            {
                return(true);
            }

            if (methodDef.HasAttribute <MethodAttribute>())
            {
                return(false);
            }

            if (methodDef.HasAttribute <CustomIncDeclarationAttribute>() ||
                methodDef.HasAttribute <CustomCppDeclarationAttribute>())
            {
                return(false);
            }

            string name = methodDef.GetRenameName();

            if (!name.StartsWith("set") || !Char.IsUpper(name[3]))
            {
                return(false);
            }

            // Check to see if there is a "get" function
            string propName = name.Substring(3);
            MemberMethodDefinition method;

            // TODO by manski: Allow the case that the getter and the setter come from different classes.
            //   Special care must be taken in this case as for example "Property.ContainingClass" can't
            //   be used anymore, since there are two classes involved. This could be solved by returning
            //   the "lowest" subclass (i.e. the class in which both accessors are defined then).
            //   Then the second argument should be changed to "true" here.
            method = methodDef.ContainingClass.GetMethodByNativeName("get" + propName, false, false);
            if (method == null)
            {
                method = methodDef.ContainingClass.GetMethodByNativeName("is" + propName, false, false);
                if (method == null)
                {
                    method = methodDef.ContainingClass.GetMethodByNativeName("has" + propName, false, false);
                }
            }

            // NOTE: Most checks done in "CheckForGetAccessor()" are represented in "method.IsPropertyGetAccessor".
            return(method != null && method.IsPropertyGetAccessor && method.MemberTypeName == methodDef.Parameters[0].TypeName &&
                   (!methodDef.ContainingClass.AllowVirtuals ||
                    (method.IsVirtual == methodDef.IsVirtual && method.IsOverriding == methodDef.IsOverriding)));
        }
        /// <summary>
        /// Checks whether this method is a get accessor for a CLR property.
        /// </summary>
        /// <seealso cref="IsPropertyGetAccessor"/>
        public static bool CheckForGetAccessor(MemberMethodDefinition methodDef)
        {
            //
            // IMPORTANT: Don't use any of the "IsProperty..." properties of MemberMethodDefinition
            //   in this method as those properties use this method to determine their values.
            //
            if (methodDef.IsOverriding)
            {
                // Check this before checking possible attributes
                return(methodDef.BaseMethod.IsPropertyGetAccessor);
            }

            if (methodDef.IsConstructor || methodDef.MemberTypeName == "void" || methodDef.Parameters.Count != 0)
            {
                // Check this before checking possible attributes
                return(false);
            }

            if (methodDef.HasAttribute <PropertyAttribute>())
            {
                return(true);
            }

            if (methodDef.HasAttribute <MethodAttribute>())
            {
                return(false);
            }

            if (methodDef.HasAttribute <CustomIncDeclarationAttribute>() ||
                methodDef.HasAttribute <CustomCppDeclarationAttribute>())
            {
                return(false);
            }

            string name = methodDef.GetRenameName();

            if (methodDef.MemberTypeName == "bool" &&
                ((name.StartsWith("is") && Char.IsUpper(name[2]) && methodDef.MetaDef.CodeStyleDef.AllowIsInPropertyName) ||
                 (name.StartsWith("has") && Char.IsUpper(name[3]))) &&
                methodDef.Parameters.Count == 0)
            {
                return(true);
            }

            if (!methodDef.MemberType.IsValueType &&
                (methodDef.MemberType.IsSharedPtr || methodDef.MemberType is DefTemplateOneType || methodDef.MemberType is DefTemplateTwoTypes))
            {
                return(false);
            }

            if (methodDef.MemberType.HasAttribute <ReturnOnlyByMethodAttribute>())
            {
                // Invalid type for a property
                return(false);
            }

            string propName;

            if (name.StartsWith("get") && Char.IsUpper(name[3]))
            {
                propName = name.Substring(3);
            }
            else if (name.StartsWith("is") && Char.IsUpper(name[2]))
            {
                propName = name.Substring(2);
            }
            else
            {
                // Not a valid getter prefix.
                return(false);
            }

            // Check if the property's name collides with the name of a nested type. In this
            // case we can't convert the method into a property.
            AbstractTypeDefinition type = methodDef.ContainingClass.GetNestedType(propName, false);

            if (type != null)
            {
                return(false);
            }

            // Check if the property's name collides with the name of a method. In this
            // case we can't convert the method into a property.
            MemberMethodDefinition method = methodDef.ContainingClass.GetMethodByCLRName(propName, true, false);

            // If there is no method == valid property name
            return(method == null);
        }
        protected override void GenerateCodeMethod(MemberMethodDefinition f)
        {
            if (f.HasAttribute <CustomCppDeclarationAttribute>())
            {
                if (f.IsAbstract && AllowSubclassing)
                {
                    return;
                }
                else
                {
                    string txt = f.GetAttribute <CustomCppDeclarationAttribute>().DeclarationText;
                    txt = ReplaceCustomVariables(txt, f);
                    _codeBuilder.AppendLine(txt);
                    _codeBuilder.AppendEmptyLine();
                    return;
                }
            }

            int defcount = 0;

            if (!f.HasAttribute <NoDefaultParamOverloadsAttribute>())
            {
                foreach (ParamDefinition param in f.Parameters)
                {
                    if (param.DefaultValue != null)
                    {
                        defcount++;
                    }
                }
            }

            bool methodIsVirtual = DeclareAsVirtual(f);

            for (int dc = 0; dc <= defcount; dc++)
            {
                if (dc == 0 && f.IsAbstract && AllowSubclassing)
                {
                    //It's abstract, no body definition
                    continue;
                }

                if (!AllowMethodOverloads && dc > 0)
                {
                    continue;
                }

                if (dc < defcount && f.HasAttribute <HideParamsWithDefaultValuesAttribute>())
                {
                    continue;
                }

                _codeBuilder.AppendIndent(GetCLRTypeName(f) + " " + GetClassName() + "::" + f.CLRName);
                AddMethodParameters(f, f.Parameters.Count - dc);
                _codeBuilder.Append("\n");
                _codeBuilder.AppendLine("{");
                _codeBuilder.IncreaseIndent();

                bool isVirtualOverload = dc > 0 && methodIsVirtual && AllowVirtualMethods;

                if (isVirtualOverload)
                {
                    // Overloads (because of default values)
                    // main method is virtual, call it with CLR default values if _isOverriden=true,
                    // else do a normal native call

                    _codeBuilder.AppendLine("if (_isOverriden)");
                    _codeBuilder.AppendLine("{");
                    _codeBuilder.IncreaseIndent();

                    bool hasPostConversions = false;
                    for (int i = f.Parameters.Count - dc; i < f.Parameters.Count; i++)
                    {
                        ParamDefinition p = f.Parameters[i];
                        if (!String.IsNullOrEmpty(p.CLRDefaultValuePreConversion))
                        {
                            _codeBuilder.AppendLine(p.CLRDefaultValuePreConversion);
                        }
                        if (!String.IsNullOrEmpty(p.CLRDefaultValuePostConversion))
                        {
                            hasPostConversions = true;
                        }

                        string n1, n2, n3;
                        AbstractTypeDefinition dependancy;
                        p.Type.ProduceDefaultParamValueConversionCode(p, out n1, out n2, out n3, out dependancy);
                        if (dependancy != null)
                        {
                            AddTypeDependancy(dependancy);
                        }
                    }

                    _codeBuilder.AppendIndent("");
                    if (!f.HasReturnValue)
                    {
                        if (hasPostConversions)
                        {
                            _codeBuilder.Append(GetCLRTypeName(f) + " mp_return = ");
                        }
                        else
                        {
                            _codeBuilder.Append("return ");
                        }
                    }

                    _codeBuilder.Append(f.CLRName + "(");
                    for (int i = 0; i < f.Parameters.Count; i++)
                    {
                        ParamDefinition p = f.Parameters[i];
                        _codeBuilder.Append(" ");
                        if (i < f.Parameters.Count - dc)
                        {
                            _codeBuilder.Append(p.Name);
                        }
                        else
                        {
                            _codeBuilder.Append(p.CLRDefaultValue);
                        }
                        if (i < f.Parameters.Count - 1)
                        {
                            _codeBuilder.Append(",");
                        }
                    }
                    _codeBuilder.Append(" );\n");

                    for (int i = f.Parameters.Count - dc; i < f.Parameters.Count; i++)
                    {
                        ParamDefinition p = f.Parameters[i];
                        if (!String.IsNullOrEmpty(p.CLRDefaultValuePostConversion))
                        {
                            _codeBuilder.AppendLine(p.CLRDefaultValuePostConversion);
                        }
                    }

                    if (!f.HasReturnValue && hasPostConversions)
                    {
                        _codeBuilder.AppendLine("return mp_return;");
                    }

                    _codeBuilder.DecreaseIndent();
                    _codeBuilder.AppendLine("}");
                    _codeBuilder.AppendLine("else");
                    _codeBuilder.AppendLine("{");
                    _codeBuilder.IncreaseIndent();
                }

                AddMethodBody(f, f.Parameters.Count - dc);

                if (isVirtualOverload)
                {
                    _codeBuilder.DecreaseIndent();
                    _codeBuilder.AppendLine("}");
                }

                _codeBuilder.DecreaseIndent();
                _codeBuilder.AppendLine("}");
            }
        }
        protected override void GenerateCodeMethod(MemberMethodDefinition f)
        {
            if (f.HasAttribute <CustomIncDeclarationAttribute>())
            {
                string txt = f.GetAttribute <CustomIncDeclarationAttribute>().DeclarationText;
                txt = ReplaceCustomVariables(txt, f);
                _codeBuilder.AppendLine(txt);
                _codeBuilder.AppendEmptyLine();
                return;
            }

            int defcount = 0;

            if (!f.HasAttribute <NoDefaultParamOverloadsAttribute>())
            {
                foreach (ParamDefinition param in f.Parameters)
                {
                    if (param.DefaultValue != null)
                    {
                        defcount++;
                    }
                }
            }

            bool methodIsVirtual = DeclareAsVirtual(f);

            // The main method
            AddComments(f);

            if (AllowMethodIndexAttributes && f.IsVirtual && !f.IsAbstract)
            {
                AddMethodIndexAttribute(f);
            }

            _codeBuilder.AppendIndent("");
            if (f.IsStatic)
            {
                _codeBuilder.Append("static ");
            }
            if (methodIsVirtual)
            {
                _codeBuilder.Append("virtual ");
            }
            _codeBuilder.Append(GetCLRTypeName(f) + " " + f.CLRName);
            AddMethodParameters(f, f.Parameters.Count);
            if (DeclareAsOverride(f))
            {
                _codeBuilder.Append(" override");
            }
            else if (f.IsAbstract && AllowSubclassing)
            {
                _codeBuilder.Append(" abstract");
            }

            _codeBuilder.Append(";\n");

            if (AllowMethodOverloads)
            {
                // The overloads (because of default values)
                for (int dc = 1; dc <= defcount; dc++)
                {
                    if (dc < defcount && f.HasAttribute <HideParamsWithDefaultValuesAttribute>())
                    {
                        continue;
                    }

                    AddComments(f);
                    _codeBuilder.AppendIndent("");
                    if (f.IsStatic)
                    {
                        _codeBuilder.Append("static ");
                    }
                    _codeBuilder.Append(GetCLRTypeName(f) + " " + f.CLRName);
                    AddMethodParameters(f, f.Parameters.Count - dc);
                    _codeBuilder.Append(";\n");
                }
            }
        }
Beispiel #11
0
        protected override void AddOverridableFunction(MemberMethodDefinition f)
        {
            _wrapper.CppCheckTypeForDependancy(f.MemberType);
            foreach (ParamDefinition param in f.Parameters)
            {
                _wrapper.CppCheckTypeForDependancy(param.Type);
            }

            _codeBuilder.AppendIndent("");
            _codeBuilder.Append(f.MemberTypeNativeName + " " + ProxyName + "::" + f.NativeName + "(");
            AddNativeMethodParams(f);
            _codeBuilder.Append(" )");
            if (f.IsConstMethod)
            {
                _codeBuilder.Append(" const");
            }
            _codeBuilder.Append("\n");
            _codeBuilder.AppendLine("{");
            _codeBuilder.IncreaseIndent();

            if (!f.IsAbstract)
            {
                _codeBuilder.AppendLine("if (_overriden[ " + _methodIndices[f] + " ])");
                _codeBuilder.AppendLine("{");
                _codeBuilder.IncreaseIndent();
            }

            if (f.HasAttribute <CustomNativeProxyDeclarationAttribute>())
            {
                string txt = f.GetAttribute <CustomNativeProxyDeclarationAttribute>().DeclarationText;
                txt = ReplaceCustomVariables(txt, f).Replace("@MANAGED@", "_managed");
                _codeBuilder.AppendLine(txt);
            }
            else
            {
                AddNativeProxyMethodBody(f, "_managed", _codeBuilder);
            }

            if (!f.IsAbstract)
            {
                _codeBuilder.DecreaseIndent();
                _codeBuilder.AppendLine("}");
                _codeBuilder.AppendLine("else");
                _codeBuilder.AppendIndent("\t");
                if (!f.HasReturnValue)
                {
                    _codeBuilder.Append("return ");
                }
                _codeBuilder.Append(f.ContainingClass.Name + "::" + f.NativeName + "(");
                for (int i = 0; i < f.Parameters.Count; i++)
                {
                    ParamDefinition param = f.Parameters[i];
                    _codeBuilder.Append(" " + param.Name);
                    if (i < f.Parameters.Count - 1)
                    {
                        _codeBuilder.Append(",");
                    }
                }
                _codeBuilder.Append(" );\n");
            }

            _codeBuilder.DecreaseIndent();
            _codeBuilder.AppendLine("}");
        }
Beispiel #12
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);
                }
            }
        }