コード例 #1
0
        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);
            }
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        public bool BuildCodeCommands()
        {
            var scopes = new Stack <CodeScope>();

            contentRootScope = new CodeScope();
            var ctx = new CodeContext()
            {
                latestComments = null,
                activeScope    = contentRootScope,
                latestCommand  = null
            };

#if true
            while (CurrentSymbol != null)
            {
                var currentSymbol = CurrentSymbol;
                if (currentSymbol is ISeparator)
                {
                    if (ctx.latestCommand == null)
                    {
                        return(false);
                    }
                }

                if (ctx.latestCommand != null &&
                    ctx.latestCommand.CanAbsorb(currentSymbol))
                {
                    ctx.latestCommand.Add(currentSymbol);
                    cursorSymbol++;
                    continue;
                }

                if (ctx.activeScope != null &&
                    ctx.activeScope.CanAbsorb(currentSymbol))
                {
                    cursorSymbol++;
                    continue;
                }

                switch (currentSymbol)
                {
                case ISeparator separator:
                {
                    //If the separator has not been absorbed, it's considered a failure
                    return(false);
                }

                case IScopeOpen opener:
                {
                    if (ctx.latestCommand != null && !ctx.latestCommand.AllowInternalScope)
                    {
                        return(false);
                    }

                    var newScope = registry.SymbolToCommand[currentSymbol.GetType()]() as CodeScope;
                    if (newScope == null)
                    {
                        return(false);
                    }

                    newScope.opener = opener;
                    newScope.Add(ctx.latestComments);
                    if (ctx.latestCommand != null)
                    {
                        newScope.owner = ctx.latestCommand;
                        ctx.latestCommand.Add(newScope);
                    }
                    else
                    {
                        newScope.owner = ctx.activeScope;
                        ctx.activeScope.Add(newScope);
                    }

                    scopes.Push(ctx.activeScope);
                    ctx.activeScope   = newScope;
                    ctx.latestCommand = null;

                    break;
                }

                case IScopeClose closer:
                {
                    if (scopes.Count == 0)
                    {
                        return(false);
                    }

                    if (!ctx.activeScope.Match(closer))
                    {
                        return(false);
                    }

                    ctx.activeScope.Close();
                    ctx.activeScope    = scopes.Pop();
                    ctx.latestComments = new CodeComment();
                    ctx.latestCommand  = null;

                    break;
                }

                case IComment keywordComment:
                case LiteralValue literalValue:
                {
                    var newCommand = registry.SymbolToCommand[currentSymbol.GetType()]() as CodeCommand;
                    if (newCommand == null)
                    {
                        return(false);
                    }

                    if (currentSymbol is IComment)
                    {
                        if (ctx.latestComments == null)
                        {
                            ctx.latestComments = newCommand as CodeComment;
                        }
                    }
                    else
                    {
                        ctx.latestCommand = newCommand;
                    }

                    newCommand.Add(currentSymbol);
                    break;
                }

                case IKeyword keywordSymbol:
                {
                    var newKeyword = registry.SymbolToCommand[currentSymbol.GetType()]() as CodeKeyword;
                    if (newKeyword == null)
                    {
                        return(false);
                    }

                    newKeyword.keyword = keywordSymbol;
                    newKeyword.Add(ctx.latestComments);
                    ctx.activeScope.Add(newKeyword);

                    ctx.latestCommand = newKeyword;

                    break;
                }

                case IClearCommand clearCommand:
                {
                    ctx.latestCommand = null;

                    break;
                }
                }

                cursorSymbol++;
            }
#else
            while (CurrentSymbol != null)
            {
                var currentSymbol = CurrentSymbol;
                if (currentSymbol is IComment commentSymbol)
                {
                    ctx.latestComments.Set(commentSymbol);

                    cursorSymbol++;
                    continue;
                }

                if (currentSymbol is ScopeCodeBegin)
                {
                    if (ctx.latestCommand != null && !ctx.latestCommand.AllowInternalScope)
                    {
                        return(false);
                    }

                    var newScope = new CodeScope();
                    newScope.Add(ctx.latestComments);
                    if (ctx.latestCommand != null)
                    {
                        ctx.latestCommand.Add(newScope);
                    }
                    else
                    {
                        ctx.activeScope.Add(newScope);
                    }

                    scopes.Push(ctx.activeScope);
                    ctx.activeScope   = newScope;
                    ctx.latestCommand = null;
                }
                else if (currentSymbol is ScopeCodeEnd)
                {
                    if (scopes.Count == 0)
                    {
                        return(false);
                    }

                    ctx.activeScope    = scopes.Pop();
                    ctx.latestComments = new CodeComment();
                    ctx.latestCommand  = null;
                }

                if (currentSymbol is Keyword keywordSymbol)
                {
                    var newKeyword = new CodeKeyword {
                        keyword = keywordSymbol
                    };
                    newKeyword.Add(ctx.latestComments);
                    ctx.activeScope.Add(newKeyword);

                    ctx.latestCommand = newKeyword;

                    if (!(NextSymbol is ScopeInvokeBegin))
                    {
                        if (NextSymbol == null)
                        {
                            return(false);
                        }

                        cursorSymbol++;
                        continue;
                    }

                    cursorSymbol++;

                    var latestSymbol   = (Symbol)null;
                    var foundInvokeEnd = false;
                    cursorSymbol++;
                    while (CurrentSymbol != null)
                    {
                        currentSymbol = CurrentSymbol;
                        if (currentSymbol is VariableSeparator)
                        {
                            if (!(latestSymbol is Keyword))
                            {
                                return(false);
                            }
                        }
                        else if (currentSymbol is Keyword argSymbol)
                        {
                            if (latestSymbol is Keyword)
                            {
                                return(false);
                            }

                            newKeyword.Add(argSymbol);
                        }
                        else if (currentSymbol is ScopeInvokeEnd)
                        {
                            foundInvokeEnd = true;
                            break;
                        }
                        else
                        {
                            return(false);
                        }

                        latestSymbol = currentSymbol;
                        cursorSymbol++;
                    }

                    if (!foundInvokeEnd)
                    {
                        return(false);
                    }
                }

                if (currentSymbol is LiteralValue literalSymbol)
                {
                    var newLiteral = new CodeLiteral();
                    newLiteral.Add(literalSymbol);

                    ctx.activeScope.Add(newLiteral);

                    if (!(NextSymbol is LiteralValue))
                    {
                        if (NextSymbol == null)
                        {
                            return(false);
                        }

                        cursorSymbol++;
                        continue;
                    }

                    cursorSymbol++;
                    while (CurrentSymbol != null)
                    {
                        currentSymbol = CurrentSymbol;
                        if (currentSymbol is LiteralValue nextLiteral)
                        {
                            newLiteral.Add(nextLiteral);
                        }

                        if (!(NextSymbol is LiteralValue))
                        {
                            break;
                        }

                        cursorSymbol++;
                    }
                }

                cursorSymbol++;
            }
#endif

            return(true);
        }
コード例 #4
0
        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);
            }
        }