Exemple #1
0
        bool ImportParameters(MethodReference fromMethod)
        {
            bool appendSuffix = false;

            int n = fromMethod.HasParameters ? fromMethod.Parameters.Count : 0;

            Parameters = new List <JavaFieldRef>(n);

            for (int i = 0; i < n; i++)
            {
                var fromParameterType = fromMethod.Parameters[i].ParameterType;
                var paramType         = CilType.From(fromParameterType);

                if (paramType.IsPointer)
                {
                    if (paramType.IsValueClass || (!paramType.IsReference))
                    {
                        paramType = CilType.MakeSpanOf(paramType);
                    }
                    else
                    {
                        throw CilMain.Where.Exception("invalid pointer type in parameter");
                    }
                }

                if (paramType.IsGenericParameter)
                {
                    appendSuffix = true;
                    var nm = "-generic-";
                    if (paramType.IsArray)
                    {
                        nm += "array-" + paramType.ArrayRank + "-";
                    }
                    if (paramType.IsByReference)
                    {
                        nm = "-ref" + nm.Replace("&", "");
                    }
                    nm       += "$" + ((GenericParameter)fromParameterType.GetElementType()).Position;
                    paramType = CilType.WrapMethodGenericParameter(paramType, nm);
                    Flags    |= GEN_ARGS;
                }

                if (paramType.IsByReference)
                {
                    if (paramType.IsValueClass)
                    {
                        paramType    = paramType.MakeByRef();
                        appendSuffix = true;
                    }
                    else
                    {
                        // byref of any reference type parameter becomes system.Reference,
                        // so add a unique method name suffix
                        var paramBoxedType = new BoxedType(paramType, false);
                        paramType     = paramBoxedType;
                        appendSuffix |= paramBoxedType.IsBoxedReference;
                    }
                }

                if (!appendSuffix)
                {
                    appendSuffix |= fromParameterType.IsGenericInstance;

                    if (!paramType.IsReference)
                    {
                        appendSuffix |= ShouldRenamePrimitive(paramType);
                    }
                }

                Parameters.Add(new JavaFieldRef("", paramType));
            }

            var returnType = CilType.From(fromMethod.ReturnType);

            if (returnType.IsGenericParameter)
            {
                returnType = CilType.WrapMethodGenericParameter(returnType);
            }
            else if (returnType.IsByReference)
            {
                returnType = new BoxedType(returnType, false);
            }
            ReturnType = returnType;

            appendSuffix |= fromMethod.ReturnType.IsGenericInstance;

            return(appendSuffix);
Exemple #2
0
        int InitLocalsVars(MethodBody cilBody, List <CilType> localTypes, int nextIndex)
        {
            int highestIndex = -1;

            if (cilBody.HasVariables)
            {
                foreach (var cilVar in cilBody.Variables)
                {
                    if (cilVar.Index > highestIndex)
                    {
                        highestIndex = cilVar.Index;
                    }
                }
            }

            varToLocalMap = new int[highestIndex + 1];

            if (cilBody.HasVariables)
            {
                foreach (var cilVar in cilBody.Variables)
                {
                    CilMain.Where.Push("local #" + cilVar.Index);

                    var genericMark = CilMain.GenericStack.Mark();
                    var varType     = CilMain.GenericStack.EnterType(cilVar.VariableType);

                    if (varType.IsValueClass || varType.IsPointer)
                    {
                        bool isByReference = varType.IsByReference;

                        if (varType.IsPointer)
                        {
                            varType = CilType.MakeSpanOf(varType);
                        }

                        // value classes are allocated at the top of the method
                        if (!isByReference)
                        {
                            ValueUtil.InitLocal(varType, nextIndex, code);
                            varType = varType.MakeClonedAtTop();
                        }
                    }
                    else if (varType.IsByReference)
                    {
                        varType = new BoxedType(varType, false);
                    }
                    else if (varType.IsArray && varType.IsGenericParameter)
                    {
                        // note that GenericArrayType is compared by reference
                        // in CodeArrays, to detect an array of a generic type T[]
                        varType = CodeArrays.GenericArrayType;
                    }

                    varToLocalMap[cilVar.Index] = nextIndex;
                    nextIndex += varType.Category;

                    localTypes.Add(varType);
                    while (nextIndex > localTypes.Count)
                    {
                        localTypes.Add(null);
                    }

                    CilMain.GenericStack.Release(genericMark);
                    CilMain.Where.Pop();
                }
            }

            return(nextIndex);
        }