internal FunctionDefinition( AttributeCollection?attributes, Modifiers modifiers, ExtendedTypeName?returnType, TypeName methodName, CallConstructor thisOrBaseConstructorCall, string?thisOrBaseConstructorParameters, bool isIndexer, IReadOnlyList <Parameter>?parameters, IReadOnlyList <TypeParameterConstraint>?constraints, StringBuilder buffer ) { Attributes = attributes ?? new AttributeCollection(); Modifiers = modifiers; ReturnType = returnType; MethodName = methodName; ThisOrBaseConstructorCall = thisOrBaseConstructorCall; ThisOrBaseConstructorParameters = thisOrBaseConstructorParameters; IsIndexer = isIndexer; Parameters = parameters ?? Array.Empty <Parameter>(); Constraints = constraints ?? Array.Empty <TypeParameterConstraint>(); Key = ComputeFunctionKey(methodName, parameters, buffer); }
public static object __Translate(IExternalTranslationContext ctx, object src) { CallConstructor x = src as CallConstructor; if (x == null) { return(null); } if (x.Arguments.Length != 0) { throw new NotSupportedException(); } var g = new PhpArrayCreateExpression(); var a = x.Initializers.Cast <IValueTable_PseudoValue>().ToArray(); List <IPhpValue> o = new List <IPhpValue>(); foreach (var i in a) { var a1 = i as IValueTable_PseudoValue; var a2 = a1.Items[0]; var a3 = a1.Items[1]; var key = ctx.TranslateValue(a2); var value = ctx.TranslateValue(a3); var t = new PhpAssignExpression(key, value); o.Add(t); } g.Initializers = o.ToArray(); return(g); }
public IPhpValue TranslateToPhp(IExternalTranslationContext ctx, CallConstructor src) { if (src.Info.DeclaringType == typeof(StringBuilder)) { return(new PhpConstValue("")); } return(null); }
protected override IValue VisitObjectCreationExpression(ObjectCreationExpressionSyntax node) { var nodeInfo = ModelExtensions.GetSymbolInfo(Model, node); // var t = model.GetTypeInfo(node); var methodSymbol = nodeInfo.Symbol as IMethodSymbol; var constructorInfo = context.Roslyn_ResolveMethod(methodSymbol) as ConstructorInfo; if (constructorInfo == null) { throw new NotSupportedException(); } var argList = _internalVisitArgumentList(node.ArgumentList); IValue[] initializer = null; { if (node.Initializer != null) { var _initializer = Visit(node.Initializer); if (_initializer == null) { throw new ArgumentNullException("_initializer"); } if (_initializer is IValueTable2_PseudoValue) { initializer = (_initializer as IValueTable2_PseudoValue).Items.OfType <IValue>().ToArray(); } else if (_initializer is IValueTable_PseudoValue) { initializer = (_initializer as IValueTable_PseudoValue).Items; } else { throw new NotSupportedException(); } } } if (initializer == null) { initializer = new IValue[0]; } var co = new CallConstructor(constructorInfo, argList, initializer); return(co); }
private static object __Translate_CallConstructor(CallConstructor x, IExternalTranslationContext ctx) { if (x.Arguments.Length != 0) { throw new NotSupportedException(); } if (x.Initializers.Length == 0) { return(new PyMethodCallExpression("dict")); } var d = new PyDictionaryCreateExpression(); foreach (var i in x.Initializers) { if (i is CsharpAssignExpression ae) { if (ae.Left is FunctionArguments_PseudoValue pv) { if (pv.Arguments.Length != 1) { throw new NotSupportedException("Key is multivalue"); } var key = ctx.TranslateValue(pv.Arguments[0].MyValue); var value = ctx.TranslateValue(ae.Right); var item = new KeyValuePair <IPyValue, IPyValue>(key, value); d.Initializers.Add(item); } else { throw new NotSupportedException(ae.Left?.GetType().ToString()); } } else { throw new NotSupportedException(i?.GetType().ToString()); } } return(d); }
protected override IPhpValue VisitCallConstructor(CallConstructor src) { var tmp = state.Principles.NodeTranslators.Translate(state, src); if (tmp != null) { return(SimplifyPhpExpression(tmp)); } var r = new PhpMethodCallExpression(PhpMethodCallExpression.ConstructorMethodName); if (src.Info.ReflectedType != src.Info.DeclaringType) { throw new NotSupportedException(); } // we can use "state.Principles.CurrentType" as third parameter if we prefer "new self()" or "new parent()" contructor calls r.SetClassName( state.Principles.GetPhpType(src.Info.ReflectedType, true, null), state.Principles.GetOrMakeTranslationInfo(src.Info) ); // class name for constructor var cti = state.Principles.GetTi(src.Info.ReflectedType, true); if (cti.DontIncludeModuleForClassMembers) { r.DontIncludeClass = true; } if (cti.IsArray) { if (src.Initializers != null && src.Initializers.Any()) { var ggg = src.Initializers.Select(TransValue).ToArray(); var h = new PhpArrayCreateExpression(ggg); return(SimplifyPhpExpression(h)); } else { var h = new PhpArrayCreateExpression(); return(SimplifyPhpExpression(h)); } } { // cti = state.Principles.GetTi(src.Info.ReflectedType); if (cti.IsReflected) { var replacer = state.FindOneClassReplacer(src.Info.ReflectedType); if (replacer != null) { var translationMethods = replacer.ReplaceBy .GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic) .Where(m => m.IsDefined(typeof(TranslatorAttribute))).ToArray(); foreach (var m in translationMethods) { var translated = m.Invoke(null, new object[] { state, src }); if (translated is IPhpValue) { return(translated as IPhpValue); } } throw new Exception(string.Format("Klasa {0} nie umie przetłumaczyć konstruktora {1}", replacer.ReplaceBy.FullName, replacer.SourceType.FullName)); } } } foreach (var functionArgument in src.Arguments) { r.Arguments.Add(TransFunctionArgument(functionArgument)); } return(r); }
protected override IValue VisitObjectCreationExpression(ObjectCreationExpressionSyntax node) { var nodeInfo = ModelExtensions.GetSymbolInfo(Model, node); // var t = model.GetTypeInfo(node); if (nodeInfo.Symbol == null) { var tmp = node.FindParentNode <MethodDeclarationSyntax>(); if (tmp != null) { var headerInfo = context.GetMethodMethodHeaderInfo(tmp); var ti = ModelExtensions.GetTypeInfo(_state.Context.RoslynModel, node); var ti2 = ti.Type; if (ti2 != null) { var ti3 = context.Roslyn_ResolveType(ti2); if (ti3 != null) { var g = headerInfo.TypeParameterConstraints.FirstOrDefault(a => a.Type == ti3); if (g != null && g.RequiresParameterlessConstructor) { headerInfo.PassTypeName(ti3); } } } } throw new NullReferenceException("nodeInfo.Symbol"); } var methodSymbol = nodeInfo.Symbol as IMethodSymbol; var constructorInfo = context.Roslyn_ResolveMethod(methodSymbol) as ConstructorInfo; if (constructorInfo == null) { throw new NotSupportedException(); } var argList = _internalVisitArgumentList(node.ArgumentList); IValue[] initializer = null; { if (node.Initializer != null) { var initializer1 = Visit(node.Initializer); if (initializer1 == null) { throw new ArgumentNullException(nameof(initializer1)); } if (initializer1 is IValueTable2_PseudoValue) { initializer = (initializer1 as IValueTable2_PseudoValue).Items.OfType <IValue>().ToArray(); } else if (initializer1 is IValueTable_PseudoValue) { initializer = (initializer1 as IValueTable_PseudoValue).Items; } else { throw new NotSupportedException(); } } } if (initializer == null) { initializer = new IValue[0]; } var co = new CallConstructor(constructorInfo, argList, initializer); return(co); }