public IPhpValue TranslateToPhp(IExternalTranslationContext ctx, CsharpMethodCallExpression src) { if (src.MethodInfo.DeclaringType == typeof(StringBuilder)) { var fn = src.MethodInfo.ToString(); if (fn == "System.Text.StringBuilder Append(System.String)") { var sb = ctx.TranslateValue(src.TargetObject); var arg = ctx.TranslateValue(src.Arguments[0].MyValue); return(new PhpAssignExpression(sb, arg, ".")); } if (fn == "System.Text.StringBuilder AppendLine(System.String)") { var sb = ctx.TranslateValue(src.TargetObject); var arg = ctx.TranslateValue(src.Arguments[0].MyValue); var eol = new PhpDefinedConstExpression("PHP_EOL", null); var arg_eol = PhpBinaryOperatorExpression.ConcatStrings(arg, eol); return(new PhpAssignExpression(sb, arg_eol, ".")); } if (fn == "System.String ToString()") { return(ctx.TranslateValue(src.TargetObject)); } Console.WriteLine(fn); throw new NotSupportedException(); } return(null); }
protected virtual T VisitPhpDefinedConstExpression(PhpDefinedConstExpression node) { if (ThrowNotImplementedException) { throw new NotImplementedException(string.Format("Method {0} is not supported in class {1}", "VisitPhpDefinedConstExpression", this.GetType().FullName)); } return(default(T)); }
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); }
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); }
protected override IPhpValue VisitPhpDefinedConstExpression(PhpDefinedConstExpression node) { return(node); }
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)); } } }