コード例 #1
0
        protected override IPhpStatement[] VisitLocalDeclarationStatement(LocalDeclarationStatement src)
        {
            var s = new List <IPhpStatement>();

            foreach (var i in src.Declaration.Declarators)
            {
                /// to jest przypadek z c# 'int x;', dla php można to pominąć
                if (i.Value == null)
                {
                    continue;
                }
                if (i.Value is UnknownIdentifierValue)
                {
                    throw new NotImplementedException();
                }
                var l  = new PhpVariableExpression(PhpVariableExpression.AddDollar(i.Name), PhpVariableKind.Local);
                var r  = TransValue(i.Value);
                var tt = new PhpAssignExpression(l, r);
                s.Add(new PhpExpressionStatement(tt));

                //var r = new PhpAssignVariable( PhpVariableExpression.AddDollar(i.Name), false );
                //// r.Name = "$" + i.Name;
                //r.Value = TV(i.Value);
                //s.Add(r);
            }

            return(s.ToArray());
        }
コード例 #2
0
ファイル: PhpBaseVisitor.cs プロジェクト: yekainew/cs2php
 protected virtual T VisitPhpVariableExpression(PhpVariableExpression node)
 {
     if (ThrowNotImplementedException)
     {
         throw new NotImplementedException(string.Format("Method {0} is not supported in class {1}", "VisitPhpVariableExpression", this.GetType().FullName));
     }
     return(default(T));
 }
コード例 #3
0
        protected override IPhpValue VisitLocalVariableExpression(LocalVariableExpression src)
        {
            if (state.Principles.CurrentMethod == null)
            {
                return(PhpVariableExpression.MakeLocal(src.Name, false));
            }
            var isArgument = state.Principles.CurrentMethod.GetParameters().Any(u => u.Name == src.Name);

            return(PhpVariableExpression.MakeLocal(src.Name, isArgument));
        }
コード例 #4
0
        private static IPhpValue _WpPost(CsharpMethodCallExpression src)
        {
            if (src.MethodInfo.Name == "PostType")
            {
                var a1 = new PhpVariableExpression("$_POST", PhpVariableKind.Global);
                var a2 = new PhpArrayAccessExpression(a1, new PhpConstValue("post_type"));
                return(a2);
            }
            return(null);

            throw new NotSupportedException();
        }
コード例 #5
0
        private IPhpValue _KeyValuePair()
        {
            var s = src.Member.Name;

            if (s == "Key" || s == "Value")
            {
                var targetObject = ctx.TranslateValue(src.TargetObject);
                if (targetObject is PhpVariableExpression)
                {
                    var to2 = targetObject as PhpVariableExpression;
                    var a   = new PhpVariableExpression(to2.VariableName + "@" + src.Member.Name, to2.Kind);
                    return(a);
                }
                return(null);
            }
            throw new NotSupportedException();
        }
コード例 #6
0
        protected override IPhpStatement[] VisitVariableDeclaration(VariableDeclaration src)
        {
            //throw new Exception("DELETE THIS ??????");
            var s = new List <IPhpStatement>();

            foreach (var i in src.Declarators)
            {
                var l  = new PhpVariableExpression(PhpVariableExpression.AddDollar(i.Name), PhpVariableKind.Local);
                var r  = TransValue(i.Value);
                var tt = new PhpAssignExpression(l, r);
                s.Add(new PhpExpressionStatement(tt));

                //var r = new PhpAssignVariable(PhpVariableExpression.AddDollar(i.Name), false);
                //r.Value = TV(i.Value);
                //s.Add(r);
            }
            return(s.ToArray());
        }
コード例 #7
0
        public IPhpValue TranslateToPhp(IExternalTranslationContext ctx, ClassFieldAccessExpression src)
        {
            var    t = src.Member.DeclaringType;
            string n = src.Member.Name;

            if (t == typeof(Wp))
            {
                if (n == "DoingAutosave")
                {
                    //  if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
                    var a1 = new PhpMethodCallExpression("defined", new PhpConstValue("DOING_AUTOSAVE"));
                    var a2 = new PhpDefinedConstExpression("DOING_AUTOSAVE", null);
                    var a3 = new PhpBinaryOperatorExpression("&&", a1, a2);
                    var a4 = new PhpParenthesizedExpression(a3);
                    return(a4);
                }
                if (n == "HookSuffix")
                {
                    var a1 = new PhpVariableExpression("hook_suffix", PhpVariableKind.Global);
                    return(a1);
                }
            }
            return(null);
        }
コード例 #8
0
        private void TranslateField(PhpCodeModule module, PhpClassDefinition phpClass, FieldDeclaration field)
        {
            PhpValueTranslator phpValueTranslator = null;

            foreach (var item in field.Items)
            {
                if (item.OptionalFieldInfo == null)
                {
                    continue;
                }
                var fti = Info.GetOrMakeTranslationInfo(item.OptionalFieldInfo);
                switch (fti.Destination)
                {
                case FieldTranslationDestionations.DefinedConst:
                    if (item.Value == null)
                    {
                        throw new NotSupportedException();
                    }
                    if (phpValueTranslator == null)
                    {
                        phpValueTranslator = new PhpValueTranslator(_state);
                    }
                    var definedValue = phpValueTranslator.TransValue(item.Value);
                    {
                        if (fti.IncludeModule != module.Name)
                        {
                            module = GetOrMakeModuleByName(fti.IncludeModule);
                        }
                    }
                    module.DefinedConsts.Add(new KeyValuePair <string, IPhpValue>(fti.ScriptName, definedValue));
                    break;

                case FieldTranslationDestionations.GlobalVariable:
                    if (item.Value != null)
                    {
                        IPhpValue value;
                        // muszę na chwilę wyłączyć current type, bo to jes poza klasą generowane
                        {
                            var saveCurrentType = _state.Principles.CurrentType;
                            _state.Principles.CurrentType = null;
                            try
                            {
                                if (phpValueTranslator == null)
                                {
                                    phpValueTranslator = new PhpValueTranslator(_state);
                                }
                                value = phpValueTranslator.TransValue(item.Value);
                            }
                            finally
                            {
                                _state.Principles.CurrentType = saveCurrentType;
                            }
                        }

                        var assign = new PhpAssignExpression(PhpVariableExpression.MakeGlobal(fti.ScriptName),
                                                             value);
                        module.TopCode.Statements.Add(new PhpExpressionStatement(assign));
                    }

                    break;

                case FieldTranslationDestionations.JustValue:
                    continue;     // don't define

                case FieldTranslationDestionations.NormalField:
                case FieldTranslationDestionations.ClassConst:
                {
                    var def = new PhpClassFieldDefinition();
                    var cti = _state.Principles.GetTi(_state.Principles.CurrentType, true);
                    if (cti.IsArray)
                    {
                        continue;
                    }
                    if (field.Modifiers.Has("const") ^
                        (fti.Destination == FieldTranslationDestionations.ClassConst))
                    {
                        throw new Exception("beige lion");
                    }

                    def.IsConst =
                        fti.Destination ==
                        FieldTranslationDestionations.ClassConst;     // field.Modifiers.Has("const");
                    def.Name = fti.ScriptName;

                    def.IsStatic = def.IsConst || field.Modifiers.Has("static");
                    if (field.Modifiers.Has("public"))
                    {
                        def.Visibility = Visibility.Public;
                    }
                    else if (field.Modifiers.Has("protected"))
                    {
                        def.Visibility = Visibility.Protected;
                    }
                    else
                    {
                        def.Visibility = Visibility.Private;
                    }

                    if (item.Value != null)
                    {
                        if (phpValueTranslator == null)
                        {
                            phpValueTranslator = new PhpValueTranslator(_state);
                        }
                        def.ConstValue = phpValueTranslator.TransValue(item.Value);
                    }

                    phpClass.Fields.Add(def);
                    break;
                }

                default:
                    throw new NotSupportedException();
                }
            }
        }
コード例 #9
0
 protected override IPhpValue VisitPhpVariableExpression(PhpVariableExpression node)
 {
     return(node);
 }
コード例 #10
0
        protected override IPhpValue VisitClassFieldAccessExpression(ClassFieldAccessExpression src)
        {
            var tmp = state.Principles.NodeTranslators.Translate(state, src);

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

            var isStatic            = src.IsStatic;
            var member              = src.Member;
            var memberName          = member.Name;
            var memberDeclaringType = member.DeclaringType;

            {
                var tInfo = state.Principles.GetOrMakeTranslationInfo(src.Member);
                if (tInfo.IsDefinedInNonincludableModule)
                {
                    var b = state.Principles.GetTi(state.Principles.CurrentType, true);
                    if (tInfo.IncludeModule != b.ModuleName)
                    {
                        throw new Exception(
                                  string.Format(
                                      "Unable to reference to field {1}.{0} from {2}.{3}. Containing module is page and cannot be included.",
                                      memberName,
                                      memberDeclaringType == null
                                    ? "?"
                                    : (memberDeclaringType.FullName ?? memberDeclaringType.Name),
                                      state.Principles.CurrentType.FullName,
                                      state.Principles.CurrentMethod
                                      ));
                    }
                }

                var fieldDeclaringType = memberDeclaringType;
                if (fieldDeclaringType == null)
                {
                    throw new Exception("fieldDeclaringType");
                }
                state.Principles.GetTi(fieldDeclaringType, false);
                {
                    if (fieldDeclaringType.IsEnum)
                    {
                        if (!isStatic)
                        {
                            throw new NotSupportedException();
                        }
                        var asDefinedConstAttribute = member.GetCustomAttribute <AsDefinedConstAttribute>();
                        if (asDefinedConstAttribute != null)
                        {
                            var definedExpression =
                                new PhpDefinedConstExpression(asDefinedConstAttribute.DefinedConstName,
                                                              tInfo.IncludeModule);
                            return(SimplifyPhpExpression(definedExpression));
                        }

                        var renderValueAttribute = member.GetCustomAttribute <RenderValueAttribute>();
                        if (renderValueAttribute != null)
                        {
                            if (PhpValues.TryGetPhpStringValue(renderValueAttribute.Name, out var strCandidate))
                            {
                                var valueExpression = new PhpConstValue(strCandidate);
#if DEBUG
                                {
                                    var a1 = renderValueAttribute.Name.Trim();
                                    var a2 = valueExpression.ToString();
                                    if (a1 != a2)
                                    {
                                        throw new InvalidOperationException();
                                    }
                                }
#endif
                                return(SimplifyPhpExpression(valueExpression));
                            }
                            else
                            {
                                var valueExpression = new PhpFreeExpression(renderValueAttribute.Name);
                                return(SimplifyPhpExpression(valueExpression));
                            }
                        }

                        {
                            // object v1 = ReadEnumValueAndProcessForPhp(member);
                            var v1 = member.GetValue(null);
                            var g  = new PhpConstValue(v1);
                            return(SimplifyPhpExpression(g));
                        }
                        //throw new NotSupportedException();
                    }
                }

                var principles = state.Principles;
                switch (tInfo.Destination)
                {
                case FieldTranslationDestionations.DefinedConst:
                    if (!member.IsStatic)
                    {
                        throw new NotSupportedException("Unable to convert instance field into PHP defined const");
                    }
                    if (tInfo.IsScriptNamePhpEncoded)
                    {
                        throw new Exception("Encoded php values are not supported");
                    }
                    var definedExpression = new PhpDefinedConstExpression(tInfo.ScriptName, tInfo.IncludeModule);
                    return(SimplifyPhpExpression(definedExpression));

                case FieldTranslationDestionations.GlobalVariable:
                    if (!member.IsStatic)
                    {
                        throw new NotSupportedException(
                                  "Unable to convert instance field into PHP global variable");
                    }
                    if (tInfo.IsScriptNamePhpEncoded)
                    {
                        throw new Exception("Encoded php values are not supported");
                    }
                    var globalVariable = PhpVariableExpression.MakeGlobal(tInfo.ScriptName);
                    return(SimplifyPhpExpression(globalVariable));

                case FieldTranslationDestionations.JustValue:
                    if (!member.IsStatic)
                    {
                        throw new NotSupportedException("Unable to convert instance field into compile-time value");
                    }
                    var constValue    = member.GetValue(null);
                    var phpConstValue = new PhpConstValue(constValue, tInfo.UsGlueForValue);
                    return(SimplifyPhpExpression(phpConstValue));

                case FieldTranslationDestionations.NormalField:
                    if (tInfo.IsScriptNamePhpEncoded)
                    {
                        throw new Exception("Encoded php values are not supported");
                    }
                    var rr = new PhpClassFieldAccessExpression
                    {
                        FieldName = tInfo.ScriptName,
                        IsConst   = tInfo.Destination == FieldTranslationDestionations.ClassConst
                    };
                    rr.SetClassName(
                        principles.GetPhpType(memberDeclaringType, true, principles.CurrentType),
                        principles.GetOrMakeTranslationInfo(memberDeclaringType)
                        );
                    return(SimplifyPhpExpression(rr));

                case FieldTranslationDestionations.ClassConst:
                    if (tInfo.IsScriptNamePhpEncoded)
                    {
                        throw new Exception("Encoded php values are not supported");
                    }
                    rr = new PhpClassFieldAccessExpression
                    {
                        FieldName = tInfo.ScriptName,
                        IsConst   = true
                    };
                    rr.SetClassName(
                        principles.GetPhpType(memberDeclaringType, true, principles.CurrentType),
                        principles.GetOrMakeTranslationInfo(memberDeclaringType));

                    return(SimplifyPhpExpression(rr));

                default:
                    throw new NotSupportedException(string.Format(
                                                        "Unable to translate class field with destination option equal {0}", tInfo.Destination));
                }
            }
        }
コード例 #11
0
        // Protected Methods 

        protected override IPhpValue VisitArgumentExpression(ArgumentExpression src)
        {
            return(PhpVariableExpression.MakeLocal(src.Name, true));
        }