Example #1
0
        /// <summary>
        /// Create the body of the valueOf(string) method.
        /// </summary>
        private AstBlock CreateValueOfBody(XSyntheticMethodDefinition method, XTypeSystem typeSystem)
        {
            var fields = XType.Fields.Where(x => x.IsStatic && !(x is XSyntheticFieldDefinition)).ToList();
            var ast    = AstBlock.Create <AstExpression>();

            // Find name
            foreach (var field in fields)
            {
                var notEqualLabel = new AstLabel(AstNode.NoSource, "not_equal_to" + field.Name);
                var equalsExpr    = new AstExpression(AstNode.NoSource, AstCode.Call, FrameworkReferences.StringEquals(typeSystem),
                                                      new AstExpression(AstNode.NoSource, AstCode.Ldstr, field.Name),
                                                      new AstExpression(AstNode.NoSource, AstCode.Ldloc, method.AstParameters[0]));

                // If !equals(name, field.name) goto notEqualLabel
                ast.Body.Add(new AstExpression(AstNode.NoSource, AstCode.Brfalse, notEqualLabel, equalsExpr));
                // Return field object
                ast.Body.Add(new AstExpression(AstNode.NoSource, AstCode.Ret, null,
                                               new AstExpression(AstNode.NoSource, AstCode.Ldsfld, field)));
                // notEqualLabel:
                ast.Body.Add(notEqualLabel);
            }

            // Return null
            ast.Body.Add(new AstExpression(AstNode.NoSource, AstCode.Ret, null,
                                           new AstExpression(AstNode.NoSource, AstCode.Ldnull, null)));
            return(ast);
        }