public IPyValue TranslateToPython(IExternalTranslationContext ctx, CsharpMethodCallExpression src) { if (src.IsDelegate) { return(TranspileDelegateToPyton(ctx, src)); } var principles = ctx.GetTranslationInfo(); src = SubstituteByReplacerMethod(ctx, src); { var value = Try_DirectCallAttribute(ctx, src); if (value != null) { return(value); } value = Try_UseExpressionAttribute(ctx, src); if (value != null) { return(value); } } ctx.GetTranslationInfo().CheckAccesibility(src); var declaringType = src.MethodInfo.DeclaringType; if (declaringType.IsGenericType) { declaringType = declaringType.GetGenericTypeDefinition(); } var cti = principles.FindClassTranslationInfo(declaringType); if (cti == null) { throw new NotSupportedException(); } var mti = principles.GetOrMakeTranslationInfo(src.MethodInfo); { var pyMethod = new PyMethodCallExpression(mti.ScriptName); if (src.MethodInfo.IsStatic) { var a = principles.GetPyType(src.MethodInfo.DeclaringType, true, principles.CurrentType); pyMethod.SetClassName(a, mti); } pyMethod.TargetObject = ctx.TranslateValue(src.TargetObject); CopyArguments(ctx, src.Arguments, pyMethod, null); if (cti.DontIncludeModuleForClassMembers) { pyMethod.DontIncludeClass = true; } return(pyMethod); } throw new Exception(string.Format("bright cow, {0}", src.MethodInfo.DeclaringType.FullName)); }
private void TranslateClass( ClassTranslationInfo classTranslationInfo, FullInterfaceDeclaration[] interfaces, FullClassDeclaration[] classes) { if (classTranslationInfo.Skip) { Debug.Write(""); } var pyModule = GetOrMakeModuleByName(classTranslationInfo.ModuleName); // var assemblyTI = _info.GetOrMakeTranslationInfo(_info.CurrentAssembly); PyQualifiedName GetBaseClassName() { var netBaseType = classTranslationInfo.Type.BaseType; if ((object)netBaseType == null || netBaseType == typeof(object)) { return(PyQualifiedName.Empty); } var baseTypeTranslationInfo = _state.Principles.GetOrMakeTranslationInfo(netBaseType); if (baseTypeTranslationInfo.Skip) { return(PyQualifiedName.Empty); } return(_state.Principles.GetPyType(netBaseType, true, null)); } var pyClass = classTranslationInfo.ExportAsModule ? null : pyModule.FindOrCreateClass(classTranslationInfo.PyName, GetBaseClassName()); Console.WriteLine(classTranslationInfo.ModuleName); _state.Principles.CurrentType = classTranslationInfo.Type; try { _state.Principles.CurrentAssembly = _state.Principles.CurrentType.Assembly; var fullname = classTranslationInfo.Type.FullName; var srcs = classTranslationInfo.Type.IsInterface ? interfaces .Where(q => q.FullName == fullname) .Select(q => q.ClassDeclaration) .OfType <IClassOrInterface>().ToArray() : classes .Where(q => q.FullName == fullname) .Select(q => q.ClassDeclaration) .OfType <IClassOrInterface>().ToArray(); var members = srcs.SelectMany(i => i.Members).ToArray(); { var constructors = members.OfType <ConstructorDeclaration>().ToArray(); if (pyClass == null && constructors.Length > 0) { throw new Exception("Class exported as module cannot have constructors"); } if (constructors.Length > 1) { throw new Exception("Python supports only one constructor per class"); } if (constructors.Any()) { TranslateConstructor(pyClass, constructors.First()); } } foreach (var methodDeclaration in members.OfType <MethodDeclaration>()) { TranslateMethod(pyModule, pyClass, methodDeclaration); } foreach (var pDeclaration in members.OfType <CsharpPropertyDeclaration>()) { TranslateProperty(pyModule, pyClass, pDeclaration); } foreach (var constDeclaration in members.OfType <FieldDeclaration>()) { TranslateField(pyModule, pyClass, constDeclaration); } } finally { _state.Principles.CurrentType = null; } { if (classTranslationInfo.IsPage) { var mti = MethodTranslationInfo.FromMethodInfo(classTranslationInfo.PageMethod, classTranslationInfo); var callMain = new PyMethodCallExpression(mti.ScriptName); callMain.SetClassName( classTranslationInfo.PyName, mti ); pyModule.BottomCode.Statements.Add(new PyExpressionStatement(callMain)); } } TranslateClass_AddModuleRequests(classTranslationInfo, pyModule); }
protected override IPyValue VisitCallConstructor(CallConstructor src) { var tmp = _state.Principles.NodeTranslators.Translate(_state, src); if (tmp != null) { return(SimplifyPyExpression(tmp)); } var r = new PyMethodCallExpression(PyMethodCallExpression.ConstructorMethodName); if (src.Info.ReflectedType != src.Info.DeclaringType) { throw new NotSupportedException(); } // we can use "state.Principles.CurrentType" as third parameter if we prefer "new self()" or "new parent()" contructor calls r.SetClassName( _state.Principles.GetPyType(src.Info.ReflectedType, true, null), _state.Principles.GetOrMakeTranslationInfo(src.Info) ); // class name for constructor { // var a = src.Info.GetCustomAttribute(); } var cti = _state.Principles.GetTi(src.Info.ReflectedType, true); if (cti.DontIncludeModuleForClassMembers) { r.DontIncludeClass = true; } if (cti.IsArray) { if (src.Initializers != null && src.Initializers.Any()) { var ggg = src.Initializers.Select(TransValue).ToArray(); var h = new PyArrayCreateExpression(ggg); return(SimplifyPyExpression(h)); } else { var h = new PyArrayCreateExpression(); return(SimplifyPyExpression(h)); } } { // cti = state.Principles.GetTi(src.Info.ReflectedType); // if (cti.IsReflected) { var replacer = _state.FindOneClassReplacer(src.Info.ReflectedType); if (replacer != null) { var translationMethods = replacer.ReplaceBy .GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic) .Where(m => m.IsDefined(typeof(TranslatorAttribute))).ToArray(); foreach (var m in translationMethods) { var translated = m.Invoke(null, new object[] { _state, src }); if (translated is IPyValue) { return(translated as IPyValue); } } //throw new Exception(string.Format("Klasa {0} nie umie przetłumaczyć konstruktora {1}",replacer.ReplaceBy.FullName, replacer.SourceType.FullName)); } } } foreach (var functionArgument in src.Arguments) { r.Arguments.Add(TransFunctionArgument(functionArgument)); } return(r); }