Ejemplo n.º 1
0
 private static bool AddArrayInitializer(Exprent first, Exprent second)
 {
     if (first.type == Exprent.Exprent_Assignment)
     {
         AssignmentExprent @as = (AssignmentExprent)first;
         if (@as.GetRight().type == Exprent.Exprent_New && @as.GetLeft().type == Exprent.Exprent_Var)
         {
             NewExprent newExpr = (NewExprent)@as.GetRight();
             if (!(newExpr.GetLstArrayElements().Count == 0))
             {
                 VarExprent arrVar = (VarExprent)@as.GetLeft();
                 if (second.type == Exprent.Exprent_Assignment)
                 {
                     AssignmentExprent aas = (AssignmentExprent)second;
                     if (aas.GetLeft().type == Exprent.Exprent_Array)
                     {
                         ArrayExprent arrExpr = (ArrayExprent)aas.GetLeft();
                         if (arrExpr.GetArray().type == Exprent.Exprent_Var && arrVar.Equals(arrExpr.GetArray
                                                                                                 ()) && arrExpr.GetIndex().type == Exprent.Exprent_Const)
                         {
                             int constValue = ((ConstExprent)arrExpr.GetIndex()).GetIntValue();
                             if (constValue < newExpr.GetLstArrayElements().Count)
                             {
                                 Exprent init = newExpr.GetLstArrayElements()[constValue];
                                 if (init.type == Exprent.Exprent_Const)
                                 {
                                     ConstExprent cinit      = (ConstExprent)init;
                                     VarType      arrType    = newExpr.GetNewType().DecreaseArrayDim();
                                     ConstExprent defaultVal = ExprProcessor.GetDefaultArrayValue(arrType);
                                     if (cinit.Equals(defaultVal))
                                     {
                                         Exprent tempExpr = aas.GetRight();
                                         if (!tempExpr.ContainsExprent(arrVar))
                                         {
                                             newExpr.GetLstArrayElements()[constValue] = tempExpr;
                                             if (tempExpr.type == Exprent.Exprent_New)
                                             {
                                                 NewExprent tempNewExpr = (NewExprent)tempExpr;
                                                 int        dims        = newExpr.GetNewType().arrayDim;
                                                 if (dims > 1 && !(tempNewExpr.GetLstArrayElements().Count == 0))
                                                 {
                                                     tempNewExpr.SetDirectArrayInit(true);
                                                 }
                                             }
                                             return(true);
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     return(false);
 }
Ejemplo n.º 2
0
        private static int IsArrayInitializer(List <Exprent> list, int index)
        {
            Exprent current = list[index];

            if (current.type == Exprent.Exprent_Assignment)
            {
                AssignmentExprent @as = (AssignmentExprent)current;
                if (@as.GetRight().type == Exprent.Exprent_New && @as.GetLeft().type == Exprent.Exprent_Var)
                {
                    NewExprent newExpr = (NewExprent)@as.GetRight();
                    if (newExpr.GetExprType().arrayDim > 0 && newExpr.GetLstDims().Count == 1 && (newExpr
                                                                                                  .GetLstArrayElements().Count == 0) && newExpr.GetLstDims()[0].type == Exprent.Exprent_Const)
                    {
                        int size = (int)((ConstExprent)newExpr.GetLstDims()[0]).GetValue();
                        if (size == 0)
                        {
                            return(0);
                        }
                        VarExprent arrVar = (VarExprent)@as.GetLeft();
                        Dictionary <int, Exprent> mapInit = new Dictionary <int, Exprent>();
                        int i = 1;
                        while (index + i < list.Count && i <= size)
                        {
                            bool    found = false;
                            Exprent expr  = list[index + i];
                            if (expr.type == Exprent.Exprent_Assignment)
                            {
                                AssignmentExprent aas = (AssignmentExprent)expr;
                                if (aas.GetLeft().type == Exprent.Exprent_Array)
                                {
                                    ArrayExprent arrExpr = (ArrayExprent)aas.GetLeft();
                                    if (arrExpr.GetArray().type == Exprent.Exprent_Var && arrVar.Equals(arrExpr.GetArray
                                                                                                            ()) && arrExpr.GetIndex().type == Exprent.Exprent_Const)
                                    {
                                        // TODO: check for a number type. Failure extremely improbable, but nevertheless...
                                        int constValue = ((ConstExprent)arrExpr.GetIndex()).GetIntValue();
                                        if (constValue < size && !mapInit.ContainsKey(constValue))
                                        {
                                            if (!aas.GetRight().ContainsExprent(arrVar))
                                            {
                                                Sharpen.Collections.Put(mapInit, constValue, aas.GetRight());
                                                found = true;
                                            }
                                        }
                                    }
                                }
                            }
                            if (!found)
                            {
                                break;
                            }
                            i++;
                        }
                        double fraction = ((double)mapInit.Count) / size;
                        if ((arrVar.IsStack() && fraction > 0) || (size <= 7 && fraction >= 0.3) || (size
                                                                                                     > 7 && fraction >= 0.7))
                        {
                            List <Exprent> lstRet     = new List <Exprent>();
                            VarType        arrayType  = newExpr.GetNewType().DecreaseArrayDim();
                            ConstExprent   defaultVal = ExprProcessor.GetDefaultArrayValue(arrayType);
                            for (int j = 0; j < size; j++)
                            {
                                lstRet.Add(defaultVal.Copy());
                            }
                            int dims = newExpr.GetNewType().arrayDim;
                            foreach (KeyValuePair <int, Exprent> ent in mapInit)
                            {
                                Exprent tempExpr = ent.Value;
                                lstRet[ent.Key] = tempExpr;
                                if (tempExpr.type == Exprent.Exprent_New)
                                {
                                    NewExprent tempNewExpr = (NewExprent)tempExpr;
                                    if (dims > 1 && !(tempNewExpr.GetLstArrayElements().Count == 0))
                                    {
                                        tempNewExpr.SetDirectArrayInit(true);
                                    }
                                }
                            }
                            newExpr.SetLstArrayElements(lstRet);
                            return(mapInit.Count);
                        }
                    }
                }
            }
            return(0);
        }
        /// <exception cref="IOException"/>
        public static Exprent ParseAnnotationElement(DataInputStream data, ConstantPool pool
                                                     )
        {
            int tag = data.ReadUnsignedByte();

            switch (tag)
            {
            case 'e':
            {
                // enum constant
                string className = pool.GetPrimitiveConstant(data.ReadUnsignedShort()).GetString(
                    );
                string constName = pool.GetPrimitiveConstant(data.ReadUnsignedShort()).GetString(
                    );
                FieldDescriptor descr = FieldDescriptor.ParseDescriptor(className);
                return(new FieldExprent(constName, descr.type.value, true, null, descr, null));
            }

            case 'c':
            {
                // class
                string descriptor = pool.GetPrimitiveConstant(data.ReadUnsignedShort()).GetString
                                        ();
                VarType type = FieldDescriptor.ParseDescriptor(descriptor).type;
                string  value;
                switch (type.type)
                {
                case ICodeConstants.Type_Object:
                {
                    value = type.value;
                    break;
                }

                case ICodeConstants.Type_Byte:
                {
                    value = typeof(byte).FullName;
                    break;
                }

                case ICodeConstants.Type_Char:
                {
                    value = typeof(char).FullName;
                    break;
                }

                case ICodeConstants.Type_Double:
                {
                    value = typeof(double).FullName;
                    break;
                }

                case ICodeConstants.Type_Float:
                {
                    value = typeof(float).FullName;
                    break;
                }

                case ICodeConstants.Type_Int:
                {
                    value = typeof(int).FullName;
                    break;
                }

                case ICodeConstants.Type_Long:
                {
                    value = typeof(long).FullName;
                    break;
                }

                case ICodeConstants.Type_Short:
                {
                    value = typeof(short).FullName;
                    break;
                }

                case ICodeConstants.Type_Boolean:
                {
                    value = typeof(bool).FullName;
                    break;
                }

                case ICodeConstants.Type_Void:
                {
                    value = typeof(void).FullName;
                    break;
                }

                default:
                {
                    throw new Exception("invalid class type: " + type.type);
                }
                }
                return(new ConstExprent(VarType.Vartype_Class, value, null));
            }

            case '[':
            {
                // array
                List <Exprent> elements = new System.Collections.Generic.List <Exprent>();
                int            len      = data.ReadUnsignedShort();
                if (len > 0)
                {
                    elements = new List <Exprent>(len);
                    for (int i = 0; i < len; i++)
                    {
                        elements.Add(ParseAnnotationElement(data, pool));
                    }
                }
                VarType newType;
                if ((elements.Count == 0))
                {
                    newType = new VarType(ICodeConstants.Type_Object, 1, "java/lang/Object");
                }
                else
                {
                    VarType elementType = elements[0].GetExprType();
                    newType = new VarType(elementType.type, 1, elementType.value);
                }
                NewExprent newExpr = new NewExprent(newType, new System.Collections.Generic.List <
                                                        Exprent>(), null);
                newExpr.SetDirectArrayInit(true);
                newExpr.SetLstArrayElements(elements);
                return(newExpr);
            }

            case '@':
            {
                // annotation
                return(ParseAnnotation(data, pool));
            }

            default:
            {
                PrimitiveConstant cn = pool.GetPrimitiveConstant(data.ReadUnsignedShort());
                switch (tag)
                {
                case 'B':
                {
                    return(new ConstExprent(VarType.Vartype_Byte, cn.value, null));
                }

                case 'C':
                {
                    return(new ConstExprent(VarType.Vartype_Char, cn.value, null));
                }

                case 'D':
                {
                    return(new ConstExprent(VarType.Vartype_Double, cn.value, null));
                }

                case 'F':
                {
                    return(new ConstExprent(VarType.Vartype_Float, cn.value, null));
                }

                case 'I':
                {
                    return(new ConstExprent(VarType.Vartype_Int, cn.value, null));
                }

                case 'J':
                {
                    return(new ConstExprent(VarType.Vartype_Long, cn.value, null));
                }

                case 'S':
                {
                    return(new ConstExprent(VarType.Vartype_Short, cn.value, null));
                }

                case 'Z':
                {
                    return(new ConstExprent(VarType.Vartype_Boolean, cn.value, null));
                }

                case 's':
                {
                    return(new ConstExprent(VarType.Vartype_String, cn.value, null));
                }

                default:
                {
                    throw new Exception("invalid element type!");
                }
                }
                break;
            }
            }
        }