Package soot

Examples of soot.Type


        // Object, then the first argument to non-static methods will be
        // the interface, not Object.
        if ((expression instanceof VirtualInvokeExpr)
                && (Scene.v().getSootClass("java.lang.Object")
                        .declaresMethod(method.getSubSignature()))) {
            Type baseType = expression.getBase().getType();

            if (baseType instanceof RefType) {
                declaringClass = ((RefType) baseType).getSootClass();
            }
        }
View Full Code Here


                Local nextLocal = (Local) (locals.next());

                if (!parameterAndThisLocals.contains(nextLocal)) {
                    code.append(_indent(1));

                    Type localType = nextLocal.getType();
                    code.append(CNames.typeNameOf(localType));
                    code.append(" " + CNames.localNameOf(nextLocal) + ";\n");
                    _updateRequiredTypes(localType);
                }
            }
View Full Code Here

            HashSet parameterAndThisLocals, String thisLocalName) {
        JimpleBody body = (JimpleBody) method.retrieveActiveBody();
        StringBuffer code = new StringBuffer();
        String description = "Function that implements Method "
                + method.getSignature();
        Type returnType = method.getReturnType();
        code.append(_comment(description));
        code.append(CNames.typeNameOf(returnType));
        _updateRequiredTypes(returnType);
        code.append(" ");
        code.append(CNames.functionNameOf(method));
        code.append("(");

        int parameterIndex;
        int parameterCount = 0;

        if (!method.isStatic()) {
            parameterAndThisLocals.add(body.getThisLocal());
            if (thisLocalName == null) {
                thisLocalName = CNames.localNameOf(body.getThisLocal());
            }
            code.append(CNames.instanceNameOf(method.getDeclaringClass()) + " "
                    + thisLocalName);
            parameterCount++;
        }

        for (parameterIndex = 0; parameterIndex < method.getParameterCount(); parameterIndex++) {
            if (parameterCount++ > 0) {
                code.append(", ");
            }

            Local local = body.getParameterLocal(parameterIndex);
            parameterAndThisLocals.add(local);

            Type parameterType = local.getType();
            code.append(CNames.typeNameOf(parameterType) + " "
                    + CNames.localNameOf(local));
            _updateRequiredTypes(parameterType);
        }
View Full Code Here

            if (expr instanceof SpecialInvokeExpr) {
                SpecialInvokeExpr r = (SpecialInvokeExpr) expr;
                //String methodName = r.getMethod().getName();

                Type type = r.getBase().getType();

                //   System.out.println("baseType = " + type);
                //  System.out.println("methodName = " + methodName);
                if (type instanceof NullType) {
                    // Note: The control path that causes this to be
                    // null should never occur in practice.
                    return;
                }

                SootClass baseClass = ((RefType) type).getSootClass();

                // FIXME: match better.
                // If we are invoking a method on a token, then...
                if (SootUtilities.derivesFrom(baseClass,
                        PtolemyUtilities.tokenClass)) {
                    if (r.getMethod().equals(
                            PtolemyUtilities.arrayTokenConstructor)) {
                        // The arrayToken constructor depends on the type
                        // of its constructor argument.
                        //     System.out.println("found array invoke: " + r);
                        //     System.out.println("Argument type is : " + in.get(r.getArg(0)));
                        ptolemy.data.type.Type argType = (ptolemy.data.type.Type) in
                                .get(r.getArg(0));

                        if (argType == null) {
                            argType = BaseType.UNKNOWN;
                        }

                        out.put(r.getBase(), new ArrayType(argType));
                    } else if (r.getMethod().equals(
                            PtolemyUtilities.arrayTokenWithTypeConstructor)) {
                        // The arrayToken constructor depends on the type
                        // of its constructor argument.
                        //     System.out.println("found array invoke: " + r);
                        //     System.out.println("Argument type is : " + in.get(r.getArg(0)));

                        ptolemy.data.type.Type argType = (ptolemy.data.type.Type) in
                                .get(r.getArg(0));

                        if (argType == null) {
                            argType = BaseType.UNKNOWN;
                        }

                        out.put(r.getBase(), new ArrayType(argType));
                    }
                }
            }
        } else if (stmt instanceof AssignStmt) {
            Value leftOp = ((AssignStmt) stmt).getLeftOp();

            if (!_isTokenType(leftOp.getType())) {
                //     System.out.println("type " + leftOp.getType()
                //             + " is not a token");
                return;
            }

            //  System.out.println("from " + in);
            Value rightOp = ((AssignStmt) stmt).getRightOp();

            if (rightOp instanceof StaticInvokeExpr) {
                StaticInvokeExpr r = (StaticInvokeExpr) rightOp;

                if (r.getMethod().equals(PtolemyUtilities.arraycopyMethod)) {
                    out.put(r.getArg(0), in.get(r.getArg(2)));
                }
            } else if (rightOp instanceof InstanceInvokeExpr
                    || rightOp instanceof InterfaceInvokeExpr) {
                InstanceInvokeExpr r = (InstanceInvokeExpr) rightOp;
                String methodName = r.getMethod().getName();

                Type type = r.getBase().getType();

                //   System.out.println("baseType = " + type);
                //  System.out.println("methodName = " + methodName);
                if (type instanceof NullType) {
                    // Note: The control path that causes this to be
                    // null should never occur in practice.
                    return;
                }

                SootClass baseClass = ((RefType) type).getSootClass();

                // FIXME: match better.
                // If we are invoking a method on a token, then...
                if (SootUtilities.derivesFrom(baseClass,
                        PtolemyUtilities.tokenClass)) {
                    if (methodName.equals("one") || methodName.equals("zero")
                            || methodName.equals("not")
                            || methodName.equals("bitwiseNot")
                            || methodName.equals("leftShift")
                            || methodName.equals("rightShift")
                            || methodName.equals("logicalRightShift")
                            || methodName.equals("pow")) {
                        // The returned type must be equal to the type
                        // we are calling the method on.
                        _updateTypeInAssignment(leftOp, in.get(r.getBase()),
                                out);
                    } else if (methodName.equals("add")
                            || methodName.equals("addReverse")
                            || methodName.equals("subtract")
                            || methodName.equals("subtractReverse")
                            || methodName.equals("multiply")
                            || methodName.equals("multiplyReverse")
                            || methodName.equals("divide")
                            || methodName.equals("divideReverse")
                            || methodName.equals("modulo")
                            || methodName.equals("moduloReverse")
                            || methodName.equals("bitwiseAnd")
                            || methodName.equals("bitwiseOr")
                            || methodName.equals("bitwiseXor")) {
                        //                         System.out.println("methodName = " + methodName);
                        //                         System.out.println("r.getBase() = " + r.getBase());
                        //                         System.out.println("r.getArg(0) = " + r.getArg(0));
                        //                         System.out.println("type(r.getBase()) = " + in.get(r.getBase()));
                        //                         System.out.println("type(r.getArg(0)) = " + in.get(r.getArg(0)));
                        ptolemy.data.type.Type baseType = (ptolemy.data.type.Type) in
                                .get(r.getBase());
                        ptolemy.data.type.Type argType = (ptolemy.data.type.Type) in
                                .get(r.getArg(0));

                        if ((baseType == null) || (argType == null)) {
                            out.put(leftOp, null);
                        } else {
                            _updateTypeInAssignment(leftOp, TypeLattice
                                    .lattice().leastUpperBound(baseType,
                                            argType), out);
                        }
                    } else if (methodName.equals("convert")) {
                        // The return rightOp type is equal to the base type.
                        // The first argument type is less than or equal to the base type.
                        _updateTypeInAssignment(leftOp, in.get(r.getBase()),
                                out);
                    } else if (methodName.equals("getElement")
                            || methodName.equals("arrayValue")) {
                        ptolemy.data.type.Type arrayType = (ptolemy.data.type.Type) in
                                .get(r.getBase());

                        if ((arrayType != null)
                                && arrayType instanceof ArrayType) {
                            _updateTypeInAssignment(leftOp,
                                    ((ArrayType) arrayType).getElementType(),
                                    out);
                        }
                    } else if (methodName.equals("getElementAsToken")) {
                        ptolemy.data.type.Type matrixType = (ptolemy.data.type.Type) in
                                .get(r.getBase());

                        if ((matrixType != null)
                                && matrixType instanceof MatrixType) {
                            _updateTypeInAssignment(leftOp,
                                    ((MatrixType) matrixType).getElementType(),
                                    out);
                        }
                    } else if (methodName.equals("absolute")) {
                        // Return the same as the input type, unless complex,
                        // in which case, return double.
                        ptolemy.data.type.Type inType = (ptolemy.data.type.Type) in
                                .get(r.getBase());

                        if (inType.equals(BaseType.COMPLEX)) {
                            _updateTypeInAssignment(leftOp, BaseType.DOUBLE,
                                    out);
                        } else {
                            _updateTypeInAssignment(leftOp, inType, out);
                        }
                    }
                } else if (SootUtilities.derivesFrom(baseClass,
                        PtolemyUtilities.componentPortClass)) {
                    // If we are invoking a method on a port.
                    TypedIOPort port = (TypedIOPort) _namedObjAnalysis
                            .getObject((Local) r.getBase());

                    //System.out.println("port for " + r.getBase() + " = " + port);
                    if (methodName.equals("broadcast")) {
                        // The type of the argument must be less than the
                        // type of the port.
                    } else if (methodName.equals("get")) {
                        // The port here may be null if the model does not
                        // actually contain the port...  This happens, for
                        // instance, in MathFunction
                        if (port != null) {
                            _updateTypeInAssignment(leftOp, port.getType(), out);
                        }
                    } else if (methodName.equals("send")) {
                        if (r.getArgCount() == 3) {
                            // The type of the argument must be less than the
                            // type of the port.
                            //r.getArg(1));
                        } else if (r.getArgCount() == 2) {
                            // The type of the argument must be less than the
                            // type of the port.
                            //            r.getArg(1));
                        }
                    }
                } else if (SootUtilities.derivesFrom(baseClass,
                        PtolemyUtilities.attributeClass)) {
                    // If we are invoking a method on a parameter.
                    Attribute attribute = (Attribute) _namedObjAnalysis
                            .getObject((Local) r.getBase());

                    if (attribute == null) {
                        // A method invocation with a null base is bogus,
                        // so don't create a type constraint.
                    }

                    if (attribute instanceof Variable) {
                        Variable parameter = (Variable) attribute;

                        if (methodName.equals("setToken")) {
                            // The type of the argument must be less than the
                            // type of the parameter.
                            // r.getArg(0));
                        } else if (methodName.equals("getToken")) {
                            // Return the type of the parameter.
                            _updateTypeInAssignment(leftOp,
                                    parameter.getType(), out);
                        }
                    }
                }
            } else if (rightOp instanceof ArrayRef) {
                //  System.out.println("arrayRef stmt = " + stmt);
                //                 System.out.println("right type = " + in.get(((ArrayRef)rightOp).getBase()));
                _updateTypeInAssignment(leftOp, in.get(((ArrayRef) rightOp)
                        .getBase()), out);
            } else if (rightOp instanceof CastExpr) {
                CastExpr castExpr = (CastExpr) rightOp;
                Type type = castExpr.getType();

                // FIXME: what if downcast???
                /*RefType tokenType =*/PtolemyUtilities.getBaseTokenType(type);

                //       System.out.println("castType = " + tokenType);
                //                 System.out.println("castOp = " + castExpr.getOp());
                //                 System.out.println("currentType = " + in.get(castExpr.getOp()));
                //if (tokenType != null) {
                _updateTypeInAssignment(leftOp, in.get(castExpr.getOp()), out);

                // } else {
                // Otherwise there is nothing to be done.
                //}
            } else if (rightOp instanceof Local) {
                Local local = (Local) rightOp;

                _updateTypeInAssignment(leftOp, in.get(local), out);
            } else if (rightOp instanceof NewExpr) {
                NewExpr newExpr = (NewExpr) rightOp;
                RefType type = newExpr.getBaseType();
                SootClass castClass = type.getSootClass();

                // If we are creating a Token type...
                if (SootUtilities.derivesFrom(castClass,
                        PtolemyUtilities.tokenClass)) {
                    // Then the rightOp of the expression is the type of the
                    // constructor.
                    _updateTypeInAssignment(leftOp, PtolemyUtilities
                            .getTokenTypeForSootType(type), out);
                } else {
                    // Otherwise there is nothing to be done.
                }
            } else if (rightOp instanceof NewArrayExpr) {
                // Since arrays are aliasable, we must update their types.
                NewArrayExpr newExpr = (NewArrayExpr) rightOp;
                Type type = newExpr.getBaseType();
                RefType tokenType = PtolemyUtilities.getBaseTokenType(type);

                if (tokenType != null) {
                    _updateTypeInAssignment(leftOp, PtolemyUtilities
                            .getTokenTypeForSootType(tokenType), out);
View Full Code Here

     @param method The method for which the stub header is needed.
     *  @return The code for the head of the stub.
     */
    private static String _getStubHeader(SootMethod method) {
        StringBuffer code = new StringBuffer();
        Type returnType = method.getReturnType();
        int numParameters = method.getParameterCount();

        code.append(CNames.typeNameOf(returnType) + " ");
        code.append(CNames.functionNameOf(method));

        code.append("( ");

        // The first parameter is an instance of the class the
        // method belongs to, if the method is non-static.
        if (!method.isStatic()) {
            code.append(CNames.instanceNameOf(method.getDeclaringClass())
                    + " instance");

            // Put a comma if there are more parameters.
            if (numParameters > 0) {
                code.append(", ");
            }
        }

        Iterator i = method.getParameterTypes().iterator();
        int parameterIndex = 0;
        Type parameterType;

        while (i.hasNext()) {
            parameterType = (Type) i.next();
            code.append(CNames.typeNameOf(parameterType));
            code.append(" n" + parameterIndex);
View Full Code Here

            SootClass oldClass, SootClass newClass) {
        Iterator fields = theClass.getFields().snapshotIterator();

        while (fields.hasNext()) {
            SootField oldField = (SootField) fields.next();
            Type type = oldField.getType();

            //  System.out.println("field with type " + type);
            if (type instanceof RefType) {
                SootClass refClass = ((RefType) type).getSootClass();
View Full Code Here

        for (Iterator methods = methodList.iterator(); methods.hasNext();) {
            SootMethod newMethod = (SootMethod) methods.next();

            // System.out.println("newMethod = " + newMethod.getSignature());
            Type returnType = newMethod.getReturnType();

            if (returnType instanceof RefType
                    && (((RefType) returnType).getSootClass() == oldClass)) {
                newMethod.setReturnType(RefType.v(newClass));
            }

            List paramTypes = new LinkedList();

            for (Iterator oldParamTypes = newMethod.getParameterTypes()
                    .iterator(); oldParamTypes.hasNext();) {
                Type type = (Type) oldParamTypes.next();

                if (type instanceof RefType
                        && (((RefType) type).getSootClass() == oldClass)) {
                    paramTypes.add(RefType.v(newClass));
                } else if (type instanceof RefType
                        && (((RefType) type).getSootClass().getName()
                                .startsWith(oldClass.getName()))) {
                    SootClass changeClass = _getInnerClassCopy(oldClass,
                            ((RefType) type).getSootClass(), newClass);
                    paramTypes.add(RefType.v(changeClass));
                } else {
                    paramTypes.add(type);
                }
            }

            newMethod.setParameterTypes(paramTypes);

            // we have to do this seemingly useless
            // thing, since the scene caches a pointer
            // to the method based on it's parameter types.
            theClass.removeMethod(newMethod);
            theClass.addMethod(newMethod);

            Body newBody = newMethod.retrieveActiveBody();

            for (Iterator locals = newBody.getLocals().iterator(); locals
                    .hasNext();) {
                Local local = (Local) locals.next();
                Type type = local.getType();

                if (type instanceof RefType
                        && (((RefType) type).getSootClass() == oldClass)) {
                    local.setType(RefType.v(newClass));
                }
            }

            Iterator j = newBody.getUnits().iterator();

            while (j.hasNext()) {
                Unit unit = (Unit) j.next();
                Iterator boxes = unit.getUseAndDefBoxes().iterator();
                //  System.out.println("unit = " + unit);

                while (boxes.hasNext()) {
                    ValueBox box = (ValueBox) boxes.next();
                    Value value = box.getValue();

                    if (value instanceof FieldRef) {
                        // Fix references to fields
                        FieldRef r = (FieldRef) value;
                        SootFieldRef fieldRef = r.getFieldRef();
                        if (fieldRef.type() instanceof RefType) {
                            RefType fieldType = (RefType) fieldRef.type();
                            SootClass fieldClass = fieldType.getSootClass();
                            if (fieldClass == oldClass) {
                                r.setFieldRef(Scene.v().makeFieldRef(
                                        fieldRef.declaringClass(),
                                        fieldRef.name(), RefType.v(newClass),
                                        fieldRef.isStatic()));
                            }
                            fieldRef = r.getFieldRef();
                        }

                        if (fieldRef.declaringClass() == oldClass) {
                            // We might also have a reference to a field
                            // which is not actually declared in the
                            // oldclass, in which case, we just fix up
                            // the ref to point to the new super class
                            r.setFieldRef(Scene.v().makeFieldRef(newClass,
                                    fieldRef.name(), fieldRef.type(),
                                    fieldRef.isStatic()));
                        } else if (fieldRef.declaringClass().getName()
                                .startsWith(oldClass.getName())) {
                            SootClass changeClass = _getInnerClassCopy(
                                    oldClass, r.getField().getDeclaringClass(),
                                    newClass);
                            r.setFieldRef(changeClass.getFieldByName(
                                    r.getField().getName()).makeRef());
                        }//  else if (r.getField().getDeclaringClass() == oldClass) {
                        //                             r.setFieldRef(
                        //                                     newClass.getFieldByName(
                        //                                             r.getField().getName()).makeRef());

                        //                             //   System.out.println("fieldRef = " +
                        //                             //              box.getValue());
                        //                         }

                    } else if (value instanceof CastExpr) {
                        // Fix casts
                        CastExpr r = (CastExpr) value;
                        Type type = r.getType();

                        if (type instanceof RefType) {
                            SootClass refClass = ((RefType) type)
                                    .getSootClass();

                            if (refClass == oldClass) {
                                r.setCastType(RefType.v(newClass));

                                // System.out.println("newValue = " +
                                //        box.getValue());
                            } else if (refClass.getName().startsWith(
                                    oldClass.getName())) {
                                SootClass changeClass = _getInnerClassCopy(
                                        oldClass, refClass, newClass);
                                r.setCastType(RefType.v(changeClass));
                            }
                        }
                    } else if (value instanceof ThisRef) {
                        // Fix references to 'this'
                        ThisRef r = (ThisRef) value;
                        Type type = r.getType();

                        if (type instanceof RefType
                                && (((RefType) type).getSootClass() == oldClass)) {
                            box.setValue(Jimple.v().newThisRef(
                                    RefType.v(newClass)));
                        }
                    } else if (value instanceof ParameterRef) {
                        // Fix references to a parameter
                        ParameterRef r = (ParameterRef) value;
                        Type type = r.getType();

                        if (type instanceof RefType
                                && (((RefType) type).getSootClass() == oldClass)) {
                            box.setValue(Jimple.v().newParameterRef(
                                    RefType.v(newClass), r.getIndex()));
                        }
                    } else if (value instanceof InvokeExpr) {
                        // Fix up the method invokes.
                        InvokeExpr r = (InvokeExpr) value;
                        SootMethodRef methodRef = r.getMethodRef();
                        System.out.println("invoke = " + r);

                        List newParameterTypes = new LinkedList();
                        for (Iterator i = methodRef.parameterTypes().iterator(); i
                                .hasNext();) {
                            Type type = (Type) i.next();
                            if (type instanceof RefType
                                    && (((RefType) type).getSootClass() == oldClass)) {
                                System.out.println("matchedParameter = "
                                        + newClass);
                                newParameterTypes.add(RefType.v(newClass));
                            } else if (type instanceof RefType
                                    && (((RefType) type).getSootClass()
                                            .getName().startsWith(oldClass
                                            .getName()))) {
                                System.out.println("matchedParameter = "
                                        + newClass);
                                SootClass changeClass = _getInnerClassCopy(
                                        oldClass, ((RefType) type)
                                                .getSootClass(), newClass);
                                newParameterTypes.add(RefType.v(changeClass));
                            } else {
                                newParameterTypes.add(type);
                            }

                        }

                        Type newReturnType = methodRef.returnType();
                        if (newReturnType instanceof RefType
                                && (((RefType) newReturnType).getSootClass() == oldClass)) {
                            newReturnType = RefType.v(newClass);
                        }
View Full Code Here

        if (type1 instanceof RefType && type2 instanceof RefType) {
            SootClass class1 = ((RefType) type1).getSootClass();
            SootClass class2 = ((RefType) type2).getSootClass();
            return derivesFrom(class1, class2);
        } else if (type1 instanceof Type && type2 instanceof RefType) {
            Type elementType1 = ((ArrayType) type1).baseType;

            // FIXME: FindBugs: Impossible cast from soot.RefType to
            // soot.ArrayType
            Type elementType2 = ((ArrayType) type2).baseType;
            return isSubtypeOf(elementType1, elementType2);
        } else {
            return type1.equals(type2); // numeric types.
        }
    }
View Full Code Here

                Iterator parameterTypes = method.getParameterTypes().iterator();
                Iterator arguments = args.iterator();
                boolean isEqual = true;

                while (parameterTypes.hasNext()) {
                    Type parameterType = (Type) parameterTypes.next();
                    Value argument = (Value) arguments.next();
                    Type argumentType = argument.getType();

                    if (argumentType != parameterType) {
                        // This is inefficient.  Full type merging is
                        // expensive and unnecessary.
                        isEqual = (parameterType == argumentType.merge(
                                parameterType, Scene.v()));
                    }

                    if (!isEqual) {
                        break;
View Full Code Here

        int i = 0;
        Object[] args = new Object[argValues.length];

        for (Iterator parameterTypes = sootMethod.getParameterTypes()
                .iterator(); parameterTypes.hasNext();) {
            Type parameterType = (Type) parameterTypes.next();

            try {
                args[i] = convertConstantValueToArgument(argValues[i]);
                parameterClasses[i] = getClassForType(parameterType);
                i++;
            } catch (Exception ex) {
                ex.printStackTrace();
                throw new RuntimeException("Class not found = "
                        + parameterType.toString());
            }
        }

        Object returned;
View Full Code Here

TOP

Related Classes of soot.Type

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.