Пример #1
0
        public IPhpValue TranslateToPhp(IExternalTranslationContext ctx, CsharpMethodCallExpression src)
        {
            if (src.IsDelegate)
            {
                return(_Delegate(ctx, src));
            }

            if (src.MethodInfo.DeclaringType == typeof(Html))
            {
                var tmp = new Lang__Php__Html__Methods().TranslateToPhp(ctx, src);
                if (tmp != null)
                {
                    return(tmp);
                }
            }
            var principles = ctx.GetTranslationInfo();

            src = SubstituteByReplacerMethod(ctx, src);
            #region Konwersja atrybutami
            {
                var value = Try_DirectCallAttribute(ctx, src);
                if (value != null)
                {
                    return(value);
                }
                value = Try_UseExpressionAttribute(ctx, src);
                if (value != null)
                {
                    return(value);
                }
            }
            #endregion

            ctx.GetTranslationInfo().CheckAccesibility(src);
            var cti = principles.FindClassTranslationInfo(src.MethodInfo.DeclaringType);
            if (cti == null)
            {
                throw new NotSupportedException();
            }
            var mti = principles.GetOrMakeTranslationInfo(src.MethodInfo);
            {
                var phpMethod = new PhpMethodCallExpression(mti.ScriptName);
                if (src.MethodInfo.IsStatic)
                {
                    var a = principles.GetPhpType(src.MethodInfo.DeclaringType, true, principles.CurrentType);
                    phpMethod.SetClassName(a, mti);
                }
                phpMethod.TargetObject = ctx.TranslateValue(src.TargetObject);
                CopyArguments(ctx, src.Arguments, phpMethod, null);

                if (cti.DontIncludeModuleForClassMembers)
                {
                    phpMethod.DontIncludeClass = true;
                }
                return(phpMethod);
            }

            throw new Exception(string.Format("bright cow, {0}", src.MethodInfo.DeclaringType.FullName));
        }
Пример #2
0
 protected override IPhpValue VisitPhpMethodCallExpression(PhpMethodCallExpression node)
 {
     if (node.Name == "_urlencode_" || node.Name == "_htmlspecialchars_")
     {
         if (node.Arguments[0].Expression is PhpConstValue)
         {
             var cv = (node.Arguments[0].Expression as PhpConstValue).Value;
             if (cv == null)
             {
                 return(Simplify(node.Arguments[0].Expression));
             }
             if (cv is int)
             {
                 cv = cv.ToString();
             }
             else if (cv is string)
             {
                 if (node.Name == "_urlencode_")
                 {
                     cv = HttpUtility.UrlEncode(cv as string);
                 }
                 else
                 {
                     cv = HttpUtility.HtmlEncode(cv as string);
                 }
             }
             else
             {
                 throw new NotSupportedException();
             }
             return(Simplify(new PhpConstValue(cv)));
         }
     }
     {
         var list1 = node.Arguments.Select(Simplify).Cast <PhpMethodInvokeValue>().ToList();
         var to    = node.TargetObject == null ? null : Simplify(node.TargetObject);
         if (PhpSourceBase.EqualCode_List(list1, node.Arguments) && PhpSourceBase.EqualCode(to, node.TargetObject))
         {
             return(node);
         }
         var xx = new PhpMethodCallExpression(node.Name)
         {
             Arguments        = list1,
             DontIncludeClass = node.DontIncludeClass,
             TargetObject     = to
         };
         xx.SetClassName(node.ClassName, node.TranslationInfo);
         return(xx);
     }
     return(node);
 }
Пример #3
0
        // Private Methods 

        /*
         *      static IPhpStatement[] MkArray(IPhpStatement x)
         *      {
         *          return new IPhpStatement[] { x };
         *      }
         */

        // Public Methods 


        public void Translate(AssemblySandbox sandbox)
        {
            var classes            = Info.GetClasses();
            var classesToTranslate = Info.ClassTranslations.Values
                                     .Where(u => u.Type.Assembly.FullName == Info.CurrentAssembly.FullName).ToArray();
            //            classesToTranslate = (from i in _info.ClassTranslations.Values
            //                                      where i.Type.Assembly.FullName == _info.CurrentAssembly.FullName
            //                                      select this.ge.ToArray();
            var interfaces = Info.GetInterfaces();

            //     var interfacesToTranslate = info.ClassTranslations.Values.Where(u => u.Type.Assembly == info.CurrentAssembly).ToArray();
            foreach (var classTranslationInfo in classesToTranslate)
            {
                if (classTranslationInfo.Skip)
                {
                    Debug.Write("");
                }
                PhpClassDefinition phpClass;
                var phpModule = GetOrMakeModuleByName(classTranslationInfo.ModuleName);
                // var assemblyTI = _info.GetOrMakeTranslationInfo(_info.CurrentAssembly);

                {
                    PhpQualifiedName phpBaseClassName;
                    {
                        var netBaseType = classTranslationInfo.Type.BaseType;
                        if ((object)netBaseType == null || netBaseType == typeof(object))
                        {
                            phpBaseClassName = PhpQualifiedName.Empty;
                        }
                        else
                        {
                            // _state.Principles.CurrentTyp is null so we will obtain absolute name
                            phpBaseClassName =
                                _state.Principles.GetPhpType(netBaseType, true, null); // absolute name
                            var baseTypeTranslationInfo = _state.Principles.GetOrMakeTranslationInfo(netBaseType);
                            if (baseTypeTranslationInfo.Skip)
                            {
                                phpBaseClassName = PhpQualifiedName.Empty;
                            }
                        }
                    }
                    phpClass = phpModule.FindOrCreateClass(classTranslationInfo.ScriptName, phpBaseClassName);
                }
                _state.Principles.CurrentType     = classTranslationInfo.Type;
                _state.Principles.CurrentAssembly = _state.Principles.CurrentType.Assembly;
                Console.WriteLine(classTranslationInfo.ModuleName);

                IClassMember[] members;

                if (classTranslationInfo.Type.IsInterface)
                {
                    var sources = interfaces.Where(i => i.FullName == classTranslationInfo.Type.FullName).ToArray();
                    members = (from i in sources
                               from j in i.ClassDeclaration.Members
                               select j).ToArray();
                    {
                        var fileNames = classTranslationInfo.Type.GetCustomAttributes <RequireOnceAttribute>()
                                        .Select(i => i.Filename).Distinct().ToArray();
                        if (fileNames.Any())
                        {
                            var b = fileNames.Select(u => new PhpConstValue(u)).ToArray();
                            phpModule.RequiredFiles.AddRange(b);
                        }
                    }
                }
                else
                {
                    var sources = classes.Where(i => i.FullName == classTranslationInfo.Type.FullName).ToArray();
                    members = (from i in sources
                               from j in i.ClassDeclaration.Members
                               select j).ToArray();
                    {
                        var fileNames = classTranslationInfo.Type.GetCustomAttributes <RequireOnceAttribute>()
                                        .Select(i => i.Filename).Distinct().ToArray();
                        if (fileNames.Any())
                        {
                            var b = fileNames.Select(u => new PhpConstValue(u)).ToArray();
                            phpModule.RequiredFiles.AddRange(b);
                        }
                    }
                }

                {
                    var c = members.OfType <ConstructorDeclaration>().ToArray();
                    if (c.Length > 1)
                    {
                        throw new Exception("PHP supports only one constructor per class");
                    }
                    if (c.Any())
                    {
                        TranslateConstructor(phpClass, c.First());
                    }
                }

                {
                    foreach (var methodDeclaration in members.OfType <MethodDeclaration>())
                    {
                        TranslateMethod(phpClass, methodDeclaration);
                    }
                }

                {
                    foreach (var pDeclaration in members.OfType <CsharpPropertyDeclaration>())
                    {
                        TranslateProperty(phpClass, pDeclaration);
                    }
                }

                {
                    foreach (var constDeclaration in members.OfType <FieldDeclaration>())
                    {
                        TranslateField(phpModule, phpClass, constDeclaration);
                    }
                }

                _state.Principles.CurrentType = null;
                {
                    if (classTranslationInfo.IsPage)
                    {
                        {
                            var ati = Info.GetOrMakeTranslationInfo(Info.CurrentAssembly);
                            if (ati.DefaultTimezone.HasValue)
                            {
                                // date_default_timezone_set('America/Los_Angeles');
                                var a  = new PhpValueTranslator(_state);
                                var aa = a.Visit(new ConstValue(ati.DefaultTimezone.Value));
                                var dateDefaultTimezoneSet =
                                    new PhpMethodCallExpression("date_default_timezone_set", aa);
                                phpModule.BottomCode.Statements.Add(new PhpExpressionStatement(dateDefaultTimezoneSet));
                            }
                        }

                        {
                            var mti = MethodTranslationInfo.FromMethodInfo(classTranslationInfo.PageMethod,
                                                                           classTranslationInfo);
                            var callMain = new PhpMethodCallExpression(mti.ScriptName);
                            callMain.SetClassName(
                                classTranslationInfo.ScriptName,
                                mti
                                );
                            phpModule.BottomCode.Statements.Add(new PhpExpressionStatement(callMain));
                        }
                    }
                }

                {
                    var moduleCodeRequests = new List <ModuleCodeRequest>();
                    var codeRequests       = (phpModule as ICodeRelated).GetCodeRequests().ToArray();
                    {
                        var classCodeRequests = (from request in codeRequests.OfType <ClassCodeRequest>()
                                                 where request.ClassName != null
                                                 select request.ClassName.FullName)
                                                .Distinct()
                                                .ToArray();

                        foreach (var req in classCodeRequests)
                        {
                            var m = Info.ClassTranslations.Values.Where(i => i.ScriptName.FullName == req).ToArray();
                            if (m.Length != 1)
                            {
                                throw new NotSupportedException();
                            }
                            var mm = m[0];
                            if (mm.DontIncludeModuleForClassMembers)
                            {
                                continue;
                            }
                            var includeModule = mm.IncludeModule;
                            if (includeModule == null || mm.ModuleName == phpModule.Name)
                            {
                                continue;
                            }
                            var h = new ModuleCodeRequest(includeModule, "class request: " + req);
                            moduleCodeRequests.Add(h);
                        }
                    }
                    {
                        var moduleRequests = (from i in codeRequests.OfType <ModuleCodeRequest>()
                                              where i.ModuleName != null
                                              select i).Union(moduleCodeRequests).ToArray();
                        var moduleNames = (from mReq in moduleRequests
                                           where mReq.ModuleName != phpModule.Name
                                           let mName = mReq.ModuleName
                                                       where mName != null
                                                       select mName
                                           ).Distinct().ToArray();
                        foreach (var i in moduleNames.Where(x => !PhpCodeModuleName.IsFrameworkName(x)))
                        {
                            AppendCodeReq(i, phpModule);
                        }
                    }
                }
            }

            {
                var emptyModules = Modules.Where(a => a.IsEmpty).ToArray();
                foreach (var module in Modules)
                {
                    // if (module.IsEmpty)
                }
            }
        }
Пример #4
0
        protected override IPhpValue VisitCallConstructor(CallConstructor src)
        {
            var tmp = state.Principles.NodeTranslators.Translate(state, src);

            if (tmp != null)
            {
                return(SimplifyPhpExpression(tmp));
            }

            var r = new PhpMethodCallExpression(PhpMethodCallExpression.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.GetPhpType(src.Info.ReflectedType, true, null),
                state.Principles.GetOrMakeTranslationInfo(src.Info)
                ); // class name for constructor

            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 PhpArrayCreateExpression(ggg);
                    return(SimplifyPhpExpression(h));
                }
                else
                {
                    var h = new PhpArrayCreateExpression();
                    return(SimplifyPhpExpression(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 IPhpValue)
                            {
                                return(translated as IPhpValue);
                            }
                        }

                        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);
        }