Exemple #1
0
        internal void Identifier(string name, TypeReferenceContext context, bool includeParens = false)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            Identifier(name);
        }
 public void Identifier(TypeReference type, TypeReferenceContext context, bool includeParens = false)
 {
     if (type.FullName == "JSIL.Proxy.AnyType")
     {
         WriteRaw("JSIL.AnyType");
     }
     else
     {
         TypeIdentifier(type as dynamic, context, includeParens);
     }
 }
 public void Identifier(ILVariable variable, TypeReferenceContext context, bool includeParens = false)
 {
     if (variable.Type.IsByReference)
     {
         throw new NotImplementedException("Old-style use of JavascriptFormatter.Identifier on a ref variable");
     }
     else
     {
         Identifier(variable.Name);
     }
 }
 protected void TypeReferenceInternal(ArrayType at, TypeReferenceContext context)
 {
     WriteRaw("$jsilcore");
     Dot();
     WriteRaw("TypeRef");
     LPar();
     Value("System.Array");
     Comma();
     OpenBracket();
     TypeReference(at.ElementType, context);
     CloseBracket();
     RPar();
 }
Exemple #5
0
 public void Identifier(ILVariable variable, TypeReferenceContext context, bool includeParens = false)
 {
     if (variable.Type.IsByReference)
     {
         Identifier(variable.Name);
         Dot();
         Identifier("value");
     }
     else
     {
         Identifier(variable.Name);
     }
 }
        protected void TypeReferenceInternal(PointerType pt, TypeReferenceContext context)
        {
            WriteRaw("$jsilcore");
            Dot();
            WriteRaw("TypeRef");
            LPar();

            Value("JSIL.Pointer");
            Comma();
            OpenBracket(false);

            TypeReference(pt.ElementType, context);

            CloseBracket(false);
            RPar();
        }
        protected void TypeReferenceInternal(ByReferenceType byref, TypeReferenceContext context)
        {
            WriteRaw("$jsilcore");
            Dot();
            WriteRaw("TypeRef");
            LPar();

            Value("JSIL.Reference");
            Comma();
            OpenBracket(false);

            TypeReference(byref.ElementType, context);

            CloseBracket(false);
            RPar();
        }
        protected void TypeIdentifier(ArrayType type, TypeReferenceContext context, bool includeParens)
        {
            if (includeParens)
            {
                LPar();
            }

            WriteRaw("System.Array.Of");
            LPar();
            TypeIdentifier(type.ElementType as dynamic, context, false);
            RPar();

            if (includeParens)
            {
                RPar();
                Space();
            }
        }
        protected void TypeIdentifier(GenericInstanceType type, TypeReferenceContext context, bool includeParens)
        {
            if (includeParens)
            {
                LPar();
            }

            Identifier(type.ElementType as dynamic, context, includeParens);

            Dot();
            WriteRaw("Of");
            LPar();
            CommaSeparatedList(type.GenericArguments, context, ListValueType.TypeIdentifier);
            RPar();

            if (includeParens)
            {
                RPar();
                Space();
            }
        }
        protected void TypeReferenceInternal(GenericParameter gp, TypeReferenceContext context)
        {
            var ownerType   = gp.Owner as TypeReference;
            var ownerMethod = gp.Owner as MethodReference;

            if (context != null)
            {
                if (ownerType != null)
                {
                    if (TypeUtil.TypesAreAssignable(TypeInfo, ownerType, context.SignatureMethodType))
                    {
                        var resolved = JSIL.Internal.MethodSignature.ResolveGenericParameter(gp, context.SignatureMethodType);

                        if (resolved != null)
                        {
                            if (resolved != gp)
                            {
                                TypeReference(resolved, context);
                                return;
                            }
                            else
                            {
                                TypeIdentifier(resolved, context, false);
                                return;
                            }
                        }
                    }

                    if (TypeUtil.TypesAreEqual(ownerType, context.EnclosingMethodType))
                    {
                        TypeIdentifier(gp, context, false);
                        return;
                    }

                    if (TypeUtil.TypesAreEqual(ownerType, context.DefiningType))
                    {
                        OpenGenericParameter(gp, context.DefiningType.FullName);
                        return;
                    }

                    if (TypeUtil.TypesAreEqual(ownerType, context.EnclosingType))
                    {
                        LocalOpenGenericParameter(gp);
                        return;
                    }

                    var ownerTypeResolved = ownerType.Resolve();
                    if (ownerTypeResolved != null)
                    {
                        // Is it a generic parameter of a compiler-generated class (i.e. enumerator function, delegate, etc)
                        //  nested inside our EnclosingType? If so, uhhhh, shit.
                        if (
                            TypeUtil.TypesAreEqual(context.EnclosingType, ownerTypeResolved.DeclaringType) &&
                            ownerTypeResolved.CustomAttributes.Any(
                                (ca) => ca.AttributeType.FullName == "System.Runtime.CompilerServices.CompilerGeneratedAttribute"
                                )
                            )
                        {
                            // FIXME: I HAVE NO IDEA WHAT I AM DOING
                            OpenGenericParameter(gp, ownerTypeResolved.FullName);
                            return;
                        }
                    }

                    throw new NotImplementedException(String.Format(
                                                          "Unimplemented form of generic type parameter: '{0}'.",
                                                          gp
                                                          ));
                }
                else if (ownerMethod != null)
                {
                    Func <MethodReference, int> getPosition = (mr) => {
                        for (var i = 0; i < mr.GenericParameters.Count; i++)
                        {
                            if (mr.GenericParameters[i].Name == gp.Name)
                            {
                                return(i);
                            }
                        }

                        throw new NotImplementedException(String.Format(
                                                              "Generic parameter '{0}' not found in method '{1}' parameter list",
                                                              gp, ownerMethod
                                                              ));
                    };

                    var ownerMethodIdentifier = new QualifiedMemberIdentifier(
                        new TypeIdentifier(ownerMethod.DeclaringType.Resolve()),
                        new MemberIdentifier(TypeInfo, ownerMethod)
                        );

                    if (ownerMethodIdentifier.Equals(ownerMethod, context.InvokingMethod, TypeInfo))
                    {
                        var gim = (GenericInstanceMethod)context.InvokingMethod;
                        TypeReference(gim.GenericArguments[getPosition(ownerMethod)], context);

                        return;
                    }

                    if (ownerMethodIdentifier.Equals(ownerMethod, context.EnclosingMethod, TypeInfo))
                    {
                        Identifier(gp.Name);

                        return;
                    }

                    if (
                        ownerMethodIdentifier.Equals(ownerMethod, context.DefiningMethod, TypeInfo) ||
                        ownerMethodIdentifier.Equals(ownerMethod, context.SignatureMethod, TypeInfo)
                        )
                    {
                        Value(String.Format("!!{0}", getPosition(ownerMethod)));

                        return;
                    }

                    throw new NotImplementedException(String.Format(
                                                          "Unimplemented form of generic method parameter: '{0}'.",
                                                          gp
                                                          ));
                }
            }
            else
            {
                throw new NotImplementedException("Cannot resolve generic parameter without a TypeReferenceContext.");
            }

            throw new NotImplementedException(String.Format(
                                                  "Unimplemented form of generic parameter: '{0}'.",
                                                  gp
                                                  ));
        }
 public void CommaSeparatedList(IEnumerable <TypeReference> types, TypeReferenceContext context)
 {
     CommaSeparatedListCore(
         types, (type) => TypeReference(type, context)
         );
 }
Exemple #12
0
 internal void Identifier(string name, TypeReferenceContext context, bool includeParens = false)
 {
     Identifier(name);
 }
 public void ConstructorSignature(MethodReference method, MethodSignature signature, TypeReferenceContext context)
 {
     Signature(method, signature, context, true, true);
 }
 protected void TypeIdentifier(PointerType ptr, TypeReferenceContext context, bool includeParens)
 {
     Identifier(ptr.ElementType as dynamic, context, includeParens);
 }
 protected void TypeIdentifier(RequiredModifierType modreq, TypeReferenceContext context, bool includeParens)
 {
     Identifier(modreq.ElementType as dynamic, context, includeParens);
 }
 protected void TypeIdentifier(OptionalModifierType modopt, TypeReferenceContext context, bool includeParens)
 {
     Identifier(modopt.ElementType as dynamic, context, includeParens);
 }
        public void TypeReference(TypeReference type, TypeReferenceContext context)
        {
            if (
                (context != null) &&
                (context.EnclosingType != null)
                )
            {
                if (TypeUtil.TypesAreEqual(type, context.EnclosingType))
                {
                    // Types can reference themselves, so this prevents recursive initialization.
                    if (Stubbed && Configuration.GenerateSkeletonsForStubbedAssemblies.GetValueOrDefault(false))
                    {
                    }
                    else
                    {
                        WriteRaw("$.Type");
                        return;
                    }
                }

                // The interface builder provides helpful shorthand for corlib type references.
                if (type.Scope.Name == "mscorlib" || type.Scope.Name == "CommonLanguageRuntimeLibrary")
                {
                    if (CorlibTypes.Contains(type.FullName))
                    {
                        WriteRaw("$.");
                        WriteRaw(type.Name);
                        return;
                    }
                }
            }

            if (type.FullName == "JSIL.Proxy.AnyType")
            {
                Value("JSIL.AnyType");
                return;
            }
            else if (type.FullName == "JSIL.Proxy.AnyType[]")
            {
                TypeReference(TypeUtil.GetTypeDefinition(type), context);
                Space();
                Comment("AnyType[]");
                return;
            }

            var byref = type as ByReferenceType;
            var gp    = type as GenericParameter;
            var at    = type as ArrayType;
            var pt    = type as PointerType;

            if (byref != null)
            {
                TypeReferenceInternal(byref, context);
            }
            else if (gp != null)
            {
                TypeReferenceInternal(gp, context);
            }
            else if (at != null)
            {
                TypeReferenceInternal(at, context);
            }
            else if (pt != null)
            {
                TypeReferenceInternal(pt, context);
            }
            else
            {
                TypeReferenceInternal(type, context);
            }
        }
 protected void TypeIdentifier(TypeInfo type, TypeReferenceContext context, bool includeParens)
 {
     TypeIdentifier(type.Definition as dynamic, context, includeParens);
 }
 public void TypeReference(TypeInfo type, TypeReferenceContext context)
 {
     TypeReference(type.Definition, context);
 }
Exemple #20
0
        public void MethodSignature(MethodReference method, MethodSignature signature, TypeReferenceContext context)
        {
            // The signature cache can cause problems inside methods for generic signatures.
            var cached = Configuration.Optimizer.CacheMethodSignatures.GetValueOrDefault(true);

            // We also don't want to use the cache for method definitions in skeletons.
            if (Stubbed && Configuration.GenerateSkeletonsForStubbedAssemblies.GetValueOrDefault(false))
            {
                cached = false;
            }

            if (cached && ((context.InvokingMethod != null) || (context.EnclosingMethod != null)))
            {
                if (TypeUtil.IsOpenType(signature.ReturnType))
                {
                    cached = false;
                }
                else if (signature.ParameterTypes.Any(TypeUtil.IsOpenType))
                {
                    cached = false;
                }
            }

            if (cached)
            {
                WriteRaw("$sig.get");
                LPar();

                Value(signature.ID);
                Comma();
            }
            else
            {
                LPar();

                WriteRaw("new JSIL.MethodSignature");
                LPar();
            }

            var oldSignature = context.SignatureMethod;

            context.SignatureMethod = method;

            try {
                if ((signature.ReturnType == null) || (signature.ReturnType.FullName == "System.Void"))
                {
                    WriteRaw("null");
                }
                else
                {
                    if ((context.EnclosingMethod != null) && !TypeUtil.IsOpenType(signature.ReturnType))
                    {
                        TypeIdentifier(signature.ReturnType as dynamic, context, false);
                    }
                    else
                    {
                        TypeReference(signature.ReturnType, context);
                    }
                }

                Comma();
                OpenBracket(false);

                CommaSeparatedListCore(
                    signature.ParameterTypes, (pt) => {
                    if ((context.EnclosingMethod != null) && !TypeUtil.IsOpenType(pt))
                    {
                        TypeIdentifier(pt as dynamic, context, false);
                    }
                    else
                    {
                        TypeReference(pt, context);
                    }
                }
                    );

                CloseBracket(false);

                if (signature.GenericParameterNames != null)
                {
                    Comma();
                    OpenBracket(false);
                    CommaSeparatedList(signature.GenericParameterNames, context, ListValueType.Primitive);
                    CloseBracket(false);
                }
            } finally {
                context.SignatureMethod = oldSignature;
            }

            RPar();

            if (!cached)
            {
                RPar();
            }
        }
 public void MethodSignature(MethodReference method, MethodSignature signature, TypeReferenceContext context)
 {
     Signature(method, signature, context, false, true);
 }
Exemple #22
0
        protected void TypeReferenceInternal(GenericParameter gp, TypeReferenceContext context)
        {
            var ownerType   = gp.Owner as TypeReference;
            var ownerMethod = gp.Owner as MethodReference;

            if (context != null)
            {
                if (ownerType != null)
                {
                    if (TypeUtil.TypesAreAssignable(TypeInfo, ownerType, context.SignatureMethodType))
                    {
                        TypeReference resolved = null;

                        var git = (context.SignatureMethodType as GenericInstanceType);
                        if (git != null)
                        {
                            for (var i = 0; i < git.ElementType.GenericParameters.Count; i++)
                            {
                                var _ = git.ElementType.GenericParameters[i];
                                if ((_.Name == gp.Name) || (_.Position == gp.Position))
                                {
                                    resolved = git.GenericArguments[i];
                                    break;
                                }
                            }

                            if (resolved == null)
                            {
                                throw new NotImplementedException(String.Format(
                                                                      "Could not find generic parameter '{0}' in type {1}",
                                                                      gp, context.SignatureMethodType
                                                                      ));
                            }
                        }

                        if (resolved != null)
                        {
                            if (resolved != gp)
                            {
                                TypeReference(resolved, context);
                                return;
                            }
                            else
                            {
                                TypeIdentifier(resolved, context, false);
                                return;
                            }
                        }
                    }

                    if (TypeUtil.TypesAreEqual(ownerType, context.EnclosingMethodType))
                    {
                        TypeIdentifier(gp, context, false);
                        return;
                    }

                    if (TypeUtil.TypesAreEqual(ownerType, context.DefiningType))
                    {
                        OpenGenericParameter(gp.Name, context.DefiningType.FullName);
                        return;
                    }

                    if (TypeUtil.TypesAreEqual(ownerType, context.EnclosingType))
                    {
                        WriteRaw("$.GenericParameter");
                        LPar();
                        Value(gp.Name);
                        RPar();

                        return;
                    }

                    throw new NotImplementedException(String.Format(
                                                          "Unimplemented form of generic type parameter: '{0}'.",
                                                          gp
                                                          ));
                }
                else if (ownerMethod != null)
                {
                    Func <MethodReference, int> getPosition = (mr) => {
                        for (var i = 0; i < mr.GenericParameters.Count; i++)
                        {
                            if (mr.GenericParameters[i].Name == gp.Name)
                            {
                                return(i);
                            }
                        }

                        throw new NotImplementedException(String.Format(
                                                              "Generic parameter '{0}' not found in method '{1}' parameter list",
                                                              gp, ownerMethod
                                                              ));
                    };

                    var ownerMethodIdentifier = new QualifiedMemberIdentifier(
                        new TypeIdentifier(ownerMethod.DeclaringType),
                        new MemberIdentifier(TypeInfo, ownerMethod)
                        );

                    if (ownerMethodIdentifier.Equals(ownerMethod, context.InvokingMethod, TypeInfo))
                    {
                        var gim = (GenericInstanceMethod)context.InvokingMethod;
                        TypeReference(gim.GenericArguments[getPosition(ownerMethod)], context);

                        return;
                    }

                    if (ownerMethodIdentifier.Equals(ownerMethod, context.DefiningMethod, TypeInfo))
                    {
                        Value(String.Format("!!{0}", getPosition(ownerMethod)));

                        return;
                    }

                    if (ownerMethodIdentifier.Equals(ownerMethod, context.EnclosingMethod, TypeInfo))
                    {
                        throw new NotImplementedException(String.Format(
                                                              "Unimplemented form of generic method parameter: '{0}'.",
                                                              gp
                                                              ));
                    }

                    Value(String.Format("!!{0}", getPosition(ownerMethod)));
                    return;
                }
            }
            else
            {
                throw new NotImplementedException("Cannot resolve generic parameter without a TypeReferenceContext.");
            }

            throw new NotImplementedException(String.Format(
                                                  "Unimplemented form of generic parameter: '{0}'.",
                                                  gp
                                                  ));
        }
        protected void TypeIdentifier(TypeReference type, TypeReferenceContext context, bool includeParens)
        {
            if (type.FullName == "JSIL.Proxy.AnyType")
            {
                WriteRaw("JSIL.AnyType");
                return;
            }

            if (TypeUtil.TypesAreEqual(context.EnclosingType, type, true))
            {
                WriteRaw("$thisType");
                return;
            }

            if (type.IsGenericParameter)
            {
                var gp        = (GenericParameter)type;
                var ownerType = gp.Owner as TypeReference;

                if (gp.Owner == null)
                {
                    Value(gp.Name);
                }
                else if (
                    (CurrentMethod != null) &&
                    ((CurrentMethod.Equals(gp.Owner)) ||
                     (CurrentMethod.DeclaringType.Equals(gp.Owner))
                    )
                    )
                {
                    if (ownerType != null)
                    {
                        Identifier(ownerType, context);
                        Dot();
                        Identifier(gp.Name);
                        Dot();
                        Identifier("get");
                        LPar();
                        WriteRaw("this");
                        RPar();
                    }
                    else
                    {
                        Identifier(gp.Name);
                    }
                }
                else
                {
                    if (EmitThisForParameter(gp))
                    {
                        WriteRaw("this");
                        Dot();
                    }

                    Identifier(type.FullName);
                }
            }
            else
            {
                var info = TypeInfo.Get(type);
                if ((info != null) && (info.Replacement != null))
                {
                    WriteRaw(info.Replacement);
                    return;
                }

                var typedef = type.Resolve();
                if (typedef != null)
                {
                    if (GetContainingAssemblyName(typedef) == Assembly.FullName)
                    {
                        WriteToken(PrivateToken);
                        WriteRaw(".");
                    }
                    else
                    {
                        AssemblyReference(typedef);
                        WriteRaw(".");
                    }
                }

                if (info != null)
                {
                    WriteRaw(Util.EscapeIdentifier(
                                 info.FullName, EscapingMode.TypeIdentifier
                                 ));
                }
                else
                {
                    // FIXME: Is this right?
                    WriteRaw(Util.EscapeIdentifier(
                                 type.FullName, EscapingMode.TypeIdentifier
                                 ));
                }
            }
        }
        public void Signature(MethodReference method, MethodSignature signature, TypeReferenceContext context, bool forConstructor, bool allowCache)
        {
            if (forConstructor)
            {
                WriteRaw("new JSIL.ConstructorSignature");
            }
            else
            {
                WriteRaw("new JSIL.MethodSignature");
            }

            LPar();

            var oldSignature = context.SignatureMethod;

            context.SignatureMethod = method;

            try {
                if (forConstructor)
                {
                    TypeReference(method.DeclaringType, context);

                    Comma();
                }
                else
                {
                    if ((signature.ReturnType == null) || (signature.ReturnType.FullName == "System.Void"))
                    {
                        WriteRaw("null");
                    }
                    else
                    {
                        if ((context.EnclosingMethod != null) && !TypeUtil.IsOpenType(signature.ReturnType))
                        {
                            TypeIdentifier(signature.ReturnType as dynamic, context, false);
                        }
                        else
                        {
                            TypeReference(signature.ReturnType, context);
                        }
                    }

                    Comma();
                }

                OpenBracket(false);

                CommaSeparatedListCore(
                    signature.ParameterTypes, (pt) => {
                    if ((context.EnclosingMethod != null) && !TypeUtil.IsOpenType(pt))
                    {
                        TypeIdentifier(pt as dynamic, context, false);
                    }
                    else
                    {
                        TypeReference(pt, context);
                    }
                }
                    );

                CloseBracket(false);

                if (!forConstructor && (signature.GenericParameterNames != null))
                {
                    Comma();
                    OpenBracket(false);
                    CommaSeparatedList(signature.GenericParameterNames, context, ListValueType.Primitive);
                    CloseBracket(false);
                }
            } finally {
                context.SignatureMethod = oldSignature;
            }

            RPar();
        }