private void EmitClassInstance(TypeDefinition typedef)
        {
            Formatter.WriteRaw("class");
            Formatter.Space();
            Formatter.WriteSelfReference(typedef, Facade.Instance);
            Formatter.Space();
            Formatter.OpenBrace();

            Formatter.WriteRaw("private __$brand_");
            Formatter.Identifier(typedef.FullName);
            Formatter.WriteRaw(" : any");
            Formatter.Semicolon();

            foreach (var genericParameter in DefinitelyTypedUtilities.BuildGenericParemetersMap(typedef.GenericParameters, null))
            {
                Formatter.WriteRaw("private");
                Formatter.Space();
                Formatter.WriteRaw("__$" + genericParameter.Value + "_brand_");
                Formatter.Identifier(typedef.FullName);
                Formatter.Space();
                Formatter.WriteRaw(":");
                Formatter.Space();
                if (genericParameter.Key.IsCovariant)
                {
                    Formatter.Identifier(DefinitelyTypedUtilities.GetGenericParameterOutParameterName(genericParameter.Value));
                }
                else if (genericParameter.Key.IsContravariant)
                {
                    Formatter.Identifier(DefinitelyTypedUtilities.GetGenericParameterInParameterName(genericParameter.Value));
                }
                else
                {
                    Formatter.Identifier(DefinitelyTypedUtilities.GetGenericParameterInstanceParameterName(genericParameter.Value));
                }
                Formatter.Semicolon();
            }

            if (!typedef.IsInterface)
            {
                var instanceFields = typedef.Fields.Where(it => it.IsPublic && !it.IsStatic && !Translator.ShouldSkipMember(it)).OrderBy(md => md.Name);
                foreach (var instanceField in instanceFields)
                {
                    EmitField(instanceField);
                }

                // TODO: We need filter them to not hide fields
                var instanceProperties = typedef.Properties
                                         .Where(it => !Translator.ShouldSkipMember(it) && !it.HasParameters && (
                                                    (it.GetMethod != null && it.GetMethod.IsPublic && !it.GetMethod.IsStatic && !Translator.ShouldSkipMember(it.GetMethod)) ||
                                                    (it.SetMethod != null && it.SetMethod.IsPublic && !it.SetMethod.IsStatic && !Translator.ShouldSkipMember(it.SetMethod))))
                                         .OrderBy(md => md.Name);
                foreach (var instanceProperty in instanceProperties)
                {
                    EmitProperty(instanceProperty);
                }

                var instanceMethods = typedef.Methods.Where(it => it.IsPublic && !it.IsStatic && !it.IsConstructor && !Translator.ShouldSkipMember(it)).OrderBy(md => md.Name);
                foreach (var instanceMethod in instanceMethods)
                {
                    EmitMethod(instanceMethod);
                }
            }

            Formatter.CloseBrace();
        }
Exemplo n.º 2
0
        public void EmitInterfaceDefinition(
            DecompilerContext context, IAstEmitter astEmitter, TypeDefinition iface
            )
        {
            Formatter.Identifier("JSIL.MakeInterface", EscapingMode.None);
            Formatter.LPar();
            Formatter.NewLine();

            Formatter.Value(Util.DemangleCecilTypeName(iface.FullName));
            Formatter.Comma();

            Formatter.Value(iface.IsPublic);
            Formatter.Comma();

            Formatter.OpenBracket();
            WriteGenericParameterNames(iface.GenericParameters);
            Formatter.CloseBracket();

            Formatter.Comma();
            Formatter.OpenFunction(null, (f) =>
                                   f.Identifier("$")
                                   );

            var refContext = new TypeReferenceContext {
                EnclosingType = iface,
                DefiningType  = iface
            };

            bool isFirst = true;

            foreach (var methodGroup in iface.Methods.GroupBy(md => md.Name))
            {
                foreach (var m in methodGroup)
                {
                    if (Translator.ShouldSkipMember(m))
                    {
                        continue;
                    }

                    var methodInfo = _TypeInfoProvider.GetMethod(m);

                    if ((methodInfo == null) || methodInfo.IsIgnored)
                    {
                        continue;
                    }

                    Formatter.Identifier("$", EscapingMode.None);
                    Formatter.Dot();
                    Formatter.Identifier("Method", EscapingMode.None);
                    Formatter.LPar();

                    Formatter.WriteRaw("{}");
                    Formatter.Comma();

                    Formatter.Value(Util.EscapeIdentifier(m.Name, EscapingMode.String));
                    Formatter.Comma();

                    Formatter.MethodSignature(m, methodInfo.Signature, refContext);

                    Formatter.RPar();
                    Formatter.Semicolon(true);
                }
            }

            foreach (var p in iface.Properties)
            {
                var propertyInfo = _TypeInfoProvider.GetProperty(p);
                if ((propertyInfo != null) && propertyInfo.IsIgnored)
                {
                    continue;
                }

                Formatter.Identifier("$", EscapingMode.None);
                Formatter.Dot();
                Formatter.Identifier("Property", EscapingMode.None);
                Formatter.LPar();

                Formatter.WriteRaw("{}");
                Formatter.Comma();

                Formatter.Value(Util.EscapeIdentifier(p.Name, EscapingMode.String));

                Formatter.RPar();
                Formatter.Semicolon(true);
            }

            Formatter.CloseBrace(false);

            Formatter.Comma();

            refContext = new TypeReferenceContext {
                EnclosingType = iface.DeclaringType,
                DefiningType  = iface
            };

            Formatter.OpenBracket();
            foreach (var i in iface.Interfaces)
            {
                if (!isFirst)
                {
                    Formatter.Comma();
                }

                Formatter.TypeReference(i, refContext);

                isFirst = false;
            }
            Formatter.CloseBracket();

            Formatter.RPar();

            EmitCustomAttributes(context, iface, iface, astEmitter);

            Formatter.Semicolon();
            Formatter.NewLine();
        }