static void GenerateEndMethodImpl(CodeClass c, MethodInfo endProcessMethod, string name, MethodInfo mi) { CodeMethod m = c.ImplementMethod(mi); CodeBuilder b = m.CodeBuilder; ParameterInfo [] pinfos = mi.GetParameters(); ParameterInfo p = pinfos [0]; CodeArgumentReference asyncResultRef = m.GetArg(0); CodeVariableDeclaration paramsDecl = new CodeVariableDeclaration(typeof(object []), "parameters"); b.CurrentBlock.Add(paramsDecl); CodeVariableReference paramsRef = paramsDecl.Variable; b.Assign(paramsRef, new CodeNewArray(typeof(object), new CodeLiteral(pinfos.Length - 1))); /* * for (int i = 0; i < pinfos.Length - 2; i++) { * ParameterInfo par = pinfos [i]; * if (!par.IsOut) * b.Assign ( * new CodeArrayItem (paramsRef, new CodeLiteral (i)), * new CodeCast (typeof (object), * new CodeArgumentReference (par.ParameterType, par.Position + 1, "arg" + i))); * } */ #if USE_OD_REFERENCE_IN_PROXY CodePropertyReference argMethodInfo = GetOperationMethod(m, b, name, "EndMethod"); #else CodeMethodCall argMethodInfo = new CodeMethodCall(typeof(MethodBase), "GetCurrentMethod"); #endif CodeLiteral argOperName = new CodeLiteral(name); CodeVariableReference retValue = null; if (mi.ReturnType == typeof(void)) { b.Call(m.GetThis(), endProcessMethod, argMethodInfo, argOperName, paramsRef, asyncResultRef); } else { CodeVariableDeclaration retValueDecl = new CodeVariableDeclaration(mi.ReturnType, "retValue"); b.CurrentBlock.Add(retValueDecl); retValue = retValueDecl.Variable; b.Assign(retValue, new CodeCast(mi.ReturnType, b.CallFunc(m.GetThis(), endProcessMethod, argMethodInfo, argOperName, paramsRef, asyncResultRef))); } // FIXME: fill out parameters if (retValue != null) { b.Return(retValue); } }
static void GenerateBeginMethodImpl(CodeClass c, MethodInfo beginProcessMethod, string name, MethodInfo mi) { CodeMethod m = c.ImplementMethod(mi); CodeBuilder b = m.CodeBuilder; // object [] parameters = new object [x]; // parameters [0] = arg1; // parameters [1] = arg2; // ... // (return) BeginProcess (Contract.Operations [operName].BeginMethod, operName, parameters, asyncCallback, userState); ParameterInfo [] pinfos = mi.GetParameters(); CodeVariableDeclaration paramsDecl = new CodeVariableDeclaration(typeof(object []), "parameters"); b.CurrentBlock.Add(paramsDecl); CodeVariableReference paramsRef = paramsDecl.Variable; b.Assign(paramsRef, new CodeNewArray(typeof(object), new CodeLiteral(pinfos.Length - 2))); for (int i = 0; i < pinfos.Length - 2; i++) { ParameterInfo par = pinfos [i]; if (!par.IsOut) { b.Assign( new CodeArrayItem(paramsRef, new CodeLiteral(i)), new CodeCast(typeof(object), m.GetArg(i))); } } #if USE_OD_REFERENCE_IN_PROXY CodePropertyReference argMethodInfo = GetOperationMethod(m, b, name, "BeginMethod"); #else CodeMethodCall argMethodInfo = new CodeMethodCall(typeof(MethodBase), "GetCurrentMethod"); #endif CodeLiteral argOperName = new CodeLiteral(name); ParameterInfo p = pinfos [pinfos.Length - 2]; CodeArgumentReference callbackRef = new CodeArgumentReference(typeof(AsyncCallback), p.Position + 1, p.Name); p = pinfos [pinfos.Length - 1]; CodeArgumentReference stateRef = new CodeArgumentReference(typeof(object), p.Position + 1, p.Name); CodeVariableDeclaration retValueDecl = new CodeVariableDeclaration(mi.ReturnType, "retValue"); b.CurrentBlock.Add(retValueDecl); CodeVariableReference retValue = retValueDecl.Variable; b.Assign(retValue, new CodeCast(mi.ReturnType, b.CallFunc(m.GetThis(), beginProcessMethod, argMethodInfo, argOperName, paramsRef, callbackRef, stateRef))); b.Return(retValue); }
public CodeObject Compile(AstNode syntaxNode, CodeProgram prog) { var syntax = (ScriptQualifiedName)syntaxNode; if (syntax.IsVariable) { var code = new CodeVariableReference(syntax.Identifier) { SourceSpan = syntaxNode.Span }; return(code); } var modifiers = syntax.Modifiers.Select(modifier => AstDomCompiler.Compile(modifier, prog)).ToList(); CodeObjectReference objectReference; if (syntax.NextFirst) { var o = AstDomCompiler.Compile(syntax.NextPart, prog); var variable = o as CodeVariableReference; if (variable != null) { objectReference = new CodeObjectReference( variable.Id, new CodeObjectReference(syntax.Identifier, null, modifiers), new CodeObject[0]); } else { objectReference = (CodeObjectReference)o; ((CodeObjectReference)objectReference.Next).Next = new CodeObjectReference(syntax.Identifier, null, modifiers); } } else { objectReference = new CodeObjectReference( syntax.Identifier, AstDomCompiler.Compile(syntax.NextPart, prog), modifiers); } return(objectReference); }
public CodeObject Compile(AstNode syntaxNode, CodeProgram prog) { var syntax = (ScriptQualifiedName)syntaxNode; if (syntax.IsVariable) { var code = new CodeVariableReference(syntax.Identifier) {SourceSpan = syntaxNode.Span}; return code; } var modifiers = syntax.Modifiers.Select(modifier => AstDomCompiler.Compile(modifier, prog)).ToList(); CodeObjectReference objectReference; if (syntax.NextFirst) { var o = AstDomCompiler.Compile(syntax.NextPart, prog); var variable = o as CodeVariableReference; if (variable != null) { objectReference = new CodeObjectReference( variable.Id, new CodeObjectReference(syntax.Identifier, null, modifiers), new CodeObject[0]); } else { objectReference = (CodeObjectReference)o; ((CodeObjectReference)objectReference.Next).Next = new CodeObjectReference(syntax.Identifier, null, modifiers); } } else { objectReference = new CodeObjectReference( syntax.Identifier, AstDomCompiler.Compile(syntax.NextPart, prog), modifiers); } return objectReference; }
// I gave up making it common to generator and SRE... generator cannot provide // System.Type that is required for DeclareVariable. CodeMethod GenerateNativeCallbackDelegate(string generatedMethodName) { // sw.WriteLine ("{0}static {1} n_{2} (IntPtr jnienv, IntPtr native__this {3})", indent, RetVal.NativeType, Name + IDSignature, Parameters.CallbackSignature); var args = new List <Type> (); args.Add(typeof(IntPtr)); args.Add(typeof(IntPtr)); args.AddRange(parameter_type_infos.ConvertAll <Type> (p => p.NativeType).ToArray()); var mgen = DynamicCallbackFactory.CodeClass.CreateMethod(generatedMethodName, MethodAttributes.Static, return_type_info.NativeType, args.ToArray()); //sw.WriteLine ("{0}{{", indent); var builder = mgen.CodeBuilder; // non-static only CodeVariableReference varThis = null; if (!method.IsStatic) { //sw.WriteLine ("{0}\t{1} __this = Java.Lang.Object.GetObject<{1}> (native__this, JniHandleOwnership.DoNotTransfer);", indent, type.Name); varThis = builder.DeclareVariable(method.DeclaringType, new CodeMethodCall( get_object_method.MakeGenericMethod(method.DeclaringType), mgen.GetArg(0), mgen.GetArg(1), do_not_transfer_literal)); } //foreach (string s in Parameters.GetCallbackPrep (opt)) // sw.WriteLine ("{0}\t{1}", indent, s); // ... is handled as PrepareCallback (CodeExpression) // unlike generator, we don't check if it is property method, so ... (cont.) //if (String.IsNullOrEmpty (property_name)) { // string call = "__this." + Name + (as_formatted ? "Formatted" : String.Empty) + " (" + Parameters.Call + ")"; // if (IsVoid) // sw.WriteLine ("{0}\t{1};", indent, call); // else // sw.WriteLine ("{0}\t{1} {2};", indent, Parameters.HasCleanup ? RetVal.NativeType + " __ret =" : "return", RetVal.ToNative (opt, call)); var callArgs = new List <CodeExpression> (); for (int i = 0; i < parameter_type_infos.Count; i++) { if (parameter_type_infos [i].NeedsPrep) { callArgs.Add(parameter_type_infos [i].PrepareCallback(mgen.GetArg(i + 2))); } else { callArgs.Add(parameter_type_infos [i].FromNative(mgen.GetArg(i + 2))); } } CodeMethodCall call; if (method.IsStatic) { call = new CodeMethodCall(method, callArgs.ToArray()); } else { call = new CodeMethodCall(varThis, method, callArgs.ToArray()); } CodeExpression ret = null; if (method.ReturnType == typeof(void)) { builder.CurrentBlock.Add(call); } else { ret = builder.DeclareVariable(return_type_info.NativeType, return_type_info.ToNative(call)); } // ... (contd.) ignore the following part ... //} else { // if (IsVoid) // sw.WriteLine ("{0}\t__this.{1} = {2};", indent, property_name, Parameters.Call); // else // sw.WriteLine ("{0}\t{1} {2};", indent, Parameters.HasCleanup ? RetVal.NativeType + " __ret =" : "return", RetVal.ToNative (opt, "__this." + property_name)); //} // ... until here. //foreach (string cleanup in Parameters.CallbackCleanup) // sw.WriteLine ("{0}\t{1}", indent, cleanup); var callbackCleanup = new List <CodeStatement> (); for (int i = 0; i < parameter_type_infos.Count; i++) { builder.CurrentBlock.Add(parameter_type_infos [i].CleanupCallback(callArgs [i], mgen.GetArg(i))); } //if (!IsVoid && Parameters.HasCleanup) // sw.WriteLine ("{0}\treturn __ret;", indent); if (method.ReturnType != typeof(void)) { builder.Return(ret); } //sw.WriteLine ("{0}}}", indent); //sw.WriteLine (); return(mgen); }
internal CodeExpression PushStash(TypeSystem typeSystem, CodeExpressionEntry item) { this.stashSize++; int index = this.StashSize; var variableName = string.Format("R_{0}", index); var eval = new CodeTypeEvaluator(typeSystem, this.method); var variableType = eval.Evaluate(item.Expression); var variable = new VariableDefinition(variableName, variableType); var lhs = new CodeVariableReference(variable); this.stash[index] = lhs; return lhs; }
public CodeForEachStatement(CodeVariableReference id, CodeExpression container, CodeStatement statement) { Container = container; Id = id; Statement = statement; }
static void GenerateMethodImpl(CodeClass c, MethodInfo processMethod, string name, MethodInfo mi) { CodeMethod m = c.ImplementMethod(mi); CodeBuilder b = m.CodeBuilder; // object [] parameters = new object [x]; // parameters [0] = arg1; // parameters [1] = arg2; // ... // (return) Process (Contract.Operations [operName].SyncMethod, operName, parameters); ParameterInfo [] pinfos = mi.GetParameters(); CodeVariableDeclaration paramsDecl = new CodeVariableDeclaration(typeof(object []), "parameters"); b.CurrentBlock.Add(paramsDecl); CodeVariableReference paramsRef = paramsDecl.Variable; b.Assign(paramsRef, new CodeNewArray(typeof(object), new CodeLiteral(pinfos.Length))); for (int i = 0; i < pinfos.Length; i++) { ParameterInfo par = pinfos [i]; if (!par.IsOut) { b.Assign( new CodeArrayItem(paramsRef, new CodeLiteral(i)), new CodeCast(typeof(object), new CodeArgumentReference(par.ParameterType, par.Position + 1, "arg" + i))); } } #if USE_OD_REFERENCE_IN_PROXY CodePropertyReference argMethodInfo = GetOperationMethod(m, b, name, "SyncMethod"); #else CodeMethodCall argMethodInfo = new CodeMethodCall(typeof(MethodBase), "GetCurrentMethod"); #endif CodeLiteral argOperName = new CodeLiteral(name); CodeVariableReference retValue = null; if (mi.ReturnType == typeof(void)) { b.Call(m.GetThis(), processMethod, argMethodInfo, argOperName, paramsRef); } else { CodeVariableDeclaration retValueDecl = new CodeVariableDeclaration(mi.ReturnType, "retValue"); b.CurrentBlock.Add(retValueDecl); retValue = retValueDecl.Variable; b.Assign(retValue, new CodeCast(mi.ReturnType, b.CallFunc(m.GetThis(), processMethod, argMethodInfo, argOperName, paramsRef))); } for (int i = 0; i < pinfos.Length; i++) { ParameterInfo par = pinfos [i]; if (par.IsOut || par.ParameterType.IsByRef) { b.Assign( new CodeArgumentReference(par.ParameterType, par.Position + 1, "arg" + i), new CodeCast(par.ParameterType.GetElementType(), new CodeArrayItem(paramsRef, new CodeLiteral(i)))); } } if (retValue != null) { b.Return(retValue); } }
public static CodeVariableReference CreateExceptionVariableReference(MethodDefinition method, TypeReference typeRef) { var variable = new VariableDefinition("__ex__", typeRef); var code = new CodeVariableReference(variable); return code; }
private void Dup(Instruction il) { var rhs = Pop(); if (rhs is CodeThisReference) { Push(rhs); Push(rhs); return; } // store this expression into a variable so that multiple pops reference // the same expression instead of duplicating it // this is needed because of the mismatch between the stack containing // elements of values vs elements of expressions int index = this.duplicateCounter++; var variableName = string.Format("D_{0}", index); var variableType = this.typeEvaluator.Evaluate(rhs); //Console.WriteLine("Dup: {0}", variableType); // HACK: the variable index shouldn't really be used in higher-levels // so we just set the index to something that won't collide with existing ones. var variable = new VariableDefinition(variableName, variableType); var lhs = new CodeVariableReference(variable); AddAssignment(lhs, rhs); Push(lhs); Push(lhs); }
private void StoreLocal(VariableReference variable) { var lhs = new CodeVariableReference(variable); var rhs = RefinePrimitiveExpression(Pop(), variable.VariableType); AddAssignment(lhs, rhs); }