+ System.out.println("\nSSJAVA:Calculate extra locations: " + md);
+
+ SSJavaLattice<String> methodLattice = getMethodLattice(md);
+
+ MethodSummary methodSummary = getMethodSummary(md);
+
+ FlowGraph fg = getFlowGraph(md);
+ Set<FlowNode> nodeSet = fg.getNodeSet();
+
+ // for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
+ // FlowNode flowNode = (FlowNode) iterator.next();
+ // if (flowNode.isDeclaratonNode()) {
+ // CompositeLocation inferLoc = methodInfo.getInferLocation(flowNode.getDescTuple().get(0));
+ // String locIdentifier = inferLoc.get(0).getLocIdentifier();
+ // if (!methodLattice.containsKey(locIdentifier)) {
+ // methodLattice.put(locIdentifier);
+ // }
+ // }
+ // }
+ MethodLocationInfo methodInfo = getMethodLocationInfo(md);
+
+ Map<Integer, CompositeLocation> mapParamToLoc = methodSummary.getMapParamIdxToInferLoc();
+ Set<Integer> paramIdxSet = mapParamToLoc.keySet();
+
+ if (!ssjava.getMethodContainingSSJavaLoop().equals(md)) {
+ // calculate the initial program counter location
+ // PC location is higher than location types of parameters which has incoming flows.
+ String pcLocSymbol = "PCLOC";
+
+ Set<NTuple<Location>> paramLocTupleHavingInFlowSet = new HashSet<NTuple<Location>>();
+
+ int numParams = fg.getNumParameters();
+ for (int i = 0; i < numParams; i++) {
+ FlowNode paramFlowNode = fg.getParamFlowNode(i);
+
+ Descriptor prefix = paramFlowNode.getDescTuple().get(0);
+
+ // if (fg.getIncomingFlowNodeSet(paramFlowNode).size() > 0) {
+ if (fg.getIncomingNodeSetByPrefix(prefix).size() > 0) {
+ // parameter has in-value flows
+ NTuple<Descriptor> paramDescTuple = paramFlowNode.getCurrentDescTuple();
+ NTuple<Location> paramLocTuple = translateToLocTuple(md, paramDescTuple);
+
+ paramLocTupleHavingInFlowSet.add(paramLocTuple);
+ // CompositeLocation inferLoc = mapParamToLoc.get(paramIdx);
+ // paramInFlowSet.add(inferLoc);
+ }
+ }
+
+ System.out.println("paramLocTupleHavingInFlowSet=" + paramLocTupleHavingInFlowSet);
+
+ int numParamsWithIncomingValue = paramLocTupleHavingInFlowSet.size();
+
+ if (paramLocTupleHavingInFlowSet.size() > 0
+ && numParamsWithIncomingValue == fg.getNumParameters()) {
+ // If the PC loc is going to be higher than all paramters, we dont' need to do anything
+
+ // Generates a location in the method lattice that is higher than the
+ // paramLocTupleHavingInFlowSet
+ NTuple<Location> pcLocTuple = generateLocTupleHigherThan(md, paramLocTupleHavingInFlowSet);
+
+ int pcLocTupleIdx = pcLocTuple.size() - 1;
+ Location pcLoc = pcLocTuple.get(pcLocTupleIdx);
+ Descriptor pcDesc = pcLoc.getLocDescriptor();
+ Descriptor enclosingDesc = pcLocTuple.get(pcLocTupleIdx).getDescriptor();
+
+ HierarchyGraph hierarchyGraph = getHierarchyGraph(enclosingDesc);
+ HNode pcNode = hierarchyGraph.getHNode(pcDesc);
+ pcNode.setSkeleton(true);
+
+ for (Iterator iterator = paramLocTupleHavingInFlowSet.iterator(); iterator.hasNext();) {
+ NTuple<Location> paramLocTuple = (NTuple<Location>) iterator.next();
+ Descriptor lowerDesc = paramLocTuple.get(pcLocTupleIdx).getLocDescriptor();
+ hierarchyGraph.addEdge(pcDesc, lowerDesc);
+ }
+
+ }
+
+ }
+
+ // calculate a return location
+ // the return location type is lower than all parameters and location
+ // types of return values
+ if (!md.getReturnType().isVoid()) {
+ // first, generate the set of return value location types that starts
+ // with 'this' reference
+
+ Set<CompositeLocation> inferFieldReturnLocSet = new HashSet<CompositeLocation>();
+
+ Set<FlowNode> paramFlowNodeFlowingToReturnValueSet = getParamNodeFlowingToReturnValue(md);
+ Set<CompositeLocation> inferParamLocSet = new HashSet<CompositeLocation>();
+ for (Iterator iterator = paramFlowNodeFlowingToReturnValueSet.iterator(); iterator.hasNext();) {
+ FlowNode fn = (FlowNode) iterator.next();
+ CompositeLocation inferLoc =
+ generateInferredCompositeLocation(methodInfo, getFlowGraph(md).getLocationTuple(fn));
+ inferParamLocSet.add(inferLoc);
+ }
+
+ Set<FlowNode> returnNodeSet = fg.getReturnNodeSet();
+
+ skip: for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
+ FlowNode returnNode = (FlowNode) iterator.next();
+ CompositeLocation inferReturnLoc =
+ generateInferredCompositeLocation(methodInfo, fg.getLocationTuple(returnNode));
+ if (inferReturnLoc.get(0).getLocIdentifier().equals("this")) {
+ // if the location type of the return value matches "this" reference
+ // then, check whether this return value is equal to/lower than all
+ // of parameters that possibly flow into the return values
+ for (Iterator iterator2 = inferParamLocSet.iterator(); iterator2.hasNext();) {
+ CompositeLocation paramInferLoc = (CompositeLocation) iterator2.next();
+
+ if ((!paramInferLoc.equals(inferReturnLoc))
+ && !isGreaterThan(methodLattice, paramInferLoc, inferReturnLoc)) {
+ continue skip;
+ }
+ }
+ inferFieldReturnLocSet.add(inferReturnLoc);
+
+ }
+ }
+
+ if (inferFieldReturnLocSet.size() > 0) {
+
+ CompositeLocation returnLoc = getLowest(methodLattice, inferFieldReturnLocSet);
+ if (returnLoc == null) {
+ // in this case, assign <'this',bottom> to the RETURNLOC
+ returnLoc = new CompositeLocation(new Location(md, md.getThis().getSymbol()));
+ returnLoc.addLocation(new Location(md.getClassDesc(), getLattice(md.getClassDesc())
+ .getBottomItem()));
+ }
+ methodInfo.setReturnLoc(returnLoc);
+
+ } else {
+ String returnLocSymbol = "RETURNLOC";
+ CompositeLocation returnLocInferLoc =
+ new CompositeLocation(new Location(md, returnLocSymbol));
+ methodInfo.setReturnLoc(returnLocInferLoc);
+
+ for (Iterator iterator = paramIdxSet.iterator(); iterator.hasNext();) {
+ Integer paramIdx = (Integer) iterator.next();
+ CompositeLocation inferLoc = mapParamToLoc.get(paramIdx);
+ String paramLocLocalSymbol = inferLoc.get(0).getLocIdentifier();
+ if (!methodLattice.isGreaterThan(paramLocLocalSymbol, returnLocSymbol)) {
+ // TODO
+ // addRelationHigherToLower(methodLattice, methodInfo,
+ // paramLocLocalSymbol,
+ // returnLocSymbol);
+ }
+ }
+
+ for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
+ FlowNode returnNode = (FlowNode) iterator.next();
+ CompositeLocation inferLoc =
+ generateInferredCompositeLocation(methodInfo, fg.getLocationTuple(returnNode));
+ if (!isGreaterThan(methodLattice, inferLoc, returnLocInferLoc)) {
+ // TODO
+ // addRelation(methodLattice, methodInfo, inferLoc,
+ // returnLocInferLoc);
+ }
+ }
+
+ }
+
+ }
+ }
+
+ private NTuple<Location> generateLocTupleHigherThan(MethodDescriptor md,
+ Set<NTuple<Location>> paramLocTupleHavingInFlowSet) {
+
+ System.out.println("-generateLocTupleHigherThan=" + paramLocTupleHavingInFlowSet);
+
+ NTuple<Location> higherLocTuple = new NTuple<Location>();
+
+ VarDescriptor thisVarDesc = md.getThis();
+ // check if all paramter loc tuple is started with 'this' reference
+ boolean hasParamNotStartedWithThisRef = false;
+
+ int maxSize = 0;
+
+ Set<NTuple<Location>> paramLocTupleStartedWithThis = new HashSet<NTuple<Location>>();
+
+ for (Iterator iterator = paramLocTupleHavingInFlowSet.iterator(); iterator.hasNext();) {
+ NTuple<Location> paramLocTuple = (NTuple<Location>) iterator.next();
+ if (!paramLocTuple.get(0).getLocDescriptor().equals(thisVarDesc)) {
+ hasParamNotStartedWithThisRef = true;
+ } else if (paramLocTuple.size() > 1) {
+ paramLocTupleStartedWithThis.add(paramLocTuple);
+ if (maxSize < paramLocTuple.size()) {
+ maxSize = paramLocTuple.size();
+ }
+ }
+ }
+
+ Descriptor enclosingDesc = md;
+ if (hasParamNotStartedWithThisRef) {
+ // in this case, PCLOC will be the local location
+ } else {
+ // all parameter is started with 'this', so PCLOC will be set relative to the composite
+ // location started with 'this'.
+ for (int idx = 0; idx < maxSize - 1; idx++) {
+ Set<Descriptor> locDescSet = new HashSet<Descriptor>();
+ Location curLoc = null;
+ for (Iterator iterator = paramLocTupleHavingInFlowSet.iterator(); iterator.hasNext();) {
+ NTuple<Location> paramLocTuple = (NTuple<Location>) iterator.next();
+ curLoc = paramLocTuple.get(idx);
+ Descriptor locDesc = curLoc.getLocDescriptor();
+ locDescSet.add(locDesc);
+ }
+ System.out.println("locDescSet=" + locDescSet + " idx=" + idx);
+ if (locDescSet.size() != 1) {
+ break;
+ }
+ Location newLocElement = new Location(curLoc.getDescriptor(), curLoc.getLocDescriptor());
+ higherLocTuple.add(newLocElement);
+ enclosingDesc = getClassTypeDescriptor(curLoc.getLocDescriptor());
+ }
+
+ }
+
+ String pcLocIdentifier = PCLOC + (locSeed++);
+ NameDescriptor pcLocDesc = new NameDescriptor(pcLocIdentifier);
+ Location newLoc = new Location(enclosingDesc, pcLocDesc);
+ higherLocTuple.add(newLoc);
+
+ System.out.println("---higherLocTuple=" + higherLocTuple);
+
+ return higherLocTuple;
+
+ }
+
+ public ClassDescriptor getClassTypeDescriptor(Descriptor in) {
+
+ if (in instanceof VarDescriptor) {
+ return ((VarDescriptor) in).getType().getClassDesc();
+ } else {/* if (desc instanceof FieldDescriptor) { */
+ return ((FieldDescriptor) in).getType().getClassDesc();
+ }
+
+ }
+
+ private Set<NTuple<Location>> calculateHighestLocTupleSet(
+ Set<NTuple<Location>> paramLocTupleHavingInFlowSet) {
+
+ Set<NTuple<Location>> highestSet = new HashSet<NTuple<Location>>();
+
+ Iterator<NTuple<Location>> iterator = paramLocTupleHavingInFlowSet.iterator();
+ NTuple<Location> highest = iterator.next();
+
+ for (; iterator.hasNext();) {
+ NTuple<Location> curLocTuple = (NTuple<Location>) iterator.next();
+ if (isHigherThan(curLocTuple, highest)) {
+ System.out.println(curLocTuple + " is greater than " + highest);
+ highest = curLocTuple;
+ }
+ }
+
+ highestSet.add(highest);
+
+ MethodDescriptor md = (MethodDescriptor) highest.get(0).getDescriptor();
+ VarDescriptor thisVarDesc = md.getThis();
+
+ System.out.println("highest=" + highest);
+
+ for (Iterator<NTuple<Location>> iter = paramLocTupleHavingInFlowSet.iterator(); iter.hasNext();) {
+ NTuple<Location> curLocTuple = iter.next();
+
+ if (!curLocTuple.equals(highest) && !hasOrderingRelation(highest, curLocTuple)) {
+
+ System.out.println("add it to the highest set=" + curLocTuple);
+ highestSet.add(curLocTuple);
+
+ }
+ }
+
+ return highestSet;
+
+ }
+
+ private void calculateExtraLocations2(MethodDescriptor md) {
+ // calcualte pcloc, returnloc,...
+