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

GenerateLocals() private method

private GenerateLocals ( ZMethod zMethod, Class localsClass ) : void
zMethod ZMethod
localsClass Class
return void
        private void GenerateLocals(ZMethod zMethod, Class localsClass)
        {
            List<Field> locals = new List<Field>(10);

            Method localsGetValue = (Method)Templates.GetMemberByName(localsClass.Members, "GetValue");
            Method localsSetValue = (Method)Templates.GetMemberByName(localsClass.Members, "SetValue");
            System.Compiler.Switch switchLocalsGetValue =
                (System.Compiler.Switch)Templates.GetStatementTemplate("GetFieldInfoSwitch");
            localsGetValue.Body.Statements.Add(switchLocalsGetValue);
            System.Compiler.Switch switchLocalsSetValue =
                (System.Compiler.Switch)Templates.GetStatementTemplate("SetFieldInfoSwitch");
            localsSetValue.Body.Statements.Add(switchLocalsSetValue);
            Method copier = (Method)Templates.GetMemberByName(localsClass.Members, "CopyContents");

            for (int i = 0, n = zMethod.LocalVars.Count; i < n; i++)
            {
                Field localVar = zMethod.LocalVars[i];
                TypeNode zingType = localVar.Type;
                if (localVar.Type == null)
                    continue;

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

                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_" + localVar.Name.Name);
                Field idf = new Field(localsClass, 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));
                switchLocalsGetValue.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));
                switchLocalsSetValue.Cases.Add(setCase);

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

                Property accessor = GetAccessorProperty("localAccessor", f.Type,
                                                        localVar.Name, f.Name, idf.Name, localInputOrOutput);

                Identifier qualifier = new Identifier("locals");

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

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

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

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

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