Microsoft.Zing.Splicer.GenerateInputs C# (CSharp) Method

GenerateInputs() private method

private GenerateInputs ( ZMethod zMethod, Class inputsClass ) : void
zMethod ZMethod
inputsClass Class
return void
        private void GenerateInputs(ZMethod zMethod, Class inputsClass)
        {
            List<Field> inputs = new List<Field>(10);

            Method inputsGetValue = (Method)Templates.GetMemberByName(inputsClass.Members, "GetValue");
            Method inputsSetValue = (Method)Templates.GetMemberByName(inputsClass.Members, "SetValue");
            System.Compiler.Switch switchInputsGetValue =
                (System.Compiler.Switch)Templates.GetStatementTemplate("GetFieldInfoSwitch");
            inputsGetValue.Body.Statements.Add(switchInputsGetValue);
            System.Compiler.Switch switchInputsSetValue =
                (System.Compiler.Switch)Templates.GetStatementTemplate("SetFieldInfoSwitch");
            inputsSetValue.Body.Statements.Add(switchInputsSetValue);
            Method copier = (Method)Templates.GetMemberByName(inputsClass.Members, "CopyContents");

            // Create fields in Inputs or Outputs for the parameters
            for (int i = 0, n = zMethod.Parameters.Count; i < n; i++)
            {
                Parameter param = zMethod.Parameters[i];
                if (param == null || param.Type == null || (param.Flags & ParameterFlags.Out) != 0)
                    continue;

                TypeNode zingType = param.Type;

                Field f = new Field(inputsClass, null, FieldFlags.Public,
                    new Identifier("priv_" + param.Name.Name),
                    param.Type, null);

                if (f.Type is Reference)
                    f.Type = ((Reference)f.Type).ElementType;

                if (GetTypeClassification(f.Type) == TypeClassification.Heap)
                {
                    f.Type = this.ZingPtrType;
                }
                else if (!IsPredefinedType(f.Type))
                    f.Type = new TypeExpression(new QualifiedIdentifier(
                        new Identifier("Application"), zingType.Name), zingType.SourceContext);

                Identifier idFieldName = new Identifier("id_" + param.Name.Name);
                Field idf = new Field(inputsClass, null, FieldFlags.Public | FieldFlags.Static,
                    idFieldName,
                    SystemTypes.Int32, null);
                idf.Initializer = new Literal(i, SystemTypes.Int32);

                SwitchCase getCase = ((System.Compiler.Switch)Templates.GetStatementTemplate("GetFieldInfoCase")).Cases[0];
                Replacer.Replace(getCase, "_fieldId", new Literal(i, SystemTypes.Int32));
                Replacer.Replace(getCase, "_fieldName", new Identifier(f.Name.Name));
                switchInputsGetValue.Cases.Add(getCase);

                SwitchCase setCase = ((System.Compiler.Switch)Templates.GetStatementTemplate("SetFieldInfoCase")).Cases[0];
                Replacer.Replace(setCase, "_fieldId", new Literal(i, SystemTypes.Int32));
                Replacer.Replace(setCase, "_fieldName", new Identifier(f.Name.Name));
                TypeExpression tn = f.Type as TypeExpression;
                Replacer.Replace(setCase, "_fieldType", tn != null ? tn.Expression : new Identifier(f.Type.Name.Name));
                switchInputsSetValue.Cases.Add(setCase);

                QualifiedIdentifier localInputOrOutput = new QualifiedIdentifier(new Identifier("LocType"), new Identifier("Input"));

                Property accessor = GetAccessorProperty("inputAccessor", f.Type, param.Name, f.Name, idf.Name, localInputOrOutput);

                inputsClass.Members.Add(f);
                inputsClass.Members.Add(idf);
                inputsClass.Members.Add(accessor);
                f.DeclaringType = inputsClass;
                idf.DeclaringType = inputsClass;
                accessor.DeclaringType = inputsClass;
                if (accessor.Getter != null)
                {
                    inputsClass.Members.Add(accessor.Getter);
                    accessor.Getter.DeclaringType = inputsClass;
                }
                if (accessor.Setter != null)
                {
                    inputsClass.Members.Add(accessor.Setter);
                    accessor.Setter.DeclaringType = inputsClass;
                }

                if (zingType is Struct && !zingType.IsPrimitive && f.Type != SystemTypes.Decimal)
                    collectStructAccessors(false, (Struct)zingType, f.Name,
                        param.Name.Name, inputsClass);

                copier.Body.Statements.Add(GetCopyStatement(f.Name));
                inputs.Add(f);
            }

            Method writer = (Method)Templates.GetMemberByName(inputsClass.Members, "WriteString");
            Method traverser = (Method)Templates.GetMemberByName(inputsClass.Members, "TraverseFields");

            for (int i = 0, n = inputs.Count; i < n; i++)
            {
                Field f = (Field)inputs[i];
                writer.Body.Statements.Add(GetWriterStatement("this", f.Type, f.Name));
                traverser.Body.Statements.Add(GetTraverserStatement("this", f.Type, f.Name));
            }
        }