Search in sources :

Example 1 with IdentityKey

use of polyglot.util.IdentityKey in project soot by Sable.

the class ClassResolver method addFinalLocals.

private ArrayList<SootField> addFinalLocals(polyglot.ast.ClassBody cBody, ArrayList<IdentityKey> finalLocalsAvail, polyglot.types.ClassType nodeKeyType, AnonLocalClassInfo info) {
    ArrayList<SootField> finalFields = new ArrayList<SootField>();
    LocalUsesChecker luc = new LocalUsesChecker();
    cBody.visit(luc);
    /* Iterator localsNeededIt = luc.getLocals().iterator(); */
    ArrayList<IdentityKey> localsUsed = new ArrayList<IdentityKey>();
    /*
		 * while (localsNeededIt.hasNext()){ polyglot.types.LocalInstance li =
		 * (polyglot.types.LocalInstance)((polyglot.util.IdentityKey)
		 * localsNeededIt.next()).object(); //if
		 * (luc.getLocalDecls().contains(new polyglot.util.IdentityKey(li))){
		 * //} //else { //} if (finalLocalsAvail.contains(new
		 * polyglot.util.IdentityKey(li)) && !luc.getLocalDecls().contains(new
		 * polyglot.util.IdentityKey(li))){
		 * 
		 * addFinals(li,finalFields);
		 * 
		 * localsUsed.add(new polyglot.util.IdentityKey(li)); } }
		 */
    Iterator<IdentityKey> fieldsNeededIt = finalLocalsAvail.iterator();
    while (fieldsNeededIt.hasNext()) {
        polyglot.types.LocalInstance li = (polyglot.types.LocalInstance) fieldsNeededIt.next().object();
        if (!luc.getLocalDecls().contains(new polyglot.util.IdentityKey(li))) {
            localsUsed.add(new polyglot.util.IdentityKey(li));
            addFinals(li, finalFields);
        }
    }
    // this part is broken it adds all final locals available for the new
    // not just the ones used (which is a problem)
    Iterator<Node> newsIt = luc.getNews().iterator();
    while (newsIt.hasNext()) {
        polyglot.ast.New tempNew = (polyglot.ast.New) newsIt.next();
        polyglot.types.ClassType tempNewType = (polyglot.types.ClassType) tempNew.objectType().type();
        if (InitialResolver.v().finalLocalInfo().containsKey(new polyglot.util.IdentityKey(tempNewType))) {
            AnonLocalClassInfo lInfo = InitialResolver.v().finalLocalInfo().get(new polyglot.util.IdentityKey(tempNewType));
            Iterator<IdentityKey> it = lInfo.finalLocalsAvail().iterator();
            while (it.hasNext()) {
                polyglot.types.LocalInstance li2 = (polyglot.types.LocalInstance) it.next().object();
                if (!sootClass.declaresField("val$" + li2.name(), Util.getSootType(li2.type()))) {
                    if (!luc.getLocalDecls().contains(new polyglot.util.IdentityKey(li2))) {
                        addFinals(li2, finalFields);
                        localsUsed.add(new polyglot.util.IdentityKey(li2));
                    }
                }
            }
        }
    }
    // also need to add them if any super class all the way up needs one
    // because the super() will be made in init and it will require
    // possibly eventually to send in the finals
    polyglot.types.ClassType superType = (polyglot.types.ClassType) nodeKeyType.superType();
    while (!Util.getSootType(superType).equals(soot.Scene.v().getSootClass("java.lang.Object").getType())) {
        if (InitialResolver.v().finalLocalInfo().containsKey(new polyglot.util.IdentityKey(superType))) {
            AnonLocalClassInfo lInfo = InitialResolver.v().finalLocalInfo().get(new polyglot.util.IdentityKey(superType));
            Iterator<IdentityKey> it = lInfo.finalLocalsAvail().iterator();
            while (it.hasNext()) {
                polyglot.types.LocalInstance li2 = (polyglot.types.LocalInstance) it.next().object();
                if (!sootClass.declaresField("val$" + li2.name(), Util.getSootType(li2.type()))) {
                    if (!luc.getLocalDecls().contains(new polyglot.util.IdentityKey(li2))) {
                        addFinals(li2, finalFields);
                        localsUsed.add(new polyglot.util.IdentityKey(li2));
                    }
                }
            }
        }
        superType = (polyglot.types.ClassType) superType.superType();
    }
    info.finalLocalsUsed(localsUsed);
    InitialResolver.v().finalLocalInfo().put(new polyglot.util.IdentityKey(nodeKeyType), info);
    return finalFields;
}
Also used : IdentityKey(polyglot.util.IdentityKey) Node(polyglot.ast.Node) ArrayList(java.util.ArrayList) IdentityKey(polyglot.util.IdentityKey) SootField(soot.SootField)

Example 2 with IdentityKey

use of polyglot.util.IdentityKey in project soot by Sable.

the class JimpleBodyBuilder method handleFinalLocalParams.

/**
 * Adds extra params
 */
private void handleFinalLocalParams(ArrayList sootParams, ArrayList sootParamTypes, polyglot.types.ClassType keyType) {
    HashMap<IdentityKey, AnonLocalClassInfo> finalLocalInfo = soot.javaToJimple.InitialResolver.v().finalLocalInfo();
    if (finalLocalInfo != null) {
        if (finalLocalInfo.containsKey(new polyglot.util.IdentityKey(keyType))) {
            AnonLocalClassInfo alci = finalLocalInfo.get(new polyglot.util.IdentityKey(keyType));
            ArrayList<IdentityKey> finalLocals = alci.finalLocalsUsed();
            if (finalLocals != null) {
                Iterator<IdentityKey> it = finalLocals.iterator();
                while (it.hasNext()) {
                    Object next = it.next();
                    polyglot.types.LocalInstance li = (polyglot.types.LocalInstance) ((polyglot.util.IdentityKey) next).object();
                    sootParamTypes.add(Util.getSootType(li.type()));
                    sootParams.add(getLocal(li));
                }
            }
        }
    }
}
Also used : IdentityKey(polyglot.util.IdentityKey) IdentityKey(polyglot.util.IdentityKey)

Example 3 with IdentityKey

use of polyglot.util.IdentityKey in project soot by Sable.

the class InitialResolver method createAnonClassName.

private void createAnonClassName(polyglot.ast.New nextNew) {
    // maybe this anon has already been resolved
    if (anonClassMap == null) {
        anonClassMap = new BiMap();
    }
    if (anonTypeMap == null) {
        anonTypeMap = new HashMap<IdentityKey, String>();
    }
    if (!anonClassMap.containsKey(nextNew)) {
        int nextAvailNum = 1;
        polyglot.types.ClassType outerToMatch = nextNew.anonType().outer();
        while (outerToMatch.isNested()) {
            outerToMatch = outerToMatch.outer();
        }
        if (!anonTypeMap.isEmpty()) {
            Iterator<IdentityKey> matchIt = anonTypeMap.keySet().iterator();
            while (matchIt.hasNext()) {
                polyglot.types.ClassType pType = (polyglot.types.ClassType) matchIt.next().object();
                polyglot.types.ClassType outerMatch = pType.outer();
                while (outerMatch.isNested()) {
                    outerMatch = outerMatch.outer();
                }
                if (outerMatch.equals(outerToMatch)) {
                    int numFound = getAnonClassNum(anonTypeMap.get(new polyglot.util.IdentityKey(pType)));
                    if (numFound >= nextAvailNum) {
                        nextAvailNum = numFound + 1;
                    }
                }
            }
        }
        String realName = outerToMatch.fullName() + "$" + nextAvailNum;
        anonClassMap.put(nextNew, realName);
        anonTypeMap.put(new polyglot.util.IdentityKey(nextNew.anonType()), realName);
        addNameToAST(realName);
    }
}
Also used : IdentityKey(polyglot.util.IdentityKey) IdentityKey(polyglot.util.IdentityKey)

Example 4 with IdentityKey

use of polyglot.util.IdentityKey in project soot by Sable.

the class InitialResolver method createLocalClassName.

private void createLocalClassName(polyglot.ast.LocalClassDecl lcd) {
    // maybe this localdecl has already been resolved
    if (localClassMap == null) {
        localClassMap = new BiMap();
    }
    if (localTypeMap == null) {
        localTypeMap = new HashMap<IdentityKey, String>();
    }
    if (!localClassMap.containsKey(lcd)) {
        int nextAvailNum = 1;
        polyglot.types.ClassType outerToMatch = lcd.decl().type().outer();
        while (outerToMatch.isNested()) {
            outerToMatch = outerToMatch.outer();
        }
        if (!localTypeMap.isEmpty()) {
            Iterator<IdentityKey> matchIt = localTypeMap.keySet().iterator();
            while (matchIt.hasNext()) {
                polyglot.types.ClassType pType = (polyglot.types.ClassType) matchIt.next().object();
                polyglot.types.ClassType outerMatch = pType.outer();
                while (outerMatch.isNested()) {
                    outerMatch = outerMatch.outer();
                }
                if (outerMatch.equals(outerToMatch)) {
                    int numFound = getLocalClassNum(localTypeMap.get(new polyglot.util.IdentityKey(pType)), lcd.decl().name());
                    if (numFound >= nextAvailNum) {
                        nextAvailNum = numFound + 1;
                    }
                }
            }
        }
        String realName = outerToMatch.fullName() + "$" + nextAvailNum + lcd.decl().name();
        localClassMap.put(lcd, realName);
        localTypeMap.put(new polyglot.util.IdentityKey(lcd.decl().type()), realName);
        addNameToAST(realName);
    }
}
Also used : IdentityKey(polyglot.util.IdentityKey) IdentityKey(polyglot.util.IdentityKey)

Example 5 with IdentityKey

use of polyglot.util.IdentityKey in project soot by Sable.

the class InitialResolver method handleFinalLocals.

private void handleFinalLocals(polyglot.ast.ClassMember member) {
    MethodFinalsChecker mfc = new MethodFinalsChecker();
    member.visit(mfc);
    // System.out.println("mfc inners: "+mfc.inners());
    if (cCallList == null) {
        cCallList = new ArrayList<Node>();
    }
    cCallList.addAll(mfc.ccallList());
    // System.out.println("cCallList: "+cCallList);
    AnonLocalClassInfo alci = new AnonLocalClassInfo();
    if (member instanceof polyglot.ast.ProcedureDecl) {
        polyglot.ast.ProcedureDecl procedure = (polyglot.ast.ProcedureDecl) member;
        // not sure if this will break deep nesting
        alci.finalLocalsAvail(mfc.finalLocals());
        if (procedure.flags().isStatic()) {
            alci.inStaticMethod(true);
        }
    } else if (member instanceof polyglot.ast.FieldDecl) {
        alci.finalLocalsAvail(new ArrayList<IdentityKey>());
        if (((polyglot.ast.FieldDecl) member).flags().isStatic()) {
            alci.inStaticMethod(true);
        }
    } else if (member instanceof polyglot.ast.Initializer) {
        // for now don't make final locals avail in init blocks
        // need to test this
        alci.finalLocalsAvail(mfc.finalLocals());
        if (((polyglot.ast.Initializer) member).flags().isStatic()) {
            alci.inStaticMethod(true);
        }
    }
    if (finalLocalInfo == null) {
        finalLocalInfo = new HashMap<IdentityKey, AnonLocalClassInfo>();
    }
    Iterator<IdentityKey> it = mfc.inners().iterator();
    while (it.hasNext()) {
        polyglot.types.ClassType cType = (polyglot.types.ClassType) it.next().object();
        // do the comparison about locals avail and locals used here
        HashMap<IdentityKey, ArrayList<IdentityKey>> typeToLocalUsed = mfc.typeToLocalsUsed();
        ArrayList<IdentityKey> localsUsed = new ArrayList<IdentityKey>();
        if (typeToLocalUsed.containsKey(new polyglot.util.IdentityKey(cType))) {
            ArrayList localsNeeded = typeToLocalUsed.get(new polyglot.util.IdentityKey(cType));
            Iterator usesIt = localsNeeded.iterator();
            while (usesIt.hasNext()) {
                polyglot.types.LocalInstance li = (polyglot.types.LocalInstance) ((polyglot.util.IdentityKey) usesIt.next()).object();
                if (alci.finalLocalsAvail().contains(new polyglot.util.IdentityKey(li))) {
                    localsUsed.add(new polyglot.util.IdentityKey(li));
                }
            }
        }
        AnonLocalClassInfo info = new AnonLocalClassInfo();
        info.inStaticMethod(alci.inStaticMethod());
        info.finalLocalsAvail(localsUsed);
        if (!finalLocalInfo.containsKey(new polyglot.util.IdentityKey(cType))) {
            finalLocalInfo.put(new polyglot.util.IdentityKey(cType), info);
        }
    }
}
Also used : Node(polyglot.ast.Node) ArrayList(java.util.ArrayList) IdentityKey(polyglot.util.IdentityKey) Iterator(java.util.Iterator) IdentityKey(polyglot.util.IdentityKey)

Aggregations

IdentityKey (polyglot.util.IdentityKey)5 ArrayList (java.util.ArrayList)2 Node (polyglot.ast.Node)2 Iterator (java.util.Iterator)1 SootField (soot.SootField)1