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 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 AddDisposerBody() { if (_classDefinition.HasAttribute <CustomDisposingAttribute>()) { string text = _classDefinition.GetAttribute <CustomDisposingAttribute>().Text; _codeBuilder.AppendLine(text); } foreach (ClassDefinition cls in _listeners) { MemberMethodDefinition removerFunc = null; foreach (MemberMethodDefinition func in _classDefinition.PublicMethods) { if (func.IsListenerRemover && func.Parameters[0].Type == cls) { removerFunc = func; break; } } if (removerFunc == null) { throw new Exception("Unexpected"); } string native = "_native"; _codeBuilder.AppendLine(String.Format("if ({0} != 0)\n{{\n\tif (" + native + " != 0) " + GetNativeInvokationTarget(removerFunc) + "({0});\n\tdelete {0}; {0} = 0;\n}}", NameToPrivate(cls.Name))); } }
protected override void GenerateCodeInterfaceMethod(MemberMethodDefinition f) { _codeBuilder.DecreaseIndent(); _codeBuilder.AppendLine(f.ProtectionLevel.GetCLRProtectionName() + ":"); _codeBuilder.IncreaseIndent(); base.GenerateCodeInterfaceMethod(f); }
protected virtual void AddMethodBody(MemberMethodDefinition f, int count) { string preCall = GetMethodPreNativeCall(f, count); string nativeCall = GetMethodNativeCall(f, count); string postCall = GetMethodPostNativeCall(f, count); if (!String.IsNullOrEmpty(preCall)) { _codeBuilder.AppendLine(preCall); } if (f.HasReturnValue) { _codeBuilder.AppendLine(nativeCall + ";"); if (!String.IsNullOrEmpty(postCall)) { _codeBuilder.AppendLine(postCall); } } else { if (String.IsNullOrEmpty(postCall)) { _codeBuilder.AppendLine("return " + nativeCall + ";"); } else { _codeBuilder.AppendLine(GetCLRTypeName(f) + " retres = " + nativeCall + ";"); _codeBuilder.AppendLine(postCall); _codeBuilder.AppendLine("return retres;"); } } }
protected override void GenerateCodeMethod(MemberMethodDefinition f) { if (f.IsVirtual) { base.GenerateCodeMethod(f); } }
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); } } }
public ClassDefinition(NamespaceDefinition nsDef, ClassDefinition surroundingClass, XmlElement elem) : base(nsDef, surroundingClass, elem) { if (GetType() == typeof(ClassDefinition) && elem.Name != "class") { throw new Exception("Not class element"); } foreach (XmlElement child in elem.ChildNodes) { switch (child.Name) { case "function": MemberMethodDefinition func = new MemberMethodDefinition(child, this); if (func.NativeName != "DECLARE_INIT_CLROBJECT_METHOD_OVERRIDE" && func.NativeName != "_Init_CLRObject" && !func.NativeName.StartsWith("OGRE_")) { AddNewFunction(func); } break; case "variable": MemberFieldDefinition field = new MemberFieldDefinition(child, this); if (field.NativeName != Name && field.NativeName != "DECLARE_CLRHANDLE" && field.NativeName != "_CLRHandle" && !field.NativeName.StartsWith("OGRE_")) { Members.Add(field); } break; case "inherits": List <string> ilist = new List <string>(); foreach (XmlElement sub in child.ChildNodes) { if (sub.Name != "baseClass") { throw new Exception("Unknown element; expected 'baseClass'"); } if (sub.InnerText != "") { if (sub.InnerText == "CLRObject") { this.AddAttribute(new CLRObjectAttribute()); this._isDirectSubclassOfCLRObject = true; } else { ilist.Add(sub.InnerText); } } } BaseClassesNames = ilist.ToArray(); break; default: AbstractTypeDefinition type = MetaDef.Factory.CreateType(Namespace, this, child); NestedTypes.Add(type); break; } } }
protected override bool DeclareAsOverride(MemberMethodDefinition f) { if (f.ProtectionLevel == ProtectionLevel.Public) { return(true); } else { return(false); } }
protected bool ContainsFunction(MemberMethodDefinition func, List <MemberMethodDefinition> list) { foreach (MemberMethodDefinition lf in list) { if (lf.Signature == func.Signature) { return(true); } } return(false); }
protected virtual string GetMethodPostNativeCall(MemberMethodDefinition f, int paramCount) { string res = String.Empty; for (int i = 0; i < paramCount; i++) { ParamDefinition p = f.Parameters[i]; res += p.Type.ProducePostCallParamConversionCleanupCode(p); } return(res); }
protected virtual string GetMethodPreNativeCall(MemberMethodDefinition f, int paramCount) { string res = String.Empty; for (int i = 0; i < paramCount; i++) { ParamDefinition p = f.Parameters[i]; string newname; res += p.Type.ProducePreCallParamConversionCode(p, out newname); } return(res); }
protected virtual void AddMethodParameters(MemberMethodDefinition f, int count) { _codeBuilder.Append("("); for (int i = 0; i < count; i++) { ParamDefinition p = f.Parameters[i]; _codeBuilder.Append(" " + GetCLRParamTypeName(p) + " " + p.Name); if (i < count - 1) { _codeBuilder.Append(","); } } _codeBuilder.Append(" )"); }
protected override void AddConstructor(MemberMethodDefinition f) { string className; if (_classDefinition.IsNested) { className = _classDefinition.SurroundingClass.FullyQualifiedCLRName + "::" + _classDefinition.Name; } else { className = this.MetaDef.ManagedNamespace + "::" + _classDefinition.Name; } _codeBuilder.AppendIndent(ProxyName + "( " + className + "^ managedObj"); if (f != null) { foreach (ParamDefinition param in f.Parameters) { _codeBuilder.Append(", " + param.MemberTypeNativeName + " " + param.Name); } } _codeBuilder.Append(" ) :\n"); if (f != null) { _codeBuilder.AppendIndent("\t" + _classDefinition.FullyQualifiedNativeName + "("); 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.AppendIndent("\t_managed(managedObj)"); //foreach (DefField field in _protectedFields) //{ // _sb.Append(",\n"); // _sb.AppendIndent("\tref_" + field.Name + "(" + field.Name + ")"); //} _codeBuilder.Append("\n"); _codeBuilder.AppendLine("{"); _codeBuilder.AppendLine("}"); }
protected override void GenerateCodeMethod(MemberMethodDefinition f) { _codeBuilder.AppendIndent(f.Definition.Replace(f.ContainingClass.FullyQualifiedNativeName + "::", "") + "("); 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(" ) override;\n"); }
protected virtual void AddNativeMethodParams(MemberMethodDefinition f) { for (int i = 0; i < f.Parameters.Count; i++) { ParamDefinition param = f.Parameters[i]; _codeBuilder.Append(" "); _codeBuilder.Append(param.MemberTypeNativeName); _codeBuilder.Append(" " + param.Name); if (i < f.Parameters.Count - 1) { _codeBuilder.Append(","); } } }
protected override void AddOverridableFunction(MemberMethodDefinition f) { _codeBuilder.AppendIndent(""); if (f.IsVirtual) { _codeBuilder.Append("virtual "); } _codeBuilder.Append(f.MemberTypeNativeName + " " + f.NativeName + "("); AddNativeMethodParams(f); _codeBuilder.Append(" ) "); if (f.IsConstMethod) { _codeBuilder.Append("const "); } _codeBuilder.Append("override;\n"); }
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("}"); }
private void AddNewFunction(MemberMethodDefinition func) { MemberDefinitionBase[] members = GetMembers(func.NativeName, false); MemberMethodDefinition prevf = null; foreach (MemberDefinitionBase m in members) { if (m is MemberMethodDefinition) { if (((MemberMethodDefinition)m).Signature.Equals(func.Signature, false)) { prevf = m as MemberMethodDefinition; break; } } } if (prevf != null) { if ((prevf.IsConstMethod && func.IsConstMethod) || (!prevf.IsConstMethod && !func.IsConstMethod)) { //throw new Exception("couldn't pick a function to keep"); //Add it and sort them out later Members.Add(func); } if ((prevf.ProtectionLevel == func.ProtectionLevel && prevf.IsConstMethod) || (prevf.ProtectionLevel == ProtectionLevel.Protected && func.ProtectionLevel == ProtectionLevel.Public)) { Members.Remove(prevf); Members.Add(func); } else { // Don't add func to Members; } } else { Members.Add(func); } }
protected virtual string GetMethodNativeCall(MemberMethodDefinition f, int paramCount) { string invoke; if (f.IsStatic) { if (f.ProtectionLevel == ProtectionLevel.Protected) { string classname = NativeProtectedStaticsProxy.GetProtectedStaticsProxyName(_classDefinition); invoke = classname + "::" + f.NativeName + "("; } else { invoke = _classDefinition.FullyQualifiedNativeName + "::" + f.NativeName + "("; } } else { invoke = GetNativeInvokationTarget(f) + "("; } for (int i = 0; i < paramCount; i++) { ParamDefinition p = f.Parameters[i]; string newname; p.Type.ProducePreCallParamConversionCode(p, out newname); invoke += " " + newname; if (i < paramCount - 1) { invoke += ","; } } invoke += " )"; if (f.HasReturnValue) { return(invoke); } else { return(f.MemberType.ProduceNativeCallConversionCode(invoke, f)); } }
/// <summary> /// Checks whether the specified function can be added to the generated source code. /// </summary> protected virtual bool IsFunctionAllowed(MemberMethodDefinition f) { // If the function is ignored or the return value type is unhandled if (f.IsIgnored() || !f.IsTypeHandled()) { return(false); } // Check whether all parameter types are handled foreach (ParamDefinition param in f.Parameters) { if (!param.IsTypeHandled()) { return(false); } } return(true); }
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"); } } }
protected virtual string ReplaceCustomVariables(string txt, MemberMethodDefinition func) { txt = ReplaceCustomVariables(txt); string replace; if (DeclareAsOverride(func)) { replace = "override"; } else if (func.IsAbstract && AllowSubclassing) { replace = "abstract"; } else { replace = ""; } txt = txt.Replace("@OVERRIDE@", replace); txt = txt.Replace("@NATIVE_INVOKATION_TARGET_FOR_FUNCTION@", GetNativeInvokationTarget(func)); return(txt); }
public static string GetPropertyName(MemberMethodDefinition methodDef) { // property string name = methodDef.GetRenameName(); if (name.StartsWith("get")) { return(name.Substring(3)); } if (name.StartsWith("set")) { return(name.Substring(3)); } if (!methodDef.MetaDef.CodeStyleDef.AllowIsInPropertyName && name.StartsWith("is")) { return(name.Substring(2)); } // For properties named like "hasEnabledAnimationState". return(methodDef.MetaDef.CodeStyleDef.ConvertPropertyName(methodDef.CLRName, methodDef)); }
protected virtual string GetNativeInvokationTarget(MemberMethodDefinition f) { if (!f.IsStatic) { if (f.ProtectionLevel == ProtectionLevel.Public) { return(GetNativeInvokationTarget(f.IsConstMethod) + "->" + f.NativeName); } else if (f.ProtectionLevel == ProtectionLevel.Protected) { if (!f.IsVirtual) { string proxyName = NativeProtectedStaticsProxy.GetProtectedStaticsProxyName(_classDefinition); return("static_cast<" + proxyName + "*>(_native)->" + f.NativeName); } else { throw new Exception("Unexpected"); } } else { throw new Exception("Unexpected"); } } else { if (f.ProtectionLevel == ProtectionLevel.Public) { return(f.ContainingClass.FullyQualifiedNativeName + "::" + f.NativeName); } else { return(NativeProtectedStaticsProxy.GetProtectedStaticsProxyName(f.ContainingClass) + "::" + f.NativeName); } } }
/// <summary> /// Converts the name into a CLR property name. <b>Be careful:</b> Don't call /// <see cref="MemberMethodDefinition.CLRName"/> from this method! /// </summary> /// <param name="name">the name of one of the accessor methods with the prefixes /// "get", "set", and possibly "is" (see <see cref="RemoveIsFromPropertyName"/>) removed.</param> /// <param name="methodDef">the definition of one of the accessor methods for this property</param> /// <returns>the CLR name of this property</returns> /// <seealso cref="CLRPropertyIsPrefix"/> /// <seealso cref="CLRPropertyHasPrefix"/> public virtual string ConvertPropertyName(string name, MemberMethodDefinition methodDef) { return(ToCamelCase(name)); }
/// <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 AddPublicConstructor(MemberMethodDefinition f) { }
protected override string GetNativeInvokationTarget(MemberMethodDefinition f) { return("static_cast<" + ProxyName + "*>(_native)->" + f.ContainingClass.Name + "::" + f.NativeName); }