Exemple #1
0
 protected virtual T VisitPhpClassFieldAccessExpression(PhpClassFieldAccessExpression node)
 {
     if (ThrowNotImplementedException)
     {
         throw new NotImplementedException(string.Format("Method {0} is not supported in class {1}", "VisitPhpClassFieldAccessExpression", this.GetType().FullName));
     }
     return(default(T));
 }
Exemple #2
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));
        }
Exemple #3
0
 protected override IPhpValue VisitPhpClassFieldAccessExpression(PhpClassFieldAccessExpression node)
 {
     return(node);
 }
Exemple #4
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));
                }
            }
        }