Esempio n. 1
0
        protected override IPhpValue VisitMethodExpression(MethodExpression src)
        {
            if (src.Method.IsStatic)
            {
                var phpClassName =
                    state.Principles.GetPhpType(src.Method.DeclaringType, true, state.Principles.CurrentType);
                if (phpClassName.IsEmpty)
                {
                    throw new Exception("phpClassName cannot be null");
                }
                phpClassName = phpClassName.MakeAbsolute();
                var className             = new PhpConstValue(phpClassName.FullName);
                var methodTranslationInfo = state.Principles.GetOrMakeTranslationInfo(src.Method);
                if (!src.Method.IsPublic)
                {
                    WriteWarning(string.Format("Using not public method {0}.{1} as expression",
                                               src.Method.DeclaringType, src.Method.Name));
                }
                var methodName    = new PhpConstValue(methodTranslationInfo.ScriptName);
                var arrayCreation = new PhpArrayCreateExpression(className, methodName);
                return(SimplifyPhpExpression(arrayCreation));
            }

            {
                // ryzykuję z this
                var targetObject          = new PhpThisExpression();
                var methodTranslationInfo = state.Principles.GetOrMakeTranslationInfo(src.Method);
                var methodName            = new PhpConstValue(methodTranslationInfo.ScriptName);
                var arrayCreation         = new PhpArrayCreateExpression(targetObject, methodName);
                return(SimplifyPhpExpression(arrayCreation));
            }
        }
Esempio n. 2
0
        protected override IPhpValue VisitInstanceFieldAccessExpression(InstanceFieldAccessExpression src)
        {
            var fti = state.Principles.GetOrMakeTranslationInfo(src.Member);
            var to  = TransValue(src.TargetObject);

            if (src.Member.DeclaringType.IsDefined(typeof(AsArrayAttribute)))
            {
                switch (fti.Destination)
                {
                case FieldTranslationDestionations.NormalField:
                    IPhpValue index;
                    if (fti.IsScriptNamePhpEncoded)
                    {
                        index = PhpConstValue.FromPhpValue(fti.ScriptName);
                    }
                    else
                    {
                        index = new PhpConstValue(fti.ScriptName);
                    }
                    var tmp = new PhpArrayAccessExpression(to, index);
                    return(SimplifyPhpExpression(tmp));

                case FieldTranslationDestionations.DefinedConst:
                    break;     // obsłużę to dalej jak dla zwykłej klasy

                default:
                    throw new NotSupportedException();
                }
            }
            var a = new PhpInstanceFieldAccessExpression(fti.ScriptName, to, fti.IncludeModule);

            return(a);
        }
Esempio n. 3
0
 protected virtual T VisitPhpConstValue(PhpConstValue node)
 {
     if (ThrowNotImplementedException)
     {
         throw new NotImplementedException(string.Format("Method {0} is not supported in class {1}", "VisitPhpConstValue", this.GetType().FullName));
     }
     return(default(T));
 }
Esempio n. 4
0
        public static IPhpValue MakePathValueRelatedToFile(string path)
        {
            path = MakeUnixPath(path + UNIX_SEP);
            if (!path.StartsWith(UNIX_SEP))
            {
                path = UNIX_SEP + path;
            }
            var _FILE_  = new PhpDefinedConstExpression("__FILE__", null);
            var dirinfo = new PhpMethodCallExpression("dirname", _FILE_);
            var a2      = new PhpConstValue(path);
            var concat  = new PhpBinaryOperatorExpression(".", dirinfo, a2);

            return(concat);
        }
        private static IPhpValue MkHeader(IExternalTranslationContext ctx, string key, IValue v, IValue replace = null)
        {
            if (v is FunctionArgument)
            {
                v = (v as FunctionArgument).MyValue;
            }
            var a1     = new PhpConstValue(key + ": ");
            var a2     = ctx.TranslateValue(v);
            var concat = new PhpBinaryOperatorExpression(".", a1, a2);
            PhpMethodCallExpression phpm;

            if (replace != null)
            {
                var a3 = ctx.TranslateValue(replace);
                phpm = new PhpMethodCallExpression("header", concat, a3);
            }
            else
            {
                phpm = new PhpMethodCallExpression("header", concat);
            }
            return(phpm);
        }
Esempio n. 6
0
        protected override IPhpValue VisitInstanceMemberAccessExpression(InstanceMemberAccessExpression src)
        {
            if (src.Member == null)
            {
                throw new NotSupportedException();
            }
            if (!(src.Member is MethodInfo))
            {
                throw new NotSupportedException();
            }
            var mi = src.Member as MethodInfo;

            if (mi.IsStatic)
            {
                throw new Exception("Metoda nie może być statyczna");
            }
            var mmi = state.Principles.GetOrMakeTranslationInfo(mi); // MethodTranslationInfo.FromMethodInfo(mi);
            var a   = new PhpConstValue(TransValue(src.Expression));
            var b   = new PhpConstValue(mmi.ScriptName);
            var o   = new PhpArrayCreateExpression(a, b);

            return(o);
        }
Esempio n. 7
0
        protected override IPhpValue VisitPhpBinaryOperatorExpression(PhpBinaryOperatorExpression node)
        {
            switch (node.Operator)
            {
            case ".":
            {
                var _left  = Simplify(node.Left);
                var _right = Simplify(node.Right);
                var n      = new PhpBinaryOperatorExpression(node.Operator, _left, _right);
                var c      = ExplodeConcats(n, ".").ToList();



                for (var i = 1; i < c.Count; i++)
                {
                    var L = c[i - 1];
                    var R = c[i];
                    if (L is PhpConstValue && R is PhpConstValue)
                    {
                        var LValue = (L as PhpConstValue).Value;
                        var RValue = (R as PhpConstValue).Value;
                        if (LValue is string && RValue is string)
                        {
                            c[i - 1] = new PhpConstValue((string)LValue + (string)RValue);
                            c.RemoveAt(i);
                            i--;
                            continue;
                        }
                        var    LCode = PhpValues.ToPhpCodeValue(LValue);
                        var    RCode = PhpValues.ToPhpCodeValue(RValue);
                        string left, right;
                        if (LCode.TryGetPhpString(out left) && RCode.TryGetPhpString(out right))
                        {
                            c[i - 1] = new PhpConstValue(left + right);
                            c.RemoveAt(i);
                            i--;
                            continue;
                        }

                        var msg = string.Format("left={0}, right={1} '{2}+{3}'", LValue, RValue, LValue == null ? null : LValue.GetType().FullName, RValue == null ? null : RValue.GetType().FullName);

#if DEBUG
                        throw new NotImplementedException(msg);
#else
                        Console.WriteLine(msg);
                        Console.WriteLine(L.GetPhpCode(null));
                        Console.WriteLine(R.GetPhpCode(null));
                        continue;
#endif
                    }
                }
                var result = c[0];
                if (c.Count > 1)
                {
                    foreach (var x2 in c.Skip(1))
                    {
                        result = new PhpBinaryOperatorExpression(".", result, x2);
                    }
                }
                return(ReturnSubst(node, result));
            }

            case "|":
            {
                //                    var aLeft = node.Left as PhpConstValue;
                //                    var aRight = node.Right as PhpConstValue;
                //                    if (aLeft != null && aRight != null && aLeft.Value!=null && aRight.Value!=null)
                //                    {
                //                        var typeLeft = aLeft.Value.GetType();
                //                        var typeRight = aRight.Value.GetType();
                //                        if (typeLeft.IsEnum && typeLeft == typeRight)
                //                        {
                //                            var leftValue = (int)aLeft.Value;
                //                            var rightValue = (int)aRight.Value;
                //                            var c = leftValue | rightValue;
                //                            var dd = PhpCodeValue.FromInt(c, true);
                //                            var d = new PhpConstValue(c, false);
                //                            return d;
                //                        }
                //                    }
            }
            break;
            }
            return(node);
        }
Esempio n. 8
0
 protected override IPhpValue VisitPhpConstValue(PhpConstValue node)
 {
     return(node);
 }
Esempio n. 9
0
        protected override IPhpValue VisitInstancePropertyAccessExpression(CsharpInstancePropertyAccessExpression src)
        {
            var pri       = PropertyTranslationInfo.FromPropertyInfo(src.Member);
            var ownerInfo = state.Principles.GetOrMakeTranslationInfo(src.Member.DeclaringType);

            if (src.TargetObject == null)
            {
                throw new NotImplementedException("statyczny");
            }
            var translatedByExternalNodeTranslator = state.Principles.NodeTranslators.Translate(state, src);

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

            var phpTargetObject = TransValue(src.TargetObject);

            if (ownerInfo.IsArray)
            {
                var idx       = new PhpConstValue(pri.FieldScriptName);
                var arrayExpr = new PhpArrayAccessExpression(phpTargetObject, idx);
                return(arrayExpr);
            }

            {
                var propertyInfo  = src.Member;
                var classReplacer = state.FindOneClassReplacer(propertyInfo.DeclaringType);
                if (classReplacer != null)
                {
                    var newPropertyInfo = classReplacer.ReplaceBy.GetProperty(src.Member.Name,
                                                                              BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                    if (newPropertyInfo == null)
                    {
                        throw new Exception(string.Format("Klasa {0} nie zawiera własności {1}",
                                                          classReplacer.ReplaceBy, src.Member));
                    }
                    if (newPropertyInfo.GetIndexParameters().Length > 0)
                    {
                        throw new NotSupportedException("energetic gecko, Property with index");
                    }
                    propertyInfo = newPropertyInfo;
                }

                {
                    var ats = propertyInfo.GetCustomAttribute <DirectCallAttribute>(true);
                    if (ats != null)
                    {
                        if (string.IsNullOrEmpty(ats.Name))
                        {
                            var tmp = ats.MapArray;
                            if (tmp == null || tmp.Length <= 0)
                            {
                                return(phpTargetObject);
                            }
                            if (tmp.Length > 1 || tmp[0] != DirectCallAttribute.This)
                            {
                                throw new NotSupportedException(string.Format(
                                                                    "Property {1}.{0} has invalid 'Map' parameter in DirectCallAttribute",
                                                                    propertyInfo.Name, propertyInfo.DeclaringType));
                            }
                            return(phpTargetObject);
                        }

                        switch (ats.MemberToCall)
                        {
                        case ClassMembers.Method:
                            if (ats.Name == "this")
                            {
                                return(phpTargetObject);
                            }

                            var method = new PhpMethodCallExpression(ats.Name);
                            switch (ats.CallType)
                            {
                            case MethodCallStyles.Procedural:
                                method.Arguments.Add(new PhpMethodInvokeValue(phpTargetObject));
                                return(method);
                                //    case MethodCallStyles.:
                                //        method.Arguments.Add(new PhpMethodInvokeValue(phpTargetObject));
                                //        return method;
                                //    default:
                                //        throw new NotSupportedException();
                            }

                            throw new NotImplementedException();

                        case ClassMembers.Field:
                            switch (ats.CallType)
                            {
                            case MethodCallStyles.Instance:
                                if (ats.Name == "this")
                                {
                                    return(phpTargetObject);
                                }
                                var includeModule = ownerInfo.IncludeModule;
                                var field         = new PhpInstanceFieldAccessExpression(ats.Name,
                                                                                         phpTargetObject,
                                                                                         includeModule);
                                return(field);

                            default:
                                throw new NotSupportedException();
                            }

                        //var f = new PhpMethodCallExpression(ats.Name);
                        //method.Arguments.Add(new PhpMethodInvokeValue(phpTargetObject));
                        //return method;
                        default:
                            throw new NotSupportedException();
                        }
                    }
                }

                {
                    var ats = propertyInfo.GetCustomAttribute <UseBinaryExpressionAttribute>(true);
                    if (ats != null)
                    {
                        var left   = GetValueForExpression(phpTargetObject, ats.Left);
                        var right  = GetValueForExpression(phpTargetObject, ats.Right);
                        var method = new PhpBinaryOperatorExpression(ats.Operator, left, right);
                        return(method);
                    }
                }
                {
                    pri = PropertyTranslationInfo.FromPropertyInfo(src.Member);
                    var to = TransValue(src.TargetObject);
                    var a  = new PhpPropertyAccessExpression(pri, to);
                    return(a);
                }
            }
        }
Esempio n. 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));
                }
            }
        }