コード例 #1
0
        public IPhpValue TranslateToPhp(IExternalTranslationContext ctx, ClassPropertyAccessExpression src)
        {
            var da = src.Member.GetCustomAttribute(typeof(DirectCallAttribute), true) as DirectCallAttribute;

            if (da != null)
            {
                if (da.MapArray != null && da.MapArray.Length != 0)
                {
                    throw new NotSupportedException();
                }
                switch (da.CallType)
                {
                case MethodCallStyles.Procedural:
                    return(new PhpMethodCallExpression(da.Name));

                default:
                    throw new NotSupportedException();
                }
            }
            var pti = PropertyTranslationInfo.FromPropertyInfo(src.Member);

            //ctx.GetTranslationInfo().GetOrMakeTranslationInfo(src.Member);
            if (!pti.GetSetByMethod)
            {
                var fieldAccessExpression = new PhpClassFieldAccessExpression
                {
                    FieldName = pti.FieldScriptName
                };
                var principles   = ctx.GetTranslationInfo();
                var phpClassName = principles.GetPhpType(src.Member.DeclaringType, true, principles.CurrentType);
                var classTi      = principles.GetOrMakeTranslationInfo(src.Member.DeclaringType);
                fieldAccessExpression.SetClassName(phpClassName, classTi);
                return(fieldAccessExpression);
            }
            throw new NotImplementedException(string.Format("Unable to translate ClassPropertyAccessExpression {0}.{1}", src.Member.DeclaringType, src.Member.Name));
        }
コード例 #2
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);
                }
            }
        }