Пример #1
0
        private JsFunctionDefinitionExpression GenerateStructGetHashCodeMethod(ITypeDefinition type)
        {
            JsExpression h     = JsExpression.Identifier("h");
            var          stmts = new List <JsStatement>();

            foreach (var f in type.Fields.Where(f => !f.IsStatic))
            {
                var expr = GetFieldHashCode(f);
                if (expr != null)
                {
                    if (stmts.Count == 0)
                    {
                        stmts.Add(JsStatement.Var("h", expr));
                    }
                    else
                    {
                        stmts.Add(JsExpression.Assign(h, JsExpression.BitwiseXor(JsExpression.Multiply(h, JsExpression.Number(397)), expr)));
                    }
                }
            }
            switch (stmts.Count)
            {
            case 0:
                stmts.Add(JsStatement.Return(JsExpression.Number(0)));
                break;

            case 1:
                stmts[0] = JsStatement.Return(JsExpression.BitwiseOr(((JsVariableDeclarationStatement)stmts[0]).Declarations[0].Initializer, JsExpression.Number(0)));
                break;

            default:
                stmts.Add(JsStatement.Return(h));
                break;
            }

            return(JsExpression.FunctionDefinition(EmptyList <string> .Instance, JsStatement.Block(stmts)));
        }